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