1 /******************************************************************************
2 *
3 * Copyright 2015 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #define LOG_TAG "bt_osi_wakelock"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <hardware/bluetooth.h>
24 #include <inttypes.h>
25 #include <limits.h>
26 #include <pthread.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <time.h>
31 #include <unistd.h>
32
33 #include <mutex>
34 #include <string>
35
36 #include "base/logging.h"
37 #include "check.h"
38 #include "common/metrics.h"
39 #include "osi/include/alarm.h"
40 #include "osi/include/allocator.h"
41 #include "osi/include/log.h"
42 #include "osi/include/osi.h"
43 #include "osi/include/thread.h"
44 #include "osi/include/wakelock.h"
45
46 using bluetooth::common::BluetoothMetricsLogger;
47
48 static bt_os_callouts_t* wakelock_os_callouts = NULL;
49 static bool is_native = true;
50
51 static const clockid_t CLOCK_ID = CLOCK_BOOTTIME;
52 static const char* WAKE_LOCK_ID = "bluetooth_timer";
53 static const std::string DEFAULT_WAKE_LOCK_PATH = "/sys/power/wake_lock";
54 static const std::string DEFAULT_WAKE_UNLOCK_PATH = "/sys/power/wake_unlock";
55 static std::string wake_lock_path;
56 static std::string wake_unlock_path;
57 static ssize_t locked_id_len = -1;
58 static pthread_once_t initialized = PTHREAD_ONCE_INIT;
59 static int wake_lock_fd = INVALID_FD;
60 static int wake_unlock_fd = INVALID_FD;
61
62 // Wakelock statistics for the "bluetooth_timer"
63 typedef struct {
64 bool is_acquired;
65 size_t acquired_count;
66 size_t released_count;
67 size_t acquired_errors;
68 size_t released_errors;
69 uint64_t min_acquired_interval_ms;
70 uint64_t max_acquired_interval_ms;
71 uint64_t last_acquired_interval_ms;
72 uint64_t total_acquired_interval_ms;
73 uint64_t last_acquired_timestamp_ms;
74 uint64_t last_released_timestamp_ms;
75 uint64_t last_reset_timestamp_ms;
76 int last_acquired_error;
77 int last_released_error;
78 } wakelock_stats_t;
79
80 static wakelock_stats_t wakelock_stats;
81
82 // This mutex ensures that the functions that update and dump the statistics
83 // are executed serially.
84 static std::mutex stats_mutex;
85
86 static bt_status_t wakelock_acquire_callout(void);
87 static bt_status_t wakelock_acquire_native(void);
88 static bt_status_t wakelock_release_callout(void);
89 static bt_status_t wakelock_release_native(void);
90 static void wakelock_initialize(void);
91 static void wakelock_initialize_native(void);
92 static void reset_wakelock_stats(void);
93 static void update_wakelock_acquired_stats(bt_status_t acquired_status);
94 static void update_wakelock_released_stats(bt_status_t released_status);
95
wakelock_set_os_callouts(bt_os_callouts_t * callouts)96 void wakelock_set_os_callouts(bt_os_callouts_t* callouts) {
97 wakelock_os_callouts = callouts;
98 is_native = (wakelock_os_callouts == NULL);
99 LOG_INFO("%s set to %s", __func__, (is_native) ? "native" : "non-native");
100 }
101
wakelock_acquire(void)102 bool wakelock_acquire(void) {
103 pthread_once(&initialized, wakelock_initialize);
104
105 bt_status_t status = BT_STATUS_FAIL;
106
107 if (is_native)
108 status = wakelock_acquire_native();
109 else
110 status = wakelock_acquire_callout();
111
112 update_wakelock_acquired_stats(status);
113
114 if (status != BT_STATUS_SUCCESS)
115 LOG_ERROR("%s unable to acquire wake lock: %d", __func__, status);
116
117 return (status == BT_STATUS_SUCCESS);
118 }
119
wakelock_acquire_callout(void)120 static bt_status_t wakelock_acquire_callout(void) {
121 return static_cast<bt_status_t>(
122 wakelock_os_callouts->acquire_wake_lock(WAKE_LOCK_ID));
123 }
124
wakelock_acquire_native(void)125 static bt_status_t wakelock_acquire_native(void) {
126 if (wake_lock_fd == INVALID_FD) {
127 LOG_ERROR("%s lock not acquired, invalid fd", __func__);
128 return BT_STATUS_PARM_INVALID;
129 }
130
131 if (wake_unlock_fd == INVALID_FD) {
132 LOG_ERROR("%s not acquiring lock: can't release lock", __func__);
133 return BT_STATUS_PARM_INVALID;
134 }
135
136 long lock_name_len = strlen(WAKE_LOCK_ID);
137 locked_id_len = write(wake_lock_fd, WAKE_LOCK_ID, lock_name_len);
138 if (locked_id_len == -1) {
139 LOG_ERROR("%s wake lock not acquired: %s", __func__, strerror(errno));
140 return BT_STATUS_FAIL;
141 } else if (locked_id_len < lock_name_len) {
142 // TODO (jamuraa): this is weird. maybe we should release and retry.
143 LOG_WARN("%s wake lock truncated to %zd chars", __func__, locked_id_len);
144 }
145 return BT_STATUS_SUCCESS;
146 }
147
wakelock_release(void)148 bool wakelock_release(void) {
149 pthread_once(&initialized, wakelock_initialize);
150
151 bt_status_t status = BT_STATUS_FAIL;
152
153 if (is_native)
154 status = wakelock_release_native();
155 else
156 status = wakelock_release_callout();
157
158 update_wakelock_released_stats(status);
159
160 return (status == BT_STATUS_SUCCESS);
161 }
162
wakelock_release_callout(void)163 static bt_status_t wakelock_release_callout(void) {
164 return static_cast<bt_status_t>(
165 wakelock_os_callouts->release_wake_lock(WAKE_LOCK_ID));
166 }
167
wakelock_release_native(void)168 static bt_status_t wakelock_release_native(void) {
169 if (wake_unlock_fd == INVALID_FD) {
170 LOG_ERROR("%s lock not released, invalid fd", __func__);
171 return BT_STATUS_PARM_INVALID;
172 }
173
174 ssize_t wrote_name_len = write(wake_unlock_fd, WAKE_LOCK_ID, locked_id_len);
175 if (wrote_name_len == -1) {
176 LOG_ERROR("%s can't release wake lock: %s", __func__, strerror(errno));
177 } else if (wrote_name_len < locked_id_len) {
178 LOG_ERROR("%s lock release only wrote %zd, assuming released", __func__,
179 wrote_name_len);
180 }
181 return BT_STATUS_SUCCESS;
182 }
183
wakelock_initialize(void)184 static void wakelock_initialize(void) {
185 reset_wakelock_stats();
186
187 if (is_native) wakelock_initialize_native();
188 }
189
wakelock_initialize_native(void)190 static void wakelock_initialize_native(void) {
191 LOG_INFO("%s opening wake locks", __func__);
192
193 if (wake_lock_path.empty()) wake_lock_path = DEFAULT_WAKE_LOCK_PATH;
194
195 wake_lock_fd = open(wake_lock_path.c_str(), O_RDWR | O_CLOEXEC);
196 if (wake_lock_fd == INVALID_FD) {
197 LOG_ERROR("%s can't open wake lock %s: %s", __func__,
198 wake_lock_path.c_str(), strerror(errno));
199 }
200
201 if (wake_unlock_path.empty()) wake_unlock_path = DEFAULT_WAKE_UNLOCK_PATH;
202
203 wake_unlock_fd = open(wake_unlock_path.c_str(), O_RDWR | O_CLOEXEC);
204 if (wake_unlock_fd == INVALID_FD) {
205 LOG_ERROR("%s can't open wake unlock %s: %s", __func__,
206 wake_unlock_path.c_str(), strerror(errno));
207 }
208 }
209
wakelock_cleanup(void)210 void wakelock_cleanup(void) {
211 if (wakelock_stats.is_acquired) {
212 LOG_ERROR("%s releasing wake lock as part of cleanup", __func__);
213 wakelock_release();
214 }
215 wake_lock_path.clear();
216 wake_unlock_path.clear();
217 initialized = PTHREAD_ONCE_INIT;
218 }
219
wakelock_set_paths(const char * lock_path,const char * unlock_path)220 void wakelock_set_paths(const char* lock_path, const char* unlock_path) {
221 if (lock_path) wake_lock_path = lock_path;
222
223 if (unlock_path) wake_unlock_path = unlock_path;
224 }
225
now_ms(void)226 static uint64_t now_ms(void) {
227 struct timespec ts;
228 if (clock_gettime(CLOCK_ID, &ts) == -1) {
229 LOG_ERROR("%s unable to get current time: %s", __func__, strerror(errno));
230 return 0;
231 }
232
233 return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL);
234 }
235
236 // Reset the Bluetooth wakelock statistics.
237 // This function is thread-safe.
reset_wakelock_stats(void)238 static void reset_wakelock_stats(void) {
239 std::lock_guard<std::mutex> lock(stats_mutex);
240
241 wakelock_stats.is_acquired = false;
242 wakelock_stats.acquired_count = 0;
243 wakelock_stats.released_count = 0;
244 wakelock_stats.acquired_errors = 0;
245 wakelock_stats.released_errors = 0;
246 wakelock_stats.min_acquired_interval_ms = 0;
247 wakelock_stats.max_acquired_interval_ms = 0;
248 wakelock_stats.last_acquired_interval_ms = 0;
249 wakelock_stats.total_acquired_interval_ms = 0;
250 wakelock_stats.last_acquired_timestamp_ms = 0;
251 wakelock_stats.last_released_timestamp_ms = 0;
252 wakelock_stats.last_reset_timestamp_ms = now_ms();
253 }
254
255 //
256 // Update the Bluetooth acquire wakelock statistics.
257 //
258 // This function should be called every time when the wakelock is acquired.
259 // |acquired_status| is the status code that was return when the wakelock was
260 // acquired.
261 // This function is thread-safe.
262 //
update_wakelock_acquired_stats(bt_status_t acquired_status)263 static void update_wakelock_acquired_stats(bt_status_t acquired_status) {
264 const uint64_t just_now_ms = now_ms();
265
266 std::lock_guard<std::mutex> lock(stats_mutex);
267
268 if (acquired_status != BT_STATUS_SUCCESS) {
269 wakelock_stats.acquired_errors++;
270 wakelock_stats.last_acquired_error = acquired_status;
271 }
272
273 if (wakelock_stats.is_acquired) {
274 return;
275 }
276
277 wakelock_stats.is_acquired = true;
278 wakelock_stats.acquired_count++;
279 wakelock_stats.last_acquired_timestamp_ms = just_now_ms;
280
281 BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
282 bluetooth::common::WAKE_EVENT_ACQUIRED, "", "", just_now_ms);
283 }
284
285 //
286 // Update the Bluetooth release wakelock statistics.
287 //
288 // This function should be called every time when the wakelock is released.
289 // |released_status| is the status code that was return when the wakelock was
290 // released.
291 // This function is thread-safe.
292 //
update_wakelock_released_stats(bt_status_t released_status)293 static void update_wakelock_released_stats(bt_status_t released_status) {
294 const uint64_t just_now_ms = now_ms();
295
296 std::lock_guard<std::mutex> lock(stats_mutex);
297
298 if (released_status != BT_STATUS_SUCCESS) {
299 wakelock_stats.released_errors++;
300 wakelock_stats.last_released_error = released_status;
301 }
302
303 if (!wakelock_stats.is_acquired) {
304 return;
305 }
306
307 wakelock_stats.is_acquired = false;
308 wakelock_stats.released_count++;
309 wakelock_stats.last_released_timestamp_ms = just_now_ms;
310
311 // Compute the acquired interval and update the statistics
312 uint64_t delta_ms = just_now_ms - wakelock_stats.last_acquired_timestamp_ms;
313 if (delta_ms < wakelock_stats.min_acquired_interval_ms ||
314 wakelock_stats.released_count == 1) {
315 wakelock_stats.min_acquired_interval_ms = delta_ms;
316 }
317 if (delta_ms > wakelock_stats.max_acquired_interval_ms) {
318 wakelock_stats.max_acquired_interval_ms = delta_ms;
319 }
320 wakelock_stats.last_acquired_interval_ms = delta_ms;
321 wakelock_stats.total_acquired_interval_ms += delta_ms;
322
323 BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
324 bluetooth::common::WAKE_EVENT_RELEASED, "", "", just_now_ms);
325 }
326
wakelock_debug_dump(int fd)327 void wakelock_debug_dump(int fd) {
328 const uint64_t just_now_ms = now_ms();
329
330 std::lock_guard<std::mutex> lock(stats_mutex);
331
332 // Compute the last acquired interval if the wakelock is still acquired
333 uint64_t delta_ms = 0;
334 uint64_t last_interval_ms = wakelock_stats.last_acquired_interval_ms;
335 uint64_t min_interval_ms = wakelock_stats.min_acquired_interval_ms;
336 uint64_t max_interval_ms = wakelock_stats.max_acquired_interval_ms;
337 uint64_t avg_interval_ms = 0;
338
339 if (wakelock_stats.is_acquired) {
340 delta_ms = just_now_ms - wakelock_stats.last_acquired_timestamp_ms;
341 if (delta_ms > max_interval_ms) max_interval_ms = delta_ms;
342 if (delta_ms < min_interval_ms) min_interval_ms = delta_ms;
343 last_interval_ms = delta_ms;
344 }
345 uint64_t total_interval_ms =
346 wakelock_stats.total_acquired_interval_ms + delta_ms;
347
348 if (wakelock_stats.acquired_count > 0)
349 avg_interval_ms = total_interval_ms / wakelock_stats.acquired_count;
350
351 dprintf(fd, "\nBluetooth Wakelock Statistics:\n");
352 dprintf(fd, " Is acquired : %s\n",
353 wakelock_stats.is_acquired ? "true" : "false");
354 dprintf(fd, " Acquired/released count : %zu / %zu\n",
355 wakelock_stats.acquired_count, wakelock_stats.released_count);
356 dprintf(fd, " Acquired/released error count : %zu / %zu\n",
357 wakelock_stats.acquired_errors, wakelock_stats.released_errors);
358 dprintf(fd, " Last acquire/release error code: %d / %d\n",
359 wakelock_stats.last_acquired_error,
360 wakelock_stats.last_released_error);
361 dprintf(fd, " Last acquired time (ms) : %llu\n",
362 (unsigned long long)last_interval_ms);
363 dprintf(fd, " Acquired time min/max/avg (ms) : %llu / %llu / %llu\n",
364 (unsigned long long)min_interval_ms,
365 (unsigned long long)max_interval_ms,
366 (unsigned long long)avg_interval_ms);
367 dprintf(fd, " Total acquired time (ms) : %llu\n",
368 (unsigned long long)total_interval_ms);
369 dprintf(fd, " Total run time (ms) : %llu\n",
370 (unsigned long long)(just_now_ms -
371 wakelock_stats.last_reset_timestamp_ms));
372 }
373