blob: d6e414da9bb65eda302f788ab161da396d2b9af2 [file] [log] [blame]
Tri Vo18177502018-10-20 16:11:24 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tri Vo934f1752019-06-26 10:34:28 -070017#include <android/system/suspend/ISuspendControlService.h>
18#include <binder/IServiceManager.h>
19#include <gtest/gtest.h>
Tri Vo18177502018-10-20 16:11:24 -070020#include <hardware_legacy/power.h>
Tri Vo934f1752019-06-26 10:34:28 -070021#include <wakelock/wakelock.h>
Tri Vo18177502018-10-20 16:11:24 -070022
Tri Voca0b45a2018-11-27 17:56:56 -080023#include <csignal>
24#include <cstdlib>
Tri Vo18177502018-10-20 16:11:24 -070025#include <string>
26#include <thread>
27#include <vector>
28
Tri Vo934f1752019-06-26 10:34:28 -070029using android::sp;
30using android::system::suspend::ISuspendControlService;
31using android::system::suspend::WakeLockInfo;
Tri Voca0b45a2018-11-27 17:56:56 -080032using namespace std::chrono_literals;
33
Tri Vo18177502018-10-20 16:11:24 -070034namespace android {
35
Tri Voca0b45a2018-11-27 17:56:56 -080036// Test acquiring/releasing WakeLocks concurrently with process exit.
37TEST(LibpowerTest, ProcessExitTest) {
38 std::atexit([] {
39 // We want to give the other thread enough time trigger a failure and
40 // dump the stack traces.
41 std::this_thread::sleep_for(1s);
42 });
43
44 ASSERT_EXIT(
45 {
46 constexpr int numThreads = 20;
47 std::vector<std::thread> tds;
48 for (int i = 0; i < numThreads; i++) {
49 tds.emplace_back([] {
50 while (true) {
51 // We want ids to be unique.
52 std::string id = std::to_string(rand());
53 ASSERT_EQ(acquire_wake_lock(PARTIAL_WAKE_LOCK, id.c_str()), 0);
54 ASSERT_EQ(release_wake_lock(id.c_str()), 0);
55 }
56 });
57 }
58 for (auto& td : tds) {
59 td.detach();
60 }
61
62 // Give some time for the threads to actually start.
63 std::this_thread::sleep_for(100ms);
64 std::exit(0);
65 },
66 ::testing::ExitedWithCode(0), "");
67}
68
Tri Vo18177502018-10-20 16:11:24 -070069// Stress test acquiring/releasing WakeLocks.
70TEST(LibpowerTest, WakeLockStressTest) {
71 // numThreads threads will acquire/release numLocks locks each.
72 constexpr int numThreads = 20;
Tri Voca0b45a2018-11-27 17:56:56 -080073 constexpr int numLocks = 1000;
Tri Vo18177502018-10-20 16:11:24 -070074 std::vector<std::thread> tds;
75
76 for (int i = 0; i < numThreads; i++) {
77 tds.emplace_back([i] {
78 for (int j = 0; j < numLocks; j++) {
79 // We want ids to be unique.
80 std::string id = std::to_string(i) + "/" + std::to_string(j);
Ruslan Tkhakokhovacf23a22019-06-21 10:31:37 +000081 ASSERT_EQ(acquire_wake_lock(PARTIAL_WAKE_LOCK, id.c_str()), 0)
82 << "id: " << id;
83 ASSERT_EQ(release_wake_lock(id.c_str()), 0)
84 << "id: " << id;;
Tri Vo18177502018-10-20 16:11:24 -070085 }
86 });
87 }
88 for (auto& td : tds) {
89 td.join();
90 }
91}
92
Tri Vo934f1752019-06-26 10:34:28 -070093class WakeLockTest : public ::testing::Test {
94 public:
95 virtual void SetUp() override {
96 sp<IBinder> control =
97 android::defaultServiceManager()->getService(android::String16("suspend_control"));
98 ASSERT_NE(control, nullptr) << "failed to get the suspend control service";
99 controlService = interface_cast<ISuspendControlService>(control);
100 }
101
102 // Returns true iff found.
103 bool findWakeLockInfoByName(const sp<ISuspendControlService>& service, const std::string& name,
104 WakeLockInfo* info) {
105 std::vector<WakeLockInfo> wlStats;
106 service->getWakeLockStats(&wlStats);
107 auto it = std::find_if(wlStats.begin(), wlStats.end(),
108 [&name](const auto& x) { return x.name == name; });
109 if (it != wlStats.end()) {
110 *info = *it;
111 return true;
112 }
113 return false;
114 }
115
116 // All userspace wake locks are registered with system suspend.
117 sp<ISuspendControlService> controlService;
118};
119
120// Test RAII properties of WakeLock destructor.
121TEST_F(WakeLockTest, WakeLockDestructor) {
122 auto name = std::to_string(rand());
123 {
124 android::wakelock::WakeLock wl{name};
125
126 WakeLockInfo info;
127 auto success = findWakeLockInfoByName(controlService, name, &info);
128 ASSERT_TRUE(success);
129 ASSERT_EQ(info.name, name);
130 ASSERT_EQ(info.pid, getpid());
131 ASSERT_TRUE(info.isActive);
132 }
133
134 // SystemSuspend receives wake lock release requests on hwbinder thread, while stats requests
135 // come on binder thread. Sleep to make sure that stats are reported *after* wake lock release.
136 std::this_thread::sleep_for(1ms);
137 WakeLockInfo info;
138 auto success = findWakeLockInfoByName(controlService, name, &info);
139 ASSERT_TRUE(success);
140 ASSERT_EQ(info.name, name);
141 ASSERT_EQ(info.pid, getpid());
142 ASSERT_FALSE(info.isActive);
143}
144
Tri Vo18177502018-10-20 16:11:24 -0700145} // namespace android