1 /******************************************************************************
2 *
3 * Copyright 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "internal_include/bt_target.h"
20
21 #define LOG_TAG "bt_osi_alarm"
22
23 #include "osi/include/alarm.h"
24
25 #include <base/cancelable_callback.h>
26 #include <base/logging.h>
27 #include <base/message_loop/message_loop.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <inttypes.h>
31 #include <malloc.h>
32 #include <pthread.h>
33 #include <signal.h>
34 #include <string.h>
35 #include <time.h>
36
37 #include <hardware/bluetooth.h>
38
39 #include <mutex>
40
41 #include "osi/include/allocator.h"
42 #include "osi/include/fixed_queue.h"
43 #include "osi/include/list.h"
44 #include "osi/include/log.h"
45 #include "osi/include/osi.h"
46 #include "osi/include/semaphore.h"
47 #include "osi/include/thread.h"
48 #include "osi/include/wakelock.h"
49 #include "stack/include/btu.h"
50
51 using base::Bind;
52 using base::CancelableClosure;
53 using base::MessageLoop;
54
55 // Callback and timer threads should run at RT priority in order to ensure they
56 // meet audio deadlines. Use this priority for all audio/timer related thread.
57 static const int THREAD_RT_PRIORITY = 1;
58
59 typedef struct {
60 size_t count;
61 uint64_t total_ms;
62 uint64_t max_ms;
63 } stat_t;
64
65 // Alarm-related information and statistics
66 typedef struct {
67 const char* name;
68 size_t scheduled_count;
69 size_t canceled_count;
70 size_t rescheduled_count;
71 size_t total_updates;
72 uint64_t last_update_ms;
73 stat_t overdue_scheduling;
74 stat_t premature_scheduling;
75 } alarm_stats_t;
76
77 /* Wrapper around CancellableClosure that let it be embedded in structs, without
78 * need to define copy operator. */
79 struct CancelableClosureInStruct {
80 base::CancelableClosure i;
81
operator =CancelableClosureInStruct82 CancelableClosureInStruct& operator=(const CancelableClosureInStruct& in) {
83 if (!in.i.callback().is_null()) i.Reset(in.i.callback());
84 return *this;
85 }
86 };
87
88 struct alarm_t {
89 // The mutex is held while the callback for this alarm is being executed.
90 // It allows us to release the coarse-grained monitor lock while a
91 // potentially long-running callback is executing. |alarm_cancel| uses this
92 // mutex to provide a guarantee to its caller that the callback will not be
93 // in progress when it returns.
94 std::shared_ptr<std::recursive_mutex> callback_mutex;
95 uint64_t creation_time_ms;
96 uint64_t period_ms;
97 uint64_t deadline_ms;
98 uint64_t prev_deadline_ms; // Previous deadline - used for accounting of
99 // periodic timers
100 bool is_periodic;
101 fixed_queue_t* queue; // The processing queue to add this alarm to
102 alarm_callback_t callback;
103 void* data;
104 alarm_stats_t stats;
105
106 bool for_msg_loop; // True, if the alarm should be processed on message loop
107 CancelableClosureInStruct closure; // posted to message loop for processing
108 };
109
110 // If the next wakeup time is less than this threshold, we should acquire
111 // a wakelock instead of setting a wake alarm so we're not bouncing in
112 // and out of suspend frequently. This value is externally visible to allow
113 // unit tests to run faster. It should not be modified by production code.
114 int64_t TIMER_INTERVAL_FOR_WAKELOCK_IN_MS = 3000;
115 static const clockid_t CLOCK_ID = CLOCK_BOOTTIME;
116
117 // This mutex ensures that the |alarm_set|, |alarm_cancel|, and alarm callback
118 // functions execute serially and not concurrently. As a result, this mutex
119 // also protects the |alarms| list.
120 static std::mutex alarms_mutex;
121 static list_t* alarms;
122 static timer_t timer;
123 static timer_t wakeup_timer;
124 static bool timer_set;
125
126 // All alarm callbacks are dispatched from |dispatcher_thread|
127 static thread_t* dispatcher_thread;
128 static bool dispatcher_thread_active;
129 static semaphore_t* alarm_expired;
130
131 // Default alarm callback thread and queue
132 static thread_t* default_callback_thread;
133 static fixed_queue_t* default_callback_queue;
134
135 static alarm_t* alarm_new_internal(const char* name, bool is_periodic);
136 static bool lazy_initialize(void);
137 static uint64_t now_ms(void);
138 static void alarm_set_internal(alarm_t* alarm, uint64_t period_ms,
139 alarm_callback_t cb, void* data,
140 fixed_queue_t* queue, bool for_msg_loop);
141 static void alarm_cancel_internal(alarm_t* alarm);
142 static void remove_pending_alarm(alarm_t* alarm);
143 static void schedule_next_instance(alarm_t* alarm);
144 static void reschedule_root_alarm(void);
145 static void alarm_queue_ready(fixed_queue_t* queue, void* context);
146 static void timer_callback(void* data);
147 static void callback_dispatch(void* context);
148 static bool timer_create_internal(const clockid_t clock_id, timer_t* timer);
149 static void update_scheduling_stats(alarm_stats_t* stats, uint64_t now_ms,
150 uint64_t deadline_ms);
151 // Registers |queue| for processing alarm callbacks on |thread|.
152 // |queue| may not be NULL. |thread| may not be NULL.
153 static void alarm_register_processing_queue(fixed_queue_t* queue,
154 thread_t* thread);
155
update_stat(stat_t * stat,uint64_t delta_ms)156 static void update_stat(stat_t* stat, uint64_t delta_ms) {
157 if (stat->max_ms < delta_ms) stat->max_ms = delta_ms;
158 stat->total_ms += delta_ms;
159 stat->count++;
160 }
161
alarm_new(const char * name)162 alarm_t* alarm_new(const char* name) { return alarm_new_internal(name, false); }
163
alarm_new_periodic(const char * name)164 alarm_t* alarm_new_periodic(const char* name) {
165 return alarm_new_internal(name, true);
166 }
167
alarm_new_internal(const char * name,bool is_periodic)168 static alarm_t* alarm_new_internal(const char* name, bool is_periodic) {
169 // Make sure we have a list we can insert alarms into.
170 if (!alarms && !lazy_initialize()) {
171 CHECK(false); // if initialization failed, we should not continue
172 return NULL;
173 }
174
175 alarm_t* ret = static_cast<alarm_t*>(osi_calloc(sizeof(alarm_t)));
176
177 std::shared_ptr<std::recursive_mutex> ptr(new std::recursive_mutex());
178 ret->callback_mutex = ptr;
179 ret->is_periodic = is_periodic;
180 ret->stats.name = osi_strdup(name);
181
182 ret->for_msg_loop = false;
183 // placement new
184 new (&ret->closure) CancelableClosureInStruct();
185
186 // NOTE: The stats were reset by osi_calloc() above
187
188 return ret;
189 }
190
alarm_free(alarm_t * alarm)191 void alarm_free(alarm_t* alarm) {
192 if (!alarm) return;
193
194 alarm_cancel(alarm);
195
196 osi_free((void*)alarm->stats.name);
197 alarm->closure.~CancelableClosureInStruct();
198 osi_free(alarm);
199 }
200
alarm_get_remaining_ms(const alarm_t * alarm)201 uint64_t alarm_get_remaining_ms(const alarm_t* alarm) {
202 CHECK(alarm != NULL);
203 uint64_t remaining_ms = 0;
204 uint64_t just_now_ms = now_ms();
205
206 std::lock_guard<std::mutex> lock(alarms_mutex);
207 if (alarm->deadline_ms > just_now_ms)
208 remaining_ms = alarm->deadline_ms - just_now_ms;
209
210 return remaining_ms;
211 }
212
alarm_set(alarm_t * alarm,uint64_t interval_ms,alarm_callback_t cb,void * data)213 void alarm_set(alarm_t* alarm, uint64_t interval_ms, alarm_callback_t cb,
214 void* data) {
215 alarm_set_internal(alarm, interval_ms, cb, data, default_callback_queue,
216 false);
217 }
218
alarm_set_on_mloop(alarm_t * alarm,uint64_t interval_ms,alarm_callback_t cb,void * data)219 void alarm_set_on_mloop(alarm_t* alarm, uint64_t interval_ms,
220 alarm_callback_t cb, void* data) {
221 alarm_set_internal(alarm, interval_ms, cb, data, NULL, true);
222 }
223
224 // Runs in exclusion with alarm_cancel and timer_callback.
alarm_set_internal(alarm_t * alarm,uint64_t period_ms,alarm_callback_t cb,void * data,fixed_queue_t * queue,bool for_msg_loop)225 static void alarm_set_internal(alarm_t* alarm, uint64_t period_ms,
226 alarm_callback_t cb, void* data,
227 fixed_queue_t* queue, bool for_msg_loop) {
228 CHECK(alarms != NULL);
229 CHECK(alarm != NULL);
230 CHECK(cb != NULL);
231
232 std::lock_guard<std::mutex> lock(alarms_mutex);
233
234 alarm->creation_time_ms = now_ms();
235 alarm->period_ms = period_ms;
236 alarm->queue = queue;
237 alarm->callback = cb;
238 alarm->data = data;
239 alarm->for_msg_loop = for_msg_loop;
240
241 schedule_next_instance(alarm);
242 alarm->stats.scheduled_count++;
243 }
244
alarm_cancel(alarm_t * alarm)245 void alarm_cancel(alarm_t* alarm) {
246 CHECK(alarms != NULL);
247 if (!alarm) return;
248
249 std::shared_ptr<std::recursive_mutex> local_mutex_ref;
250 {
251 std::lock_guard<std::mutex> lock(alarms_mutex);
252 local_mutex_ref = alarm->callback_mutex;
253 alarm_cancel_internal(alarm);
254 }
255
256 // If the callback for |alarm| is in progress, wait here until it completes.
257 std::lock_guard<std::recursive_mutex> lock(*local_mutex_ref);
258 }
259
260 // Internal implementation of canceling an alarm.
261 // The caller must hold the |alarms_mutex|
alarm_cancel_internal(alarm_t * alarm)262 static void alarm_cancel_internal(alarm_t* alarm) {
263 bool needs_reschedule =
264 (!list_is_empty(alarms) && list_front(alarms) == alarm);
265
266 remove_pending_alarm(alarm);
267
268 alarm->deadline_ms = 0;
269 alarm->prev_deadline_ms = 0;
270 alarm->callback = NULL;
271 alarm->data = NULL;
272 alarm->stats.canceled_count++;
273 alarm->queue = NULL;
274
275 if (needs_reschedule) reschedule_root_alarm();
276 }
277
alarm_is_scheduled(const alarm_t * alarm)278 bool alarm_is_scheduled(const alarm_t* alarm) {
279 if ((alarms == NULL) || (alarm == NULL)) return false;
280 return (alarm->callback != NULL);
281 }
282
alarm_cleanup(void)283 void alarm_cleanup(void) {
284 // If lazy_initialize never ran there is nothing else to do
285 if (!alarms) return;
286
287 dispatcher_thread_active = false;
288 semaphore_post(alarm_expired);
289 thread_free(dispatcher_thread);
290 dispatcher_thread = NULL;
291
292 std::lock_guard<std::mutex> lock(alarms_mutex);
293
294 fixed_queue_free(default_callback_queue, NULL);
295 default_callback_queue = NULL;
296 thread_free(default_callback_thread);
297 default_callback_thread = NULL;
298
299 timer_delete(wakeup_timer);
300 timer_delete(timer);
301 semaphore_free(alarm_expired);
302 alarm_expired = NULL;
303
304 list_free(alarms);
305 alarms = NULL;
306 }
307
lazy_initialize(void)308 static bool lazy_initialize(void) {
309 CHECK(alarms == NULL);
310
311 // timer_t doesn't have an invalid value so we must track whether
312 // the |timer| variable is valid ourselves.
313 bool timer_initialized = false;
314 bool wakeup_timer_initialized = false;
315
316 std::lock_guard<std::mutex> lock(alarms_mutex);
317
318 alarms = list_new(NULL);
319 if (!alarms) {
320 LOG_ERROR(LOG_TAG, "%s unable to allocate alarm list.", __func__);
321 goto error;
322 }
323
324 if (!timer_create_internal(CLOCK_ID, &timer)) goto error;
325 timer_initialized = true;
326
327 if (!timer_create_internal(CLOCK_BOOTTIME_ALARM, &wakeup_timer)) {
328 if (!timer_create_internal(CLOCK_BOOTTIME, &wakeup_timer)) {
329 goto error;
330 }
331 }
332 wakeup_timer_initialized = true;
333
334 alarm_expired = semaphore_new(0);
335 if (!alarm_expired) {
336 LOG_ERROR(LOG_TAG, "%s unable to create alarm expired semaphore", __func__);
337 goto error;
338 }
339
340 default_callback_thread =
341 thread_new_sized("alarm_default_callbacks", SIZE_MAX);
342 if (default_callback_thread == NULL) {
343 LOG_ERROR(LOG_TAG, "%s unable to create default alarm callbacks thread.",
344 __func__);
345 goto error;
346 }
347 thread_set_rt_priority(default_callback_thread, THREAD_RT_PRIORITY);
348 default_callback_queue = fixed_queue_new(SIZE_MAX);
349 if (default_callback_queue == NULL) {
350 LOG_ERROR(LOG_TAG, "%s unable to create default alarm callbacks queue.",
351 __func__);
352 goto error;
353 }
354 alarm_register_processing_queue(default_callback_queue,
355 default_callback_thread);
356
357 dispatcher_thread_active = true;
358 dispatcher_thread = thread_new("alarm_dispatcher");
359 if (!dispatcher_thread) {
360 LOG_ERROR(LOG_TAG, "%s unable to create alarm callback thread.", __func__);
361 goto error;
362 }
363 thread_set_rt_priority(dispatcher_thread, THREAD_RT_PRIORITY);
364 thread_post(dispatcher_thread, callback_dispatch, NULL);
365 return true;
366
367 error:
368 fixed_queue_free(default_callback_queue, NULL);
369 default_callback_queue = NULL;
370 thread_free(default_callback_thread);
371 default_callback_thread = NULL;
372
373 thread_free(dispatcher_thread);
374 dispatcher_thread = NULL;
375
376 dispatcher_thread_active = false;
377
378 semaphore_free(alarm_expired);
379 alarm_expired = NULL;
380
381 if (wakeup_timer_initialized) timer_delete(wakeup_timer);
382
383 if (timer_initialized) timer_delete(timer);
384
385 list_free(alarms);
386 alarms = NULL;
387
388 return false;
389 }
390
now_ms(void)391 static uint64_t now_ms(void) {
392 CHECK(alarms != NULL);
393
394 struct timespec ts;
395 if (clock_gettime(CLOCK_ID, &ts) == -1) {
396 LOG_ERROR(LOG_TAG, "%s unable to get current time: %s", __func__,
397 strerror(errno));
398 return 0;
399 }
400
401 return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL);
402 }
403
404 // Remove alarm from internal alarm list and the processing queue
405 // The caller must hold the |alarms_mutex|
remove_pending_alarm(alarm_t * alarm)406 static void remove_pending_alarm(alarm_t* alarm) {
407 list_remove(alarms, alarm);
408
409 if (alarm->for_msg_loop) {
410 alarm->closure.i.Cancel();
411 } else {
412 while (fixed_queue_try_remove_from_queue(alarm->queue, alarm) != NULL) {
413 // Remove all repeated alarm instances from the queue.
414 // NOTE: We are defensive here - we shouldn't have repeated alarm
415 // instances
416 }
417 }
418 }
419
420 // Must be called with |alarms_mutex| held
schedule_next_instance(alarm_t * alarm)421 static void schedule_next_instance(alarm_t* alarm) {
422 // If the alarm is currently set and it's at the start of the list,
423 // we'll need to re-schedule since we've adjusted the earliest deadline.
424 bool needs_reschedule =
425 (!list_is_empty(alarms) && list_front(alarms) == alarm);
426 if (alarm->callback) remove_pending_alarm(alarm);
427
428 // Calculate the next deadline for this alarm
429 uint64_t just_now_ms = now_ms();
430 uint64_t ms_into_period = 0;
431 if ((alarm->is_periodic) && (alarm->period_ms != 0))
432 ms_into_period =
433 ((just_now_ms - alarm->creation_time_ms) % alarm->period_ms);
434 alarm->deadline_ms = just_now_ms + (alarm->period_ms - ms_into_period);
435
436 // Add it into the timer list sorted by deadline (earliest deadline first).
437 if (list_is_empty(alarms) ||
438 ((alarm_t*)list_front(alarms))->deadline_ms > alarm->deadline_ms) {
439 list_prepend(alarms, alarm);
440 } else {
441 for (list_node_t* node = list_begin(alarms); node != list_end(alarms);
442 node = list_next(node)) {
443 list_node_t* next = list_next(node);
444 if (next == list_end(alarms) ||
445 ((alarm_t*)list_node(next))->deadline_ms > alarm->deadline_ms) {
446 list_insert_after(alarms, node, alarm);
447 break;
448 }
449 }
450 }
451
452 // If the new alarm has the earliest deadline, we need to re-evaluate our
453 // schedule.
454 if (needs_reschedule ||
455 (!list_is_empty(alarms) && list_front(alarms) == alarm)) {
456 reschedule_root_alarm();
457 }
458 }
459
460 // NOTE: must be called with |alarms_mutex| held
reschedule_root_alarm(void)461 static void reschedule_root_alarm(void) {
462 CHECK(alarms != NULL);
463
464 const bool timer_was_set = timer_set;
465 alarm_t* next;
466 int64_t next_expiration;
467
468 // If used in a zeroed state, disarms the timer.
469 struct itimerspec timer_time;
470 memset(&timer_time, 0, sizeof(timer_time));
471
472 if (list_is_empty(alarms)) goto done;
473
474 next = static_cast<alarm_t*>(list_front(alarms));
475 next_expiration = next->deadline_ms - now_ms();
476 if (next_expiration < TIMER_INTERVAL_FOR_WAKELOCK_IN_MS) {
477 if (!timer_set) {
478 if (!wakelock_acquire()) {
479 LOG_ERROR(LOG_TAG, "%s unable to acquire wake lock", __func__);
480 goto done;
481 }
482 }
483
484 timer_time.it_value.tv_sec = (next->deadline_ms / 1000);
485 timer_time.it_value.tv_nsec = (next->deadline_ms % 1000) * 1000000LL;
486
487 // It is entirely unsafe to call timer_settime(2) with a zeroed timerspec
488 // for timers with *_ALARM clock IDs. Although the man page states that the
489 // timer would be canceled, the current behavior (as of Linux kernel 3.17)
490 // is that the callback is issued immediately. The only way to cancel an
491 // *_ALARM timer is to delete the timer. But unfortunately, deleting and
492 // re-creating a timer is rather expensive; every timer_create(2) spawns a
493 // new thread. So we simply set the timer to fire at the largest possible
494 // time.
495 //
496 // If we've reached this code path, we're going to grab a wake lock and
497 // wait for the next timer to fire. In that case, there's no reason to
498 // have a pending wakeup timer so we simply cancel it.
499 struct itimerspec end_of_time;
500 memset(&end_of_time, 0, sizeof(end_of_time));
501 end_of_time.it_value.tv_sec = (time_t)(1LL << (sizeof(time_t) * 8 - 2));
502 timer_settime(wakeup_timer, TIMER_ABSTIME, &end_of_time, NULL);
503 } else {
504 // WARNING: do not attempt to use relative timers with *_ALARM clock IDs
505 // in kernels before 3.17 unless you have the following patch:
506 // https://lkml.org/lkml/2014/7/7/576
507 struct itimerspec wakeup_time;
508 memset(&wakeup_time, 0, sizeof(wakeup_time));
509
510 wakeup_time.it_value.tv_sec = (next->deadline_ms / 1000);
511 wakeup_time.it_value.tv_nsec = (next->deadline_ms % 1000) * 1000000LL;
512 if (timer_settime(wakeup_timer, TIMER_ABSTIME, &wakeup_time, NULL) == -1)
513 LOG_ERROR(LOG_TAG, "%s unable to set wakeup timer: %s", __func__,
514 strerror(errno));
515 }
516
517 done:
518 timer_set =
519 timer_time.it_value.tv_sec != 0 || timer_time.it_value.tv_nsec != 0;
520 if (timer_was_set && !timer_set) {
521 wakelock_release();
522 }
523
524 if (timer_settime(timer, TIMER_ABSTIME, &timer_time, NULL) == -1)
525 LOG_ERROR(LOG_TAG, "%s unable to set timer: %s", __func__, strerror(errno));
526
527 // If next expiration was in the past (e.g. short timer that got context
528 // switched) then the timer might have diarmed itself. Detect this case and
529 // work around it by manually signalling the |alarm_expired| semaphore.
530 //
531 // It is possible that the timer was actually super short (a few
532 // milliseconds) and the timer expired normally before we called
533 // |timer_gettime|. Worst case, |alarm_expired| is signaled twice for that
534 // alarm. Nothing bad should happen in that case though since the callback
535 // dispatch function checks to make sure the timer at the head of the list
536 // actually expired.
537 if (timer_set) {
538 struct itimerspec time_to_expire;
539 timer_gettime(timer, &time_to_expire);
540 if (time_to_expire.it_value.tv_sec == 0 &&
541 time_to_expire.it_value.tv_nsec == 0) {
542 LOG_DEBUG(
543 LOG_TAG,
544 "%s alarm expiration too close for posix timers, switching to guns",
545 __func__);
546 semaphore_post(alarm_expired);
547 }
548 }
549 }
550
alarm_register_processing_queue(fixed_queue_t * queue,thread_t * thread)551 static void alarm_register_processing_queue(fixed_queue_t* queue,
552 thread_t* thread) {
553 CHECK(queue != NULL);
554 CHECK(thread != NULL);
555
556 fixed_queue_register_dequeue(queue, thread_get_reactor(thread),
557 alarm_queue_ready, NULL);
558 }
559
alarm_ready_generic(alarm_t * alarm,std::unique_lock<std::mutex> & lock)560 static void alarm_ready_generic(alarm_t* alarm,
561 std::unique_lock<std::mutex>& lock) {
562 if (alarm == NULL) {
563 return; // The alarm was probably canceled
564 }
565
566 //
567 // If the alarm is not periodic, we've fully serviced it now, and can reset
568 // some of its internal state. This is useful to distinguish between expired
569 // alarms and active ones.
570 //
571 if (!alarm->callback) {
572 LOG(FATAL) << __func__
573 << ": timer callback is NULL! Name=" << alarm->stats.name;
574 }
575 alarm_callback_t callback = alarm->callback;
576 void* data = alarm->data;
577 uint64_t deadline_ms = alarm->deadline_ms;
578 if (alarm->is_periodic) {
579 // The periodic alarm has been rescheduled and alarm->deadline has been
580 // updated, hence we need to use the previous deadline.
581 deadline_ms = alarm->prev_deadline_ms;
582 } else {
583 alarm->deadline_ms = 0;
584 alarm->callback = NULL;
585 alarm->data = NULL;
586 alarm->queue = NULL;
587 }
588
589 // Increment the reference count of the mutex so it doesn't get freed
590 // before the callback gets finished executing.
591 std::shared_ptr<std::recursive_mutex> local_mutex_ref = alarm->callback_mutex;
592 std::lock_guard<std::recursive_mutex> cb_lock(*local_mutex_ref);
593 lock.unlock();
594
595 // Update the statistics
596 update_scheduling_stats(&alarm->stats, now_ms(), deadline_ms);
597
598 // NOTE: Do NOT access "alarm" after the callback, as a safety precaution
599 // in case the callback itself deleted the alarm.
600 callback(data);
601 }
602
alarm_ready_mloop(alarm_t * alarm)603 static void alarm_ready_mloop(alarm_t* alarm) {
604 std::unique_lock<std::mutex> lock(alarms_mutex);
605 alarm_ready_generic(alarm, lock);
606 }
607
alarm_queue_ready(fixed_queue_t * queue,UNUSED_ATTR void * context)608 static void alarm_queue_ready(fixed_queue_t* queue, UNUSED_ATTR void* context) {
609 CHECK(queue != NULL);
610
611 std::unique_lock<std::mutex> lock(alarms_mutex);
612 alarm_t* alarm = (alarm_t*)fixed_queue_try_dequeue(queue);
613 alarm_ready_generic(alarm, lock);
614 }
615
616 // Callback function for wake alarms and our posix timer
timer_callback(UNUSED_ATTR void * ptr)617 static void timer_callback(UNUSED_ATTR void* ptr) {
618 semaphore_post(alarm_expired);
619 }
620
621 // Function running on |dispatcher_thread| that performs the following:
622 // (1) Receives a signal using |alarm_exired| that the alarm has expired
623 // (2) Dispatches the alarm callback for processing by the corresponding
624 // thread for that alarm.
callback_dispatch(UNUSED_ATTR void * context)625 static void callback_dispatch(UNUSED_ATTR void* context) {
626 while (true) {
627 semaphore_wait(alarm_expired);
628 if (!dispatcher_thread_active) break;
629
630 std::lock_guard<std::mutex> lock(alarms_mutex);
631 alarm_t* alarm;
632
633 // Take into account that the alarm may get cancelled before we get to it.
634 // We're done here if there are no alarms or the alarm at the front is in
635 // the future. Exit right away since there's nothing left to do.
636 if (list_is_empty(alarms) ||
637 (alarm = static_cast<alarm_t*>(list_front(alarms)))->deadline_ms >
638 now_ms()) {
639 reschedule_root_alarm();
640 continue;
641 }
642
643 list_remove(alarms, alarm);
644
645 if (alarm->is_periodic) {
646 alarm->prev_deadline_ms = alarm->deadline_ms;
647 schedule_next_instance(alarm);
648 alarm->stats.rescheduled_count++;
649 }
650 reschedule_root_alarm();
651
652 // Enqueue the alarm for processing
653 if (alarm->for_msg_loop) {
654 if (!get_main_message_loop()) {
655 LOG_ERROR(LOG_TAG, "%s: message loop already NULL. Alarm: %s", __func__,
656 alarm->stats.name);
657 continue;
658 }
659
660 alarm->closure.i.Reset(Bind(alarm_ready_mloop, alarm));
661 get_main_message_loop()->task_runner()->PostTask(
662 FROM_HERE, alarm->closure.i.callback());
663 } else {
664 fixed_queue_enqueue(alarm->queue, alarm);
665 }
666 }
667
668 LOG_DEBUG(LOG_TAG, "%s Callback thread exited", __func__);
669 }
670
timer_create_internal(const clockid_t clock_id,timer_t * timer)671 static bool timer_create_internal(const clockid_t clock_id, timer_t* timer) {
672 CHECK(timer != NULL);
673
674 struct sigevent sigevent;
675 // create timer with RT priority thread
676 pthread_attr_t thread_attr;
677 pthread_attr_init(&thread_attr);
678 pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO);
679 struct sched_param param;
680 param.sched_priority = THREAD_RT_PRIORITY;
681 pthread_attr_setschedparam(&thread_attr, ¶m);
682
683 memset(&sigevent, 0, sizeof(sigevent));
684 sigevent.sigev_notify = SIGEV_THREAD;
685 sigevent.sigev_notify_function = (void (*)(union sigval))timer_callback;
686 sigevent.sigev_notify_attributes = &thread_attr;
687 if (timer_create(clock_id, &sigevent, timer) == -1) {
688 LOG_ERROR(LOG_TAG, "%s unable to create timer with clock %d: %s", __func__,
689 clock_id, strerror(errno));
690 if (clock_id == CLOCK_BOOTTIME_ALARM) {
691 LOG_ERROR(LOG_TAG,
692 "The kernel might not have support for "
693 "timer_create(CLOCK_BOOTTIME_ALARM): "
694 "https://lwn.net/Articles/429925/");
695 LOG_ERROR(LOG_TAG,
696 "See following patches: "
697 "https://git.kernel.org/cgit/linux/kernel/git/torvalds/"
698 "linux.git/log/?qt=grep&q=CLOCK_BOOTTIME_ALARM");
699 }
700 return false;
701 }
702
703 return true;
704 }
705
update_scheduling_stats(alarm_stats_t * stats,uint64_t now_ms,uint64_t deadline_ms)706 static void update_scheduling_stats(alarm_stats_t* stats, uint64_t now_ms,
707 uint64_t deadline_ms) {
708 stats->total_updates++;
709 stats->last_update_ms = now_ms;
710
711 if (deadline_ms < now_ms) {
712 // Overdue scheduling
713 uint64_t delta_ms = now_ms - deadline_ms;
714 update_stat(&stats->overdue_scheduling, delta_ms);
715 } else if (deadline_ms > now_ms) {
716 // Premature scheduling
717 uint64_t delta_ms = deadline_ms - now_ms;
718 update_stat(&stats->premature_scheduling, delta_ms);
719 }
720 }
721
dump_stat(int fd,stat_t * stat,const char * description)722 static void dump_stat(int fd, stat_t* stat, const char* description) {
723 uint64_t average_time_ms = 0;
724 if (stat->count != 0) average_time_ms = stat->total_ms / stat->count;
725
726 dprintf(fd, "%-51s: %llu / %llu / %llu\n", description,
727 (unsigned long long)stat->total_ms, (unsigned long long)stat->max_ms,
728 (unsigned long long)average_time_ms);
729 }
730
alarm_debug_dump(int fd)731 void alarm_debug_dump(int fd) {
732 dprintf(fd, "\nBluetooth Alarms Statistics:\n");
733
734 std::lock_guard<std::mutex> lock(alarms_mutex);
735
736 if (alarms == NULL) {
737 dprintf(fd, " None\n");
738 return;
739 }
740
741 uint64_t just_now_ms = now_ms();
742
743 dprintf(fd, " Total Alarms: %zu\n\n", list_length(alarms));
744
745 // Dump info for each alarm
746 for (list_node_t* node = list_begin(alarms); node != list_end(alarms);
747 node = list_next(node)) {
748 alarm_t* alarm = (alarm_t*)list_node(node);
749 alarm_stats_t* stats = &alarm->stats;
750
751 dprintf(fd, " Alarm : %s (%s)\n", stats->name,
752 (alarm->is_periodic) ? "PERIODIC" : "SINGLE");
753
754 dprintf(fd, "%-51s: %zu / %zu / %zu / %zu\n",
755 " Action counts (sched/resched/exec/cancel)",
756 stats->scheduled_count, stats->rescheduled_count,
757 stats->total_updates, stats->canceled_count);
758
759 dprintf(fd, "%-51s: %zu / %zu\n",
760 " Deviation counts (overdue/premature)",
761 stats->overdue_scheduling.count, stats->premature_scheduling.count);
762
763 dprintf(fd, "%-51s: %llu / %llu / %lld\n",
764 " Time in ms (since creation/interval/remaining)",
765 (unsigned long long)(just_now_ms - alarm->creation_time_ms),
766 (unsigned long long)alarm->period_ms,
767 (long long)(alarm->deadline_ms - just_now_ms));
768
769 dump_stat(fd, &stats->overdue_scheduling,
770 " Overdue scheduling time in ms (total/max/avg)");
771
772 dump_stat(fd, &stats->premature_scheduling,
773 " Premature scheduling time in ms (total/max/avg)");
774
775 dprintf(fd, "\n");
776 }
777 }
778