blob: e5f9b55b6936154768c14b3f7bd2704c624bef12 [file] [log] [blame]
Marie Janssena84a4ee2016-01-15 16:14:14 -08001/******************************************************************************
2 *
3 * Copyright (C) 2016 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 ******************************************************************************/
Marie Janssena84a4ee2016-01-15 16:14:14 -080018#define LOG_TAG "bt_osi_metrics"
19
Jack He777228a2016-12-08 19:29:00 -080020#include <unistd.h>
21#include <algorithm>
22#include <cerrno>
23#include <chrono>
24#include <cstdint>
25#include <cstring>
26#include <memory>
27#include <mutex>
Marie Janssena84a4ee2016-01-15 16:14:14 -080028
Jack He777228a2016-12-08 19:29:00 -080029#include <base/base64.h>
30#include <base/logging.h>
Marie Janssena84a4ee2016-01-15 16:14:14 -080031
Jack He777228a2016-12-08 19:29:00 -080032#include "osi/include/leaky_bonded_queue.h"
Marie Janssena84a4ee2016-01-15 16:14:14 -080033#include "osi/include/log.h"
34#include "osi/include/osi.h"
Jack He777228a2016-12-08 19:29:00 -080035#include "osi/include/time.h"
36#include "stack/include/btm_api_types.h"
Marie Janssena84a4ee2016-01-15 16:14:14 -080037
38#include "osi/src/protos/bluetooth.pb.h"
39
Jack He777228a2016-12-08 19:29:00 -080040#include "osi/include/metrics.h"
41
42namespace system_bt_osi {
Marie Janssena84a4ee2016-01-15 16:14:14 -080043
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -080044using clearcut::connectivity::A2DPSession;
Marie Janssena84a4ee2016-01-15 16:14:14 -080045using clearcut::connectivity::BluetoothLog;
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -080046using clearcut::connectivity::BluetoothSession;
Jack He777228a2016-12-08 19:29:00 -080047using clearcut::connectivity::BluetoothSession_ConnectionTechnologyType;
Marie Janssena84a4ee2016-01-15 16:14:14 -080048using clearcut::connectivity::DeviceInfo;
49using clearcut::connectivity::DeviceInfo_DeviceType;
50using clearcut::connectivity::PairEvent;
51using clearcut::connectivity::ScanEvent;
52using clearcut::connectivity::ScanEvent_ScanTechnologyType;
53using clearcut::connectivity::ScanEvent_ScanEventType;
54using clearcut::connectivity::WakeEvent;
55using clearcut::connectivity::WakeEvent_WakeEventType;
56
Jack He777228a2016-12-08 19:29:00 -080057namespace {
58// Maximum number of log entries for each repeated field
59const size_t global_max_num_bluetooth_session = 50;
60const size_t global_max_num_pair_event = 50;
61const size_t global_max_num_wake_event = 50;
62const size_t global_max_num_scan_event = 50;
63const std::string global_next_session_start_without_ending_previous =
64 "NEXT_SESSION_START_WITHOUT_ENDING_PREVIOUS";
65const std::string global_metrics_dump = "METRICS_DUMP";
66}
Marie Janssena84a4ee2016-01-15 16:14:14 -080067
Jack He777228a2016-12-08 19:29:00 -080068/*
69 * Get current OS boot time in millisecond
70 */
71static int64_t time_get_os_boottime_ms(void) {
72 return time_get_os_boottime_us() / 1000;
73}
74
75static float combine_averages(float avg_a, int64_t ct_a, float avg_b,
76 int64_t ct_b) {
77 if (ct_a > 0 && ct_b > 0) {
78 return (avg_a * ct_a + avg_b * ct_b) / (ct_a + ct_b);
79 } else if (ct_b > 0) {
80 return avg_b;
81 } else {
82 return avg_a;
Marie Janssena84a4ee2016-01-15 16:14:14 -080083 }
84}
85
Jack He777228a2016-12-08 19:29:00 -080086static int32_t combine_averages(int32_t avg_a, int64_t ct_a, int32_t avg_b,
87 int64_t ct_b) {
88 if (ct_a > 0 && ct_b > 0) {
89 return (avg_a * ct_a + avg_b * ct_b) / (ct_a + ct_b);
90 } else if (ct_b > 0) {
91 return avg_b;
92 } else {
93 return avg_a;
94 }
95}
Marie Janssena84a4ee2016-01-15 16:14:14 -080096
Jack He777228a2016-12-08 19:29:00 -080097void A2dpSessionMetrics::Update(const A2dpSessionMetrics& metrics) {
98 if (metrics.audio_duration_ms > 0) {
99 audio_duration_ms = std::max(static_cast<int64_t>(0), audio_duration_ms);
100 audio_duration_ms += metrics.audio_duration_ms;
101 }
102 if (metrics.media_timer_min_ms > 0) {
103 if (media_timer_min_ms < 0) {
104 media_timer_min_ms = metrics.media_timer_min_ms;
105 } else {
106 media_timer_min_ms =
107 std::min(media_timer_min_ms, metrics.media_timer_min_ms);
108 }
109 }
110 if (metrics.media_timer_max_ms > 0) {
111 media_timer_max_ms =
112 std::max(media_timer_max_ms, metrics.media_timer_max_ms);
113 }
114 if (metrics.media_timer_avg_ms > 0 && metrics.total_scheduling_count > 0) {
115 if (media_timer_avg_ms < 0 || total_scheduling_count < 0) {
116 media_timer_avg_ms = metrics.media_timer_avg_ms;
117 total_scheduling_count = metrics.total_scheduling_count;
118 } else {
119 media_timer_avg_ms = combine_averages(
120 media_timer_avg_ms, total_scheduling_count,
121 metrics.media_timer_avg_ms, metrics.total_scheduling_count);
122 total_scheduling_count += metrics.total_scheduling_count;
123 }
124 }
125 if (metrics.buffer_overruns_max_count > 0) {
126 buffer_overruns_max_count =
127 std::max(buffer_overruns_max_count, metrics.buffer_overruns_max_count);
128 }
129 if (metrics.buffer_overruns_total > 0) {
130 buffer_overruns_total =
131 std::max(static_cast<int32_t>(0), buffer_overruns_total);
132 buffer_overruns_total += metrics.buffer_overruns_total;
133 }
134 if (metrics.buffer_underruns_average > 0 &&
135 metrics.buffer_underruns_count > 0) {
136 if (buffer_underruns_average < 0 || buffer_underruns_count < 0) {
137 buffer_underruns_average = metrics.buffer_underruns_average;
138 buffer_underruns_count = metrics.buffer_underruns_count;
139 } else {
140 buffer_underruns_average = combine_averages(
141 metrics.buffer_underruns_average, metrics.buffer_underruns_count,
142 buffer_underruns_average, buffer_underruns_count);
143 buffer_underruns_count += metrics.buffer_underruns_count;
144 }
145 }
146}
Marie Janssena84a4ee2016-01-15 16:14:14 -0800147
Jack He777228a2016-12-08 19:29:00 -0800148bool A2dpSessionMetrics::operator==(const A2dpSessionMetrics& rhs) const {
149 return audio_duration_ms == rhs.audio_duration_ms &&
150 media_timer_min_ms == rhs.media_timer_min_ms &&
151 media_timer_max_ms == rhs.media_timer_max_ms &&
152 media_timer_avg_ms == rhs.media_timer_avg_ms &&
153 total_scheduling_count == rhs.total_scheduling_count &&
154 buffer_overruns_max_count == rhs.buffer_overruns_max_count &&
155 buffer_overruns_total == rhs.buffer_overruns_total &&
156 buffer_underruns_average == rhs.buffer_underruns_average &&
157 buffer_underruns_count == rhs.buffer_underruns_count;
158}
159
160static DeviceInfo_DeviceType get_device_type(device_type_t type) {
161 switch (type) {
162 case DEVICE_TYPE_BREDR:
163 return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR;
164 case DEVICE_TYPE_LE:
165 return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_LE;
166 case DEVICE_TYPE_DUMO:
167 return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_DUMO;
168 case DEVICE_TYPE_UNKNOWN:
169 default:
170 return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_UNKNOWN;
171 }
172}
173
174static BluetoothSession_ConnectionTechnologyType get_connection_tech_type(
175 connection_tech_t type) {
176 switch (type) {
177 case CONNECTION_TECHNOLOGY_TYPE_LE:
178 return BluetoothSession_ConnectionTechnologyType::
179 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE;
180 case CONNECTION_TECHNOLOGY_TYPE_BREDR:
181 return BluetoothSession_ConnectionTechnologyType::
182 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR;
183 case CONNECTION_TECHNOLOGY_TYPE_UNKNOWN:
184 default:
185 return BluetoothSession_ConnectionTechnologyType::
186 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_UNKNOWN;
187 }
188}
189
190static ScanEvent_ScanTechnologyType get_scan_tech_type(scan_tech_t type) {
191 switch (type) {
192 case SCAN_TECH_TYPE_LE:
193 return ScanEvent_ScanTechnologyType::
194 ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_LE;
195 case SCAN_TECH_TYPE_BREDR:
196 return ScanEvent_ScanTechnologyType::
197 ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BREDR;
198 case SCAN_TECH_TYPE_BOTH:
199 return ScanEvent_ScanTechnologyType::
200 ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BOTH;
201 case SCAN_TYPE_UNKNOWN:
202 default:
203 return ScanEvent_ScanTechnologyType::
204 ScanEvent_ScanTechnologyType_SCAN_TYPE_UNKNOWN;
205 }
206}
207
208static WakeEvent_WakeEventType get_wake_event_type(wake_event_type_t type) {
209 switch (type) {
210 case WAKE_EVENT_ACQUIRED:
211 return WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED;
212 case WAKE_EVENT_RELEASED:
213 return WakeEvent_WakeEventType::WakeEvent_WakeEventType_RELEASED;
214 case WAKE_EVENT_UNKNOWN:
215 default:
216 return WakeEvent_WakeEventType::WakeEvent_WakeEventType_UNKNOWN;
217 }
218}
219
220struct BluetoothMetricsLogger::impl {
221 impl(size_t max_bluetooth_session, size_t max_pair_event,
222 size_t max_wake_event, size_t max_scan_event)
223 : bt_session_queue_(
224 new LeakyBondedQueue<BluetoothSession>(max_bluetooth_session)),
225 pair_event_queue_(new LeakyBondedQueue<PairEvent>(max_pair_event)),
226 wake_event_queue_(new LeakyBondedQueue<WakeEvent>(max_wake_event)),
227 scan_event_queue_(new LeakyBondedQueue<ScanEvent>(max_scan_event)) {
228 bluetooth_log_ = BluetoothLog::default_instance().New();
229 bluetooth_session_ = nullptr;
230 bluetooth_session_start_time_ms_ = 0;
231 a2dp_session_metrics_ = A2dpSessionMetrics();
232 }
233
234 /* Bluetooth log lock protected */
235 BluetoothLog* bluetooth_log_;
236 std::recursive_mutex bluetooth_log_lock_;
237 /* End Bluetooth log lock protected */
238 /* Bluetooth session lock protected */
239 BluetoothSession* bluetooth_session_;
240 uint64_t bluetooth_session_start_time_ms_;
241 A2dpSessionMetrics a2dp_session_metrics_;
242 std::recursive_mutex bluetooth_session_lock_;
243 /* End bluetooth session lock protected */
244 std::unique_ptr<LeakyBondedQueue<BluetoothSession>> bt_session_queue_;
245 std::unique_ptr<LeakyBondedQueue<PairEvent>> pair_event_queue_;
246 std::unique_ptr<LeakyBondedQueue<WakeEvent>> wake_event_queue_;
247 std::unique_ptr<LeakyBondedQueue<ScanEvent>> scan_event_queue_;
248};
249
250BluetoothMetricsLogger::BluetoothMetricsLogger()
251 : pimpl_(new impl(global_max_num_bluetooth_session,
252 global_max_num_pair_event, global_max_num_wake_event,
253 global_max_num_scan_event)) {}
254
255void BluetoothMetricsLogger::LogPairEvent(uint32_t disconnect_reason,
256 uint64_t timestamp_ms,
257 uint32_t device_class,
258 device_type_t device_type) {
259 PairEvent* event = new PairEvent();
Myles Watson914a9dc2016-10-19 13:15:34 -0700260 DeviceInfo* info = event->mutable_device_paired_with();
Marie Janssena84a4ee2016-01-15 16:14:14 -0800261 info->set_device_class(device_class);
Jack He777228a2016-12-08 19:29:00 -0800262 info->set_device_type(get_device_type(device_type));
Marie Janssena84a4ee2016-01-15 16:14:14 -0800263 event->set_disconnect_reason(disconnect_reason);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800264 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800265 pimpl_->pair_event_queue_->Enqueue(event);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800266}
267
Jack He777228a2016-12-08 19:29:00 -0800268void BluetoothMetricsLogger::LogWakeEvent(wake_event_type_t type,
269 const std::string& requestor,
270 const std::string& name,
271 uint64_t timestamp_ms) {
272 WakeEvent* event = new WakeEvent();
273 event->set_wake_event_type(get_wake_event_type(type));
274 event->set_requestor(requestor);
275 event->set_name(name);
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800276 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800277 pimpl_->wake_event_queue_->Enqueue(event);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800278}
279
Jack He777228a2016-12-08 19:29:00 -0800280void BluetoothMetricsLogger::LogScanEvent(bool start,
281 const std::string& initator,
282 scan_tech_t type, uint32_t results,
283 uint64_t timestamp_ms) {
284 ScanEvent* event = new ScanEvent();
285 if (start) {
Marie Janssena84a4ee2016-01-15 16:14:14 -0800286 event->set_scan_event_type(ScanEvent::SCAN_EVENT_START);
Jack He777228a2016-12-08 19:29:00 -0800287 } else {
Marie Janssena84a4ee2016-01-15 16:14:14 -0800288 event->set_scan_event_type(ScanEvent::SCAN_EVENT_STOP);
Jack He777228a2016-12-08 19:29:00 -0800289 }
290 event->set_initiator(initator);
291 event->set_scan_technology_type(get_scan_tech_type(type));
Marie Janssena84a4ee2016-01-15 16:14:14 -0800292 event->set_number_results(results);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800293 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800294 pimpl_->scan_event_queue_->Enqueue(event);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800295}
296
Jack He777228a2016-12-08 19:29:00 -0800297void BluetoothMetricsLogger::LogBluetoothSessionStart(
298 connection_tech_t connection_tech_type, uint64_t timestamp_ms) {
299 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
300 if (pimpl_->bluetooth_session_ != nullptr) {
301 LogBluetoothSessionEnd(global_next_session_start_without_ending_previous,
302 0);
303 }
304 if (timestamp_ms == 0) {
305 timestamp_ms = time_get_os_boottime_ms();
306 }
307 pimpl_->bluetooth_session_start_time_ms_ = timestamp_ms;
308 pimpl_->bluetooth_session_ = new BluetoothSession();
309 pimpl_->bluetooth_session_->set_connection_technology_type(
310 get_connection_tech_type(connection_tech_type));
311}
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800312
Jack He777228a2016-12-08 19:29:00 -0800313void BluetoothMetricsLogger::LogBluetoothSessionEnd(
314 const std::string& disconnect_reason, uint64_t timestamp_ms) {
315 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
316 if (pimpl_->bluetooth_session_ == nullptr) {
317 return;
318 }
319 if (timestamp_ms == 0) {
320 timestamp_ms = time_get_os_boottime_ms();
321 }
322 int64_t session_duration_sec =
323 (timestamp_ms - pimpl_->bluetooth_session_start_time_ms_) / 1000;
324 pimpl_->bluetooth_session_->set_session_duration_sec(session_duration_sec);
325 pimpl_->bluetooth_session_->set_disconnect_reason(disconnect_reason);
326 pimpl_->bt_session_queue_->Enqueue(pimpl_->bluetooth_session_);
327 pimpl_->bluetooth_session_ = nullptr;
328}
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800329
Jack He777228a2016-12-08 19:29:00 -0800330void BluetoothMetricsLogger::LogBluetoothSessionDeviceInfo(
331 uint32_t device_class, device_type_t device_type) {
332 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
333 if (pimpl_->bluetooth_session_ == nullptr) {
334 LogBluetoothSessionStart(CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, 0);
335 }
336 DeviceInfo* info = pimpl_->bluetooth_session_->mutable_device_connected_to();
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800337 info->set_device_class(device_class);
338 info->set_device_type(DeviceInfo::DEVICE_TYPE_BREDR);
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800339}
340
Jack He777228a2016-12-08 19:29:00 -0800341void BluetoothMetricsLogger::LogA2dpSession(
342 const A2dpSessionMetrics& a2dp_session_metrics) {
343 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
344 if (pimpl_->bluetooth_session_ == nullptr) {
345 // When no bluetooth session exist, create one on system's behalf
346 // Set connection type: for A2DP it is always BR/EDR
347 LogBluetoothSessionStart(CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
348 LogBluetoothSessionDeviceInfo(BTM_COD_MAJOR_AUDIO, DEVICE_TYPE_BREDR);
349 }
350 // Accumulate metrics
351 pimpl_->a2dp_session_metrics_.Update(a2dp_session_metrics);
352 // Get or allocate new A2DP session object
353 A2DPSession* a2dp_session =
354 pimpl_->bluetooth_session_->mutable_a2dp_session();
355 a2dp_session->set_audio_duration_millis(
356 pimpl_->a2dp_session_metrics_.audio_duration_ms);
357 a2dp_session->set_media_timer_min_millis(
358 pimpl_->a2dp_session_metrics_.media_timer_min_ms);
359 a2dp_session->set_media_timer_max_millis(
360 pimpl_->a2dp_session_metrics_.media_timer_max_ms);
361 a2dp_session->set_media_timer_avg_millis(
362 pimpl_->a2dp_session_metrics_.media_timer_avg_ms);
363 a2dp_session->set_buffer_overruns_max_count(
364 pimpl_->a2dp_session_metrics_.buffer_overruns_max_count);
365 a2dp_session->set_buffer_overruns_total(
366 pimpl_->a2dp_session_metrics_.buffer_overruns_total);
367 a2dp_session->set_buffer_underruns_average(
368 pimpl_->a2dp_session_metrics_.buffer_underruns_average);
369 a2dp_session->set_buffer_underruns_count(
370 pimpl_->a2dp_session_metrics_.buffer_underruns_count);
371}
Marie Janssena84a4ee2016-01-15 16:14:14 -0800372
Jack He777228a2016-12-08 19:29:00 -0800373void BluetoothMetricsLogger::WriteString(std::string* serialized, bool clear) {
374 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
375 LOG_DEBUG(LOG_TAG, "%s building metrics", __func__);
376 Build();
377 LOG_DEBUG(LOG_TAG, "%s serializing metrics", __func__);
378 if (!pimpl_->bluetooth_log_->SerializeToString(serialized)) {
Marie Janssena84a4ee2016-01-15 16:14:14 -0800379 LOG_ERROR(LOG_TAG, "%s: error serializing metrics", __func__);
380 return;
381 }
Ajay Panickerfe357dc2016-02-18 12:24:10 -0800382 if (clear) {
Jack He777228a2016-12-08 19:29:00 -0800383 pimpl_->bluetooth_log_->Clear();
Ajay Panickerfe357dc2016-02-18 12:24:10 -0800384 }
Jack He777228a2016-12-08 19:29:00 -0800385}
Ajay Panickerfe357dc2016-02-18 12:24:10 -0800386
Jack He777228a2016-12-08 19:29:00 -0800387void BluetoothMetricsLogger::WriteBase64String(std::string* serialized,
388 bool clear) {
389 this->WriteString(serialized, clear);
390 base::Base64Encode(*serialized, serialized);
391}
392
393void BluetoothMetricsLogger::WriteBase64(int fd, bool clear) {
Ajay Panicker4336ba52016-02-17 18:18:00 -0800394 std::string protoBase64;
Jack He777228a2016-12-08 19:29:00 -0800395 this->WriteBase64String(&protoBase64, clear);
Pavlin Radoslavovc608acf2016-05-12 11:36:44 -0700396 ssize_t ret;
397 OSI_NO_INTR(ret = write(fd, protoBase64.c_str(), protoBase64.size()));
398 if (ret == -1) {
Marie Janssena84a4ee2016-01-15 16:14:14 -0800399 LOG_ERROR(LOG_TAG, "%s: error writing to dumpsys fd: %s (%d)", __func__,
400 strerror(errno), errno);
401 }
Marie Janssena84a4ee2016-01-15 16:14:14 -0800402}
Jack He777228a2016-12-08 19:29:00 -0800403
404void BluetoothMetricsLogger::CutoffSession() {
405 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
406 if (pimpl_->bluetooth_session_ != nullptr) {
407 BluetoothSession* new_bt_session =
408 new BluetoothSession(*pimpl_->bluetooth_session_);
409 new_bt_session->clear_a2dp_session();
410 new_bt_session->clear_rfcomm_session();
411 LogBluetoothSessionEnd(global_metrics_dump, 0);
412 pimpl_->bluetooth_session_ = new_bt_session;
413 pimpl_->bluetooth_session_start_time_ms_ = time_get_os_boottime_ms();
414 pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics();
415 }
416}
417
418void BluetoothMetricsLogger::Build() {
419 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
420 CutoffSession();
421 BluetoothLog* bluetooth_log = pimpl_->bluetooth_log_;
422 while (!pimpl_->bt_session_queue_->Empty() &&
423 static_cast<size_t>(bluetooth_log->session_size()) <=
424 pimpl_->bt_session_queue_->Capacity()) {
425 bluetooth_log->mutable_session()->AddAllocated(
426 pimpl_->bt_session_queue_->Dequeue());
427 }
428 while (!pimpl_->pair_event_queue_->Empty() &&
429 static_cast<size_t>(bluetooth_log->pair_event_size()) <=
430 pimpl_->pair_event_queue_->Capacity()) {
431 bluetooth_log->mutable_pair_event()->AddAllocated(
432 pimpl_->pair_event_queue_->Dequeue());
433 }
434 while (!pimpl_->scan_event_queue_->Empty() &&
435 static_cast<size_t>(bluetooth_log->scan_event_size()) <=
436 pimpl_->scan_event_queue_->Capacity()) {
437 bluetooth_log->mutable_scan_event()->AddAllocated(
438 pimpl_->scan_event_queue_->Dequeue());
439 }
440 while (!pimpl_->wake_event_queue_->Empty() &&
441 static_cast<size_t>(bluetooth_log->wake_event_size()) <=
442 pimpl_->wake_event_queue_->Capacity()) {
443 bluetooth_log->mutable_wake_event()->AddAllocated(
444 pimpl_->wake_event_queue_->Dequeue());
445 }
446 while (!pimpl_->bt_session_queue_->Empty() &&
447 static_cast<size_t>(bluetooth_log->wake_event_size()) <=
448 pimpl_->wake_event_queue_->Capacity()) {
449 bluetooth_log->mutable_wake_event()->AddAllocated(
450 pimpl_->wake_event_queue_->Dequeue());
451 }
452}
453
454void BluetoothMetricsLogger::ResetSession() {
455 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
456 if (pimpl_->bluetooth_session_ != nullptr) {
457 delete pimpl_->bluetooth_session_;
458 pimpl_->bluetooth_session_ = nullptr;
459 }
460 pimpl_->bluetooth_session_start_time_ms_ = 0;
461 pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics();
462}
463
464void BluetoothMetricsLogger::ResetLog() {
465 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
466 pimpl_->bluetooth_log_->Clear();
467}
468
469void BluetoothMetricsLogger::Reset() {
470 ResetSession();
471 ResetLog();
472 pimpl_->bt_session_queue_->Clear();
473 pimpl_->pair_event_queue_->Clear();
474 pimpl_->wake_event_queue_->Clear();
475 pimpl_->scan_event_queue_->Clear();
476}
477
478} // namespace system_bt_osi