blob: 684a078b48a0fb66df780a6795f0b2a1cae9b8c8 [file] [log] [blame]
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -07001/******************************************************************************
2 *
3 * Copyright (C) 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
Pavlin Radoslavov10978012016-01-21 18:04:36 -080019#include "include/bt_target.h"
20
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070021#define LOG_TAG "bt_osi_alarm"
22
Marie Janssen49a86702015-07-08 11:48:57 -070023#include "osi/include/alarm.h"
24
Jack Hef2af1c42016-12-13 01:59:12 -080025#include <base/logging.h>
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070026#include <errno.h>
Marie Janssen28bf0072015-09-21 10:49:08 -070027#include <fcntl.h>
Michael Wright1afbe152014-07-17 14:45:58 -070028#include <inttypes.h>
Elliott Hughesb9164f42015-01-28 15:31:26 -080029#include <malloc.h>
Philip Cuadraeaa42772017-03-23 10:10:34 -070030#include <pthread.h>
Marie Janssen49a86702015-07-08 11:48:57 -070031#include <signal.h>
32#include <string.h>
33#include <time.h>
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070034
Marie Janssen49a86702015-07-08 11:48:57 -070035#include <hardware/bluetooth.h>
36
Marie Janssen21da6372016-11-02 18:31:55 -070037#include <mutex>
38
Zach Johnson081e4b62015-03-25 23:32:11 -070039#include "osi/include/allocator.h"
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080040#include "osi/include/fixed_queue.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070041#include "osi/include/list.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080042#include "osi/include/log.h"
Zach Johnson081e4b62015-03-25 23:32:11 -070043#include "osi/include/osi.h"
44#include "osi/include/semaphore.h"
45#include "osi/include/thread.h"
Pavlin Radoslavovd2e25082015-08-24 16:29:21 -070046#include "osi/include/wakelock.h"
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070047
Philip Cuadraeaa42772017-03-23 10:10:34 -070048// 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.
50static const int THREAD_RT_PRIORITY = 1;
Andre Eisenbach41a91a52015-10-23 10:40:36 -070051
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080052typedef struct {
53 size_t count;
54 period_ms_t total_ms;
55 period_ms_t max_ms;
56} stat_t;
57
58// Alarm-related information and statistics
59typedef struct {
Myles Watsonb55040c2016-10-19 13:15:34 -070060 const char* name;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080061 size_t scheduled_count;
62 size_t canceled_count;
63 size_t rescheduled_count;
64 size_t total_updates;
65 period_ms_t last_update_ms;
66 stat_t callback_execution;
67 stat_t overdue_scheduling;
68 stat_t premature_scheduling;
69} alarm_stats_t;
70
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070071struct alarm_t {
Marie Janssen21da6372016-11-02 18:31:55 -070072 // The mutex is held while the callback for this alarm is being executed.
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080073 // It allows us to release the coarse-grained monitor lock while a
74 // potentially long-running callback is executing. |alarm_cancel| uses this
Marie Janssen21da6372016-11-02 18:31:55 -070075 // mutex to provide a guarantee to its caller that the callback will not be
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080076 // in progress when it returns.
Marie Janssen21da6372016-11-02 18:31:55 -070077 std::recursive_mutex* callback_mutex;
Sharvil Nanavatif2bf2302015-06-10 22:00:16 -070078 period_ms_t creation_time;
Zach Johnson71864f42015-03-11 01:55:27 -070079 period_ms_t period;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070080 period_ms_t deadline;
Myles Watsonb55040c2016-10-19 13:15:34 -070081 period_ms_t prev_deadline; // Previous deadline - used for accounting of
82 // periodic timers
Zach Johnsone2c57aa2015-03-23 13:59:10 -070083 bool is_periodic;
Myles Watsonb55040c2016-10-19 13:15:34 -070084 fixed_queue_t* queue; // The processing queue to add this alarm to
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070085 alarm_callback_t callback;
Myles Watsonb55040c2016-10-19 13:15:34 -070086 void* data;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080087 alarm_stats_t stats;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070088};
89
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070090// If the next wakeup time is less than this threshold, we should acquire
91// a wakelock instead of setting a wake alarm so we're not bouncing in
92// and out of suspend frequently. This value is externally visible to allow
93// unit tests to run faster. It should not be modified by production code.
94int64_t TIMER_INTERVAL_FOR_WAKELOCK_IN_MS = 3000;
95static const clockid_t CLOCK_ID = CLOCK_BOOTTIME;
Pavlin Radoslavov10978012016-01-21 18:04:36 -080096
Marie Janssend19e0782016-07-15 12:48:27 -070097#if (KERNEL_MISSING_CLOCK_BOOTTIME_ALARM == TRUE)
Pavlin Radoslavov10978012016-01-21 18:04:36 -080098static const clockid_t CLOCK_ID_ALARM = CLOCK_BOOTTIME;
99#else
Marie Janssen28bf0072015-09-21 10:49:08 -0700100static const clockid_t CLOCK_ID_ALARM = CLOCK_BOOTTIME_ALARM;
Pavlin Radoslavov10978012016-01-21 18:04:36 -0800101#endif
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700102
103// This mutex ensures that the |alarm_set|, |alarm_cancel|, and alarm callback
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800104// functions execute serially and not concurrently. As a result, this mutex
105// also protects the |alarms| list.
Marie Janssen21da6372016-11-02 18:31:55 -0700106static std::mutex alarms_mutex;
Myles Watsonb55040c2016-10-19 13:15:34 -0700107static list_t* alarms;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700108static timer_t timer;
Marie Janssen28bf0072015-09-21 10:49:08 -0700109static timer_t wakeup_timer;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700110static bool timer_set;
111
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800112// All alarm callbacks are dispatched from |dispatcher_thread|
Myles Watsonb55040c2016-10-19 13:15:34 -0700113static thread_t* dispatcher_thread;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800114static bool dispatcher_thread_active;
Myles Watsonb55040c2016-10-19 13:15:34 -0700115static semaphore_t* alarm_expired;
Zach Johnson081e4b62015-03-25 23:32:11 -0700116
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800117// Default alarm callback thread and queue
Myles Watsonb55040c2016-10-19 13:15:34 -0700118static thread_t* default_callback_thread;
119static fixed_queue_t* default_callback_queue;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800120
Myles Watsonb55040c2016-10-19 13:15:34 -0700121static alarm_t* alarm_new_internal(const char* name, bool is_periodic);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700122static bool lazy_initialize(void);
123static period_ms_t now(void);
Myles Watsonb55040c2016-10-19 13:15:34 -0700124static void alarm_set_internal(alarm_t* alarm, period_ms_t period,
125 alarm_callback_t cb, void* data,
126 fixed_queue_t* queue);
127static void alarm_cancel_internal(alarm_t* alarm);
128static void remove_pending_alarm(alarm_t* alarm);
129static void schedule_next_instance(alarm_t* alarm);
Zach Johnsone2c57aa2015-03-23 13:59:10 -0700130static void reschedule_root_alarm(void);
Myles Watsonb55040c2016-10-19 13:15:34 -0700131static void alarm_queue_ready(fixed_queue_t* queue, void* context);
132static void timer_callback(void* data);
133static void callback_dispatch(void* context);
134static bool timer_create_internal(const clockid_t clock_id, timer_t* timer);
135static void update_scheduling_stats(alarm_stats_t* stats, period_ms_t now_ms,
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800136 period_ms_t deadline_ms,
137 period_ms_t execution_delta_ms);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700138
Myles Watsonb55040c2016-10-19 13:15:34 -0700139static void update_stat(stat_t* stat, period_ms_t delta) {
140 if (stat->max_ms < delta) stat->max_ms = delta;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800141 stat->total_ms += delta;
142 stat->count++;
143}
144
Myles Watsonb55040c2016-10-19 13:15:34 -0700145alarm_t* alarm_new(const char* name) { return alarm_new_internal(name, false); }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800146
Myles Watsonb55040c2016-10-19 13:15:34 -0700147alarm_t* alarm_new_periodic(const char* name) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800148 return alarm_new_internal(name, true);
149}
150
Myles Watsonb55040c2016-10-19 13:15:34 -0700151static alarm_t* alarm_new_internal(const char* name, bool is_periodic) {
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700152 // Make sure we have a list we can insert alarms into.
Marie Janssen28bf0072015-09-21 10:49:08 -0700153 if (!alarms && !lazy_initialize()) {
Jack Hef2af1c42016-12-13 01:59:12 -0800154 CHECK(false); // if initialization failed, we should not continue
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700155 return NULL;
Marie Janssen28bf0072015-09-21 10:49:08 -0700156 }
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700157
Myles Watsonb55040c2016-10-19 13:15:34 -0700158 alarm_t* ret = static_cast<alarm_t*>(osi_calloc(sizeof(alarm_t)));
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700159
Marie Janssen21da6372016-11-02 18:31:55 -0700160 ret->callback_mutex = new std::recursive_mutex;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800161 ret->is_periodic = is_periodic;
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -0700162 ret->stats.name = osi_strdup(name);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800163 // NOTE: The stats were reset by osi_calloc() above
164
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700165 return ret;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700166}
167
Myles Watsonb55040c2016-10-19 13:15:34 -0700168void alarm_free(alarm_t* alarm) {
169 if (!alarm) return;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700170
171 alarm_cancel(alarm);
Marie Janssen21da6372016-11-02 18:31:55 -0700172 delete alarm->callback_mutex;
Myles Watsonb55040c2016-10-19 13:15:34 -0700173 osi_free((void*)alarm->stats.name);
Zach Johnsonee2aa452014-08-26 20:16:03 -0700174 osi_free(alarm);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700175}
176
Myles Watsonb55040c2016-10-19 13:15:34 -0700177period_ms_t alarm_get_remaining_ms(const alarm_t* alarm) {
Jack Hef2af1c42016-12-13 01:59:12 -0800178 CHECK(alarm != NULL);
Andre Eisenbach165332b2015-07-15 12:35:15 -0700179 period_ms_t remaining_ms = 0;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800180 period_ms_t just_now = now();
Andre Eisenbach165332b2015-07-15 12:35:15 -0700181
Marie Janssen21da6372016-11-02 18:31:55 -0700182 std::lock_guard<std::mutex> lock(alarms_mutex);
Myles Watsonb55040c2016-10-19 13:15:34 -0700183 if (alarm->deadline > just_now) remaining_ms = alarm->deadline - just_now;
Andre Eisenbach165332b2015-07-15 12:35:15 -0700184
185 return remaining_ms;
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -0700186}
187
Myles Watsonb55040c2016-10-19 13:15:34 -0700188void alarm_set(alarm_t* alarm, period_ms_t interval_ms, alarm_callback_t cb,
189 void* data) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800190 alarm_set_on_queue(alarm, interval_ms, cb, data, default_callback_queue);
Zach Johnson71864f42015-03-11 01:55:27 -0700191}
192
Myles Watsonb55040c2016-10-19 13:15:34 -0700193void alarm_set_on_queue(alarm_t* alarm, period_ms_t interval_ms,
194 alarm_callback_t cb, void* data, fixed_queue_t* queue) {
Jack Hef2af1c42016-12-13 01:59:12 -0800195 CHECK(queue != NULL);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800196 alarm_set_internal(alarm, interval_ms, cb, data, queue);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700197}
198
199// Runs in exclusion with alarm_cancel and timer_callback.
Myles Watsonb55040c2016-10-19 13:15:34 -0700200static void alarm_set_internal(alarm_t* alarm, period_ms_t period,
201 alarm_callback_t cb, void* data,
202 fixed_queue_t* queue) {
Jack Hef2af1c42016-12-13 01:59:12 -0800203 CHECK(alarms != NULL);
204 CHECK(alarm != NULL);
205 CHECK(cb != NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700206
Marie Janssen21da6372016-11-02 18:31:55 -0700207 std::lock_guard<std::mutex> lock(alarms_mutex);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700208
Sharvil Nanavatif2bf2302015-06-10 22:00:16 -0700209 alarm->creation_time = now();
Zach Johnsone2c57aa2015-03-23 13:59:10 -0700210 alarm->period = period;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800211 alarm->queue = queue;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700212 alarm->callback = cb;
213 alarm->data = data;
214
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800215 schedule_next_instance(alarm);
216 alarm->stats.scheduled_count++;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700217}
218
Myles Watsonb55040c2016-10-19 13:15:34 -0700219void alarm_cancel(alarm_t* alarm) {
Jack Hef2af1c42016-12-13 01:59:12 -0800220 CHECK(alarms != NULL);
Myles Watsonb55040c2016-10-19 13:15:34 -0700221 if (!alarm) return;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700222
Marie Janssen21da6372016-11-02 18:31:55 -0700223 {
224 std::lock_guard<std::mutex> lock(alarms_mutex);
225 alarm_cancel_internal(alarm);
226 }
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700227
Pavlin Radoslavov93659162016-05-02 10:52:07 -0700228 // If the callback for |alarm| is in progress, wait here until it completes.
Marie Janssen21da6372016-11-02 18:31:55 -0700229 std::lock_guard<std::recursive_mutex> lock(*alarm->callback_mutex);
Pavlin Radoslavov93659162016-05-02 10:52:07 -0700230}
231
232// Internal implementation of canceling an alarm.
Marie Janssen21da6372016-11-02 18:31:55 -0700233// The caller must hold the |alarms_mutex|
Myles Watsonb55040c2016-10-19 13:15:34 -0700234static void alarm_cancel_internal(alarm_t* alarm) {
235 bool needs_reschedule =
236 (!list_is_empty(alarms) && list_front(alarms) == alarm);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700237
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800238 remove_pending_alarm(alarm);
239
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700240 alarm->deadline = 0;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800241 alarm->prev_deadline = 0;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700242 alarm->callback = NULL;
243 alarm->data = NULL;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800244 alarm->stats.canceled_count++;
Pavlin Radoslavov93659162016-05-02 10:52:07 -0700245 alarm->queue = NULL;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700246
Myles Watsonb55040c2016-10-19 13:15:34 -0700247 if (needs_reschedule) reschedule_root_alarm();
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700248}
249
Myles Watsonb55040c2016-10-19 13:15:34 -0700250bool alarm_is_scheduled(const alarm_t* alarm) {
251 if ((alarms == NULL) || (alarm == NULL)) return false;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800252 return (alarm->callback != NULL);
253}
254
Pavlin Radoslavov0933b402015-06-01 16:08:18 -0700255void alarm_cleanup(void) {
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800256 // If lazy_initialize never ran there is nothing else to do
Myles Watsonb55040c2016-10-19 13:15:34 -0700257 if (!alarms) return;
Zach Johnsonc6a1c262015-05-18 14:48:50 -0700258
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800259 dispatcher_thread_active = false;
Andre Eisenbachcae219f2015-05-18 09:41:06 -0700260 semaphore_post(alarm_expired);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800261 thread_free(dispatcher_thread);
262 dispatcher_thread = NULL;
263
Marie Janssen21da6372016-11-02 18:31:55 -0700264 std::lock_guard<std::mutex> lock(alarms_mutex);
Pavlin Radoslavov93342472016-04-25 12:29:29 -0700265
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800266 fixed_queue_free(default_callback_queue, NULL);
267 default_callback_queue = NULL;
268 thread_free(default_callback_thread);
269 default_callback_thread = NULL;
Andre Eisenbachcae219f2015-05-18 09:41:06 -0700270
Pavlin Radoslavov93342472016-04-25 12:29:29 -0700271 timer_delete(wakeup_timer);
272 timer_delete(timer);
Andre Eisenbachcae219f2015-05-18 09:41:06 -0700273 semaphore_free(alarm_expired);
274 alarm_expired = NULL;
Pavlin Radoslavov93342472016-04-25 12:29:29 -0700275
Andre Eisenbachcae219f2015-05-18 09:41:06 -0700276 list_free(alarms);
277 alarms = NULL;
Andre Eisenbachcae219f2015-05-18 09:41:06 -0700278}
279
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700280static bool lazy_initialize(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800281 CHECK(alarms == NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700282
Marie Janssen28bf0072015-09-21 10:49:08 -0700283 // timer_t doesn't have an invalid value so we must track whether
284 // the |timer| variable is valid ourselves.
285 bool timer_initialized = false;
286 bool wakeup_timer_initialized = false;
287
Marie Janssen21da6372016-11-02 18:31:55 -0700288 std::lock_guard<std::mutex> lock(alarms_mutex);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700289
290 alarms = list_new(NULL);
291 if (!alarms) {
Marie Janssendb554582015-06-26 14:53:46 -0700292 LOG_ERROR(LOG_TAG, "%s unable to allocate alarm list.", __func__);
Marie Janssen28bf0072015-09-21 10:49:08 -0700293 goto error;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700294 }
295
Myles Watsonb55040c2016-10-19 13:15:34 -0700296 if (!timer_create_internal(CLOCK_ID, &timer)) goto error;
Marie Janssen28bf0072015-09-21 10:49:08 -0700297 timer_initialized = true;
298
Myles Watsonb55040c2016-10-19 13:15:34 -0700299 if (!timer_create_internal(CLOCK_ID_ALARM, &wakeup_timer)) goto error;
Marie Janssen28bf0072015-09-21 10:49:08 -0700300 wakeup_timer_initialized = true;
Zach Johnson518a94e2015-03-23 18:07:46 -0700301
Zach Johnson081e4b62015-03-25 23:32:11 -0700302 alarm_expired = semaphore_new(0);
303 if (!alarm_expired) {
Marie Janssendb554582015-06-26 14:53:46 -0700304 LOG_ERROR(LOG_TAG, "%s unable to create alarm expired semaphore", __func__);
Marie Janssen28bf0072015-09-21 10:49:08 -0700305 goto error;
Zach Johnson081e4b62015-03-25 23:32:11 -0700306 }
307
Myles Watsonb55040c2016-10-19 13:15:34 -0700308 default_callback_thread =
309 thread_new_sized("alarm_default_callbacks", SIZE_MAX);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800310 if (default_callback_thread == NULL) {
311 LOG_ERROR(LOG_TAG, "%s unable to create default alarm callbacks thread.",
312 __func__);
313 goto error;
314 }
Philip Cuadraeaa42772017-03-23 10:10:34 -0700315 thread_set_rt_priority(default_callback_thread, THREAD_RT_PRIORITY);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800316 default_callback_queue = fixed_queue_new(SIZE_MAX);
317 if (default_callback_queue == NULL) {
318 LOG_ERROR(LOG_TAG, "%s unable to create default alarm callbacks queue.",
319 __func__);
320 goto error;
321 }
322 alarm_register_processing_queue(default_callback_queue,
323 default_callback_thread);
324
325 dispatcher_thread_active = true;
326 dispatcher_thread = thread_new("alarm_dispatcher");
327 if (!dispatcher_thread) {
Marie Janssendb554582015-06-26 14:53:46 -0700328 LOG_ERROR(LOG_TAG, "%s unable to create alarm callback thread.", __func__);
Marie Janssen28bf0072015-09-21 10:49:08 -0700329 goto error;
Zach Johnson081e4b62015-03-25 23:32:11 -0700330 }
Philip Cuadraeaa42772017-03-23 10:10:34 -0700331 thread_set_rt_priority(dispatcher_thread, THREAD_RT_PRIORITY);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800332 thread_post(dispatcher_thread, callback_dispatch, NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700333 return true;
Marie Janssen28bf0072015-09-21 10:49:08 -0700334
335error:
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800336 fixed_queue_free(default_callback_queue, NULL);
337 default_callback_queue = NULL;
338 thread_free(default_callback_thread);
339 default_callback_thread = NULL;
Marie Janssen28bf0072015-09-21 10:49:08 -0700340
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800341 thread_free(dispatcher_thread);
342 dispatcher_thread = NULL;
343
344 dispatcher_thread_active = false;
Marie Janssen28bf0072015-09-21 10:49:08 -0700345
346 semaphore_free(alarm_expired);
347 alarm_expired = NULL;
348
Myles Watsonb55040c2016-10-19 13:15:34 -0700349 if (wakeup_timer_initialized) timer_delete(wakeup_timer);
Marie Janssen28bf0072015-09-21 10:49:08 -0700350
Myles Watsonb55040c2016-10-19 13:15:34 -0700351 if (timer_initialized) timer_delete(timer);
Marie Janssen28bf0072015-09-21 10:49:08 -0700352
353 list_free(alarms);
354 alarms = NULL;
355
Marie Janssen28bf0072015-09-21 10:49:08 -0700356 return false;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700357}
358
359static period_ms_t now(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800360 CHECK(alarms != NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700361
362 struct timespec ts;
363 if (clock_gettime(CLOCK_ID, &ts) == -1) {
Myles Watsonb55040c2016-10-19 13:15:34 -0700364 LOG_ERROR(LOG_TAG, "%s unable to get current time: %s", __func__,
365 strerror(errno));
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700366 return 0;
367 }
368
369 return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL);
370}
371
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800372// Remove alarm from internal alarm list and the processing queue
Marie Janssen21da6372016-11-02 18:31:55 -0700373// The caller must hold the |alarms_mutex|
Myles Watsonb55040c2016-10-19 13:15:34 -0700374static void remove_pending_alarm(alarm_t* alarm) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800375 list_remove(alarms, alarm);
376 while (fixed_queue_try_remove_from_queue(alarm->queue, alarm) != NULL) {
377 // Remove all repeated alarm instances from the queue.
378 // NOTE: We are defensive here - we shouldn't have repeated alarm instances
379 }
380}
381
Marie Janssen21da6372016-11-02 18:31:55 -0700382// Must be called with |alarms_mutex| held
Myles Watsonb55040c2016-10-19 13:15:34 -0700383static void schedule_next_instance(alarm_t* alarm) {
Zach Johnson71864f42015-03-11 01:55:27 -0700384 // If the alarm is currently set and it's at the start of the list,
385 // we'll need to re-schedule since we've adjusted the earliest deadline.
Myles Watsonb55040c2016-10-19 13:15:34 -0700386 bool needs_reschedule =
387 (!list_is_empty(alarms) && list_front(alarms) == alarm);
388 if (alarm->callback) remove_pending_alarm(alarm);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700389
Zach Johnsone2c57aa2015-03-23 13:59:10 -0700390 // Calculate the next deadline for this alarm
391 period_ms_t just_now = now();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800392 period_ms_t ms_into_period = 0;
Pavlin Radoslavov399f2172016-04-19 17:30:19 -0700393 if ((alarm->is_periodic) && (alarm->period != 0))
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800394 ms_into_period = ((just_now - alarm->creation_time) % alarm->period);
Zach Johnsone2c57aa2015-03-23 13:59:10 -0700395 alarm->deadline = just_now + (alarm->period - ms_into_period);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700396
Zach Johnson71864f42015-03-11 01:55:27 -0700397 // Add it into the timer list sorted by deadline (earliest deadline first).
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800398 if (list_is_empty(alarms) ||
Myles Watsonb55040c2016-10-19 13:15:34 -0700399 ((alarm_t*)list_front(alarms))->deadline > alarm->deadline) {
Zach Johnson71864f42015-03-11 01:55:27 -0700400 list_prepend(alarms, alarm);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800401 } else {
Myles Watsonb55040c2016-10-19 13:15:34 -0700402 for (list_node_t* node = list_begin(alarms); node != list_end(alarms);
403 node = list_next(node)) {
404 list_node_t* next = list_next(node);
405 if (next == list_end(alarms) ||
406 ((alarm_t*)list_node(next))->deadline > alarm->deadline) {
Zach Johnson71864f42015-03-11 01:55:27 -0700407 list_insert_after(alarms, node, alarm);
408 break;
409 }
410 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800411 }
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700412
Myles Watsonb55040c2016-10-19 13:15:34 -0700413 // If the new alarm has the earliest deadline, we need to re-evaluate our
414 // schedule.
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800415 if (needs_reschedule ||
416 (!list_is_empty(alarms) && list_front(alarms) == alarm)) {
Zach Johnsone2c57aa2015-03-23 13:59:10 -0700417 reschedule_root_alarm();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800418 }
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700419}
420
Marie Janssen21da6372016-11-02 18:31:55 -0700421// NOTE: must be called with |alarms_mutex| held
Zach Johnsone2c57aa2015-03-23 13:59:10 -0700422static void reschedule_root_alarm(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800423 CHECK(alarms != NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700424
Marie Janssen28bf0072015-09-21 10:49:08 -0700425 const bool timer_was_set = timer_set;
Myles Watsonb55040c2016-10-19 13:15:34 -0700426 alarm_t* next;
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -0700427 int64_t next_expiration;
Marie Janssen28bf0072015-09-21 10:49:08 -0700428
429 // If used in a zeroed state, disarms the timer.
430 struct itimerspec timer_time;
431 memset(&timer_time, 0, sizeof(timer_time));
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700432
Myles Watsonb55040c2016-10-19 13:15:34 -0700433 if (list_is_empty(alarms)) goto done;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700434
Myles Watsonb55040c2016-10-19 13:15:34 -0700435 next = static_cast<alarm_t*>(list_front(alarms));
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -0700436 next_expiration = next->deadline - now();
Zach Johnson518a94e2015-03-23 18:07:46 -0700437 if (next_expiration < TIMER_INTERVAL_FOR_WAKELOCK_IN_MS) {
438 if (!timer_set) {
Pavlin Radoslavovd2e25082015-08-24 16:29:21 -0700439 if (!wakelock_acquire()) {
Marie Janssen28bf0072015-09-21 10:49:08 -0700440 LOG_ERROR(LOG_TAG, "%s unable to acquire wake lock", __func__);
Zach Johnson518a94e2015-03-23 18:07:46 -0700441 goto done;
Zach Johnson71864f42015-03-11 01:55:27 -0700442 }
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700443 }
444
Marie Janssen28bf0072015-09-21 10:49:08 -0700445 timer_time.it_value.tv_sec = (next->deadline / 1000);
446 timer_time.it_value.tv_nsec = (next->deadline % 1000) * 1000000LL;
447
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800448 // It is entirely unsafe to call timer_settime(2) with a zeroed timerspec
449 // for timers with *_ALARM clock IDs. Although the man page states that the
450 // timer would be canceled, the current behavior (as of Linux kernel 3.17)
451 // is that the callback is issued immediately. The only way to cancel an
452 // *_ALARM timer is to delete the timer. But unfortunately, deleting and
453 // re-creating a timer is rather expensive; every timer_create(2) spawns a
454 // new thread. So we simply set the timer to fire at the largest possible
455 // time.
Marie Janssen28bf0072015-09-21 10:49:08 -0700456 //
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800457 // If we've reached this code path, we're going to grab a wake lock and
458 // wait for the next timer to fire. In that case, there's no reason to
459 // have a pending wakeup timer so we simply cancel it.
Marie Janssen28bf0072015-09-21 10:49:08 -0700460 struct itimerspec end_of_time;
461 memset(&end_of_time, 0, sizeof(end_of_time));
462 end_of_time.it_value.tv_sec = (time_t)(1LL << (sizeof(time_t) * 8 - 2));
463 timer_settime(wakeup_timer, TIMER_ABSTIME, &end_of_time, NULL);
464 } else {
465 // WARNING: do not attempt to use relative timers with *_ALARM clock IDs
466 // in kernels before 3.17 unless you have the following patch:
467 // https://lkml.org/lkml/2014/7/7/576
468 struct itimerspec wakeup_time;
469 memset(&wakeup_time, 0, sizeof(wakeup_time));
470
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700471 wakeup_time.it_value.tv_sec = (next->deadline / 1000);
472 wakeup_time.it_value.tv_nsec = (next->deadline % 1000) * 1000000LL;
Marie Janssen28bf0072015-09-21 10:49:08 -0700473 if (timer_settime(wakeup_timer, TIMER_ABSTIME, &wakeup_time, NULL) == -1)
Myles Watsonb55040c2016-10-19 13:15:34 -0700474 LOG_ERROR(LOG_TAG, "%s unable to set wakeup timer: %s", __func__,
475 strerror(errno));
Zach Johnson518a94e2015-03-23 18:07:46 -0700476 }
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700477
Zach Johnson518a94e2015-03-23 18:07:46 -0700478done:
Myles Watsonb55040c2016-10-19 13:15:34 -0700479 timer_set =
480 timer_time.it_value.tv_sec != 0 || timer_time.it_value.tv_nsec != 0;
Pavlin Radoslavov4be43962015-05-08 19:39:31 -0700481 if (timer_was_set && !timer_set) {
Pavlin Radoslavovd2e25082015-08-24 16:29:21 -0700482 wakelock_release();
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700483 }
Zach Johnson518a94e2015-03-23 18:07:46 -0700484
Marie Janssen28bf0072015-09-21 10:49:08 -0700485 if (timer_settime(timer, TIMER_ABSTIME, &timer_time, NULL) == -1)
Marie Janssendb554582015-06-26 14:53:46 -0700486 LOG_ERROR(LOG_TAG, "%s unable to set timer: %s", __func__, strerror(errno));
Zach Johnson081e4b62015-03-25 23:32:11 -0700487
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800488 // If next expiration was in the past (e.g. short timer that got context
489 // switched) then the timer might have diarmed itself. Detect this case and
490 // work around it by manually signalling the |alarm_expired| semaphore.
Zach Johnson081e4b62015-03-25 23:32:11 -0700491 //
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800492 // It is possible that the timer was actually super short (a few
493 // milliseconds) and the timer expired normally before we called
494 // |timer_gettime|. Worst case, |alarm_expired| is signaled twice for that
495 // alarm. Nothing bad should happen in that case though since the callback
496 // dispatch function checks to make sure the timer at the head of the list
497 // actually expired.
Zach Johnson081e4b62015-03-25 23:32:11 -0700498 if (timer_set) {
499 struct itimerspec time_to_expire;
500 timer_gettime(timer, &time_to_expire);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800501 if (time_to_expire.it_value.tv_sec == 0 &&
502 time_to_expire.it_value.tv_nsec == 0) {
Myles Watsonb55040c2016-10-19 13:15:34 -0700503 LOG_DEBUG(
504 LOG_TAG,
505 "%s alarm expiration too close for posix timers, switching to guns",
506 __func__);
Zach Johnson081e4b62015-03-25 23:32:11 -0700507 semaphore_post(alarm_expired);
508 }
509 }
510}
511
Myles Watsonb55040c2016-10-19 13:15:34 -0700512void alarm_register_processing_queue(fixed_queue_t* queue, thread_t* thread) {
Jack Hef2af1c42016-12-13 01:59:12 -0800513 CHECK(queue != NULL);
514 CHECK(thread != NULL);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800515
516 fixed_queue_register_dequeue(queue, thread_get_reactor(thread),
517 alarm_queue_ready, NULL);
518}
519
Myles Watsonb55040c2016-10-19 13:15:34 -0700520void alarm_unregister_processing_queue(fixed_queue_t* queue) {
Jack Hef2af1c42016-12-13 01:59:12 -0800521 CHECK(alarms != NULL);
522 CHECK(queue != NULL);
Pavlin Radoslavov93659162016-05-02 10:52:07 -0700523
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800524 fixed_queue_unregister_dequeue(queue);
Pavlin Radoslavov93659162016-05-02 10:52:07 -0700525
526 // Cancel all alarms that are using this queue
Marie Janssen21da6372016-11-02 18:31:55 -0700527 std::lock_guard<std::mutex> lock(alarms_mutex);
Myles Watsonb55040c2016-10-19 13:15:34 -0700528 for (list_node_t* node = list_begin(alarms); node != list_end(alarms);) {
529 alarm_t* alarm = (alarm_t*)list_node(node);
Pavlin Radoslavov93659162016-05-02 10:52:07 -0700530 node = list_next(node);
531 // TODO: Each module is responsible for tearing down its alarms; currently,
532 // this is not the case. In the future, this check should be replaced by
533 // an assert.
Myles Watsonb55040c2016-10-19 13:15:34 -0700534 if (alarm->queue == queue) alarm_cancel_internal(alarm);
Pavlin Radoslavov93659162016-05-02 10:52:07 -0700535 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800536}
537
Myles Watsonb55040c2016-10-19 13:15:34 -0700538static void alarm_queue_ready(fixed_queue_t* queue, UNUSED_ATTR void* context) {
Jack Hef2af1c42016-12-13 01:59:12 -0800539 CHECK(queue != NULL);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800540
Marie Janssen21da6372016-11-02 18:31:55 -0700541 std::unique_lock<std::mutex> lock(alarms_mutex);
Myles Watsonb55040c2016-10-19 13:15:34 -0700542 alarm_t* alarm = (alarm_t*)fixed_queue_try_dequeue(queue);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800543 if (alarm == NULL) {
Myles Watsonb55040c2016-10-19 13:15:34 -0700544 return; // The alarm was probably canceled
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800545 }
546
547 //
548 // If the alarm is not periodic, we've fully serviced it now, and can reset
549 // some of its internal state. This is useful to distinguish between expired
550 // alarms and active ones.
551 //
552 alarm_callback_t callback = alarm->callback;
Myles Watsonb55040c2016-10-19 13:15:34 -0700553 void* data = alarm->data;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800554 period_ms_t deadline = alarm->deadline;
555 if (alarm->is_periodic) {
556 // The periodic alarm has been rescheduled and alarm->deadline has been
557 // updated, hence we need to use the previous deadline.
558 deadline = alarm->prev_deadline;
559 } else {
560 alarm->deadline = 0;
561 alarm->callback = NULL;
562 alarm->data = NULL;
Kamal Negia8215322016-09-19 14:16:56 +0530563 alarm->queue = NULL;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800564 }
565
Marie Janssen21da6372016-11-02 18:31:55 -0700566 std::lock_guard<std::recursive_mutex> cb_lock(*alarm->callback_mutex);
567 lock.unlock();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800568
569 period_ms_t t0 = now();
570 callback(data);
571 period_ms_t t1 = now();
572
573 // Update the statistics
Jack Hef2af1c42016-12-13 01:59:12 -0800574 CHECK(t1 >= t0);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800575 period_ms_t delta = t1 - t0;
576 update_scheduling_stats(&alarm->stats, t0, deadline, delta);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800577}
578
Zach Johnson081e4b62015-03-25 23:32:11 -0700579// Callback function for wake alarms and our posix timer
Myles Watsonb55040c2016-10-19 13:15:34 -0700580static void timer_callback(UNUSED_ATTR void* ptr) {
Zach Johnson081e4b62015-03-25 23:32:11 -0700581 semaphore_post(alarm_expired);
582}
583
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800584// Function running on |dispatcher_thread| that performs the following:
585// (1) Receives a signal using |alarm_exired| that the alarm has expired
586// (2) Dispatches the alarm callback for processing by the corresponding
587// thread for that alarm.
Myles Watsonb55040c2016-10-19 13:15:34 -0700588static void callback_dispatch(UNUSED_ATTR void* context) {
Zach Johnson081e4b62015-03-25 23:32:11 -0700589 while (true) {
590 semaphore_wait(alarm_expired);
Myles Watsonb55040c2016-10-19 13:15:34 -0700591 if (!dispatcher_thread_active) break;
Zach Johnson081e4b62015-03-25 23:32:11 -0700592
Marie Janssen21da6372016-11-02 18:31:55 -0700593 std::lock_guard<std::mutex> lock(alarms_mutex);
Myles Watsonb55040c2016-10-19 13:15:34 -0700594 alarm_t* alarm;
Zach Johnson081e4b62015-03-25 23:32:11 -0700595
596 // Take into account that the alarm may get cancelled before we get to it.
597 // We're done here if there are no alarms or the alarm at the front is in
Marie Janssen21da6372016-11-02 18:31:55 -0700598 // the future. Exit right away since there's nothing left to do.
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800599 if (list_is_empty(alarms) ||
Myles Watsonb55040c2016-10-19 13:15:34 -0700600 (alarm = static_cast<alarm_t*>(list_front(alarms)))->deadline > now()) {
Zach Johnson081e4b62015-03-25 23:32:11 -0700601 reschedule_root_alarm();
Zach Johnson081e4b62015-03-25 23:32:11 -0700602 continue;
603 }
604
605 list_remove(alarms, alarm);
606
Zach Johnson081e4b62015-03-25 23:32:11 -0700607 if (alarm->is_periodic) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800608 alarm->prev_deadline = alarm->deadline;
609 schedule_next_instance(alarm);
610 alarm->stats.rescheduled_count++;
Zach Johnson081e4b62015-03-25 23:32:11 -0700611 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800612 reschedule_root_alarm();
Zach Johnson081e4b62015-03-25 23:32:11 -0700613
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800614 // Enqueue the alarm for processing
615 fixed_queue_enqueue(alarm->queue, alarm);
Zach Johnson081e4b62015-03-25 23:32:11 -0700616 }
Andre Eisenbachcae219f2015-05-18 09:41:06 -0700617
Marie Janssendb554582015-06-26 14:53:46 -0700618 LOG_DEBUG(LOG_TAG, "%s Callback thread exited", __func__);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700619}
Marie Janssen28bf0072015-09-21 10:49:08 -0700620
Myles Watsonb55040c2016-10-19 13:15:34 -0700621static bool timer_create_internal(const clockid_t clock_id, timer_t* timer) {
Jack Hef2af1c42016-12-13 01:59:12 -0800622 CHECK(timer != NULL);
Marie Janssen28bf0072015-09-21 10:49:08 -0700623
624 struct sigevent sigevent;
Philip Cuadraeaa42772017-03-23 10:10:34 -0700625 // create timer with RT priority thread
626 pthread_attr_t thread_attr;
627 pthread_attr_init(&thread_attr);
628 pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO);
629 struct sched_param param;
630 param.sched_priority = THREAD_RT_PRIORITY;
631 pthread_attr_setschedparam(&thread_attr, &param);
632
Marie Janssen28bf0072015-09-21 10:49:08 -0700633 memset(&sigevent, 0, sizeof(sigevent));
634 sigevent.sigev_notify = SIGEV_THREAD;
635 sigevent.sigev_notify_function = (void (*)(union sigval))timer_callback;
Philip Cuadraeaa42772017-03-23 10:10:34 -0700636 sigevent.sigev_notify_attributes = (void*)(&thread_attr);
Marie Janssen28bf0072015-09-21 10:49:08 -0700637 if (timer_create(clock_id, &sigevent, timer) == -1) {
Myles Watsonb55040c2016-10-19 13:15:34 -0700638 LOG_ERROR(LOG_TAG, "%s unable to create timer with clock %d: %s", __func__,
639 clock_id, strerror(errno));
Pavlin Radoslavov4b7f5602016-01-22 10:44:40 -0800640 if (clock_id == CLOCK_BOOTTIME_ALARM) {
Myles Watsonb55040c2016-10-19 13:15:34 -0700641 LOG_ERROR(LOG_TAG,
642 "The kernel might not have support for "
643 "timer_create(CLOCK_BOOTTIME_ALARM): "
644 "https://lwn.net/Articles/429925/");
645 LOG_ERROR(LOG_TAG,
646 "See following patches: "
647 "https://git.kernel.org/cgit/linux/kernel/git/torvalds/"
648 "linux.git/log/?qt=grep&q=CLOCK_BOOTTIME_ALARM");
Pavlin Radoslavov4b7f5602016-01-22 10:44:40 -0800649 }
Marie Janssen28bf0072015-09-21 10:49:08 -0700650 return false;
651 }
652
653 return true;
654}
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800655
Myles Watsonb55040c2016-10-19 13:15:34 -0700656static void update_scheduling_stats(alarm_stats_t* stats, period_ms_t now_ms,
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800657 period_ms_t deadline_ms,
Myles Watsonb55040c2016-10-19 13:15:34 -0700658 period_ms_t execution_delta_ms) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800659 stats->total_updates++;
660 stats->last_update_ms = now_ms;
661
662 update_stat(&stats->callback_execution, execution_delta_ms);
663
664 if (deadline_ms < now_ms) {
665 // Overdue scheduling
666 period_ms_t delta_ms = now_ms - deadline_ms;
667 update_stat(&stats->overdue_scheduling, delta_ms);
668 } else if (deadline_ms > now_ms) {
669 // Premature scheduling
670 period_ms_t delta_ms = deadline_ms - now_ms;
671 update_stat(&stats->premature_scheduling, delta_ms);
672 }
673}
674
Myles Watsonb55040c2016-10-19 13:15:34 -0700675static void dump_stat(int fd, stat_t* stat, const char* description) {
676 period_ms_t average_time_ms = 0;
677 if (stat->count != 0) average_time_ms = stat->total_ms / stat->count;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800678
Myles Watsonb55040c2016-10-19 13:15:34 -0700679 dprintf(fd, "%-51s: %llu / %llu / %llu\n", description,
680 (unsigned long long)stat->total_ms, (unsigned long long)stat->max_ms,
681 (unsigned long long)average_time_ms);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800682}
683
Myles Watsonb55040c2016-10-19 13:15:34 -0700684void alarm_debug_dump(int fd) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800685 dprintf(fd, "\nBluetooth Alarms Statistics:\n");
686
Marie Janssen21da6372016-11-02 18:31:55 -0700687 std::lock_guard<std::mutex> lock(alarms_mutex);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800688
689 if (alarms == NULL) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800690 dprintf(fd, " None\n");
691 return;
692 }
693
694 period_ms_t just_now = now();
695
696 dprintf(fd, " Total Alarms: %zu\n\n", list_length(alarms));
697
698 // Dump info for each alarm
Myles Watsonb55040c2016-10-19 13:15:34 -0700699 for (list_node_t* node = list_begin(alarms); node != list_end(alarms);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800700 node = list_next(node)) {
Myles Watsonb55040c2016-10-19 13:15:34 -0700701 alarm_t* alarm = (alarm_t*)list_node(node);
702 alarm_stats_t* stats = &alarm->stats;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800703
704 dprintf(fd, " Alarm : %s (%s)\n", stats->name,
705 (alarm->is_periodic) ? "PERIODIC" : "SINGLE");
706
Ajay Panickereeec3be2016-02-22 12:14:04 -0800707 dprintf(fd, "%-51s: %zu / %zu / %zu / %zu\n",
Pavlin Radoslavov399f2172016-04-19 17:30:19 -0700708 " Action counts (sched/resched/exec/cancel)",
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800709 stats->scheduled_count, stats->rescheduled_count,
710 stats->callback_execution.count, stats->canceled_count);
711
Ajay Panickereeec3be2016-02-22 12:14:04 -0800712 dprintf(fd, "%-51s: %zu / %zu\n",
713 " Deviation counts (overdue/premature)",
Myles Watsonb55040c2016-10-19 13:15:34 -0700714 stats->overdue_scheduling.count, stats->premature_scheduling.count);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800715
Ajay Panickereeec3be2016-02-22 12:14:04 -0800716 dprintf(fd, "%-51s: %llu / %llu / %lld\n",
717 " Time in ms (since creation/interval/remaining)",
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800718 (unsigned long long)(just_now - alarm->creation_time),
Myles Watsonb55040c2016-10-19 13:15:34 -0700719 (unsigned long long)alarm->period,
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800720 (long long)(alarm->deadline - just_now));
721
Ajay Panickereeec3be2016-02-22 12:14:04 -0800722 dump_stat(fd, &stats->callback_execution,
723 " Callback execution time in ms (total/max/avg)");
724
725 dump_stat(fd, &stats->overdue_scheduling,
726 " Overdue scheduling time in ms (total/max/avg)");
727
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800728 dump_stat(fd, &stats->premature_scheduling,
Ajay Panickereeec3be2016-02-22 12:14:04 -0800729 " Premature scheduling time in ms (total/max/avg)");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800730
731 dprintf(fd, "\n");
732 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800733}