1 /* Copyright 2018 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <stdbool.h>
7 #include <syslog.h>
8 #include "audio_thread.h"
9 #include "cras_iodev_list.h"
10 #include "cras_main_message.h"
11 #include "cras_system_state.h"
12 #include "cras_types.h"
13 #include "cras_util.h"
14
15 #define MIN_WAIT_SECOND 30
16
17 struct cras_audio_thread_event_message {
18 struct cras_main_message header;
19 enum CRAS_AUDIO_THREAD_EVENT_TYPE event_type;
20 };
21
take_snapshot(enum CRAS_AUDIO_THREAD_EVENT_TYPE event_type)22 static void take_snapshot(enum CRAS_AUDIO_THREAD_EVENT_TYPE event_type)
23 {
24 struct cras_audio_thread_snapshot *snapshot;
25
26 snapshot = (struct cras_audio_thread_snapshot *)calloc(
27 1, sizeof(struct cras_audio_thread_snapshot));
28 struct timespec now_time;
29 clock_gettime(CLOCK_MONOTONIC_RAW, &now_time);
30 snapshot->timestamp = now_time;
31 snapshot->event_type = event_type;
32 audio_thread_dump_thread_info(cras_iodev_list_get_audio_thread(),
33 &snapshot->audio_debug_info);
34 cras_system_state_add_snapshot(snapshot);
35 }
36
cras_audio_thread_event_message_init(struct cras_audio_thread_event_message * msg,enum CRAS_AUDIO_THREAD_EVENT_TYPE event_type)37 static void cras_audio_thread_event_message_init(
38 struct cras_audio_thread_event_message *msg,
39 enum CRAS_AUDIO_THREAD_EVENT_TYPE event_type)
40 {
41 msg->header.type = CRAS_MAIN_AUDIO_THREAD_EVENT;
42 msg->header.length = sizeof(*msg);
43 msg->event_type = event_type;
44 }
45
cras_audio_thread_event_send(enum CRAS_AUDIO_THREAD_EVENT_TYPE event_type)46 int cras_audio_thread_event_send(enum CRAS_AUDIO_THREAD_EVENT_TYPE event_type)
47 {
48 struct cras_audio_thread_event_message msg;
49 cras_audio_thread_event_message_init(&msg, event_type);
50 return cras_main_message_send(&msg.header);
51 }
52
cras_audio_thread_event_debug()53 int cras_audio_thread_event_debug()
54 {
55 return cras_audio_thread_event_send(AUDIO_THREAD_EVENT_DEBUG);
56 }
57
cras_audio_thread_event_busyloop()58 int cras_audio_thread_event_busyloop()
59 {
60 return cras_audio_thread_event_send(AUDIO_THREAD_EVENT_BUSYLOOP);
61 }
62
cras_audio_thread_event_underrun()63 int cras_audio_thread_event_underrun()
64 {
65 return cras_audio_thread_event_send(AUDIO_THREAD_EVENT_UNDERRUN);
66 }
67
cras_audio_thread_event_severe_underrun()68 int cras_audio_thread_event_severe_underrun()
69 {
70 return cras_audio_thread_event_send(AUDIO_THREAD_EVENT_SEVERE_UNDERRUN);
71 }
72
cras_audio_thread_event_drop_samples()73 int cras_audio_thread_event_drop_samples()
74 {
75 return cras_audio_thread_event_send(AUDIO_THREAD_EVENT_DROP_SAMPLES);
76 }
77
78 static struct timespec last_event_snapshot_time[AUDIO_THREAD_EVENT_TYPE_COUNT];
79
80 /*
81 * Callback function for handling audio thread events in main thread,
82 * which takes a snapshot of the audio thread and waits at least 30 seconds
83 * for the same event type. Events with the same event type within 30 seconds
84 * will be ignored by the handle function.
85 */
handle_audio_thread_event_message(struct cras_main_message * msg,void * arg)86 static void handle_audio_thread_event_message(struct cras_main_message *msg,
87 void *arg)
88 {
89 struct cras_audio_thread_event_message *audio_thread_msg =
90 (struct cras_audio_thread_event_message *)msg;
91 struct timespec now_time;
92
93 /*
94 * Skip invalid event types
95 */
96 if (audio_thread_msg->event_type >= AUDIO_THREAD_EVENT_TYPE_COUNT)
97 return;
98
99 struct timespec *last_snapshot_time =
100 &last_event_snapshot_time[audio_thread_msg->event_type];
101
102 clock_gettime(CLOCK_REALTIME, &now_time);
103
104 /*
105 * Wait at least 30 seconds for the same event type
106 */
107 struct timespec diff_time;
108 subtract_timespecs(&now_time, last_snapshot_time, &diff_time);
109 if ((last_snapshot_time->tv_sec == 0 &&
110 last_snapshot_time->tv_nsec == 0) ||
111 diff_time.tv_sec >= MIN_WAIT_SECOND) {
112 take_snapshot(audio_thread_msg->event_type);
113 *last_snapshot_time = now_time;
114 }
115 }
116
cras_audio_thread_monitor_init()117 int cras_audio_thread_monitor_init()
118 {
119 memset(last_event_snapshot_time, 0,
120 sizeof(struct timespec) * AUDIO_THREAD_EVENT_TYPE_COUNT);
121 cras_main_message_add_handler(CRAS_MAIN_AUDIO_THREAD_EVENT,
122 handle_audio_thread_event_message, NULL);
123 return 0;
124 }
125