1 /*
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
17 #include <android/system/suspend/ISuspendControlService.h>
18 #include <binder/IServiceManager.h>
19 #include <gtest/gtest.h>
20 #include <hardware_legacy/power.h>
21 #include <wakelock/wakelock.h>
22
23 #include <csignal>
24 #include <cstdlib>
25 #include <string>
26 #include <thread>
27 #include <vector>
28
29 using android::sp;
30 using android::system::suspend::ISuspendControlService;
31 using android::system::suspend::WakeLockInfo;
32 using namespace std::chrono_literals;
33
34 namespace android {
35
36 // Test acquiring/releasing WakeLocks concurrently with process exit.
TEST(LibpowerTest,ProcessExitTest)37 TEST(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
69 // Stress test acquiring/releasing WakeLocks.
TEST(LibpowerTest,WakeLockStressTest)70 TEST(LibpowerTest, WakeLockStressTest) {
71 // numThreads threads will acquire/release numLocks locks each.
72 constexpr int numThreads = 20;
73 constexpr int numLocks = 1000;
74 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);
81 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;;
85 }
86 });
87 }
88 for (auto& td : tds) {
89 td.join();
90 }
91 }
92
93 class WakeLockTest : public ::testing::Test {
94 public:
SetUp()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.
findWakeLockInfoByName(const sp<ISuspendControlService> & service,const std::string & name,WakeLockInfo * info)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.
TEST_F(WakeLockTest,WakeLockDestructor)121 TEST_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
145 } // namespace android
146