OSDN Git Service

GD: Hold wakelock when starting and stopping the Bluetooth stack
authorJack He <siyuanh@google.com>
Thu, 20 May 2021 05:14:19 +0000 (22:14 -0700)
committerJack He <siyuanh@google.com>
Fri, 21 May 2021 21:20:16 +0000 (14:20 -0700)
* Hold wakelock when starting and stopping the Bluetooth stack so that it
  can meet the timing requirement
* Rename GD wakelock to bluetooth_gd_timer for easier debugging
* Use native HAL based wakelock in GD

Bug: 184608842
Tag: #gd-refactor
Test: atest bluetooth_test_gd_unit
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines
Ignore-AOSP-First: Cherry-pick to release branch

Change-Id: Ib4679782061b82a70a240a2a614f7ee587e5c7c7
(cherry picked from commit 3ea82dbbb74beeb94a2ae15c6d9d1748a14c43ae)

gd/dumpsys_data.fbs
gd/module.cc
gd/os/linux_generic/wakelock_manager.cc
gd/os/linux_generic/wakelock_manager_unittest.cc
gd/os/wakelock_manager.h
gd/stack_manager.cc
main/Android.bp
test/headless/Android.bp

index 4828454..87f1461 100644 (file)
@@ -4,6 +4,7 @@ include "common/init_flags.fbs";
 include "l2cap/classic/l2cap_classic_module.fbs";
 include "hci/hci_acl_manager.fbs";
 include "module_unittest.fbs";
+include "os/wakelock_manager.fbs";
 include "shim/dumpsys.fbs";
 
 namespace bluetooth;
@@ -13,6 +14,7 @@ attribute "privacy";
 table DumpsysData {
     title:string;
     init_flags:common.InitFlagsData (privacy:"Any");
+    wakelock_manager_data:bluetooth.os.WakelockManagerData (privacy:"Any");
     shim_dumpsys_data:bluetooth.shim.DumpsysModuleData (privacy:"Any");
     l2cap_classic_dumpsys_data:bluetooth.l2cap.classic.L2capClassicModuleData (privacy:"Any");
     hci_acl_manager_dumpsys_data:bluetooth.hci.AclManagerData (privacy:"Any");
index 3297d9b..1805822 100644 (file)
 #include "module.h"
 #include "common/init_flags.h"
 #include "dumpsys/init_flags.h"
+#include "os/wakelock_manager.h"
 
 using ::bluetooth::os::Handler;
 using ::bluetooth::os::Thread;
+using ::bluetooth::os::WakelockManager;
 
 namespace bluetooth {
 
@@ -139,6 +141,7 @@ void ModuleDumper::DumpState(std::string* output) const {
   auto title = builder.CreateString(title_);
 
   auto init_flags_offset = dumpsys::InitFlags::Dump(&builder);
+  auto wakelock_offset = WakelockManager::Get().GetDumpsysData(&builder);
 
   std::queue<DumpsysDataFinisher> queue;
   for (auto it = module_registry_.start_order_.rbegin(); it != module_registry_.start_order_.rend(); it++) {
@@ -150,6 +153,7 @@ void ModuleDumper::DumpState(std::string* output) const {
   DumpsysDataBuilder data_builder(builder);
   data_builder.add_title(title);
   data_builder.add_init_flags(init_flags_offset);
+  data_builder.add_wakelock_manager_data(wakelock_offset);
 
   while (!queue.empty()) {
     queue.front()(&data_builder);
index 2994956..ea7b99a 100644 (file)
@@ -25,7 +25,6 @@
 
 #include "os/internal/wakelock_native.h"
 #include "os/log.h"
-#include "wakelock_manager_generated.h"
 
 namespace bluetooth {
 namespace os {
@@ -42,7 +41,7 @@ uint64_t now_ms() {
   return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL);
 }
 
-const std::string WakelockManager::kBtWakelockId = "bluetooth_timer";
+const std::string WakelockManager::kBtWakelockId = "bluetooth_gd_timer";
 
 // Wakelock statistics for the "bluetooth_timer"
 struct WakelockManager::Stats {
@@ -131,7 +130,8 @@ struct WakelockManager::Stats {
     total_acquired_interval_ms += delta_ms;
   }
 
-  void GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder, bool is_native) const {
+  flatbuffers::Offset<WakelockManagerData> GetDumpsysData(
+      flatbuffers::FlatBufferBuilder* fb_builder, bool is_native) const {
     const uint64_t just_now_ms = now_ms();
     // Compute the last acquired interval if the wakelock is still acquired
     uint64_t delta_ms = 0;
@@ -174,6 +174,7 @@ struct WakelockManager::Stats {
     builder.add_avg_interval_millis(avg_interval_ms);
     builder.add_total_interval_millis(total_interval_ms);
     builder.add_total_time_since_reset_millis(just_now_ms - last_reset_timestamp_ms);
+    return builder.Finish();
   }
 };
 
@@ -260,9 +261,9 @@ void WakelockManager::CleanUp() {
   initialized_ = false;
 }
 
-void WakelockManager::GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder) {
+flatbuffers::Offset<WakelockManagerData> WakelockManager::GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder) {
   std::lock_guard<std::recursive_mutex> lock_guard(mutex_);
-  pstats_->GetDumpsysData(fb_builder, is_native_);
+  return pstats_->GetDumpsysData(fb_builder, is_native_);
 }
 
 WakelockManager::WakelockManager() : pstats_(std::make_unique<Stats>()) {}
index f53edb8..0ae9704 100644 (file)
@@ -167,9 +167,7 @@ TEST_F(WakelockManagerTest, test_with_os_callouts_in_a_loop_and_dump) {
 
   {
     flatbuffers::FlatBufferBuilder builder(1024);
-    WakelockManager::Get().GetDumpsysData(&builder);
-    WakelockManagerDataBuilder specific_builder(builder);
-    auto offset = specific_builder.Finish();
+    auto offset = WakelockManager::Get().GetDumpsysData(&builder);
     FinishWakelockManagerDataBuffer(builder, offset);
     auto data = GetWakelockManagerData(builder.GetBufferPointer());
 
@@ -182,9 +180,7 @@ TEST_F(WakelockManagerTest, test_with_os_callouts_in_a_loop_and_dump) {
 
   {
     flatbuffers::FlatBufferBuilder builder(1024);
-    WakelockManager::Get().GetDumpsysData(&builder);
-    WakelockManagerDataBuilder specific_builder(builder);
-    auto offset = specific_builder.Finish();
+    auto offset = WakelockManager::Get().GetDumpsysData(&builder);
     FinishWakelockManagerDataBuffer(builder, offset);
     auto data = GetWakelockManagerData(builder.GetBufferPointer());
 
index 378d3ca..77645c3 100644 (file)
@@ -25,6 +25,7 @@
 #include <flatbuffers/flatbuffers.h>
 
 #include "handler.h"
+#include "wakelock_manager_generated.h"
 
 namespace bluetooth {
 namespace os {
@@ -72,7 +73,7 @@ class WakelockManager {
   void CleanUp();
 
   // Dump wakelock-related debug info to a flat buffer defined in wakelock_manager.fbs
-  void GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder);
+  flatbuffers::Offset<WakelockManagerData> GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder);
 
   ~WakelockManager();
 
index 8c017ba..5420b99 100644 (file)
 #include <queue>
 
 #include "common/bind.h"
-#include "hal/hci_hal.h"
 #include "module.h"
 #include "os/handler.h"
 #include "os/log.h"
 #include "os/thread.h"
+#include "os/wakelock_manager.h"
 
 using ::bluetooth::os::Handler;
 using ::bluetooth::os::Thread;
+using ::bluetooth::os::WakelockManager;
 
 namespace bluetooth {
 
@@ -40,12 +41,17 @@ void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
   management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL);
   handler_ = new Handler(management_thread_);
 
+  WakelockManager::Get().Acquire();
+
   std::promise<void> promise;
   auto future = promise.get_future();
   handler_->Post(common::BindOnce(&StackManager::handle_start_up, common::Unretained(this), modules, stack_thread,
                                   std::move(promise)));
 
   auto init_status = future.wait_for(std::chrono::seconds(3));
+
+  WakelockManager::Get().Release();
+
   ASSERT_LOG(
       init_status == std::future_status::ready,
       "Can't start stack, last instance: %s",
@@ -64,11 +70,17 @@ void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, st
 }
 
 void StackManager::ShutDown() {
+  WakelockManager::Get().Acquire();
+
   std::promise<void> promise;
   auto future = promise.get_future();
   handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), std::move(promise)));
 
   auto stop_status = future.wait_for(std::chrono::seconds(5));
+
+  WakelockManager::Get().Release();
+  WakelockManager::Get().CleanUp();
+
   ASSERT_LOG(
       stop_status == std::future_status::ready,
       "Can't stop stack, last instance: %s",
index 2da75f0..67cb3ec 100644 (file)
@@ -137,6 +137,7 @@ cc_library_shared {
         android: {
             shared_libs: [
                 "android.system.suspend.control-V1-ndk",
+                "android.system.suspend@1.0",
                 "libaaudio",
                 "libfmq",
             ],
index 0ef3f2a..0aff030 100644 (file)
@@ -61,6 +61,7 @@ cc_test {
         "android.hardware.bluetooth@1.0",
         "android.hardware.bluetooth@1.1",
         "android.system.suspend.control-V1-ndk",
+        "android.system.suspend@1.0",
         "libaaudio",
         "libbase",
         "libbinder_ndk",