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 <aidl/android/system/suspend/internal/ISuspendControlServiceInternal.h>
18 #include <android/binder_manager.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 <memory>
26 #include <string>
27 #include <thread>
28 #include <vector>
29
30 using aidl::android::system::suspend::internal::ISuspendControlServiceInternal;
31 using aidl::android::system::suspend::internal::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 ndk::SpAIBinder binder(AServiceManager_waitForService("suspend_control_internal"));
97 controlService = ISuspendControlServiceInternal::fromBinder(binder);
98 ASSERT_NE(controlService, nullptr) << "failed to get the internal suspend control service";
99 }
100
101 // Returns true iff found.
findWakeLockInfoByName(const std::string & name,WakeLockInfo * info)102 bool findWakeLockInfoByName(const std::string& name,
103 WakeLockInfo* info) {
104 std::vector<WakeLockInfo> wlStats;
105 controlService->getWakeLockStats(&wlStats);
106 auto it = std::find_if(wlStats.begin(), wlStats.end(),
107 [&name](const auto& x) { return x.name == name; });
108 if (it != wlStats.end()) {
109 *info = *it;
110 return true;
111 }
112 return false;
113 }
114
115 // All userspace wake locks are registered with system suspend.
116 std::shared_ptr<ISuspendControlServiceInternal> controlService;
117 };
118
119 // Test RAII properties of WakeLock destructor.
TEST_F(WakeLockTest,WakeLockDestructor)120 TEST_F(WakeLockTest, WakeLockDestructor) {
121 auto name = std::to_string(rand());
122 {
123 auto wl = android::wakelock::WakeLock::tryGet(name);
124 if (!wl.has_value()) {
125 return;
126 }
127
128 WakeLockInfo info;
129 auto success = findWakeLockInfoByName(name, &info);
130 ASSERT_TRUE(success);
131 ASSERT_EQ(info.name, name);
132 ASSERT_EQ(info.pid, getpid());
133 ASSERT_TRUE(info.isActive);
134 }
135
136 // SystemSuspend receives wake lock release requests on hwbinder thread, while stats requests
137 // come on binder thread. Sleep to make sure that stats are reported *after* wake lock release.
138 std::this_thread::sleep_for(1ms);
139 WakeLockInfo info;
140 auto success = findWakeLockInfoByName(name, &info);
141 ASSERT_TRUE(success);
142 ASSERT_EQ(info.name, name);
143 ASSERT_EQ(info.pid, getpid());
144 ASSERT_FALSE(info.isActive);
145 }
146
147 } // namespace android
148