Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 366e826b authored by Jack He's avatar Jack He
Browse files

Fix A2DP Metrics Logging Capacity

* Set the maximum number of wake events logged to 1000
* Stop logging wake log name as it takes too much memory
* Add counters for each of the repeated values in BluetoothLog so that
  the true number of events can be determined while oldest event get
  dropped
* Log Bluetooth session disconnect reasons using enum instead of string
  in order to save memory usage
* Apply changes to bluetooth.proto in ag/1460462 on system/bt

Bug: 33694310
Test: Code compilation and unit tests
Change-Id: I2cc6f9304725938b63b211d615eb1941eac60edf
parent db4e5890
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1527,7 +1527,7 @@ static void btif_media_thread_cleanup(UNUSED_ATTR void *context) {

  /* Clear media task flag */
  media_task_running = MEDIA_TASK_STATE_OFF;
  metrics_log_bluetooth_session_end("A2DP_SHUTDOWN", 0);
  metrics_log_bluetooth_session_end(DISCONNECT_REASON_UNKNOWN, 0);
}

/*******************************************************************************
+7 −1
Original line number Diff line number Diff line
@@ -51,6 +51,12 @@ typedef enum {
  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;

typedef struct {
  int64_t audio_duration_ms;
  int32_t media_timer_min_ms;
@@ -75,7 +81,7 @@ void metrics_log_scan_event(bool start, const char* initator, scan_tech_t type,
void metrics_log_bluetooth_session_start(connection_tech_t connection_tech_type,
                                uint64_t timestamp_ms);

void metrics_log_bluetooth_session_end(const char* disconnect_reason,
void metrics_log_bluetooth_session_end(disconnect_reason_t disconnect_reason,
  uint64_t timestamp_ms);

void metrics_log_bluetooth_session_device_info(uint32_t device_class,
+9 −1
Original line number Diff line number Diff line
@@ -142,7 +142,7 @@ class BluetoothMetricsLogger {
   *    timestamp_ms : the timestamp of session end, 0 means now
   *
   */
  void LogBluetoothSessionEnd(const std::string& disconnect_reason,
  void LogBluetoothSessionEnd(disconnect_reason_t disconnect_reason,
                              uint64_t timestamp_ms);

  /*
@@ -186,6 +186,14 @@ class BluetoothMetricsLogger {
   */
  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();

+47 −22
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ using clearcut::connectivity::A2DPSession;
using clearcut::connectivity::BluetoothLog;
using clearcut::connectivity::BluetoothSession;
using clearcut::connectivity::BluetoothSession_ConnectionTechnologyType;
using clearcut::connectivity::BluetoothSession_DisconnectReasonType;
using clearcut::connectivity::DeviceInfo;
using clearcut::connectivity::DeviceInfo_DeviceType;
using clearcut::connectivity::PairEvent;
@@ -54,17 +55,6 @@ using clearcut::connectivity::ScanEvent_ScanEventType;
using clearcut::connectivity::WakeEvent;
using clearcut::connectivity::WakeEvent_WakeEventType;

namespace {
// Maximum number of log entries for each repeated field
const size_t global_max_num_bluetooth_session = 50;
const size_t global_max_num_pair_event = 50;
const size_t global_max_num_wake_event = 50;
const size_t global_max_num_scan_event = 50;
const std::string global_next_session_start_without_ending_previous =
    "NEXT_SESSION_START_WITHOUT_ENDING_PREVIOUS";
const std::string global_metrics_dump = "METRICS_DUMP";
}

uint64_t metrics_time_get_os_boottime_us(void) {
  struct timespec ts_now;
  clock_gettime(CLOCK_BOOTTIME, &ts_now);
@@ -225,6 +215,22 @@ static WakeEvent_WakeEventType get_wake_event_type(wake_event_type_t type) {
  }
}

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;
  }
}

struct BluetoothMetricsLogger::impl {
  impl(size_t max_bluetooth_session, size_t max_pair_event,
       size_t max_wake_event, size_t max_scan_event)
@@ -256,9 +262,8 @@ struct BluetoothMetricsLogger::impl {
};

BluetoothMetricsLogger::BluetoothMetricsLogger()
    : pimpl_(new impl(global_max_num_bluetooth_session,
                      global_max_num_pair_event, global_max_num_wake_event,
                      global_max_num_scan_event)) {}
    : pimpl_(new impl(kMaxNumBluetoothSession, kMaxNumPairEvent,
                      kMaxNumWakeEvent, kMaxNumScanEvent)) {}

void BluetoothMetricsLogger::LogPairEvent(uint32_t disconnect_reason,
                                          uint64_t timestamp_ms,
@@ -271,6 +276,11 @@ void BluetoothMetricsLogger::LogPairEvent(uint32_t disconnect_reason,
  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,
@@ -283,6 +293,11 @@ void BluetoothMetricsLogger::LogWakeEvent(wake_event_type_t type,
  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,
@@ -300,13 +315,18 @@ void BluetoothMetricsLogger::LogScanEvent(bool start,
  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(global_next_session_start_without_ending_previous,
    LogBluetoothSessionEnd(DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS,
      0);
  }
  if (timestamp_ms == 0) {
@@ -319,7 +339,7 @@ void BluetoothMetricsLogger::LogBluetoothSessionStart(
}

void BluetoothMetricsLogger::LogBluetoothSessionEnd(
    const std::string& disconnect_reason, uint64_t timestamp_ms) {
    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;
@@ -330,9 +350,15 @@ void BluetoothMetricsLogger::LogBluetoothSessionEnd(
  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(disconnect_reason);
  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;
  {
    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(
@@ -416,7 +442,7 @@ void BluetoothMetricsLogger::CutoffSession() {
        new BluetoothSession(*pimpl_->bluetooth_session_);
    new_bt_session->clear_a2dp_session();
    new_bt_session->clear_rfcomm_session();
    LogBluetoothSessionEnd(global_metrics_dump, 0);
    LogBluetoothSessionEnd(DISCONNECT_REASON_METRICS_DUMP, 0);
    pimpl_->bluetooth_session_ = new_bt_session;
    pimpl_->bluetooth_session_start_time_ms_ = time_get_os_boottime_ms();
    pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics();
@@ -514,11 +540,10 @@ void metrics_log_bluetooth_session_start(connection_tech_t connection_tech_type,
      connection_tech_type, 0);
}

void metrics_log_bluetooth_session_end(const char* disconnect_reason,
void metrics_log_bluetooth_session_end(disconnect_reason_t disconnect_reason,
  uint64_t timestamp_ms) {
  std::string disconnect_reason_str(disconnect_reason);
  BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
    disconnect_reason_str, timestamp_ms);
    disconnect_reason, timestamp_ms);
}

void metrics_log_bluetooth_session_device_info(uint32_t device_class,
+30 −1
Original line number Diff line number Diff line
@@ -24,6 +24,21 @@ message BluetoothLog {

  // Scan event information.
  repeated ScanEvent scan_event = 4;

  // Number of bonded devices.
  optional int32 num_bonded_devices = 5;

  // Number of BluetoothSession including discarded ones beyond capacity
  optional int64 num_bluetooth_session = 6;

  // Number of PairEvent including discarded ones beyond capacity
  optional int64 num_pair_event = 7;

  // Number of WakeEvent including discarded ones beyond capacity
  optional int64 num_wake_event = 8;

  // Number of ScanEvent including discarded ones beyond capacity
  optional int64 num_scan_event = 9;
}

// The information about the device.
@@ -63,6 +78,17 @@ message BluetoothSession {
     CONNECTION_TECHNOLOGY_TYPE_BREDR = 2;
  }

  enum DisconnectReasonType {
    UNKNOWN = 0;

    // A metrics dump takes a snapshot of current Bluetooth session and thus
    // is not a real disconnect, but a discontinuation in metrics logging.
    // This enum indicates this situation.
    METRICS_DUMP = 1;

    NEXT_START_WITHOUT_END_PREVIOUS = 2;
  }

  // Duration of the session.
  optional int64 session_duration_sec = 2;

@@ -70,7 +96,7 @@ message BluetoothSession {
  optional ConnectionTechnologyType connection_technology_type = 3;

  // Reason for disconnecting.
  optional string disconnect_reason = 4;
  optional string disconnect_reason = 4 [deprecated=true];

  // The information about the device which it is connected to.
  optional DeviceInfo device_connected_to = 5;
@@ -80,6 +106,9 @@ message BluetoothSession {

  // The information about the A2DP audio session.
  optional A2DPSession a2dp_session = 7;

  // Numeric reason for disconnecting as defined in metrics.h
  optional DisconnectReasonType disconnect_reason_type = 8;
}

message RFCommSession {
Loading