diff options
Diffstat (limited to 'system')
-rw-r--r-- | system/audio_hal_interface/Android.bp | 1 | ||||
-rw-r--r-- | system/audio_hal_interface/fuzzer/Android.bp | 1 | ||||
-rw-r--r-- | system/btcore/fuzzer/Android.bp | 1 | ||||
-rw-r--r-- | system/btif/src/bluetooth.cc | 6 | ||||
-rw-r--r-- | system/btif/src/btif_a2dp_source.cc | 28 | ||||
-rw-r--r-- | system/btif/src/btif_dm.cc | 24 | ||||
-rw-r--r-- | system/btif/src/btif_hf.cc | 3 | ||||
-rw-r--r-- | system/common/Android.bp | 2 | ||||
-rw-r--r-- | system/common/metrics.cc | 475 | ||||
-rw-r--r-- | system/common/metrics.h | 246 | ||||
-rw-r--r-- | system/common/metrics_linux.cc | 58 | ||||
-rw-r--r-- | system/common/metrics_unittest.cc | 944 | ||||
-rw-r--r-- | system/gd/Android.bp | 3 | ||||
-rw-r--r-- | system/gd/proto/Android.bp | 22 | ||||
-rw-r--r-- | system/gd/proto/BUILD.gn | 10 | ||||
-rw-r--r-- | system/include/hardware/bluetooth.h | 8 | ||||
-rw-r--r-- | system/osi/src/wakelock.cc | 8 | ||||
-rwxr-xr-x | system/tools/scripts/dump_metrics_ascii.py | 139 |
18 files changed, 15 insertions, 1964 deletions
diff --git a/system/audio_hal_interface/Android.bp b/system/audio_hal_interface/Android.bp index a171e79fa5..c792ff1186 100644 --- a/system/audio_hal_interface/Android.bp +++ b/system/audio_hal_interface/Android.bp @@ -224,7 +224,6 @@ cc_defaults { "android.hardware.common.fmq-V1-ndk", "libFraunhoferAAC", "libbase", - "libbluetooth-protos", "libbluetooth-types", "libbluetooth_core_rs", "libbluetooth_crypto_toolbox", diff --git a/system/audio_hal_interface/fuzzer/Android.bp b/system/audio_hal_interface/fuzzer/Android.bp index faf52cb3b9..a594041275 100644 --- a/system/audio_hal_interface/fuzzer/Android.bp +++ b/system/audio_hal_interface/fuzzer/Android.bp @@ -59,7 +59,6 @@ cc_defaults { "bluetooth_flags_c_lib", "libFraunhoferAAC", "libbase", - "libbluetooth-protos", "libbluetooth-types", "libbluetooth_core_rs", "libbluetooth_crypto_toolbox", diff --git a/system/btcore/fuzzer/Android.bp b/system/btcore/fuzzer/Android.bp index c930086694..07b8c28f89 100644 --- a/system/btcore/fuzzer/Android.bp +++ b/system/btcore/fuzzer/Android.bp @@ -31,7 +31,6 @@ cc_defaults { ], static_libs: [ "libbase", - "libbluetooth-protos", "libbluetooth-types", "libbluetooth_crypto_toolbox", "libbluetooth_log", diff --git a/system/btif/src/bluetooth.cc b/system/btif/src/bluetooth.cc index 1e3db0be50..5741b05c4f 100644 --- a/system/btif/src/bluetooth.cc +++ b/system/btif/src/bluetooth.cc @@ -82,7 +82,6 @@ #include "btif/include/core_callbacks.h" #include "btif/include/stack_manager_t.h" #include "common/address_obfuscator.h" -#include "common/metrics.h" #include "common/os_utils.h" #include "device/include/device_iot_config.h" #include "device/include/esco_parameters.h" @@ -896,10 +895,6 @@ static void dump(int fd, const char** /*arguments*/) { log::debug("Finished bluetooth dumpsys"); } -static void dumpMetrics(std::string* output) { - bluetooth::common::BluetoothMetricsLogger::GetInstance()->WriteString(output); -} - static int get_remote_pbap_pce_version(const RawAddress* bd_addr) { // Read and restore the PCE version from local storage uint16_t pce_version = 0; @@ -1246,7 +1241,6 @@ EXPORT_SYMBOL bt_interface_t bluetoothInterface = { .set_os_callouts = set_os_callouts, .read_energy_info = read_energy_info, .dump = dump, - .dumpMetrics = dumpMetrics, .config_clear = config_clear, .interop_database_clear = interop_database_clear, .interop_database_add = interop_database_add, diff --git a/system/btif/src/btif_a2dp_source.cc b/system/btif/src/btif_a2dp_source.cc index 25a5fe7093..b8ace80b07 100644 --- a/system/btif/src/btif_a2dp_source.cc +++ b/system/btif/src/btif_a2dp_source.cc @@ -71,8 +71,6 @@ #endif using bluetooth::audio::a2dp::Status; -using bluetooth::common::A2dpSessionMetrics; -using bluetooth::common::BluetoothMetricsLogger; using bluetooth::common::RepeatingTimer; using namespace bluetooth; @@ -492,11 +490,6 @@ static void btif_a2dp_source_start_session_delayed(const RawAddress& peer_addres if (bluetooth::audio::a2dp::is_hal_enabled()) { bluetooth::audio::a2dp::start_session(); bluetooth::audio::a2dp::set_remote_delay(btif_av_get_audio_delay(A2dpType::kSource)); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); - } else { - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); } peer_ready_promise.set_value(); @@ -550,11 +543,6 @@ static void btif_a2dp_source_end_session_delayed(const RawAddress& peer_address) } if (bluetooth::audio::a2dp::is_hal_enabled()) { bluetooth::audio::a2dp::end_session(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( - bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0); - } else { - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( - bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0); } } @@ -1267,9 +1255,24 @@ void btif_a2dp_source_debug_dump(int fd) { (unsigned long long)ave_time_us / 1000); } +struct A2dpSessionMetrics { + int64_t audio_duration_ms = -1; + int32_t media_timer_min_ms = -1; + int32_t media_timer_max_ms = -1; + int32_t media_timer_avg_ms = -1; + int64_t total_scheduling_count = -1; + int32_t buffer_overruns_max_count = -1; + int32_t buffer_overruns_total = -1; + float buffer_underruns_average = -1; + int32_t buffer_underruns_count = -1; + int64_t codec_index = -1; + bool is_a2dp_offload = false; +}; + static void btif_a2dp_source_update_metrics(void) { BtifMediaStats stats = btif_a2dp_source_cb.stats; SchedulingStats enqueue_stats = stats.tx_queue_enqueue_stats; + A2dpSessionMetrics metrics; metrics.codec_index = stats.codec_index; metrics.is_a2dp_offload = btif_av_is_a2dp_offload_running(); @@ -1303,7 +1306,6 @@ static void btif_a2dp_source_update_metrics(void) { (float)stats.media_read_total_underflow_bytes / metrics.buffer_underruns_count; } } - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics); if (metrics.audio_duration_ms != -1) { log_a2dp_session_metrics_event(btif_av_source_active_peer(), metrics.audio_duration_ms, diff --git a/system/btif/src/btif_dm.cc b/system/btif/src/btif_dm.cc index 84c7b7e1f6..96ed5848b1 100644 --- a/system/btif/src/btif_dm.cc +++ b/system/btif/src/btif_dm.cc @@ -62,7 +62,6 @@ #include "btif_storage.h" #include "btif_util.h" #include "common/lru_cache.h" -#include "common/metrics.h" #include "common/strings.h" #include "device/include/interop.h" #include "hci/controller_interface.h" @@ -3873,29 +3872,6 @@ static void btif_stats_add_bond_event(const RawAddress& bd_addr, bt_bond_functio if (btif_events_end_index == btif_events_start_index) { btif_events_start_index = (btif_events_start_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1); } - - int type; - btif_get_device_type(bd_addr, &type); - - bluetooth::common::device_type_t device_type; - switch (type) { - case BT_DEVICE_TYPE_BREDR: - device_type = bluetooth::common::DEVICE_TYPE_BREDR; - break; - case BT_DEVICE_TYPE_BLE: - device_type = bluetooth::common::DEVICE_TYPE_LE; - break; - case BT_DEVICE_TYPE_DUMO: - device_type = bluetooth::common::DEVICE_TYPE_DUMO; - break; - default: - device_type = bluetooth::common::DEVICE_TYPE_UNKNOWN; - break; - } - - uint32_t cod = btif_get_cod(&bd_addr); - uint64_t ts = event->timestamp.tv_sec * 1000 + event->timestamp.tv_nsec / 1000000; - bluetooth::common::BluetoothMetricsLogger::GetInstance()->LogPairEvent(0, ts, cod, device_type); } void btif_debug_bond_event_dump(int fd) { diff --git a/system/btif/src/btif_hf.cc b/system/btif/src/btif_hf.cc index 49e183d06b..33fb900e07 100644 --- a/system/btif/src/btif_hf.cc +++ b/system/btif/src/btif_hf.cc @@ -55,7 +55,6 @@ #include "btif/include/btif_profile_queue.h" #include "btif/include/btif_util.h" #include "btm_api_types.h" -#include "common/metrics.h" #include "device/include/device_iot_conf_defs.h" #include "device/include/device_iot_config.h" #include "hardware/bluetooth.h" @@ -434,8 +433,6 @@ static void btif_hf_upstreams_evt(uint16_t event, char* p_param) { btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_CONNECTED; btif_hf_cb[idx].peer_feat = 0; clear_phone_state_multihf(&btif_hf_cb[idx]); - bluetooth::common::BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection( - p_data->open.service_id); bt_hf_callbacks->ConnectionStateCallback(btif_hf_cb[idx].state, &btif_hf_cb[idx].connected_bda); } else { diff --git a/system/common/Android.bp b/system/common/Android.bp index e4efd7156e..46dd295422 100644 --- a/system/common/Android.bp +++ b/system/common/Android.bp @@ -36,7 +36,6 @@ cc_library_static { target: { android: { srcs: [ - ":bluetooth-metrics-proto", "metrics.cc", ], static_libs: ["libstatslog_bt"], @@ -90,7 +89,6 @@ cc_test { ], target: { android: { - srcs: ["metrics_unittest.cc"], shared_libs: ["libstatssocket"], }, }, diff --git a/system/common/metrics.cc b/system/common/metrics.cc index 1ece069ab3..45b11acf46 100644 --- a/system/common/metrics.cc +++ b/system/common/metrics.cc @@ -33,7 +33,6 @@ #include <mutex> // NOLINT #include <utility> -#include "bluetooth/metrics/bluetooth.pb.h" #include "common/address_obfuscator.h" #include "common/leaky_bonded_queue.h" #include "common/time_util.h" @@ -64,480 +63,6 @@ namespace bluetooth { namespace common { using bluetooth::hci::Address; -using bluetooth::metrics::BluetoothMetricsProto::A2DPSession; -using bluetooth::metrics::BluetoothMetricsProto::A2dpSourceCodec; -using bluetooth::metrics::BluetoothMetricsProto::BluetoothLog; -using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession; -using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession_ConnectionTechnologyType; -using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession_DisconnectReasonType; -using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo; -using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo_DeviceType; -using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileConnectionStats; -using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType; -using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_ARRAYSIZE; -using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_IsValid; -using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_MAX; -using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_MIN; -using bluetooth::metrics::BluetoothMetricsProto::PairEvent; -using bluetooth::metrics::BluetoothMetricsProto::ScanEvent; -using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanEventType; -using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanTechnologyType; -using bluetooth::metrics::BluetoothMetricsProto::WakeEvent; -using bluetooth::metrics::BluetoothMetricsProto::WakeEvent_WakeEventType; - -static float combine_averages(float avg_a, int64_t ct_a, float avg_b, int64_t ct_b) { - if (ct_a > 0 && ct_b > 0) { - return (avg_a * ct_a + avg_b * ct_b) / (ct_a + ct_b); - } else if (ct_b > 0) { - return avg_b; - } else { - return avg_a; - } -} - -static int32_t combine_averages(int32_t avg_a, int64_t ct_a, int32_t avg_b, int64_t ct_b) { - if (ct_a > 0 && ct_b > 0) { - return (avg_a * ct_a + avg_b * ct_b) / (ct_a + ct_b); - } else if (ct_b > 0) { - return avg_b; - } else { - return avg_a; - } -} - -void A2dpSessionMetrics::Update(const A2dpSessionMetrics& metrics) { - if (metrics.audio_duration_ms >= 0) { - audio_duration_ms = std::max(static_cast<int64_t>(0), audio_duration_ms); - audio_duration_ms += metrics.audio_duration_ms; - } - if (metrics.media_timer_min_ms >= 0) { - if (media_timer_min_ms < 0) { - media_timer_min_ms = metrics.media_timer_min_ms; - } else { - media_timer_min_ms = std::min(media_timer_min_ms, metrics.media_timer_min_ms); - } - } - if (metrics.media_timer_max_ms >= 0) { - media_timer_max_ms = std::max(media_timer_max_ms, metrics.media_timer_max_ms); - } - if (metrics.media_timer_avg_ms >= 0 && metrics.total_scheduling_count >= 0) { - if (media_timer_avg_ms < 0 || total_scheduling_count < 0) { - media_timer_avg_ms = metrics.media_timer_avg_ms; - total_scheduling_count = metrics.total_scheduling_count; - } else { - media_timer_avg_ms = - combine_averages(media_timer_avg_ms, total_scheduling_count, - metrics.media_timer_avg_ms, metrics.total_scheduling_count); - total_scheduling_count += metrics.total_scheduling_count; - } - } - if (metrics.buffer_overruns_max_count >= 0) { - buffer_overruns_max_count = - std::max(buffer_overruns_max_count, metrics.buffer_overruns_max_count); - } - if (metrics.buffer_overruns_total >= 0) { - buffer_overruns_total = std::max(static_cast<int32_t>(0), buffer_overruns_total); - buffer_overruns_total += metrics.buffer_overruns_total; - } - if (metrics.buffer_underruns_average >= 0 && metrics.buffer_underruns_count >= 0) { - if (buffer_underruns_average < 0 || buffer_underruns_count < 0) { - buffer_underruns_average = metrics.buffer_underruns_average; - buffer_underruns_count = metrics.buffer_underruns_count; - } else { - buffer_underruns_average = - combine_averages(buffer_underruns_average, buffer_underruns_count, - metrics.buffer_underruns_average, metrics.buffer_underruns_count); - buffer_underruns_count += metrics.buffer_underruns_count; - } - } - if (codec_index < 0) { - codec_index = metrics.codec_index; - } - if (!is_a2dp_offload) { - is_a2dp_offload = metrics.is_a2dp_offload; - } -} - -bool A2dpSessionMetrics::operator==(const A2dpSessionMetrics& rhs) const { - return audio_duration_ms == rhs.audio_duration_ms && - media_timer_min_ms == rhs.media_timer_min_ms && - media_timer_max_ms == rhs.media_timer_max_ms && - media_timer_avg_ms == rhs.media_timer_avg_ms && - total_scheduling_count == rhs.total_scheduling_count && - buffer_overruns_max_count == rhs.buffer_overruns_max_count && - buffer_overruns_total == rhs.buffer_overruns_total && - buffer_underruns_average == rhs.buffer_underruns_average && - buffer_underruns_count == rhs.buffer_underruns_count && codec_index == rhs.codec_index && - is_a2dp_offload == rhs.is_a2dp_offload; -} - -static DeviceInfo_DeviceType get_device_type(device_type_t type) { - switch (type) { - case DEVICE_TYPE_BREDR: - return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR; - case DEVICE_TYPE_LE: - return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_LE; - case DEVICE_TYPE_DUMO: - return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_DUMO; - case DEVICE_TYPE_UNKNOWN: - default: - return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_UNKNOWN; - } -} - -static BluetoothSession_ConnectionTechnologyType get_connection_tech_type(connection_tech_t type) { - switch (type) { - case CONNECTION_TECHNOLOGY_TYPE_LE: - return BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE; - case CONNECTION_TECHNOLOGY_TYPE_BREDR: - return BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR; - case CONNECTION_TECHNOLOGY_TYPE_UNKNOWN: - default: - return BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_UNKNOWN; - } -} - -static ScanEvent_ScanTechnologyType get_scan_tech_type(scan_tech_t type) { - switch (type) { - case SCAN_TECH_TYPE_LE: - return ScanEvent_ScanTechnologyType::ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_LE; - case SCAN_TECH_TYPE_BREDR: - return ScanEvent_ScanTechnologyType::ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BREDR; - case SCAN_TECH_TYPE_BOTH: - return ScanEvent_ScanTechnologyType::ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BOTH; - case SCAN_TYPE_UNKNOWN: - default: - return ScanEvent_ScanTechnologyType::ScanEvent_ScanTechnologyType_SCAN_TYPE_UNKNOWN; - } -} - -static WakeEvent_WakeEventType get_wake_event_type(wake_event_type_t type) { - switch (type) { - case WAKE_EVENT_ACQUIRED: - return WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED; - case WAKE_EVENT_RELEASED: - return WakeEvent_WakeEventType::WakeEvent_WakeEventType_RELEASED; - case WAKE_EVENT_UNKNOWN: - default: - return WakeEvent_WakeEventType::WakeEvent_WakeEventType_UNKNOWN; - } -} - -static BluetoothSession_DisconnectReasonType get_disconnect_reason_type(disconnect_reason_t type) { - switch (type) { - case DISCONNECT_REASON_METRICS_DUMP: - return BluetoothSession_DisconnectReasonType:: - BluetoothSession_DisconnectReasonType_METRICS_DUMP; - case DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS: - return BluetoothSession_DisconnectReasonType:: - BluetoothSession_DisconnectReasonType_NEXT_START_WITHOUT_END_PREVIOUS; - case DISCONNECT_REASON_UNKNOWN: - default: - return BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_UNKNOWN; - } -} - -static A2dpSourceCodec get_a2dp_source_codec(int64_t codec_index) { - switch (codec_index) { - case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC: - return A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC; - case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC: - return A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC; - case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX: - return A2dpSourceCodec::A2DP_SOURCE_CODEC_APTX; - case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD: - return A2dpSourceCodec::A2DP_SOURCE_CODEC_APTX_HD; - case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC: - return A2dpSourceCodec::A2DP_SOURCE_CODEC_LDAC; - default: - return A2dpSourceCodec::A2DP_SOURCE_CODEC_UNKNOWN; - } -} - -struct BluetoothMetricsLogger::impl { - impl(size_t max_bluetooth_session, size_t max_pair_event, size_t max_wake_event, - size_t max_scan_event) - : bt_session_queue_(new LeakyBondedQueue<BluetoothSession>(max_bluetooth_session)), - pair_event_queue_(new LeakyBondedQueue<PairEvent>(max_pair_event)), - wake_event_queue_(new LeakyBondedQueue<WakeEvent>(max_wake_event)), - scan_event_queue_(new LeakyBondedQueue<ScanEvent>(max_scan_event)) { - bluetooth_log_ = BluetoothLog::default_instance().New(); - headset_profile_connection_counts_.fill(0); - bluetooth_session_ = nullptr; - bluetooth_session_start_time_ms_ = 0; - a2dp_session_metrics_ = A2dpSessionMetrics(); - } - - /* Bluetooth log lock protected */ - BluetoothLog* bluetooth_log_; - std::array<int, HeadsetProfileType_ARRAYSIZE> headset_profile_connection_counts_; - std::recursive_mutex bluetooth_log_lock_; - /* End Bluetooth log lock protected */ - /* Bluetooth session lock protected */ - BluetoothSession* bluetooth_session_; - uint64_t bluetooth_session_start_time_ms_; - A2dpSessionMetrics a2dp_session_metrics_; - std::recursive_mutex bluetooth_session_lock_; - /* End bluetooth session lock protected */ - std::unique_ptr<LeakyBondedQueue<BluetoothSession>> bt_session_queue_; - std::unique_ptr<LeakyBondedQueue<PairEvent>> pair_event_queue_; - std::unique_ptr<LeakyBondedQueue<WakeEvent>> wake_event_queue_; - std::unique_ptr<LeakyBondedQueue<ScanEvent>> scan_event_queue_; -}; - -BluetoothMetricsLogger::BluetoothMetricsLogger() - : pimpl_(new impl(kMaxNumBluetoothSession, kMaxNumPairEvent, kMaxNumWakeEvent, - kMaxNumScanEvent)) {} - -void BluetoothMetricsLogger::LogPairEvent(uint32_t disconnect_reason, uint64_t timestamp_ms, - uint32_t device_class, device_type_t device_type) { - PairEvent* event = new PairEvent(); - DeviceInfo* info = event->mutable_device_paired_with(); - info->set_device_class(device_class); - info->set_device_type(get_device_type(device_type)); - event->set_disconnect_reason(disconnect_reason); - event->set_event_time_millis(timestamp_ms); - pimpl_->pair_event_queue_->Enqueue(event); - { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); - pimpl_->bluetooth_log_->set_num_pair_event(pimpl_->bluetooth_log_->num_pair_event() + 1); - } -} - -void BluetoothMetricsLogger::LogWakeEvent(wake_event_type_t type, const std::string& requestor, - const std::string& name, uint64_t timestamp_ms) { - WakeEvent* event = new WakeEvent(); - event->set_wake_event_type(get_wake_event_type(type)); - event->set_requestor(requestor); - event->set_name(name); - event->set_event_time_millis(timestamp_ms); - pimpl_->wake_event_queue_->Enqueue(event); - { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); - pimpl_->bluetooth_log_->set_num_wake_event(pimpl_->bluetooth_log_->num_wake_event() + 1); - } -} - -void BluetoothMetricsLogger::LogScanEvent(bool start, const std::string& initiator, - scan_tech_t type, uint32_t results, - uint64_t timestamp_ms) { - ScanEvent* event = new ScanEvent(); - if (start) { - event->set_scan_event_type(ScanEvent::SCAN_EVENT_START); - } else { - event->set_scan_event_type(ScanEvent::SCAN_EVENT_STOP); - } - event->set_initiator(initiator); - event->set_scan_technology_type(get_scan_tech_type(type)); - event->set_number_results(results); - event->set_event_time_millis(timestamp_ms); - pimpl_->scan_event_queue_->Enqueue(event); - { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); - pimpl_->bluetooth_log_->set_num_scan_event(pimpl_->bluetooth_log_->num_scan_event() + 1); - } -} - -void BluetoothMetricsLogger::LogBluetoothSessionStart(connection_tech_t connection_tech_type, - uint64_t timestamp_ms) { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); - if (pimpl_->bluetooth_session_ != nullptr) { - LogBluetoothSessionEnd(DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS, 0); - } - if (timestamp_ms == 0) { - timestamp_ms = bluetooth::common::time_get_os_boottime_ms(); - } - pimpl_->bluetooth_session_start_time_ms_ = timestamp_ms; - pimpl_->bluetooth_session_ = new BluetoothSession(); - pimpl_->bluetooth_session_->set_connection_technology_type( - get_connection_tech_type(connection_tech_type)); -} - -void BluetoothMetricsLogger::LogBluetoothSessionEnd(disconnect_reason_t disconnect_reason, - uint64_t timestamp_ms) { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); - if (pimpl_->bluetooth_session_ == nullptr) { - return; - } - if (timestamp_ms == 0) { - timestamp_ms = bluetooth::common::time_get_os_boottime_ms(); - } - int64_t session_duration_sec = (timestamp_ms - pimpl_->bluetooth_session_start_time_ms_) / 1000; - pimpl_->bluetooth_session_->set_session_duration_sec(session_duration_sec); - pimpl_->bluetooth_session_->set_disconnect_reason_type( - get_disconnect_reason_type(disconnect_reason)); - pimpl_->bt_session_queue_->Enqueue(pimpl_->bluetooth_session_); - pimpl_->bluetooth_session_ = nullptr; - pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics(); - { - std::lock_guard<std::recursive_mutex> log_lock(pimpl_->bluetooth_log_lock_); - pimpl_->bluetooth_log_->set_num_bluetooth_session( - pimpl_->bluetooth_log_->num_bluetooth_session() + 1); - } -} - -void BluetoothMetricsLogger::LogBluetoothSessionDeviceInfo(uint32_t device_class, - device_type_t device_type) { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); - if (pimpl_->bluetooth_session_ == nullptr) { - LogBluetoothSessionStart(CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, 0); - } - DeviceInfo* info = pimpl_->bluetooth_session_->mutable_device_connected_to(); - info->set_device_class(device_class); - info->set_device_type(get_device_type(device_type)); -} - -void BluetoothMetricsLogger::LogA2dpSession(const A2dpSessionMetrics& a2dp_session_metrics) { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); - if (pimpl_->bluetooth_session_ == nullptr) { - // When no bluetooth session exist, create one on system's behalf - // Set connection type: for A2DP it is always BR/EDR - LogBluetoothSessionStart(CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); - LogBluetoothSessionDeviceInfo(BTM_COD_MAJOR_AUDIO, DEVICE_TYPE_BREDR); - } - // Accumulate metrics - pimpl_->a2dp_session_metrics_.Update(a2dp_session_metrics); - // Get or allocate new A2DP session object - A2DPSession* a2dp_session = pimpl_->bluetooth_session_->mutable_a2dp_session(); - a2dp_session->set_audio_duration_millis(pimpl_->a2dp_session_metrics_.audio_duration_ms); - a2dp_session->set_media_timer_min_millis(pimpl_->a2dp_session_metrics_.media_timer_min_ms); - a2dp_session->set_media_timer_max_millis(pimpl_->a2dp_session_metrics_.media_timer_max_ms); - a2dp_session->set_media_timer_avg_millis(pimpl_->a2dp_session_metrics_.media_timer_avg_ms); - a2dp_session->set_buffer_overruns_max_count( - pimpl_->a2dp_session_metrics_.buffer_overruns_max_count); - a2dp_session->set_buffer_overruns_total(pimpl_->a2dp_session_metrics_.buffer_overruns_total); - a2dp_session->set_buffer_underruns_average( - pimpl_->a2dp_session_metrics_.buffer_underruns_average); - a2dp_session->set_buffer_underruns_count(pimpl_->a2dp_session_metrics_.buffer_underruns_count); - a2dp_session->set_source_codec(get_a2dp_source_codec(pimpl_->a2dp_session_metrics_.codec_index)); - a2dp_session->set_is_a2dp_offload(pimpl_->a2dp_session_metrics_.is_a2dp_offload); -} - -void BluetoothMetricsLogger::LogHeadsetProfileRfcConnection(tBTA_SERVICE_ID service_id) { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); - switch (service_id) { - case BTA_HSP_SERVICE_ID: - pimpl_->headset_profile_connection_counts_[HeadsetProfileType::HSP]++; - break; - case BTA_HFP_SERVICE_ID: - pimpl_->headset_profile_connection_counts_[HeadsetProfileType::HFP]++; - break; - default: - pimpl_->headset_profile_connection_counts_[HeadsetProfileType::HEADSET_PROFILE_UNKNOWN]++; - break; - } - return; -} - -void BluetoothMetricsLogger::WriteString(std::string* serialized) { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); - log::info("building metrics"); - Build(); - log::info("serializing metrics"); - if (!pimpl_->bluetooth_log_->SerializeToString(serialized)) { - log::error("error serializing metrics"); - } - // Always clean up log objects - pimpl_->bluetooth_log_->Clear(); -} - -void BluetoothMetricsLogger::WriteBase64String(std::string* serialized) { - this->WriteString(serialized); - base::Base64Encode(*serialized, serialized); -} - -void BluetoothMetricsLogger::WriteBase64(int fd) { - std::string protoBase64; - this->WriteBase64String(&protoBase64); - ssize_t ret; - OSI_NO_INTR(ret = write(fd, protoBase64.c_str(), protoBase64.size())); - if (ret == -1) { - log::error("error writing to dumpsys fd: {} ({})", strerror(errno), errno); - } -} - -void BluetoothMetricsLogger::CutoffSession() { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); - if (pimpl_->bluetooth_session_ != nullptr) { - BluetoothSession* new_bt_session = new BluetoothSession(*pimpl_->bluetooth_session_); - new_bt_session->clear_a2dp_session(); - new_bt_session->clear_rfcomm_session(); - LogBluetoothSessionEnd(DISCONNECT_REASON_METRICS_DUMP, 0); - pimpl_->bluetooth_session_ = new_bt_session; - pimpl_->bluetooth_session_start_time_ms_ = bluetooth::common::time_get_os_boottime_ms(); - pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics(); - } -} - -void BluetoothMetricsLogger::Build() { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); - CutoffSession(); - BluetoothLog* bluetooth_log = pimpl_->bluetooth_log_; - while (!pimpl_->bt_session_queue_->Empty() && - static_cast<size_t>(bluetooth_log->session_size()) <= - pimpl_->bt_session_queue_->Capacity()) { - bluetooth_log->mutable_session()->AddAllocated(pimpl_->bt_session_queue_->Dequeue()); - } - while (!pimpl_->pair_event_queue_->Empty() && - static_cast<size_t>(bluetooth_log->pair_event_size()) <= - pimpl_->pair_event_queue_->Capacity()) { - bluetooth_log->mutable_pair_event()->AddAllocated(pimpl_->pair_event_queue_->Dequeue()); - } - while (!pimpl_->scan_event_queue_->Empty() && - static_cast<size_t>(bluetooth_log->scan_event_size()) <= - pimpl_->scan_event_queue_->Capacity()) { - bluetooth_log->mutable_scan_event()->AddAllocated(pimpl_->scan_event_queue_->Dequeue()); - } - while (!pimpl_->wake_event_queue_->Empty() && - static_cast<size_t>(bluetooth_log->wake_event_size()) <= - pimpl_->wake_event_queue_->Capacity()) { - bluetooth_log->mutable_wake_event()->AddAllocated(pimpl_->wake_event_queue_->Dequeue()); - } - while (!pimpl_->bt_session_queue_->Empty() && - static_cast<size_t>(bluetooth_log->wake_event_size()) <= - pimpl_->wake_event_queue_->Capacity()) { - bluetooth_log->mutable_wake_event()->AddAllocated(pimpl_->wake_event_queue_->Dequeue()); - } - for (size_t i = 0; i < HeadsetProfileType_ARRAYSIZE; ++i) { - int num_times_connected = pimpl_->headset_profile_connection_counts_[i]; - if (HeadsetProfileType_IsValid(i) && num_times_connected > 0) { - HeadsetProfileConnectionStats* headset_profile_connection_stats = - bluetooth_log->add_headset_profile_connection_stats(); - // Able to static_cast because HeadsetProfileType_IsValid(i) is true - headset_profile_connection_stats->set_headset_profile_type( - static_cast<HeadsetProfileType>(i)); - headset_profile_connection_stats->set_num_times_connected(num_times_connected); - } - } - pimpl_->headset_profile_connection_counts_.fill(0); -} - -void BluetoothMetricsLogger::ResetSession() { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); - if (pimpl_->bluetooth_session_ != nullptr) { - delete pimpl_->bluetooth_session_; - pimpl_->bluetooth_session_ = nullptr; - } - pimpl_->bluetooth_session_start_time_ms_ = 0; - pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics(); -} - -void BluetoothMetricsLogger::ResetLog() { - std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); - pimpl_->bluetooth_log_->Clear(); -} - -void BluetoothMetricsLogger::Reset() { - ResetSession(); - ResetLog(); - pimpl_->bt_session_queue_->Clear(); - pimpl_->pair_event_queue_->Clear(); - pimpl_->wake_event_queue_->Clear(); - pimpl_->scan_event_queue_->Clear(); -} void LogLinkLayerConnectionEvent(const RawAddress* address, uint32_t connection_handle, android::bluetooth::DirectionEnum direction, uint16_t link_type, diff --git a/system/common/metrics.h b/system/common/metrics.h index 8cd41bd6db..0aad20819e 100644 --- a/system/common/metrics.h +++ b/system/common/metrics.h @@ -36,252 +36,6 @@ namespace bluetooth { namespace common { -// Typedefs to hide protobuf definition to the rest of stack - -typedef enum { - DEVICE_TYPE_UNKNOWN, - DEVICE_TYPE_BREDR, - DEVICE_TYPE_LE, - DEVICE_TYPE_DUMO, -} device_type_t; - -typedef enum { - WAKE_EVENT_UNKNOWN, - WAKE_EVENT_ACQUIRED, - WAKE_EVENT_RELEASED, -} wake_event_type_t; - -typedef enum { - SCAN_TYPE_UNKNOWN, - SCAN_TECH_TYPE_LE, - SCAN_TECH_TYPE_BREDR, - SCAN_TECH_TYPE_BOTH, -} scan_tech_t; - -typedef enum { - CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, - CONNECTION_TECHNOLOGY_TYPE_LE, - CONNECTION_TECHNOLOGY_TYPE_BREDR, -} connection_tech_t; - -typedef enum { - DISCONNECT_REASON_UNKNOWN, - DISCONNECT_REASON_METRICS_DUMP, - DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS, -} disconnect_reason_t; - -/* Values of A2DP metrics that we care about - * - * audio_duration_ms : sum of audio duration (in milliseconds). - * device_class: device class of the paired device. - * media_timer_min_ms : minimum scheduled time (in milliseconds) - * of the media timer. - * media_timer_max_ms: maximum scheduled time (in milliseconds) - * of the media timer. - * media_timer_avg_ms: average scheduled time (in milliseconds) - * of the media timer. - * buffer_overruns_max_count: TODO - not clear what this is. - * buffer_overruns_total : number of times the media buffer with - * audio data has overrun - * buffer_underruns_average: TODO - not clear what this is. - * buffer_underruns_count: number of times there was no enough - * audio data to add to the media buffer. - * NOTE: Negative values are invalid - */ -class A2dpSessionMetrics { -public: - A2dpSessionMetrics() {} - - /* - * Update the metrics value in the current metrics object using the metrics - * objects supplied - */ - void Update(const A2dpSessionMetrics& metrics); - - /* - * Compare whether two metrics objects are equal - */ - bool operator==(const A2dpSessionMetrics& rhs) const; - - /* - * Initialize all values to -1 which is invalid in order to make a distinction - * between 0 and invalid values - */ - int64_t audio_duration_ms = -1; - int32_t media_timer_min_ms = -1; - int32_t media_timer_max_ms = -1; - int32_t media_timer_avg_ms = -1; - int64_t total_scheduling_count = -1; - int32_t buffer_overruns_max_count = -1; - int32_t buffer_overruns_total = -1; - float buffer_underruns_average = -1; - int32_t buffer_underruns_count = -1; - int64_t codec_index = -1; - bool is_a2dp_offload = false; -}; - -class BluetoothMetricsLogger { -public: - static BluetoothMetricsLogger* GetInstance() { - static BluetoothMetricsLogger* instance = new BluetoothMetricsLogger(); - return instance; - } - - /* - * Record a pairing event - * - * Parameters: - * timestamp_ms: Unix epoch time in milliseconds - * device_class: class of remote device - * device_type: type of remote device - * disconnect_reason: HCI reason for pairing disconnection. - * See: stack/include/hcidefs.h - */ - void LogPairEvent(uint32_t disconnect_reason, uint64_t timestamp_ms, uint32_t device_class, - device_type_t device_type); - - /* - * Record a wake event - * - * Parameters: - * timestamp_ms: Unix epoch time in milliseconds - * type: whether it was acquired or released - * requestor: if provided is the service requesting the wake lock - * name: the name of the wake lock held - */ - void LogWakeEvent(wake_event_type_t type, const std::string& requestor, const std::string& name, - uint64_t timestamp_ms); - - /* - * Record a scan event - * - * Parameters - * timestamp_ms : Unix epoch time in milliseconds - * start : true if this is the beginning of the scan - * initiator: a unique ID identifying the app starting the scan - * type: whether the scan reports BR/EDR, LE, or both. - * results: number of results to be reported. - */ - void LogScanEvent(bool start, const std::string& initiator, scan_tech_t type, uint32_t results, - uint64_t timestamp_ms); - - /* - * Start logging a Bluetooth session - * - * A Bluetooth session is defined a a connection between this device and - * another remote device which may include multiple profiles and protocols - * - * Only one Bluetooth session can exist at one time. Calling this method twice - * without LogBluetoothSessionEnd will result in logging a premature end of - * current Bluetooth session - * - * Parameters: - * connection_tech_type : type of connection technology - * timestamp_ms : the timestamp for session start, 0 means now - * - */ - void LogBluetoothSessionStart(connection_tech_t connection_tech_type, uint64_t timestamp_ms); - - /* - * Stop logging a Bluetooth session and pushes it to the log queue - * - * If no Bluetooth session exist, this method exits immediately - * - * Parameters: - * disconnect_reason : A string representation of disconnect reason - * timestamp_ms : the timestamp of session end, 0 means now - * - */ - void LogBluetoothSessionEnd(disconnect_reason_t disconnect_reason, uint64_t timestamp_ms); - - /* - * Log information about remote device in a current Bluetooth session - * - * If a Bluetooth session does not exist, create one with default parameter - * and timestamp now - * - * Parameters: - * device_class : device_class defined in btm_api_types.h - * device_type : type of remote device - */ - void LogBluetoothSessionDeviceInfo(uint32_t device_class, device_type_t device_type); - - /* - * Log A2DP Audio Session Information - * - * - Repeated calls to this method will override previous metrics if in the - * same Bluetooth connection - * - If a Bluetooth session does not exist, create one with default parameter - * and timestamp now - * - * Parameters: - * a2dp_session_metrics - pointer to struct holding a2dp stats - * - */ - void LogA2dpSession(const A2dpSessionMetrics& a2dp_session_metrics); - - /** - * Log Headset profile RFCOMM connection event - * - * @param service_id the BTA service ID for this headset connection - */ - void LogHeadsetProfileRfcConnection(tBTA_SERVICE_ID service_id); - - /* - * Writes the metrics, in base64 protobuf format, into the descriptor FD, - * metrics events are always cleared after dump - */ - void WriteBase64(int fd); - void WriteBase64String(std::string* serialized); - void WriteString(std::string* serialized); - - /* - * Reset the metrics logger by cleaning up its staging queues and existing - * protobuf objects. - */ - void Reset(); - - /* - * Maximum number of log entries for each session or event - */ - static const size_t kMaxNumBluetoothSession = 50; - static const size_t kMaxNumPairEvent = 50; - static const size_t kMaxNumWakeEvent = 1000; - static const size_t kMaxNumScanEvent = 50; - -private: - BluetoothMetricsLogger(); - - /* - * When a Bluetooth session is on and the user initiates a metrics dump, we - * need to be able to upload whatever we have first. This method breaks the - * ongoing Bluetooth session into two sessions with the previous one labeled - * as "METRICS_DUMP" for the disconnect reason. - */ - void CutoffSession(); - - /* - * Build the internal metrics object using information gathered - */ - void Build(); - - /* - * Reset objects related to current Bluetooth session - */ - void ResetSession(); - - /* - * Reset the underlining BluetoothLog object - */ - void ResetLog(); - - /* - * PIMPL style implementation to hide internal dependencies - */ - struct impl; - std::unique_ptr<impl> const pimpl_; -}; - /** * Unknown connection handle for metrics purpose */ diff --git a/system/common/metrics_linux.cc b/system/common/metrics_linux.cc index 009d16878e..e49dd1d2cf 100644 --- a/system/common/metrics_linux.cc +++ b/system/common/metrics_linux.cc @@ -25,64 +25,6 @@ namespace bluetooth { namespace common { -void A2dpSessionMetrics::Update(const A2dpSessionMetrics& /* metrics */) {} - -bool A2dpSessionMetrics::operator==(const A2dpSessionMetrics& /* rhs */) const { - log::info("UNIMPLEMENTED"); - return true; -} - -struct BluetoothMetricsLogger::impl { - impl(size_t /* max_bluetooth_session */, size_t /* max_pair_event */, size_t /* max_wake_event */, - size_t /* max_scan_event */) {} -}; - -BluetoothMetricsLogger::BluetoothMetricsLogger() - : pimpl_(new impl(kMaxNumBluetoothSession, kMaxNumPairEvent, kMaxNumWakeEvent, - kMaxNumScanEvent)) {} - -void BluetoothMetricsLogger::LogPairEvent(uint32_t /* disconnect_reason */, - uint64_t /* timestamp_ms */, uint32_t /* device_class */, - device_type_t /* device_type */) {} - -void BluetoothMetricsLogger::LogWakeEvent(wake_event_type_t /* type */, - const std::string& /* requestor */, - const std::string& /* name */, - uint64_t /* timestamp_ms */) {} - -void BluetoothMetricsLogger::LogScanEvent(bool /* start */, const std::string& /* initiator */, - scan_tech_t /* type */, uint32_t /* results */, - uint64_t /* timestamp_ms */) {} - -void BluetoothMetricsLogger::LogBluetoothSessionStart(connection_tech_t /* connection_tech_type */, - uint64_t /* timestamp_ms */) {} - -void BluetoothMetricsLogger::LogBluetoothSessionEnd(disconnect_reason_t /* disconnect_reason */, - uint64_t /* timestamp_ms */) {} - -void BluetoothMetricsLogger::LogBluetoothSessionDeviceInfo(uint32_t /* device_class */, - device_type_t /* device_type */) {} - -void BluetoothMetricsLogger::LogA2dpSession(const A2dpSessionMetrics& /* a2dp_session_metrics */) {} - -void BluetoothMetricsLogger::LogHeadsetProfileRfcConnection(tBTA_SERVICE_ID /* service_id */) {} - -void BluetoothMetricsLogger::WriteString(std::string* /* serialized */) {} - -void BluetoothMetricsLogger::WriteBase64String(std::string* /* serialized */) {} - -void BluetoothMetricsLogger::WriteBase64(int /* fd */) {} - -void BluetoothMetricsLogger::CutoffSession() {} - -void BluetoothMetricsLogger::Build() {} - -void BluetoothMetricsLogger::ResetSession() {} - -void BluetoothMetricsLogger::ResetLog() {} - -void BluetoothMetricsLogger::Reset() {} - void LogClassicPairingEvent(const RawAddress& /* address */, uint16_t /* handle */, uint32_t /* hci_cmd */, uint16_t /* hci_event */, uint16_t /* cmd_status */, uint16_t /* reason_code */, diff --git a/system/common/metrics_unittest.cc b/system/common/metrics_unittest.cc deleted file mode 100644 index 4bbabb242a..0000000000 --- a/system/common/metrics_unittest.cc +++ /dev/null @@ -1,944 +0,0 @@ -/****************************************************************************** - * - * Copyright 2016 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ -#include "common/metrics.h" - -#include <gmock/gmock.h> -#include <gtest/gtest.h> -#include <include/hardware/bt_av.h> - -#include <chrono> -#include <cstdint> -#include <string> -#include <thread> -#include <vector> - -#include "bluetooth/metrics/bluetooth.pb.h" -#include "common/time_util.h" - -#define BTM_COD_MAJOR_AUDIO_TEST 0x04 - -namespace testing { - -using bluetooth::common::A2dpSessionMetrics; -using bluetooth::common::BluetoothMetricsLogger; -using bluetooth::metrics::BluetoothMetricsProto::A2DPSession; -using bluetooth::metrics::BluetoothMetricsProto::A2dpSourceCodec; -using bluetooth::metrics::BluetoothMetricsProto::BluetoothLog; -using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession; -using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession_ConnectionTechnologyType; -using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession_DisconnectReasonType; -using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo; -using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo_DeviceType; -using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileConnectionStats; -using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType; -using bluetooth::metrics::BluetoothMetricsProto::PairEvent; -using bluetooth::metrics::BluetoothMetricsProto::RFCommSession; -using bluetooth::metrics::BluetoothMetricsProto::ScanEvent; -using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanEventType; -using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanTechnologyType; -using bluetooth::metrics::BluetoothMetricsProto::WakeEvent; -using bluetooth::metrics::BluetoothMetricsProto::WakeEvent_WakeEventType; - -namespace { -const size_t kMaxEventGenerationLimit = 5000; -} - -static void sleep_ms(int64_t t) { std::this_thread::sleep_for(std::chrono::milliseconds(t)); } - -static DeviceInfo* MakeDeviceInfo(int32_t device_class, DeviceInfo_DeviceType device_type) { - DeviceInfo* info = new DeviceInfo(); - info->set_device_class(device_class); - info->set_device_type(device_type); - return info; -} - -static PairEvent* MakePairEvent(int32_t disconnect_reason, int64_t timestamp_ms, - DeviceInfo* device_info) { - PairEvent* event = new PairEvent(); - event->set_disconnect_reason(disconnect_reason); - event->set_event_time_millis(timestamp_ms); - if (device_info) { - event->set_allocated_device_paired_with(device_info); - } - return event; -} - -static WakeEvent* MakeWakeEvent(WakeEvent_WakeEventType event_type, const std::string& requestor, - const std::string& name, int64_t timestamp_ms) { - WakeEvent* event = new WakeEvent(); - event->set_wake_event_type(event_type); - event->set_requestor(requestor); - event->set_name(name); - event->set_event_time_millis(timestamp_ms); - return event; -} - -static ScanEvent* MakeScanEvent(ScanEvent_ScanEventType event_type, const std::string& initiator, - ScanEvent_ScanTechnologyType tech_type, int32_t num_results, - int64_t timestamp_ms) { - ScanEvent* event = new ScanEvent(); - event->set_scan_event_type(event_type); - event->set_initiator(initiator); - event->set_scan_technology_type(tech_type); - event->set_number_results(num_results); - event->set_event_time_millis(timestamp_ms); - return event; -} - -static A2DPSession* MakeA2DPSession(const A2dpSessionMetrics& metrics, - A2dpSourceCodec source_codec) { - A2DPSession* session = new A2DPSession(); - session->set_media_timer_min_millis(metrics.media_timer_min_ms); - session->set_media_timer_max_millis(metrics.media_timer_max_ms); - session->set_media_timer_avg_millis(metrics.media_timer_avg_ms); - session->set_buffer_overruns_max_count(metrics.buffer_overruns_max_count); - session->set_buffer_overruns_total(metrics.buffer_overruns_total); - session->set_buffer_underruns_average(metrics.buffer_underruns_average); - session->set_buffer_underruns_count(metrics.buffer_underruns_count); - session->set_audio_duration_millis(metrics.audio_duration_ms); - session->set_source_codec(source_codec); - session->set_is_a2dp_offload(metrics.is_a2dp_offload); - return session; -} - -static BluetoothSession* MakeBluetoothSession( - int64_t session_duration_sec, BluetoothSession_ConnectionTechnologyType conn_type, - BluetoothSession_DisconnectReasonType disconnect_reason, DeviceInfo* device_info, - RFCommSession* rfcomm_session, A2DPSession* a2dp_session) { - BluetoothSession* session = new BluetoothSession(); - if (a2dp_session) { - session->set_allocated_a2dp_session(a2dp_session); - } - if (rfcomm_session) { - session->set_allocated_rfcomm_session(rfcomm_session); - } - if (device_info) { - session->set_allocated_device_connected_to(device_info); - } - session->set_session_duration_sec(session_duration_sec); - session->set_connection_technology_type(conn_type); - session->set_disconnect_reason_type(disconnect_reason); - return session; -} - -static void GenerateWakeEvents(size_t start, size_t end, std::vector<WakeEvent*>* wake_events) { - for (size_t i = start; i < end; ++i) { - wake_events->push_back( - MakeWakeEvent(i % 2 == 0 ? WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED - : WakeEvent_WakeEventType::WakeEvent_WakeEventType_RELEASED, - "TEST_REQ", "TEST_NAME", i)); - } -} - -#define COMPARE_A2DP_METRICS(a, b) \ - do { \ - EXPECT_EQ((a).audio_duration_ms, (b).audio_duration_ms); \ - EXPECT_EQ((a).media_timer_min_ms, (b).media_timer_min_ms); \ - EXPECT_EQ((a).media_timer_max_ms, (b).media_timer_max_ms); \ - EXPECT_EQ((a).media_timer_avg_ms, (b).media_timer_avg_ms); \ - EXPECT_EQ((a).total_scheduling_count, (b).total_scheduling_count); \ - EXPECT_EQ((a).buffer_overruns_max_count, (b).buffer_overruns_max_count); \ - EXPECT_EQ((a).buffer_overruns_total, (b).buffer_overruns_total); \ - EXPECT_THAT((a).buffer_underruns_average, FloatNear((b).buffer_underruns_average, 0.01)); \ - (a).buffer_underruns_average = (b).buffer_underruns_average; \ - EXPECT_EQ((a).buffer_underruns_count, (b).buffer_underruns_count); \ - EXPECT_EQ((a).codec_index, (b).codec_index); \ - EXPECT_EQ((a).is_a2dp_offload, (b).is_a2dp_offload); \ - } while (0) - -/* - * metrics_sum = metrics1 + metrics2 - */ -TEST(BluetoothA2DPSessionMetricsTest, TestUpdateNormal) { - A2dpSessionMetrics metrics1; - A2dpSessionMetrics metrics2; - A2dpSessionMetrics metrics_sum; - metrics1.audio_duration_ms = 10; - metrics2.audio_duration_ms = 25; - metrics_sum.audio_duration_ms = 35; - metrics1.media_timer_min_ms = 10; - metrics2.media_timer_min_ms = 25; - metrics_sum.media_timer_min_ms = 10; - metrics1.media_timer_max_ms = 100; - metrics2.media_timer_max_ms = 200; - metrics_sum.media_timer_max_ms = 200; - metrics1.media_timer_avg_ms = 50; - metrics1.total_scheduling_count = 50; - metrics2.media_timer_avg_ms = 100; - metrics2.total_scheduling_count = 50; - metrics_sum.media_timer_avg_ms = 75; - metrics_sum.total_scheduling_count = 100; - metrics1.buffer_overruns_max_count = 70; - metrics2.buffer_overruns_max_count = 80; - metrics_sum.buffer_overruns_max_count = 80; - metrics1.buffer_underruns_average = 80; - metrics1.buffer_underruns_count = 1200; - metrics2.buffer_underruns_average = 130; - metrics2.buffer_underruns_count = 2400; - metrics_sum.buffer_underruns_average = 113.33333333; - metrics_sum.buffer_underruns_count = 3600; - metrics1.codec_index = -1; - metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC; - metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC; - metrics1.is_a2dp_offload = false; - metrics2.is_a2dp_offload = true; - metrics_sum.is_a2dp_offload = true; - metrics1.Update(metrics2); - COMPARE_A2DP_METRICS(metrics1, metrics_sum); - EXPECT_TRUE(metrics1 == metrics_sum); - EXPECT_EQ(metrics1, metrics_sum); -} - -TEST(BluetoothA2DPSessionMetricsTest, TestUpdateNew) { - A2dpSessionMetrics metrics1; - A2dpSessionMetrics metrics2; - A2dpSessionMetrics metrics_sum; - metrics2.audio_duration_ms = 25; - metrics_sum.audio_duration_ms = 25; - metrics2.media_timer_min_ms = 25; - metrics_sum.media_timer_min_ms = 25; - metrics2.media_timer_max_ms = 200; - metrics_sum.media_timer_max_ms = 200; - metrics2.media_timer_avg_ms = 100; - metrics2.total_scheduling_count = 50; - metrics_sum.media_timer_avg_ms = 100; - metrics_sum.total_scheduling_count = 50; - metrics2.buffer_overruns_max_count = 80; - metrics_sum.buffer_overruns_max_count = 80; - metrics2.buffer_underruns_average = 130; - metrics2.buffer_underruns_count = 2400; - metrics_sum.buffer_underruns_average = 130; - metrics_sum.buffer_underruns_count = 2400; - metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_APTX; - metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_APTX; - metrics2.is_a2dp_offload = true; - metrics_sum.is_a2dp_offload = true; - metrics1.Update(metrics2); - COMPARE_A2DP_METRICS(metrics1, metrics_sum); - EXPECT_TRUE(metrics1 == metrics_sum); - EXPECT_EQ(metrics1, metrics_sum); -} - -TEST(BluetoothA2DPSessionMetricsTest, TestNullUpdate) { - A2dpSessionMetrics metrics1; - A2dpSessionMetrics metrics2; - A2dpSessionMetrics metrics_sum; - metrics2.audio_duration_ms = 25; - metrics_sum.audio_duration_ms = 25; - metrics2.media_timer_min_ms = 25; - metrics_sum.media_timer_min_ms = 25; - metrics2.media_timer_max_ms = 200; - metrics_sum.media_timer_max_ms = 200; - metrics2.media_timer_avg_ms = 100; - metrics2.total_scheduling_count = 50; - metrics_sum.media_timer_avg_ms = 100; - metrics_sum.total_scheduling_count = 50; - metrics2.buffer_overruns_max_count = 80; - metrics_sum.buffer_overruns_max_count = 80; - metrics2.buffer_underruns_average = 130; - metrics2.buffer_underruns_count = 2400; - metrics_sum.buffer_underruns_average = 130; - metrics_sum.buffer_underruns_count = 2400; - metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD; - metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD; - metrics2.is_a2dp_offload = true; - metrics_sum.is_a2dp_offload = true; - metrics2.Update(metrics1); - COMPARE_A2DP_METRICS(metrics2, metrics_sum); - EXPECT_TRUE(metrics2 == metrics_sum); - EXPECT_EQ(metrics2, metrics_sum); -} - -TEST(BluetoothA2DPSessionMetricsTest, TestPartialUpdate) { - A2dpSessionMetrics metrics1; - A2dpSessionMetrics metrics2; - A2dpSessionMetrics metrics_sum; - metrics1.audio_duration_ms = 10; - metrics2.audio_duration_ms = 25; - metrics_sum.audio_duration_ms = 35; - metrics1.media_timer_min_ms = 10; - metrics_sum.media_timer_min_ms = 10; - metrics1.media_timer_max_ms = 100; - metrics_sum.media_timer_max_ms = 100; - metrics1.media_timer_avg_ms = 50; - metrics1.total_scheduling_count = 50; - metrics2.media_timer_avg_ms = 100; - metrics_sum.media_timer_avg_ms = 50; - metrics_sum.total_scheduling_count = 50; - metrics1.buffer_overruns_max_count = 70; - metrics_sum.buffer_overruns_max_count = 70; - metrics1.buffer_underruns_average = 80; - metrics1.buffer_underruns_count = 1200; - metrics2.buffer_underruns_count = 2400; - metrics_sum.buffer_underruns_average = 80; - metrics_sum.buffer_underruns_count = 1200; - metrics1.Update(metrics2); - COMPARE_A2DP_METRICS(metrics1, metrics_sum); - EXPECT_TRUE(metrics1 == metrics_sum); - EXPECT_EQ(metrics1, metrics_sum); -} - -class BluetoothMetricsLoggerTest : public Test { -protected: - // Use to hold test protos - std::vector<PairEvent*> pair_events_; - std::vector<WakeEvent*> wake_events_; - std::vector<ScanEvent*> scan_events_; - std::vector<BluetoothSession*> bt_sessions_; - int64_t num_pair_event_ = 0; - int64_t num_wake_event_ = 0; - int64_t num_scan_event_ = 0; - int64_t num_bt_session_ = 0; - BluetoothLog* bt_log_; - std::string bt_log_str_; - std::string bt_log_ascii_str_; - - void UpdateLog() { - for (BluetoothSession* session : bt_sessions_) { - bt_log_->mutable_session()->AddAllocated(session); - } - if (num_bt_session_ > 0) { - bt_log_->set_num_bluetooth_session(num_bt_session_); - } else if (bt_sessions_.size() > 0) { - bt_log_->set_num_bluetooth_session(bt_sessions_.size()); - } - bt_sessions_.clear(); - for (PairEvent* event : pair_events_) { - bt_log_->mutable_pair_event()->AddAllocated(event); - } - if (num_pair_event_ > 0) { - bt_log_->set_num_pair_event(num_pair_event_); - } else if (pair_events_.size() > 0) { - bt_log_->set_num_pair_event(pair_events_.size()); - } - pair_events_.clear(); - for (WakeEvent* event : wake_events_) { - bt_log_->mutable_wake_event()->AddAllocated(event); - } - if (num_wake_event_ > 0) { - bt_log_->set_num_wake_event(num_wake_event_); - } else if (wake_events_.size() > 0) { - bt_log_->set_num_wake_event(wake_events_.size()); - } - wake_events_.clear(); - for (ScanEvent* event : scan_events_) { - bt_log_->mutable_scan_event()->AddAllocated(event); - } - if (num_scan_event_ > 0) { - bt_log_->set_num_scan_event(num_scan_event_); - } else if (scan_events_.size() > 0) { - bt_log_->set_num_scan_event(scan_events_.size()); - } - scan_events_.clear(); - bt_log_->SerializeToString(&bt_log_str_); - } - - void ClearLog() { - for (BluetoothSession* session : bt_sessions_) { - session->Clear(); - delete session; - } - bt_sessions_.clear(); - for (PairEvent* event : pair_events_) { - event->Clear(); - delete event; - } - pair_events_.clear(); - for (WakeEvent* event : wake_events_) { - event->Clear(); - delete event; - } - wake_events_.clear(); - for (ScanEvent* event : scan_events_) { - event->Clear(); - delete event; - } - scan_events_.clear(); - bt_log_->Clear(); - } - - void SetUp() override { - bt_log_ = new BluetoothLog(); - // Clear existing metrics entries, if any - BluetoothMetricsLogger::GetInstance()->Reset(); - } - void TearDown() override { - // Clear remaining metrics entries, if any - BluetoothMetricsLogger::GetInstance()->Reset(); - ClearLog(); - delete bt_log_; - } - -public: -}; - -TEST_F(BluetoothMetricsLoggerTest, PairEventTest) { - pair_events_.push_back(MakePairEvent( - 35, 12345, - MakeDeviceInfo(42, DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR))); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogPairEvent(35, 12345, 42, - bluetooth::common::DEVICE_TYPE_BREDR); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -TEST_F(BluetoothMetricsLoggerTest, WakeEventTest) { - wake_events_.push_back(MakeWakeEvent(WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED, - "TEST_REQ", "TEST_NAME", 12345)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogWakeEvent(bluetooth::common::WAKE_EVENT_ACQUIRED, - "TEST_REQ", "TEST_NAME", 12345); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -TEST_F(BluetoothMetricsLoggerTest, WakeEventOverrunTest) { - GenerateWakeEvents(kMaxEventGenerationLimit - BluetoothMetricsLogger::kMaxNumWakeEvent, - kMaxEventGenerationLimit, &wake_events_); - num_wake_event_ = kMaxEventGenerationLimit; - UpdateLog(); - for (size_t i = 0; i < kMaxEventGenerationLimit; ++i) { - BluetoothMetricsLogger::GetInstance()->LogWakeEvent( - i % 2 == 0 ? bluetooth::common::WAKE_EVENT_ACQUIRED - : bluetooth::common::WAKE_EVENT_RELEASED, - "TEST_REQ", "TEST_NAME", i); - } - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -TEST_F(BluetoothMetricsLoggerTest, ScanEventTest) { - scan_events_.push_back(MakeScanEvent( - ScanEvent_ScanEventType::ScanEvent_ScanEventType_SCAN_EVENT_STOP, "TEST_INITIATOR", - ScanEvent_ScanTechnologyType::ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BREDR, 42, - 123456)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogScanEvent( - false, "TEST_INITIATOR", bluetooth::common::SCAN_TECH_TYPE_BREDR, 42, 123456); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionTest) { - bt_sessions_.push_back(MakeBluetoothSession( - 10, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_UNKNOWN, - nullptr, nullptr, nullptr)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE, 123456); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( - bluetooth::common::DISCONNECT_REASON_UNKNOWN, 133456); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionDumpBeforeEndTest) { - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_METRICS_DUMP, - nullptr, nullptr, nullptr)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE, - bluetooth::common::time_get_os_boottime_ms()); - sleep_ms(1000); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionStartBeforeEndTest) { - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, - BluetoothSession_DisconnectReasonType:: - BluetoothSession_DisconnectReasonType_NEXT_START_WITHOUT_END_PREVIOUS, - nullptr, nullptr, nullptr)); - bt_sessions_.push_back(MakeBluetoothSession( - 2, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_METRICS_DUMP, - nullptr, nullptr, nullptr)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, 0); - sleep_ms(1000); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE, 0); - sleep_ms(2000); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -/* - * Test Case: A2DPSessionTwoUpdatesTest - * - * 1. Create Instance - * 2. LogBluetoothSessionStart - * 3. LogBluetoothSessionDeviceInfo - * 4. LogA2dpSession - * 5. LogA2dpSession - * 6. LogBluetoothSessionEnd - * 7. WriteString - * - */ -TEST_F(BluetoothMetricsLoggerTest, A2DPSessionTwoUpdatesTest) { - /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */ - A2dpSessionMetrics metrics1; - A2dpSessionMetrics metrics2; - A2dpSessionMetrics metrics_sum; - metrics1.audio_duration_ms = 10; - metrics2.audio_duration_ms = 25; - metrics_sum.audio_duration_ms = 35; - metrics1.media_timer_min_ms = 10; - metrics2.media_timer_min_ms = 25; - metrics_sum.media_timer_min_ms = 10; - metrics1.media_timer_max_ms = 100; - metrics2.media_timer_max_ms = 200; - metrics_sum.media_timer_max_ms = 200; - metrics1.media_timer_avg_ms = 50; - metrics1.total_scheduling_count = 50; - metrics2.media_timer_avg_ms = 100; - metrics2.total_scheduling_count = 50; - metrics_sum.media_timer_avg_ms = 75; - metrics_sum.total_scheduling_count = 100; - metrics1.buffer_overruns_max_count = 70; - metrics2.buffer_overruns_max_count = 80; - metrics_sum.buffer_overruns_max_count = 80; - metrics1.buffer_underruns_average = 80; - metrics1.buffer_underruns_count = 1200; - metrics2.buffer_underruns_average = 130; - metrics2.buffer_underruns_count = 2400; - metrics_sum.buffer_underruns_average = 113.33333333; - metrics_sum.buffer_underruns_count = 3600; - metrics1.codec_index = -1; - metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC; - metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC; - metrics1.is_a2dp_offload = false; - metrics2.is_a2dp_offload = true; - metrics_sum.is_a2dp_offload = true; - DeviceInfo* info = MakeDeviceInfo(BTM_COD_MAJOR_AUDIO_TEST, - DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR); - A2DPSession* session = MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC); - bt_sessions_.push_back(MakeBluetoothSession( - 10, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_UNKNOWN, - info, nullptr, session)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 123456); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo( - BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( - bluetooth::common::DISCONNECT_REASON_UNKNOWN, 133456); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -/* - * Test Case: A2DPSessionTwoUpdatesSeparatedbyDumpTest - * - * 1. Create Instance - * 2. LogBluetoothSessionStart - * 3. LogBluetoothSessionDeviceInfo - * 4. LogA2dpSession - * 5. WriteString - * 6. LogA2dpSession - * 7. LogBluetoothSessionEnd - * 8. WriteString - * - */ -TEST_F(BluetoothMetricsLoggerTest, A2DPSessionTwoUpdatesSeparatedbyDumpTest) { - /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */ - A2dpSessionMetrics metrics1; - A2dpSessionMetrics metrics2; - metrics1.audio_duration_ms = 10; - metrics2.audio_duration_ms = 25; - metrics1.media_timer_min_ms = 10; - metrics2.media_timer_min_ms = 25; - metrics1.media_timer_max_ms = 100; - metrics2.media_timer_max_ms = 200; - metrics1.media_timer_avg_ms = 50; - metrics1.total_scheduling_count = 50; - metrics2.media_timer_avg_ms = 100; - metrics2.total_scheduling_count = 50; - metrics1.buffer_overruns_max_count = 70; - metrics2.buffer_overruns_max_count = 80; - metrics1.buffer_underruns_average = 80; - metrics1.buffer_underruns_count = 1200; - metrics2.buffer_underruns_average = 130; - metrics2.buffer_underruns_count = 2400; - metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC; - metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC; - DeviceInfo* info = MakeDeviceInfo(BTM_COD_MAJOR_AUDIO_TEST, - DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR); - A2DPSession* session = MakeA2DPSession(metrics1, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC); - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_METRICS_DUMP, - info, nullptr, session)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo( - BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1); - sleep_ms(1000); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); - ClearLog(); - info = MakeDeviceInfo(BTM_COD_MAJOR_AUDIO_TEST, - DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR); - session = MakeA2DPSession(metrics2, A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC); - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_UNKNOWN, - info, nullptr, session)); - UpdateLog(); - sleep_ms(1000); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( - bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0); - msg_str.clear(); - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -/* - * Test Case: A2DPSessionTwoUpdatesSeparatedbyEndTest - * - * 1. Create Instance - * 2. LogBluetoothSessionStart - * 3. LogA2dpSession - * 4. LogBluetoothSessionEnd - * 5. LogBluetoothSessionStart - * 6. LogA2dpSession - * 7. LogBluetoothSessionEnd - * 8. WriteString - * - */ -TEST_F(BluetoothMetricsLoggerTest, A2DPSessionTwoUpdatesSeparatedbyEndTest) { - /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */ - A2dpSessionMetrics metrics1; - metrics1.audio_duration_ms = 10; - metrics1.media_timer_min_ms = 10; - metrics1.media_timer_max_ms = 100; - metrics1.media_timer_avg_ms = 50; - metrics1.total_scheduling_count = 50; - metrics1.buffer_overruns_max_count = 70; - metrics1.buffer_underruns_average = 80; - metrics1.buffer_underruns_count = 1200; - metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC; - DeviceInfo* info = MakeDeviceInfo(BTM_COD_MAJOR_AUDIO_TEST, - DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR); - A2DPSession* session = MakeA2DPSession(metrics1, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC); - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_UNKNOWN, - info, nullptr, session)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo( - BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1); - sleep_ms(1000); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( - bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); - ClearLog(); - A2dpSessionMetrics metrics2; - metrics2.audio_duration_ms = 25; - metrics2.media_timer_min_ms = 25; - metrics2.media_timer_max_ms = 200; - metrics2.media_timer_avg_ms = 100; - metrics2.total_scheduling_count = 50; - metrics2.buffer_overruns_max_count = 80; - metrics2.buffer_underruns_average = 130; - metrics2.buffer_underruns_count = 2400; - metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC; - session = MakeA2DPSession(metrics2, A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC); - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_UNKNOWN, - nullptr, nullptr, session)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); - sleep_ms(1000); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( - bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0); - msg_str.clear(); - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -/* - * Test Case 1: A2DPSessionOnlyTest - * - * 1. Create Instance - * 4. LogA2dpSession - * 5. WriteString - * 6. LogA2dpSession - * 8. WriteString - * - */ -TEST_F(BluetoothMetricsLoggerTest, A2DPSessionOnlyTest) { - /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */ - A2dpSessionMetrics metrics1; - A2dpSessionMetrics metrics2; - A2dpSessionMetrics metrics_sum; - metrics1.audio_duration_ms = 10; - metrics2.audio_duration_ms = 25; - metrics_sum.audio_duration_ms = 35; - metrics1.media_timer_min_ms = 10; - metrics2.media_timer_min_ms = 25; - metrics_sum.media_timer_min_ms = 10; - metrics1.media_timer_max_ms = 100; - metrics2.media_timer_max_ms = 200; - metrics_sum.media_timer_max_ms = 200; - metrics1.media_timer_avg_ms = 50; - metrics1.total_scheduling_count = 50; - metrics2.media_timer_avg_ms = 100; - metrics2.total_scheduling_count = 50; - metrics_sum.media_timer_avg_ms = 75; - metrics_sum.total_scheduling_count = 100; - metrics1.buffer_overruns_max_count = 70; - metrics2.buffer_overruns_max_count = 80; - metrics_sum.buffer_overruns_max_count = 80; - metrics1.buffer_underruns_average = 80; - metrics1.buffer_underruns_count = 1200; - metrics2.buffer_underruns_average = 130; - metrics2.buffer_underruns_count = 2400; - metrics_sum.buffer_underruns_average = 113.33333333; - metrics_sum.buffer_underruns_count = 3600; - metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC; - metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC; - metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC; - DeviceInfo* info = MakeDeviceInfo(BTM_COD_MAJOR_AUDIO_TEST, - DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR); - A2DPSession* session = MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC); - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_METRICS_DUMP, - info, nullptr, session)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2); - sleep_ms(1000); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -/* - * Test Case: A2DPSessionDumpBeforeTwoUpdatesTest - * - * 1. Create Instance - * 2. LogBluetoothSessionStart - * 3. LogBluetoothSessionDeviceInfo - * 5. WriteString - * 6. LogA2dpSession - * 7. LogA2dpSession - * 8. LogBluetoothSessionEnd - * 9. WriteString - * - */ -TEST_F(BluetoothMetricsLoggerTest, A2DPSessionDumpBeforeTwoUpdatesTest) { - /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */ - A2dpSessionMetrics metrics1; - A2dpSessionMetrics metrics2; - A2dpSessionMetrics metrics_sum; - metrics1.audio_duration_ms = 10; - metrics2.audio_duration_ms = 25; - metrics_sum.audio_duration_ms = 35; - metrics1.media_timer_min_ms = 10; - metrics2.media_timer_min_ms = 25; - metrics_sum.media_timer_min_ms = 10; - metrics1.media_timer_max_ms = 100; - metrics2.media_timer_max_ms = 200; - metrics_sum.media_timer_max_ms = 200; - metrics1.media_timer_avg_ms = 50; - metrics1.total_scheduling_count = 50; - metrics2.media_timer_avg_ms = 100; - metrics2.total_scheduling_count = 50; - metrics_sum.media_timer_avg_ms = 75; - metrics_sum.total_scheduling_count = 100; - metrics1.buffer_overruns_max_count = 70; - metrics2.buffer_overruns_max_count = 80; - metrics_sum.buffer_overruns_max_count = 80; - metrics1.buffer_underruns_average = 80; - metrics1.buffer_underruns_count = 1200; - metrics2.buffer_underruns_average = 130; - metrics2.buffer_underruns_count = 2400; - metrics_sum.buffer_underruns_average = 113.33333333; - metrics_sum.buffer_underruns_count = 3600; - metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC; - metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC; - metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC; - DeviceInfo* info = MakeDeviceInfo(BTM_COD_MAJOR_AUDIO_TEST, - DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR); - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_METRICS_DUMP, - info, nullptr, nullptr)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( - bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo( - BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR); - sleep_ms(1000); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); - ClearLog(); - info = MakeDeviceInfo(BTM_COD_MAJOR_AUDIO_TEST, - DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR); - A2DPSession* session = MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC); - bt_sessions_.push_back(MakeBluetoothSession( - 1, - BluetoothSession_ConnectionTechnologyType:: - BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR, - BluetoothSession_DisconnectReasonType::BluetoothSession_DisconnectReasonType_UNKNOWN, - info, nullptr, session)); - UpdateLog(); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1); - BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2); - sleep_ms(1000); - BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( - bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0); - msg_str.clear(); - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - EXPECT_THAT(msg_str, StrEq(bt_log_str_)); -} - -TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionTest) { - BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(BTA_HSP_SERVICE_ID); - BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(BTA_HFP_SERVICE_ID); - BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(BTA_HFP_SERVICE_ID); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - BluetoothLog* metrics = BluetoothLog::default_instance().New(); - metrics->ParseFromString(msg_str); - EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 2); - bool hfp_correct = false; - bool hsp_correct = false; - for (const HeadsetProfileConnectionStats& headset_profile_connection_stats : - metrics->headset_profile_connection_stats()) { - switch (headset_profile_connection_stats.headset_profile_type()) { - case HeadsetProfileType::HFP: - EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2); - hfp_correct = true; - break; - case HeadsetProfileType::HSP: - EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1); - hsp_correct = true; - break; - default: - FAIL(); - } - } - EXPECT_TRUE(hfp_correct); - EXPECT_TRUE(hsp_correct); - metrics->clear_headset_profile_connection_stats(); - EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0); - msg_str.clear(); - // Verify that dump after clean up result in an empty list - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - metrics->ParseFromString(msg_str); - EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0); - delete metrics; -} - -TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionErrorTest) { - BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(BTA_HSP_SERVICE_ID); - BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(BTA_HFP_SERVICE_ID); - BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(BTA_BIP_SERVICE_ID); - BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(BTA_HSP_SERVICE_ID); - std::string msg_str; - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - BluetoothLog* metrics = BluetoothLog::default_instance().New(); - metrics->ParseFromString(msg_str); - EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 3); - bool hfp_correct = false; - bool hsp_correct = false; - bool unknown_correct = false; - for (const HeadsetProfileConnectionStats& headset_profile_connection_stats : - metrics->headset_profile_connection_stats()) { - switch (headset_profile_connection_stats.headset_profile_type()) { - case HeadsetProfileType::HFP: - EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1); - hfp_correct = true; - break; - case HeadsetProfileType::HSP: - EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2); - hsp_correct = true; - break; - default: - EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1); - unknown_correct = true; - break; - } - } - EXPECT_TRUE(hfp_correct); - EXPECT_TRUE(hsp_correct); - EXPECT_TRUE(unknown_correct); - metrics->clear_headset_profile_connection_stats(); - EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0); - // Verify that dump after clean up result in an empty list - BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str); - metrics->ParseFromString(msg_str); - EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0); - delete metrics; -} -} // namespace testing diff --git a/system/gd/Android.bp b/system/gd/Android.bp index a57b83073d..35cbe8d217 100644 --- a/system/gd/Android.bp +++ b/system/gd/Android.bp @@ -170,7 +170,6 @@ cc_defaults { static_libs: [ "libaconfig_storage_read_api_cc", "libbase", - "libbluetooth-protos", "libbluetooth-types", "libbluetooth_crypto_toolbox", "libbluetooth_log", @@ -417,7 +416,6 @@ cc_test { static_libs: [ "bluetooth_flags_c_lib_for_test", "libbase", - "libbluetooth-protos", "libbluetooth-types", "libbluetooth_crypto_toolbox", "libbluetooth_gd_unit_tests", @@ -509,7 +507,6 @@ cc_defaults { ], static_libs: [ "bluetooth_flags_c_lib", - "libbluetooth-protos", "libbluetooth-types", "libbluetooth_crypto_toolbox", "libbluetooth_gd_fuzzing", diff --git a/system/gd/proto/Android.bp b/system/gd/proto/Android.bp index 1f7e72687f..e7aa3e48e6 100644 --- a/system/gd/proto/Android.bp +++ b/system/gd/proto/Android.bp @@ -20,25 +20,3 @@ java_library_static { min_sdk_version: "30", sdk_version: "current", } - -filegroup { - name: "bluetooth-metrics-proto", - srcs: [ - "bluetooth/metrics/bluetooth.proto", - ], -} - -cc_library_static { - name: "libbluetooth-protos", - host_supported: true, - proto: { - export_proto_headers: true, - type: "lite", - include_dirs: ["external/protobuf/src"], - }, - srcs: [ - "bluetooth/metrics/bluetooth.proto", - ], - apex_available: ["com.android.bt"], - min_sdk_version: "30", -} diff --git a/system/gd/proto/BUILD.gn b/system/gd/proto/BUILD.gn deleted file mode 100644 index ba5cd4eaaf..0000000000 --- a/system/gd/proto/BUILD.gn +++ /dev/null @@ -1,10 +0,0 @@ -import("//common-mk/proto_library.gni") - -proto_library("libbt-protos-lite") { - sources = [ - "bluetooth/metrics/bluetooth.proto", - ] - - proto_in_dir = "./bluetooth/metrics" - proto_out_dir = "include/bluetooth/metrics" -} diff --git a/system/include/hardware/bluetooth.h b/system/include/hardware/bluetooth.h index a2020b0e7f..41757cc556 100644 --- a/system/include/hardware/bluetooth.h +++ b/system/include/hardware/bluetooth.h @@ -836,14 +836,6 @@ typedef struct { void (*dump)(int fd, const char** arguments); /** - * Native support for metrics protobuf dumping. The dumping format will be - * raw byte array - * - * @param output an externally allocated string to dump serialized protobuf - */ - void (*dumpMetrics)(std::string* output); - - /** * Clear /data/misc/bt_config.conf and erase all stored connections */ int (*config_clear)(void); diff --git a/system/osi/src/wakelock.cc b/system/osi/src/wakelock.cc index 5cae5ed580..e6a41c3c12 100644 --- a/system/osi/src/wakelock.cc +++ b/system/osi/src/wakelock.cc @@ -33,10 +33,8 @@ #include <mutex> #include <string> -#include "common/metrics.h" #include "osi/include/osi.h" -using bluetooth::common::BluetoothMetricsLogger; using namespace bluetooth; static bt_os_callouts_t* wakelock_os_callouts = NULL; @@ -279,9 +277,6 @@ static void update_wakelock_acquired_stats(bt_status_t acquired_status) { wakelock_stats.is_acquired = true; wakelock_stats.acquired_count++; wakelock_stats.last_acquired_timestamp_ms = just_now_ms; - - BluetoothMetricsLogger::GetInstance()->LogWakeEvent(bluetooth::common::WAKE_EVENT_ACQUIRED, "", - "", just_now_ms); } // @@ -320,9 +315,6 @@ static void update_wakelock_released_stats(bt_status_t released_status) { } wakelock_stats.last_acquired_interval_ms = delta_ms; wakelock_stats.total_acquired_interval_ms += delta_ms; - - BluetoothMetricsLogger::GetInstance()->LogWakeEvent(bluetooth::common::WAKE_EVENT_RELEASED, "", - "", just_now_ms); } void wakelock_debug_dump(int fd) { diff --git a/system/tools/scripts/dump_metrics_ascii.py b/system/tools/scripts/dump_metrics_ascii.py deleted file mode 100755 index 0adb278322..0000000000 --- a/system/tools/scripts/dump_metrics_ascii.py +++ /dev/null @@ -1,139 +0,0 @@ -#!/usr/bin/env python3 -# Copyright 2018 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may not -# use this file except in compliance with the License. You may obtain a copy of -# the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations under -# the License. - -import base64 -import logging -import os -import subprocess -import sys -import tempfile -from distutils.spawn import find_executable -import google.protobuf.text_format as text_format -from importlib import import_module - - -def compile_proto(proto_path, output_dir): - """Invoke Protocol Compiler to generate python from given source .proto.""" - # Find compiler path - protoc = None - if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']): - protoc = os.environ['PROTOC'] - if not protoc: - protoc = find_executable('protoc') - if not protoc: - logging.error("Cannot find Protobuf compiler (>=3.0.0), please install" - "protobuf-compiler package. Prefer copying from <top>/prebuilts/tools") - logging.error(" prebuilts/tools/linux-x86_64/protoc/bin/protoc") - logging.error("If prebuilts are not available, use apt-get:") - logging.error(" sudo apt-get install protobuf-compiler") - return None - # Validate input proto path - if not os.path.exists(proto_path): - logging.error('Can\'t find required file: %s\n' % proto_path) - return None - # Validate output py-proto path - if not os.path.exists(output_dir): - os.mkdirs(output_dir) - elif not os.path.isdir(output_dir): - logging.error("Output path is not a valid directory: %s" % (output_dir)) - return None - input_dir = os.path.dirname(proto_path) - output_filename = os.path.basename(proto_path).replace('.proto', '_pb2.py') - output_path = os.path.join(output_dir, output_filename) - protoc_command = [protoc, '-I=%s' % (input_dir), '--python_out=%s' % (output_dir), proto_path] - if subprocess.call(protoc_command, stderr=subprocess.STDOUT) != 0: - logging.error("Fail to compile proto") - return None - output_module_name = os.path.splitext(output_filename)[0] - return output_module_name - - -def compile_import_proto(output_dir, proto_path): - """ - Compile protobuf from PROTO_PATH and put the result in OUTPUT_DIR. - Return the imported module to caller. - :param output_dir: To store generated python proto library - :param proto_path: Path to the .proto file that needs to be compiled - :return: python proto module - """ - output_module_name = compile_proto(proto_path, output_dir) - if not output_module_name: - return None - sys.path.append(output_dir) - output_module = None - try: - output_module = import_module(output_module_name) - except ImportError: - logging.error("Cannot import generated py-proto %s" % (output_module_name)) - return output_module - - -def parse_proto_to_ascii(binary_proto_msg): - """ - Parse binary protobuf message to human readable ascii string - :param binary_proto_msg: - :return: ascii string of the message - """ - return text_format.MessageToString(binary_proto_msg) - - -def dump_metrics(): - os.system('adb wait-for-device') - p = subprocess.Popen( - "adb shell dumpsys bluetooth_manager --proto-bin", - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - stdin=subprocess.PIPE) - return p.communicate() - - -def get_bluetooth_metrics(proto_native_str_64, bluetooth_proto_module): - bluetooth_log = bluetooth_proto_module.BluetoothLog() - proto_native_str = base64.b64decode(proto_native_str_64) - bluetooth_log.MergeFromString(proto_native_str) - return bluetooth_log - - -def main(): - root = logging.getLogger() - root.setLevel(logging.DEBUG) - log_handler = logging.StreamHandler(sys.stderr) - log_handler.setLevel(logging.DEBUG) - formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s") - log_handler.setFormatter(formatter) - root.addHandler(log_handler) - if len(sys.argv) < 2: - logging.error("Not enough arguments. Need at least 2") - logging.error("Usage: " + sys.argv[0] + " <path_to_metric_proto>") - sys.exit(1) - if sys.argv[1] == "-h": - logging.info("Usage: " + sys.argv[0] + " <path_to_metric_proto>") - logging.info("Requires Protobuf compiler, protoc, version >=3.0.0") - sys.exit(0) - bluetooth_proto_module = compile_import_proto(tempfile.gettempdir(), sys.argv[1]) - if not bluetooth_proto_module: - logging.error("Cannot compile " + sys.argv[1]) - sys.exit(1) - stdout, stderr = dump_metrics() - stdout = stdout.strip() - stderr = stderr.strip() - bluetooth_log = get_bluetooth_metrics(stdout, bluetooth_proto_module) - bluetooth_log_ascii = parse_proto_to_ascii(bluetooth_log) - print(bluetooth_log_ascii) - - -if __name__ == "__main__": - main() |