Merge "libpower: RAII wake lock implementation"
am: a53a8a7193
Change-Id: Ib3347ed0e77073dda888a5bd126190d9e8ad8bf1
This commit is contained in:
commit
aac0a1f5d3
4 changed files with 134 additions and 4 deletions
|
@ -35,7 +35,10 @@ cc_test {
|
|||
defaults: ["libpower_defaults"],
|
||||
srcs: ["power_test.cpp"],
|
||||
static_libs: ["libpower"],
|
||||
shared_libs: ["android.system.suspend@1.0"],
|
||||
shared_libs: [
|
||||
"android.system.suspend@1.0",
|
||||
"suspend_control_aidl_interface-cpp",
|
||||
],
|
||||
test_suites: ["device-tests"],
|
||||
require_root: true,
|
||||
}
|
||||
|
|
37
include/wakelock/wakelock.h
Normal file
37
include/wakelock/wakelock.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (C) 2019 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace android {
|
||||
namespace wakelock {
|
||||
|
||||
// RAII-style wake lock implementation
|
||||
class WakeLock {
|
||||
public:
|
||||
WakeLock(const std::string& name);
|
||||
~WakeLock();
|
||||
|
||||
private:
|
||||
class WakeLockImpl;
|
||||
std::unique_ptr<WakeLockImpl> mImpl;
|
||||
};
|
||||
|
||||
} // namespace wakelock
|
||||
} // namespace android
|
35
power.cpp
35
power.cpp
|
@ -17,9 +17,11 @@
|
|||
#define LOG_TAG "power"
|
||||
#define ATRACE_TAG ATRACE_TAG_POWER
|
||||
|
||||
#include <hardware_legacy/power.h>
|
||||
#include <wakelock/wakelock.h>
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android/system/suspend/1.0/ISystemSuspend.h>
|
||||
#include <hardware_legacy/power.h>
|
||||
#include <utils/Trace.h>
|
||||
|
||||
#include <mutex>
|
||||
|
@ -78,3 +80,34 @@ int release_wake_lock(const char* id) {
|
|||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
namespace android {
|
||||
namespace wakelock {
|
||||
|
||||
class WakeLock::WakeLockImpl {
|
||||
public:
|
||||
WakeLockImpl(const std::string& name);
|
||||
~WakeLockImpl();
|
||||
|
||||
private:
|
||||
sp<IWakeLock> mWakeLock;
|
||||
};
|
||||
|
||||
WakeLock::WakeLock(const std::string& name) : mImpl(std::make_unique<WakeLockImpl>(name)) {}
|
||||
|
||||
WakeLock::~WakeLock() = default;
|
||||
|
||||
WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) {
|
||||
static sp<ISystemSuspend> suspendService = ISystemSuspend::getService();
|
||||
mWakeLock = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name);
|
||||
}
|
||||
|
||||
WakeLock::WakeLockImpl::~WakeLockImpl() {
|
||||
auto ret = mWakeLock->release();
|
||||
if (!ret.isOk()) {
|
||||
LOG(ERROR) << "IWakeLock::release() call failed: " << ret.description();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wakelock
|
||||
} // namespace android
|
||||
|
|
|
@ -14,7 +14,11 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <android/system/suspend/ISuspendControlService.h>
|
||||
#include <binder/IServiceManager.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <hardware_legacy/power.h>
|
||||
#include <wakelock/wakelock.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
|
@ -22,8 +26,9 @@
|
|||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using android::sp;
|
||||
using android::system::suspend::ISuspendControlService;
|
||||
using android::system::suspend::WakeLockInfo;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
namespace android {
|
||||
|
@ -85,4 +90,56 @@ TEST(LibpowerTest, WakeLockStressTest) {
|
|||
}
|
||||
}
|
||||
|
||||
class WakeLockTest : public ::testing::Test {
|
||||
public:
|
||||
virtual void SetUp() override {
|
||||
sp<IBinder> control =
|
||||
android::defaultServiceManager()->getService(android::String16("suspend_control"));
|
||||
ASSERT_NE(control, nullptr) << "failed to get the suspend control service";
|
||||
controlService = interface_cast<ISuspendControlService>(control);
|
||||
}
|
||||
|
||||
// Returns true iff found.
|
||||
bool findWakeLockInfoByName(const sp<ISuspendControlService>& service, const std::string& name,
|
||||
WakeLockInfo* info) {
|
||||
std::vector<WakeLockInfo> wlStats;
|
||||
service->getWakeLockStats(&wlStats);
|
||||
auto it = std::find_if(wlStats.begin(), wlStats.end(),
|
||||
[&name](const auto& x) { return x.name == name; });
|
||||
if (it != wlStats.end()) {
|
||||
*info = *it;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// All userspace wake locks are registered with system suspend.
|
||||
sp<ISuspendControlService> controlService;
|
||||
};
|
||||
|
||||
// Test RAII properties of WakeLock destructor.
|
||||
TEST_F(WakeLockTest, WakeLockDestructor) {
|
||||
auto name = std::to_string(rand());
|
||||
{
|
||||
android::wakelock::WakeLock wl{name};
|
||||
|
||||
WakeLockInfo info;
|
||||
auto success = findWakeLockInfoByName(controlService, name, &info);
|
||||
ASSERT_TRUE(success);
|
||||
ASSERT_EQ(info.name, name);
|
||||
ASSERT_EQ(info.pid, getpid());
|
||||
ASSERT_TRUE(info.isActive);
|
||||
}
|
||||
|
||||
// SystemSuspend receives wake lock release requests on hwbinder thread, while stats requests
|
||||
// come on binder thread. Sleep to make sure that stats are reported *after* wake lock release.
|
||||
std::this_thread::sleep_for(1ms);
|
||||
WakeLockInfo info;
|
||||
auto success = findWakeLockInfoByName(controlService, name, &info);
|
||||
ASSERT_TRUE(success);
|
||||
ASSERT_EQ(info.name, name);
|
||||
ASSERT_EQ(info.pid, getpid());
|
||||
ASSERT_FALSE(info.isActive);
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
|
|
Loading…
Reference in a new issue