Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 Radoslavov | 1097801 | 2016-01-21 18:04:36 -0800 | [diff] [blame] | 19 | #include "include/bt_target.h" |
| 20 | |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 21 | #define LOG_TAG "bt_osi_alarm" |
| 22 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 23 | #include "osi/include/alarm.h" |
| 24 | |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 25 | #include <base/logging.h> |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 26 | #include <errno.h> |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 27 | #include <fcntl.h> |
Michael Wright | 1afbe15 | 2014-07-17 14:45:58 -0700 | [diff] [blame] | 28 | #include <inttypes.h> |
Elliott Hughes | b9164f4 | 2015-01-28 15:31:26 -0800 | [diff] [blame] | 29 | #include <malloc.h> |
Philip Cuadra | eaa4277 | 2017-03-23 10:10:34 -0700 | [diff] [blame^] | 30 | #include <pthread.h> |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 31 | #include <signal.h> |
| 32 | #include <string.h> |
| 33 | #include <time.h> |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 34 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 35 | #include <hardware/bluetooth.h> |
| 36 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 37 | #include <mutex> |
| 38 | |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 39 | #include "osi/include/allocator.h" |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 40 | #include "osi/include/fixed_queue.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 41 | #include "osi/include/list.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 42 | #include "osi/include/log.h" |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 43 | #include "osi/include/osi.h" |
| 44 | #include "osi/include/semaphore.h" |
| 45 | #include "osi/include/thread.h" |
Pavlin Radoslavov | d2e2508 | 2015-08-24 16:29:21 -0700 | [diff] [blame] | 46 | #include "osi/include/wakelock.h" |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 47 | |
Philip Cuadra | eaa4277 | 2017-03-23 10:10:34 -0700 | [diff] [blame^] | 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; |
Andre Eisenbach | 41a91a5 | 2015-10-23 10:40:36 -0700 | [diff] [blame] | 51 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 52 | typedef 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 |
| 59 | typedef struct { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 60 | const char* name; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 61 | 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 Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 71 | struct alarm_t { |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 72 | // The mutex is held while the callback for this alarm is being executed. |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 73 | // It allows us to release the coarse-grained monitor lock while a |
| 74 | // potentially long-running callback is executing. |alarm_cancel| uses this |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 75 | // mutex to provide a guarantee to its caller that the callback will not be |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 76 | // in progress when it returns. |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 77 | std::recursive_mutex* callback_mutex; |
Sharvil Nanavati | f2bf230 | 2015-06-10 22:00:16 -0700 | [diff] [blame] | 78 | period_ms_t creation_time; |
Zach Johnson | 71864f4 | 2015-03-11 01:55:27 -0700 | [diff] [blame] | 79 | period_ms_t period; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 80 | period_ms_t deadline; |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 81 | period_ms_t prev_deadline; // Previous deadline - used for accounting of |
| 82 | // periodic timers |
Zach Johnson | e2c57aa | 2015-03-23 13:59:10 -0700 | [diff] [blame] | 83 | bool is_periodic; |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 84 | fixed_queue_t* queue; // The processing queue to add this alarm to |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 85 | alarm_callback_t callback; |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 86 | void* data; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 87 | alarm_stats_t stats; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 90 | // 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. |
| 94 | int64_t TIMER_INTERVAL_FOR_WAKELOCK_IN_MS = 3000; |
| 95 | static const clockid_t CLOCK_ID = CLOCK_BOOTTIME; |
Pavlin Radoslavov | 1097801 | 2016-01-21 18:04:36 -0800 | [diff] [blame] | 96 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 97 | #if (KERNEL_MISSING_CLOCK_BOOTTIME_ALARM == TRUE) |
Pavlin Radoslavov | 1097801 | 2016-01-21 18:04:36 -0800 | [diff] [blame] | 98 | static const clockid_t CLOCK_ID_ALARM = CLOCK_BOOTTIME; |
| 99 | #else |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 100 | static const clockid_t CLOCK_ID_ALARM = CLOCK_BOOTTIME_ALARM; |
Pavlin Radoslavov | 1097801 | 2016-01-21 18:04:36 -0800 | [diff] [blame] | 101 | #endif |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 102 | |
| 103 | // This mutex ensures that the |alarm_set|, |alarm_cancel|, and alarm callback |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 104 | // functions execute serially and not concurrently. As a result, this mutex |
| 105 | // also protects the |alarms| list. |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 106 | static std::mutex alarms_mutex; |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 107 | static list_t* alarms; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 108 | static timer_t timer; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 109 | static timer_t wakeup_timer; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 110 | static bool timer_set; |
| 111 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 112 | // All alarm callbacks are dispatched from |dispatcher_thread| |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 113 | static thread_t* dispatcher_thread; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 114 | static bool dispatcher_thread_active; |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 115 | static semaphore_t* alarm_expired; |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 116 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 117 | // Default alarm callback thread and queue |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 118 | static thread_t* default_callback_thread; |
| 119 | static fixed_queue_t* default_callback_queue; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 120 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 121 | static alarm_t* alarm_new_internal(const char* name, bool is_periodic); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 122 | static bool lazy_initialize(void); |
| 123 | static period_ms_t now(void); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 124 | static void alarm_set_internal(alarm_t* alarm, period_ms_t period, |
| 125 | alarm_callback_t cb, void* data, |
| 126 | fixed_queue_t* queue); |
| 127 | static void alarm_cancel_internal(alarm_t* alarm); |
| 128 | static void remove_pending_alarm(alarm_t* alarm); |
| 129 | static void schedule_next_instance(alarm_t* alarm); |
Zach Johnson | e2c57aa | 2015-03-23 13:59:10 -0700 | [diff] [blame] | 130 | static void reschedule_root_alarm(void); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 131 | static void alarm_queue_ready(fixed_queue_t* queue, void* context); |
| 132 | static void timer_callback(void* data); |
| 133 | static void callback_dispatch(void* context); |
| 134 | static bool timer_create_internal(const clockid_t clock_id, timer_t* timer); |
| 135 | static void update_scheduling_stats(alarm_stats_t* stats, period_ms_t now_ms, |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 136 | period_ms_t deadline_ms, |
| 137 | period_ms_t execution_delta_ms); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 138 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 139 | static void update_stat(stat_t* stat, period_ms_t delta) { |
| 140 | if (stat->max_ms < delta) stat->max_ms = delta; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 141 | stat->total_ms += delta; |
| 142 | stat->count++; |
| 143 | } |
| 144 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 145 | alarm_t* alarm_new(const char* name) { return alarm_new_internal(name, false); } |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 146 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 147 | alarm_t* alarm_new_periodic(const char* name) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 148 | return alarm_new_internal(name, true); |
| 149 | } |
| 150 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 151 | static alarm_t* alarm_new_internal(const char* name, bool is_periodic) { |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 152 | // Make sure we have a list we can insert alarms into. |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 153 | if (!alarms && !lazy_initialize()) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 154 | CHECK(false); // if initialization failed, we should not continue |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 155 | return NULL; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 156 | } |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 157 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 158 | alarm_t* ret = static_cast<alarm_t*>(osi_calloc(sizeof(alarm_t))); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 159 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 160 | ret->callback_mutex = new std::recursive_mutex; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 161 | ret->is_periodic = is_periodic; |
Pavlin Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 162 | ret->stats.name = osi_strdup(name); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 163 | // NOTE: The stats were reset by osi_calloc() above |
| 164 | |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 165 | return ret; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 168 | void alarm_free(alarm_t* alarm) { |
| 169 | if (!alarm) return; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 170 | |
| 171 | alarm_cancel(alarm); |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 172 | delete alarm->callback_mutex; |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 173 | osi_free((void*)alarm->stats.name); |
Zach Johnson | ee2aa45 | 2014-08-26 20:16:03 -0700 | [diff] [blame] | 174 | osi_free(alarm); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 177 | period_ms_t alarm_get_remaining_ms(const alarm_t* alarm) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 178 | CHECK(alarm != NULL); |
Andre Eisenbach | 165332b | 2015-07-15 12:35:15 -0700 | [diff] [blame] | 179 | period_ms_t remaining_ms = 0; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 180 | period_ms_t just_now = now(); |
Andre Eisenbach | 165332b | 2015-07-15 12:35:15 -0700 | [diff] [blame] | 181 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 182 | std::lock_guard<std::mutex> lock(alarms_mutex); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 183 | if (alarm->deadline > just_now) remaining_ms = alarm->deadline - just_now; |
Andre Eisenbach | 165332b | 2015-07-15 12:35:15 -0700 | [diff] [blame] | 184 | |
| 185 | return remaining_ms; |
VenkatRaghavan VijayaRaghavan | 76356ae | 2015-04-21 11:32:29 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 188 | void alarm_set(alarm_t* alarm, period_ms_t interval_ms, alarm_callback_t cb, |
| 189 | void* data) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 190 | alarm_set_on_queue(alarm, interval_ms, cb, data, default_callback_queue); |
Zach Johnson | 71864f4 | 2015-03-11 01:55:27 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 193 | void alarm_set_on_queue(alarm_t* alarm, period_ms_t interval_ms, |
| 194 | alarm_callback_t cb, void* data, fixed_queue_t* queue) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 195 | CHECK(queue != NULL); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 196 | alarm_set_internal(alarm, interval_ms, cb, data, queue); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Runs in exclusion with alarm_cancel and timer_callback. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 200 | static void alarm_set_internal(alarm_t* alarm, period_ms_t period, |
| 201 | alarm_callback_t cb, void* data, |
| 202 | fixed_queue_t* queue) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 203 | CHECK(alarms != NULL); |
| 204 | CHECK(alarm != NULL); |
| 205 | CHECK(cb != NULL); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 206 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 207 | std::lock_guard<std::mutex> lock(alarms_mutex); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 208 | |
Sharvil Nanavati | f2bf230 | 2015-06-10 22:00:16 -0700 | [diff] [blame] | 209 | alarm->creation_time = now(); |
Zach Johnson | e2c57aa | 2015-03-23 13:59:10 -0700 | [diff] [blame] | 210 | alarm->period = period; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 211 | alarm->queue = queue; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 212 | alarm->callback = cb; |
| 213 | alarm->data = data; |
| 214 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 215 | schedule_next_instance(alarm); |
| 216 | alarm->stats.scheduled_count++; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 219 | void alarm_cancel(alarm_t* alarm) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 220 | CHECK(alarms != NULL); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 221 | if (!alarm) return; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 222 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 223 | { |
| 224 | std::lock_guard<std::mutex> lock(alarms_mutex); |
| 225 | alarm_cancel_internal(alarm); |
| 226 | } |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 227 | |
Pavlin Radoslavov | 9365916 | 2016-05-02 10:52:07 -0700 | [diff] [blame] | 228 | // If the callback for |alarm| is in progress, wait here until it completes. |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 229 | std::lock_guard<std::recursive_mutex> lock(*alarm->callback_mutex); |
Pavlin Radoslavov | 9365916 | 2016-05-02 10:52:07 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Internal implementation of canceling an alarm. |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 233 | // The caller must hold the |alarms_mutex| |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 234 | static void alarm_cancel_internal(alarm_t* alarm) { |
| 235 | bool needs_reschedule = |
| 236 | (!list_is_empty(alarms) && list_front(alarms) == alarm); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 237 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 238 | remove_pending_alarm(alarm); |
| 239 | |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 240 | alarm->deadline = 0; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 241 | alarm->prev_deadline = 0; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 242 | alarm->callback = NULL; |
| 243 | alarm->data = NULL; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 244 | alarm->stats.canceled_count++; |
Pavlin Radoslavov | 9365916 | 2016-05-02 10:52:07 -0700 | [diff] [blame] | 245 | alarm->queue = NULL; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 246 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 247 | if (needs_reschedule) reschedule_root_alarm(); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 250 | bool alarm_is_scheduled(const alarm_t* alarm) { |
| 251 | if ((alarms == NULL) || (alarm == NULL)) return false; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 252 | return (alarm->callback != NULL); |
| 253 | } |
| 254 | |
Pavlin Radoslavov | 0933b40 | 2015-06-01 16:08:18 -0700 | [diff] [blame] | 255 | void alarm_cleanup(void) { |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 256 | // If lazy_initialize never ran there is nothing else to do |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 257 | if (!alarms) return; |
Zach Johnson | c6a1c26 | 2015-05-18 14:48:50 -0700 | [diff] [blame] | 258 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 259 | dispatcher_thread_active = false; |
Andre Eisenbach | cae219f | 2015-05-18 09:41:06 -0700 | [diff] [blame] | 260 | semaphore_post(alarm_expired); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 261 | thread_free(dispatcher_thread); |
| 262 | dispatcher_thread = NULL; |
| 263 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 264 | std::lock_guard<std::mutex> lock(alarms_mutex); |
Pavlin Radoslavov | 9334247 | 2016-04-25 12:29:29 -0700 | [diff] [blame] | 265 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 266 | fixed_queue_free(default_callback_queue, NULL); |
| 267 | default_callback_queue = NULL; |
| 268 | thread_free(default_callback_thread); |
| 269 | default_callback_thread = NULL; |
Andre Eisenbach | cae219f | 2015-05-18 09:41:06 -0700 | [diff] [blame] | 270 | |
Pavlin Radoslavov | 9334247 | 2016-04-25 12:29:29 -0700 | [diff] [blame] | 271 | timer_delete(wakeup_timer); |
| 272 | timer_delete(timer); |
Andre Eisenbach | cae219f | 2015-05-18 09:41:06 -0700 | [diff] [blame] | 273 | semaphore_free(alarm_expired); |
| 274 | alarm_expired = NULL; |
Pavlin Radoslavov | 9334247 | 2016-04-25 12:29:29 -0700 | [diff] [blame] | 275 | |
Andre Eisenbach | cae219f | 2015-05-18 09:41:06 -0700 | [diff] [blame] | 276 | list_free(alarms); |
| 277 | alarms = NULL; |
Andre Eisenbach | cae219f | 2015-05-18 09:41:06 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 280 | static bool lazy_initialize(void) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 281 | CHECK(alarms == NULL); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 282 | |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 283 | // 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 Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 288 | std::lock_guard<std::mutex> lock(alarms_mutex); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 289 | |
| 290 | alarms = list_new(NULL); |
| 291 | if (!alarms) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 292 | LOG_ERROR(LOG_TAG, "%s unable to allocate alarm list.", __func__); |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 293 | goto error; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 296 | if (!timer_create_internal(CLOCK_ID, &timer)) goto error; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 297 | timer_initialized = true; |
| 298 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 299 | if (!timer_create_internal(CLOCK_ID_ALARM, &wakeup_timer)) goto error; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 300 | wakeup_timer_initialized = true; |
Zach Johnson | 518a94e | 2015-03-23 18:07:46 -0700 | [diff] [blame] | 301 | |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 302 | alarm_expired = semaphore_new(0); |
| 303 | if (!alarm_expired) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 304 | LOG_ERROR(LOG_TAG, "%s unable to create alarm expired semaphore", __func__); |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 305 | goto error; |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 308 | default_callback_thread = |
| 309 | thread_new_sized("alarm_default_callbacks", SIZE_MAX); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 310 | 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 Cuadra | eaa4277 | 2017-03-23 10:10:34 -0700 | [diff] [blame^] | 315 | thread_set_rt_priority(default_callback_thread, THREAD_RT_PRIORITY); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 316 | 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 Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 328 | LOG_ERROR(LOG_TAG, "%s unable to create alarm callback thread.", __func__); |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 329 | goto error; |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 330 | } |
Philip Cuadra | eaa4277 | 2017-03-23 10:10:34 -0700 | [diff] [blame^] | 331 | thread_set_rt_priority(dispatcher_thread, THREAD_RT_PRIORITY); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 332 | thread_post(dispatcher_thread, callback_dispatch, NULL); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 333 | return true; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 334 | |
| 335 | error: |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 336 | fixed_queue_free(default_callback_queue, NULL); |
| 337 | default_callback_queue = NULL; |
| 338 | thread_free(default_callback_thread); |
| 339 | default_callback_thread = NULL; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 340 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 341 | thread_free(dispatcher_thread); |
| 342 | dispatcher_thread = NULL; |
| 343 | |
| 344 | dispatcher_thread_active = false; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 345 | |
| 346 | semaphore_free(alarm_expired); |
| 347 | alarm_expired = NULL; |
| 348 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 349 | if (wakeup_timer_initialized) timer_delete(wakeup_timer); |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 350 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 351 | if (timer_initialized) timer_delete(timer); |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 352 | |
| 353 | list_free(alarms); |
| 354 | alarms = NULL; |
| 355 | |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 356 | return false; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static period_ms_t now(void) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 360 | CHECK(alarms != NULL); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 361 | |
| 362 | struct timespec ts; |
| 363 | if (clock_gettime(CLOCK_ID, &ts) == -1) { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 364 | LOG_ERROR(LOG_TAG, "%s unable to get current time: %s", __func__, |
| 365 | strerror(errno)); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL); |
| 370 | } |
| 371 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 372 | // Remove alarm from internal alarm list and the processing queue |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 373 | // The caller must hold the |alarms_mutex| |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 374 | static void remove_pending_alarm(alarm_t* alarm) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 375 | 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 Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 382 | // Must be called with |alarms_mutex| held |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 383 | static void schedule_next_instance(alarm_t* alarm) { |
Zach Johnson | 71864f4 | 2015-03-11 01:55:27 -0700 | [diff] [blame] | 384 | // 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 386 | bool needs_reschedule = |
| 387 | (!list_is_empty(alarms) && list_front(alarms) == alarm); |
| 388 | if (alarm->callback) remove_pending_alarm(alarm); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 389 | |
Zach Johnson | e2c57aa | 2015-03-23 13:59:10 -0700 | [diff] [blame] | 390 | // Calculate the next deadline for this alarm |
| 391 | period_ms_t just_now = now(); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 392 | period_ms_t ms_into_period = 0; |
Pavlin Radoslavov | 399f217 | 2016-04-19 17:30:19 -0700 | [diff] [blame] | 393 | if ((alarm->is_periodic) && (alarm->period != 0)) |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 394 | ms_into_period = ((just_now - alarm->creation_time) % alarm->period); |
Zach Johnson | e2c57aa | 2015-03-23 13:59:10 -0700 | [diff] [blame] | 395 | alarm->deadline = just_now + (alarm->period - ms_into_period); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 396 | |
Zach Johnson | 71864f4 | 2015-03-11 01:55:27 -0700 | [diff] [blame] | 397 | // Add it into the timer list sorted by deadline (earliest deadline first). |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 398 | if (list_is_empty(alarms) || |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 399 | ((alarm_t*)list_front(alarms))->deadline > alarm->deadline) { |
Zach Johnson | 71864f4 | 2015-03-11 01:55:27 -0700 | [diff] [blame] | 400 | list_prepend(alarms, alarm); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 401 | } else { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 402 | 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 Johnson | 71864f4 | 2015-03-11 01:55:27 -0700 | [diff] [blame] | 407 | list_insert_after(alarms, node, alarm); |
| 408 | break; |
| 409 | } |
| 410 | } |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 411 | } |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 412 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 413 | // If the new alarm has the earliest deadline, we need to re-evaluate our |
| 414 | // schedule. |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 415 | if (needs_reschedule || |
| 416 | (!list_is_empty(alarms) && list_front(alarms) == alarm)) { |
Zach Johnson | e2c57aa | 2015-03-23 13:59:10 -0700 | [diff] [blame] | 417 | reschedule_root_alarm(); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 418 | } |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 421 | // NOTE: must be called with |alarms_mutex| held |
Zach Johnson | e2c57aa | 2015-03-23 13:59:10 -0700 | [diff] [blame] | 422 | static void reschedule_root_alarm(void) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 423 | CHECK(alarms != NULL); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 424 | |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 425 | const bool timer_was_set = timer_set; |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 426 | alarm_t* next; |
Pavlin Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 427 | int64_t next_expiration; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 428 | |
| 429 | // If used in a zeroed state, disarms the timer. |
| 430 | struct itimerspec timer_time; |
| 431 | memset(&timer_time, 0, sizeof(timer_time)); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 432 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 433 | if (list_is_empty(alarms)) goto done; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 434 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 435 | next = static_cast<alarm_t*>(list_front(alarms)); |
Pavlin Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 436 | next_expiration = next->deadline - now(); |
Zach Johnson | 518a94e | 2015-03-23 18:07:46 -0700 | [diff] [blame] | 437 | if (next_expiration < TIMER_INTERVAL_FOR_WAKELOCK_IN_MS) { |
| 438 | if (!timer_set) { |
Pavlin Radoslavov | d2e2508 | 2015-08-24 16:29:21 -0700 | [diff] [blame] | 439 | if (!wakelock_acquire()) { |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 440 | LOG_ERROR(LOG_TAG, "%s unable to acquire wake lock", __func__); |
Zach Johnson | 518a94e | 2015-03-23 18:07:46 -0700 | [diff] [blame] | 441 | goto done; |
Zach Johnson | 71864f4 | 2015-03-11 01:55:27 -0700 | [diff] [blame] | 442 | } |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 445 | timer_time.it_value.tv_sec = (next->deadline / 1000); |
| 446 | timer_time.it_value.tv_nsec = (next->deadline % 1000) * 1000000LL; |
| 447 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 448 | // 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 Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 456 | // |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 457 | // 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 Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 460 | 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 Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 471 | wakeup_time.it_value.tv_sec = (next->deadline / 1000); |
| 472 | wakeup_time.it_value.tv_nsec = (next->deadline % 1000) * 1000000LL; |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 473 | if (timer_settime(wakeup_timer, TIMER_ABSTIME, &wakeup_time, NULL) == -1) |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 474 | LOG_ERROR(LOG_TAG, "%s unable to set wakeup timer: %s", __func__, |
| 475 | strerror(errno)); |
Zach Johnson | 518a94e | 2015-03-23 18:07:46 -0700 | [diff] [blame] | 476 | } |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 477 | |
Zach Johnson | 518a94e | 2015-03-23 18:07:46 -0700 | [diff] [blame] | 478 | done: |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 479 | timer_set = |
| 480 | timer_time.it_value.tv_sec != 0 || timer_time.it_value.tv_nsec != 0; |
Pavlin Radoslavov | 4be4396 | 2015-05-08 19:39:31 -0700 | [diff] [blame] | 481 | if (timer_was_set && !timer_set) { |
Pavlin Radoslavov | d2e2508 | 2015-08-24 16:29:21 -0700 | [diff] [blame] | 482 | wakelock_release(); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 483 | } |
Zach Johnson | 518a94e | 2015-03-23 18:07:46 -0700 | [diff] [blame] | 484 | |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 485 | if (timer_settime(timer, TIMER_ABSTIME, &timer_time, NULL) == -1) |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 486 | LOG_ERROR(LOG_TAG, "%s unable to set timer: %s", __func__, strerror(errno)); |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 487 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 488 | // 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 Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 491 | // |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 492 | // 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 Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 498 | if (timer_set) { |
| 499 | struct itimerspec time_to_expire; |
| 500 | timer_gettime(timer, &time_to_expire); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 501 | if (time_to_expire.it_value.tv_sec == 0 && |
| 502 | time_to_expire.it_value.tv_nsec == 0) { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 503 | LOG_DEBUG( |
| 504 | LOG_TAG, |
| 505 | "%s alarm expiration too close for posix timers, switching to guns", |
| 506 | __func__); |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 507 | semaphore_post(alarm_expired); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 512 | void alarm_register_processing_queue(fixed_queue_t* queue, thread_t* thread) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 513 | CHECK(queue != NULL); |
| 514 | CHECK(thread != NULL); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 515 | |
| 516 | fixed_queue_register_dequeue(queue, thread_get_reactor(thread), |
| 517 | alarm_queue_ready, NULL); |
| 518 | } |
| 519 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 520 | void alarm_unregister_processing_queue(fixed_queue_t* queue) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 521 | CHECK(alarms != NULL); |
| 522 | CHECK(queue != NULL); |
Pavlin Radoslavov | 9365916 | 2016-05-02 10:52:07 -0700 | [diff] [blame] | 523 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 524 | fixed_queue_unregister_dequeue(queue); |
Pavlin Radoslavov | 9365916 | 2016-05-02 10:52:07 -0700 | [diff] [blame] | 525 | |
| 526 | // Cancel all alarms that are using this queue |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 527 | std::lock_guard<std::mutex> lock(alarms_mutex); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 528 | for (list_node_t* node = list_begin(alarms); node != list_end(alarms);) { |
| 529 | alarm_t* alarm = (alarm_t*)list_node(node); |
Pavlin Radoslavov | 9365916 | 2016-05-02 10:52:07 -0700 | [diff] [blame] | 530 | 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 534 | if (alarm->queue == queue) alarm_cancel_internal(alarm); |
Pavlin Radoslavov | 9365916 | 2016-05-02 10:52:07 -0700 | [diff] [blame] | 535 | } |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 538 | static void alarm_queue_ready(fixed_queue_t* queue, UNUSED_ATTR void* context) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 539 | CHECK(queue != NULL); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 540 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 541 | std::unique_lock<std::mutex> lock(alarms_mutex); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 542 | alarm_t* alarm = (alarm_t*)fixed_queue_try_dequeue(queue); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 543 | if (alarm == NULL) { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 544 | return; // The alarm was probably canceled |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 545 | } |
| 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 553 | void* data = alarm->data; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 554 | 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 Negi | a821532 | 2016-09-19 14:16:56 +0530 | [diff] [blame] | 563 | alarm->queue = NULL; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 566 | std::lock_guard<std::recursive_mutex> cb_lock(*alarm->callback_mutex); |
| 567 | lock.unlock(); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 568 | |
| 569 | period_ms_t t0 = now(); |
| 570 | callback(data); |
| 571 | period_ms_t t1 = now(); |
| 572 | |
| 573 | // Update the statistics |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 574 | CHECK(t1 >= t0); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 575 | period_ms_t delta = t1 - t0; |
| 576 | update_scheduling_stats(&alarm->stats, t0, deadline, delta); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 579 | // Callback function for wake alarms and our posix timer |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 580 | static void timer_callback(UNUSED_ATTR void* ptr) { |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 581 | semaphore_post(alarm_expired); |
| 582 | } |
| 583 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 584 | // 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 588 | static void callback_dispatch(UNUSED_ATTR void* context) { |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 589 | while (true) { |
| 590 | semaphore_wait(alarm_expired); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 591 | if (!dispatcher_thread_active) break; |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 592 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 593 | std::lock_guard<std::mutex> lock(alarms_mutex); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 594 | alarm_t* alarm; |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 595 | |
| 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 Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 598 | // the future. Exit right away since there's nothing left to do. |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 599 | if (list_is_empty(alarms) || |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 600 | (alarm = static_cast<alarm_t*>(list_front(alarms)))->deadline > now()) { |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 601 | reschedule_root_alarm(); |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 602 | continue; |
| 603 | } |
| 604 | |
| 605 | list_remove(alarms, alarm); |
| 606 | |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 607 | if (alarm->is_periodic) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 608 | alarm->prev_deadline = alarm->deadline; |
| 609 | schedule_next_instance(alarm); |
| 610 | alarm->stats.rescheduled_count++; |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 611 | } |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 612 | reschedule_root_alarm(); |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 613 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 614 | // Enqueue the alarm for processing |
| 615 | fixed_queue_enqueue(alarm->queue, alarm); |
Zach Johnson | 081e4b6 | 2015-03-25 23:32:11 -0700 | [diff] [blame] | 616 | } |
Andre Eisenbach | cae219f | 2015-05-18 09:41:06 -0700 | [diff] [blame] | 617 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 618 | LOG_DEBUG(LOG_TAG, "%s Callback thread exited", __func__); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 619 | } |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 620 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 621 | static bool timer_create_internal(const clockid_t clock_id, timer_t* timer) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 622 | CHECK(timer != NULL); |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 623 | |
| 624 | struct sigevent sigevent; |
Philip Cuadra | eaa4277 | 2017-03-23 10:10:34 -0700 | [diff] [blame^] | 625 | // 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, ¶m); |
| 632 | |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 633 | memset(&sigevent, 0, sizeof(sigevent)); |
| 634 | sigevent.sigev_notify = SIGEV_THREAD; |
| 635 | sigevent.sigev_notify_function = (void (*)(union sigval))timer_callback; |
Philip Cuadra | eaa4277 | 2017-03-23 10:10:34 -0700 | [diff] [blame^] | 636 | sigevent.sigev_notify_attributes = (void*)(&thread_attr); |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 637 | if (timer_create(clock_id, &sigevent, timer) == -1) { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 638 | LOG_ERROR(LOG_TAG, "%s unable to create timer with clock %d: %s", __func__, |
| 639 | clock_id, strerror(errno)); |
Pavlin Radoslavov | 4b7f560 | 2016-01-22 10:44:40 -0800 | [diff] [blame] | 640 | if (clock_id == CLOCK_BOOTTIME_ALARM) { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 641 | 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 Radoslavov | 4b7f560 | 2016-01-22 10:44:40 -0800 | [diff] [blame] | 649 | } |
Marie Janssen | 28bf007 | 2015-09-21 10:49:08 -0700 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | |
| 653 | return true; |
| 654 | } |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 655 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 656 | static void update_scheduling_stats(alarm_stats_t* stats, period_ms_t now_ms, |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 657 | period_ms_t deadline_ms, |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 658 | period_ms_t execution_delta_ms) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 659 | 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 675 | static 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 Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 678 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 679 | 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 Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 684 | void alarm_debug_dump(int fd) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 685 | dprintf(fd, "\nBluetooth Alarms Statistics:\n"); |
| 686 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 687 | std::lock_guard<std::mutex> lock(alarms_mutex); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 688 | |
| 689 | if (alarms == NULL) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 690 | 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 699 | for (list_node_t* node = list_begin(alarms); node != list_end(alarms); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 700 | node = list_next(node)) { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 701 | alarm_t* alarm = (alarm_t*)list_node(node); |
| 702 | alarm_stats_t* stats = &alarm->stats; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 703 | |
| 704 | dprintf(fd, " Alarm : %s (%s)\n", stats->name, |
| 705 | (alarm->is_periodic) ? "PERIODIC" : "SINGLE"); |
| 706 | |
Ajay Panicker | eeec3be | 2016-02-22 12:14:04 -0800 | [diff] [blame] | 707 | dprintf(fd, "%-51s: %zu / %zu / %zu / %zu\n", |
Pavlin Radoslavov | 399f217 | 2016-04-19 17:30:19 -0700 | [diff] [blame] | 708 | " Action counts (sched/resched/exec/cancel)", |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 709 | stats->scheduled_count, stats->rescheduled_count, |
| 710 | stats->callback_execution.count, stats->canceled_count); |
| 711 | |
Ajay Panicker | eeec3be | 2016-02-22 12:14:04 -0800 | [diff] [blame] | 712 | dprintf(fd, "%-51s: %zu / %zu\n", |
| 713 | " Deviation counts (overdue/premature)", |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 714 | stats->overdue_scheduling.count, stats->premature_scheduling.count); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 715 | |
Ajay Panicker | eeec3be | 2016-02-22 12:14:04 -0800 | [diff] [blame] | 716 | dprintf(fd, "%-51s: %llu / %llu / %lld\n", |
| 717 | " Time in ms (since creation/interval/remaining)", |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 718 | (unsigned long long)(just_now - alarm->creation_time), |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 719 | (unsigned long long)alarm->period, |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 720 | (long long)(alarm->deadline - just_now)); |
| 721 | |
Ajay Panicker | eeec3be | 2016-02-22 12:14:04 -0800 | [diff] [blame] | 722 | 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 Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 728 | dump_stat(fd, &stats->premature_scheduling, |
Ajay Panicker | eeec3be | 2016-02-22 12:14:04 -0800 | [diff] [blame] | 729 | " Premature scheduling time in ms (total/max/avg)"); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 730 | |
| 731 | dprintf(fd, "\n"); |
| 732 | } |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 733 | } |