Skip to content

Commit

Permalink
GD: Hold wakelock when starting and stopping the Bluetooth stack
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
Jack He committed May 21, 2021
1 parent 234f5ee commit 9d88eb9
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 13 deletions.
2 changes: 2 additions & 0 deletions gd/dumpsys_data.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions gd/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
#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 {

Expand Down Expand Up @@ -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++) {
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions gd/os/linux_generic/wakelock_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include "os/internal/wakelock_native.h"
#include "os/log.h"
#include "wakelock_manager_generated.h"

namespace bluetooth {
namespace os {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
};

Expand Down Expand Up @@ -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>()) {}
Expand Down
8 changes: 2 additions & 6 deletions gd/os/linux_generic/wakelock_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());

Expand Down
3 changes: 2 additions & 1 deletion gd/os/wakelock_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <flatbuffers/flatbuffers.h>

#include "handler.h"
#include "wakelock_manager_generated.h"

namespace bluetooth {
namespace os {
Expand Down Expand Up @@ -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();

Expand Down
14 changes: 13 additions & 1 deletion gd/stack_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
#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 {

Expand All @@ -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",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions main/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ cc_library_shared {
android: {
shared_libs: [
"android.system.suspend.control-V1-ndk",
"[email protected]",
"libaaudio",
"libfmq",
],
Expand Down
1 change: 1 addition & 0 deletions test/headless/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ cc_test {
"[email protected]",
"[email protected]",
"android.system.suspend.control-V1-ndk",
"[email protected]",
"libaaudio",
"libbase",
"libbinder_ndk",
Expand Down

0 comments on commit 9d88eb9

Please sign in to comment.