Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 18 | #define LOG_TAG "bt_osi_metrics" |
| 19 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 20 | #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 Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 28 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 29 | #include <base/base64.h> |
| 30 | #include <base/logging.h> |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 31 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 32 | #include "osi/include/leaky_bonded_queue.h" |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 33 | #include "osi/include/log.h" |
| 34 | #include "osi/include/osi.h" |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 35 | #include "osi/include/time.h" |
| 36 | #include "stack/include/btm_api_types.h" |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 37 | |
| 38 | #include "osi/src/protos/bluetooth.pb.h" |
| 39 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 40 | #include "osi/include/metrics.h" |
| 41 | |
| 42 | namespace system_bt_osi { |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 43 | |
Pavlin Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 44 | using clearcut::connectivity::A2DPSession; |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 45 | using clearcut::connectivity::BluetoothLog; |
Pavlin Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 46 | using clearcut::connectivity::BluetoothSession; |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 47 | using clearcut::connectivity::BluetoothSession_ConnectionTechnologyType; |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 48 | using clearcut::connectivity::DeviceInfo; |
| 49 | using clearcut::connectivity::DeviceInfo_DeviceType; |
| 50 | using clearcut::connectivity::PairEvent; |
| 51 | using clearcut::connectivity::ScanEvent; |
| 52 | using clearcut::connectivity::ScanEvent_ScanTechnologyType; |
| 53 | using clearcut::connectivity::ScanEvent_ScanEventType; |
| 54 | using clearcut::connectivity::WakeEvent; |
| 55 | using clearcut::connectivity::WakeEvent_WakeEventType; |
| 56 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 57 | namespace { |
| 58 | // Maximum number of log entries for each repeated field |
| 59 | const size_t global_max_num_bluetooth_session = 50; |
| 60 | const size_t global_max_num_pair_event = 50; |
| 61 | const size_t global_max_num_wake_event = 50; |
| 62 | const size_t global_max_num_scan_event = 50; |
| 63 | const std::string global_next_session_start_without_ending_previous = |
| 64 | "NEXT_SESSION_START_WITHOUT_ENDING_PREVIOUS"; |
| 65 | const std::string global_metrics_dump = "METRICS_DUMP"; |
| 66 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 67 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 68 | /* |
| 69 | * Get current OS boot time in millisecond |
| 70 | */ |
| 71 | static int64_t time_get_os_boottime_ms(void) { |
| 72 | return time_get_os_boottime_us() / 1000; |
| 73 | } |
| 74 | |
| 75 | static 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 Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 86 | static 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 Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 96 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 97 | void 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 Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 147 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 148 | bool 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 | |
| 160 | static 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 | |
| 174 | static 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 | |
| 190 | static 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 | |
| 208 | static 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 | |
| 220 | struct 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 | |
| 250 | BluetoothMetricsLogger::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 | |
| 255 | void 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 Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 260 | DeviceInfo* info = event->mutable_device_paired_with(); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 261 | info->set_device_class(device_class); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 262 | info->set_device_type(get_device_type(device_type)); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 263 | event->set_disconnect_reason(disconnect_reason); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 264 | event->set_event_time_millis(timestamp_ms); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 265 | pimpl_->pair_event_queue_->Enqueue(event); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 268 | void 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 Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 276 | event->set_event_time_millis(timestamp_ms); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 277 | pimpl_->wake_event_queue_->Enqueue(event); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 280 | void 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 Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 286 | event->set_scan_event_type(ScanEvent::SCAN_EVENT_START); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 287 | } else { |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 288 | event->set_scan_event_type(ScanEvent::SCAN_EVENT_STOP); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 289 | } |
| 290 | event->set_initiator(initator); |
| 291 | event->set_scan_technology_type(get_scan_tech_type(type)); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 292 | event->set_number_results(results); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 293 | event->set_event_time_millis(timestamp_ms); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 294 | pimpl_->scan_event_queue_->Enqueue(event); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 297 | void 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 Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 312 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 313 | void 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 Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 329 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 330 | void 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 Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 337 | info->set_device_class(device_class); |
| 338 | info->set_device_type(DeviceInfo::DEVICE_TYPE_BREDR); |
Pavlin Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 341 | void 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 Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 372 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 373 | void 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 Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 379 | LOG_ERROR(LOG_TAG, "%s: error serializing metrics", __func__); |
| 380 | return; |
| 381 | } |
Ajay Panicker | fe357dc | 2016-02-18 12:24:10 -0800 | [diff] [blame] | 382 | if (clear) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 383 | pimpl_->bluetooth_log_->Clear(); |
Ajay Panicker | fe357dc | 2016-02-18 12:24:10 -0800 | [diff] [blame] | 384 | } |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 385 | } |
Ajay Panicker | fe357dc | 2016-02-18 12:24:10 -0800 | [diff] [blame] | 386 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 387 | void BluetoothMetricsLogger::WriteBase64String(std::string* serialized, |
| 388 | bool clear) { |
| 389 | this->WriteString(serialized, clear); |
| 390 | base::Base64Encode(*serialized, serialized); |
| 391 | } |
| 392 | |
| 393 | void BluetoothMetricsLogger::WriteBase64(int fd, bool clear) { |
Ajay Panicker | 4336ba5 | 2016-02-17 18:18:00 -0800 | [diff] [blame] | 394 | std::string protoBase64; |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 395 | this->WriteBase64String(&protoBase64, clear); |
Pavlin Radoslavov | c608acf | 2016-05-12 11:36:44 -0700 | [diff] [blame] | 396 | ssize_t ret; |
| 397 | OSI_NO_INTR(ret = write(fd, protoBase64.c_str(), protoBase64.size())); |
| 398 | if (ret == -1) { |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 399 | LOG_ERROR(LOG_TAG, "%s: error writing to dumpsys fd: %s (%d)", __func__, |
| 400 | strerror(errno), errno); |
| 401 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 402 | } |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame^] | 403 | |
| 404 | void 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 | |
| 418 | void 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 | |
| 454 | void 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 | |
| 464 | void BluetoothMetricsLogger::ResetLog() { |
| 465 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); |
| 466 | pimpl_->bluetooth_log_->Clear(); |
| 467 | } |
| 468 | |
| 469 | void 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 |