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

Commit e8e2f49f authored by weichinweng's avatar weichinweng Committed by Weichin Weng
Browse files

Print stopwatch log when the bluetooth HAL died.

Bug: 175754404
Test: atest BluetoothInstrumentationTests
Tag: #stability
Change-Id: I775066475cc3b14de3b613b8a3b60c34214c3703
parent f96a0eaa
Loading
Loading
Loading
Loading
+36 −8
Original line number Diff line number Diff line
@@ -28,6 +28,34 @@
namespace bluetooth {
namespace common {

static const int LOG_BUFFER_LENGTH = 10;
static std::array<std::string, LOG_BUFFER_LENGTH> stopwatch_logs;
static int current_buffer_index;

void StopWatch::RecordLog(std::string log) {
  if (current_buffer_index >= LOG_BUFFER_LENGTH) {
    current_buffer_index = 0;
  }
  stopwatch_logs[current_buffer_index] = std::move(log);
  current_buffer_index++;
}

void StopWatch::DumpStopWatchLog() {
  LOG_INFO("=====================================");
  LOG_INFO("bluetooth stopwatch log history:");
  for (int i = 0; i < LOG_BUFFER_LENGTH; i++) {
    if (current_buffer_index >= LOG_BUFFER_LENGTH) {
      current_buffer_index = 0;
    }
    if (stopwatch_logs[current_buffer_index].empty()) {
      break;
    }
    LOG_DEBUG("%s", stopwatch_logs[current_buffer_index].c_str());
    current_buffer_index++;
  }
  LOG_INFO("=====================================");
}

StopWatch::StopWatch(std::string text)
    : text_(std::move(text)), start_time_(std::chrono::high_resolution_clock::now()) {
  std::stringstream ss;
@@ -37,17 +65,17 @@ StopWatch::StopWatch(std::string text)
  ss << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S");
  ss << '.' << std::setfill('0') << std::setw(3) << millis.count();
  start_timestamp_ = ss.str();
  LOG_DEBUG(" %s: %s:", start_timestamp_.c_str(), text_.c_str());

  RecordLog(start_timestamp_ + ": " + text_);
}

StopWatch::~StopWatch() {
  LOG_DEBUG(
      "%s: %s: took %zu us",
      start_timestamp_.c_str(),
      text_.c_str(),
      static_cast<size_t>(
  RecordLog(
      start_timestamp_ + ": " + text_ + ": took " +
      std::to_string(static_cast<size_t>(
          std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start_time_)
              .count()));
              .count())) +
      " us");
}

}  // namespace common
+3 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ namespace common {

class StopWatch {
 public:
  static void DumpStopWatchLog(void);
  StopWatch(std::string text);
  ~StopWatch();

@@ -31,6 +32,7 @@ class StopWatch {
  std::string text_;
  std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
  std::string start_timestamp_;
  void RecordLog(std::string log);
};

}  // namespace common
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ class HciDeathRecipient : public ::android::hardware::hidl_death_recipient {
 public:
  virtual void serviceDied(uint64_t /*cookie*/, const android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
    LOG_ERROR("Bluetooth HAL service died!");
    common::StopWatch::DumpStopWatchLog();
    abort();
  }
};