1 /*
2 * Copyright (C) 2020 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 #include <aidl/Gtest.h>
17 #include <aidl/Vintf.h>
18
19 #include <aidl/android/hardware/power/BnPower.h>
20 #include <aidl/android/hardware/power/BnPowerHintSession.h>
21 #include <android-base/properties.h>
22 #include <android/binder_ibinder.h>
23 #include <android/binder_manager.h>
24 #include <android/binder_process.h>
25 #include <android/binder_status.h>
26
27 #include <fmq/AidlMessageQueue.h>
28 #include <fmq/EventFlag.h>
29
30 #include <unistd.h>
31 #include <cstdint>
32 #include "aidl/android/hardware/common/fmq/SynchronizedReadWrite.h"
33
34 namespace aidl::android::hardware::power {
35 namespace {
36
37 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
38 using ::android::AidlMessageQueue;
39 using ::android::hardware::EventFlag;
40 using android::hardware::power::Boost;
41 using android::hardware::power::ChannelConfig;
42 using android::hardware::power::ChannelMessage;
43 using android::hardware::power::IPower;
44 using android::hardware::power::IPowerHintSession;
45 using android::hardware::power::Mode;
46 using android::hardware::power::SessionHint;
47 using android::hardware::power::SessionMode;
48 using android::hardware::power::WorkDuration;
49 using ChannelMessageContents = ChannelMessage::ChannelMessageContents;
50 using ModeSetter = ChannelMessage::ChannelMessageContents::SessionModeSetter;
51 using MessageTag = ChannelMessage::ChannelMessageContents::Tag;
52
53 using SessionMessageQueue = AidlMessageQueue<ChannelMessage, SynchronizedReadWrite>;
54 using FlagMessageQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
55
56 const std::vector<Boost> kBoosts{ndk::enum_range<Boost>().begin(), ndk::enum_range<Boost>().end()};
57
58 const std::vector<Mode> kModes{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()};
59
60 const std::vector<SessionHint> kSessionHints{ndk::enum_range<SessionHint>().begin(),
61 ndk::enum_range<SessionHint>().end()};
62
63 const std::vector<SessionMode> kSessionModes{ndk::enum_range<SessionMode>().begin(),
64 ndk::enum_range<SessionMode>().end()};
65
66 const std::vector<Boost> kInvalidBoosts = {
67 static_cast<Boost>(static_cast<int32_t>(kBoosts.front()) - 1),
68 static_cast<Boost>(static_cast<int32_t>(kBoosts.back()) + 1),
69 };
70
71 const std::vector<Mode> kInvalidModes = {
72 static_cast<Mode>(static_cast<int32_t>(kModes.front()) - 1),
73 static_cast<Mode>(static_cast<int32_t>(kModes.back()) + 1),
74 };
75
76 const std::vector<SessionHint> kInvalidSessionHints = {
77 static_cast<SessionHint>(static_cast<int32_t>(kSessionHints.front()) - 1),
78 static_cast<SessionHint>(static_cast<int32_t>(kSessionHints.back()) + 1),
79 };
80
81 const std::vector<SessionMode> kInvalidSessionModes = {
82 static_cast<SessionMode>(static_cast<int32_t>(kSessionModes.front()) - 1),
83 static_cast<SessionMode>(static_cast<int32_t>(kSessionModes.back()) + 1),
84 };
85
86 class DurationWrapper : public WorkDuration {
87 public:
DurationWrapper(int64_t dur,int64_t time)88 DurationWrapper(int64_t dur, int64_t time) {
89 durationNanos = dur;
90 timeStampNanos = time;
91 }
92 };
93
94 const std::vector<int32_t> kSelfTids = {
95 gettid(),
96 };
97
98 const std::vector<int32_t> kEmptyTids = {};
99
100 const std::vector<WorkDuration> kDurationsWithZero = {
101 DurationWrapper(1000L, 1L),
102 DurationWrapper(0L, 2L),
103 };
104
105 const std::vector<WorkDuration> kDurationsWithNegative = {
106 DurationWrapper(1000L, 1L),
107 DurationWrapper(-1000L, 2L),
108 };
109
110 const std::vector<WorkDuration> kDurations = {
111 DurationWrapper(1L, 1L),
112 DurationWrapper(1000L, 2L),
113 DurationWrapper(1000000L, 3L),
114 DurationWrapper(1000000000L, 4L),
115 };
116
117 class PowerAidl : public testing::TestWithParam<std::string> {
118 public:
SetUp()119 virtual void SetUp() override {
120 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
121 ASSERT_NE(binder, nullptr);
122 power = IPower::fromBinder(ndk::SpAIBinder(binder));
123 auto status = power->getInterfaceVersion(&mServiceVersion);
124 ASSERT_TRUE(status.isOk());
125 if (mServiceVersion >= 2) {
126 status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &mSession);
127 mSessionSupport = status.isOk();
128 }
129 }
130
131 std::shared_ptr<IPower> power;
132 int32_t mServiceVersion;
133 std::shared_ptr<IPowerHintSession> mSession;
134 bool mSessionSupport = false;
135 };
136
137 class HintSessionAidl : public PowerAidl {
138 public:
SetUp()139 virtual void SetUp() override {
140 PowerAidl::SetUp();
141 if (mServiceVersion < 2) {
142 GTEST_SKIP() << "DEVICE not launching with Power V2 and beyond.";
143 }
144 if (!mSessionSupport) {
145 GTEST_SKIP() << "DEVICE not support Hint Session.";
146 }
147 ASSERT_NE(nullptr, mSession);
148 }
149 };
150
151 class FMQAidl : public PowerAidl {
152 public:
SetUp()153 virtual void SetUp() override {
154 PowerAidl::SetUp();
155 if (mServiceVersion < 5) {
156 GTEST_SKIP() << "DEVICE not launching with Power V5 and beyond.";
157 }
158
159 auto status =
160 power->createHintSessionWithConfig(getpid(), getuid(), kSelfTids, 16666666L,
161 SessionTag::OTHER, &mSessionConfig, &mSession);
162 mSessionSupport = status.isOk();
163 if (!mSessionSupport) {
164 GTEST_SKIP() << "DEVICE not support Hint Session.";
165 }
166 ASSERT_NE(nullptr, mSession);
167
168 status = power->getSessionChannel(getpid(), getuid(), &mChannelConfig);
169 ASSERT_TRUE(status.isOk());
170 mChannel = std::make_shared<SessionMessageQueue>(mChannelConfig.channelDescriptor, true);
171 ASSERT_TRUE(mChannel->isValid());
172
173 if (mChannelConfig.eventFlagDescriptor.has_value()) {
174 mFlagChannel =
175 std::make_shared<FlagMessageQueue>(*mChannelConfig.eventFlagDescriptor, true);
176 ASSERT_EQ(EventFlag::createEventFlag(mFlagChannel->getEventFlagWord(), &mEventFlag),
177 ::android::OK);
178 } else {
179 ASSERT_EQ(EventFlag::createEventFlag(mChannel->getEventFlagWord(), &mEventFlag),
180 ::android::OK);
181 }
182
183 ASSERT_NE(mEventFlag, nullptr);
184 }
TearDown()185 virtual void TearDown() {
186 if (mSession) {
187 mSession->close();
188 if (mChannel->isValid()) {
189 ASSERT_TRUE(power->closeSessionChannel(getpid(), getuid()).isOk());
190 }
191 }
192 }
193
194 protected:
195 std::shared_ptr<IPowerHintSession> mSession;
196 std::shared_ptr<SessionMessageQueue> mChannel;
197 std::shared_ptr<FlagMessageQueue> mFlagChannel;
198 SessionConfig mSessionConfig;
199 ChannelConfig mChannelConfig;
200 ::android::hardware::EventFlag* mEventFlag;
201 };
202
TEST_P(PowerAidl,setMode)203 TEST_P(PowerAidl, setMode) {
204 for (const auto& mode : kModes) {
205 ASSERT_TRUE(power->setMode(mode, true).isOk());
206 ASSERT_TRUE(power->setMode(mode, false).isOk());
207 }
208 for (const auto& mode : kInvalidModes) {
209 ASSERT_TRUE(power->setMode(mode, true).isOk());
210 ASSERT_TRUE(power->setMode(mode, false).isOk());
211 }
212 }
213
TEST_P(PowerAidl,isModeSupported)214 TEST_P(PowerAidl, isModeSupported) {
215 for (const auto& mode : kModes) {
216 bool supported;
217 ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
218 }
219 for (const auto& mode : kInvalidModes) {
220 bool supported;
221 ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
222 // Should return false for values outside enum
223 ASSERT_FALSE(supported);
224 }
225 }
226
TEST_P(PowerAidl,setBoost)227 TEST_P(PowerAidl, setBoost) {
228 for (const auto& boost : kBoosts) {
229 ASSERT_TRUE(power->setBoost(boost, 0).isOk());
230 ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
231 ASSERT_TRUE(power->setBoost(boost, -1).isOk());
232 }
233 for (const auto& boost : kInvalidBoosts) {
234 ASSERT_TRUE(power->setBoost(boost, 0).isOk());
235 ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
236 ASSERT_TRUE(power->setBoost(boost, -1).isOk());
237 }
238 }
239
TEST_P(PowerAidl,isBoostSupported)240 TEST_P(PowerAidl, isBoostSupported) {
241 for (const auto& boost : kBoosts) {
242 bool supported;
243 ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
244 }
245 for (const auto& boost : kInvalidBoosts) {
246 bool supported;
247 ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
248 // Should return false for values outside enum
249 ASSERT_FALSE(supported);
250 }
251 }
252
TEST_P(PowerAidl,getHintSessionPreferredRate)253 TEST_P(PowerAidl, getHintSessionPreferredRate) {
254 if (!mSessionSupport) {
255 GTEST_SKIP() << "DEVICE not support Hint Session.";
256 }
257 if (mServiceVersion < 2) {
258 GTEST_SKIP() << "DEVICE not launching with Power V2 and beyond.";
259 }
260
261 int64_t rate = -1;
262 ASSERT_TRUE(power->getHintSessionPreferredRate(&rate).isOk());
263 // At least 1ms rate limit from HAL
264 ASSERT_GE(rate, 1000000);
265 }
266
TEST_P(PowerAidl,createHintSessionWithConfig)267 TEST_P(PowerAidl, createHintSessionWithConfig) {
268 if (!mSessionSupport) {
269 GTEST_SKIP() << "DEVICE not support Hint Session.";
270 }
271 if (mServiceVersion < 5) {
272 GTEST_SKIP() << "DEVICE not launching with Power V5 and beyond.";
273 }
274 std::shared_ptr<IPowerHintSession> session;
275 SessionConfig config;
276
277 auto status = power->createHintSessionWithConfig(getpid(), getuid(), kSelfTids, 16666666L,
278 SessionTag::OTHER, &config, &session);
279 ASSERT_TRUE(status.isOk());
280 ASSERT_NE(nullptr, session);
281 }
282
283 // FIXED_PERFORMANCE mode is required for all devices which ship on Android 11
284 // or later
TEST_P(PowerAidl,hasFixedPerformance)285 TEST_P(PowerAidl, hasFixedPerformance) {
286 bool supported;
287 ASSERT_TRUE(power->isModeSupported(Mode::FIXED_PERFORMANCE, &supported).isOk());
288 ASSERT_TRUE(supported);
289 }
290
TEST_P(HintSessionAidl,createAndCloseHintSession)291 TEST_P(HintSessionAidl, createAndCloseHintSession) {
292 if (!mSessionSupport) {
293 GTEST_SKIP() << "DEVICE not support Hint Session.";
294 }
295 ASSERT_TRUE(mSession->pause().isOk());
296 ASSERT_TRUE(mSession->resume().isOk());
297 // Test normal destroy operation
298 ASSERT_TRUE(mSession->close().isOk());
299 mSession.reset();
300 }
301
TEST_P(HintSessionAidl,createHintSessionFailed)302 TEST_P(HintSessionAidl, createHintSessionFailed) {
303 if (!mSessionSupport) {
304 GTEST_SKIP() << "DEVICE not support Hint Session.";
305 }
306 std::shared_ptr<IPowerHintSession> session;
307 auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session);
308
309 // Regardless of whether V2 and beyond is supported, the status is always not STATUS_OK.
310 ASSERT_FALSE(status.isOk());
311 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
312 }
313
TEST_P(HintSessionAidl,updateAndReportDurations)314 TEST_P(HintSessionAidl, updateAndReportDurations) {
315 if (!mSessionSupport) {
316 GTEST_SKIP() << "DEVICE not support Hint Session.";
317 }
318 ASSERT_TRUE(mSession->updateTargetWorkDuration(16666667LL).isOk());
319 ASSERT_TRUE(mSession->reportActualWorkDuration(kDurations).isOk());
320 }
321
TEST_P(HintSessionAidl,sendSessionHint)322 TEST_P(HintSessionAidl, sendSessionHint) {
323 if (!mSessionSupport) {
324 GTEST_SKIP() << "DEVICE not support Hint Session.";
325 }
326 if (mServiceVersion < 4) {
327 GTEST_SKIP() << "DEVICE not launching with Power V4 and beyond.";
328 }
329
330 for (const auto& sessionHint : kSessionHints) {
331 ASSERT_TRUE(mSession->sendHint(sessionHint).isOk());
332 }
333 for (const auto& sessionHint : kInvalidSessionHints) {
334 ASSERT_TRUE(mSession->sendHint(sessionHint).isOk());
335 }
336 }
337
TEST_P(HintSessionAidl,setThreads)338 TEST_P(HintSessionAidl, setThreads) {
339 if (!mSessionSupport) {
340 GTEST_SKIP() << "DEVICE not support Hint Session.";
341 }
342 if (mServiceVersion < 4) {
343 GTEST_SKIP() << "DEVICE not launching with Power V4 and beyond.";
344 }
345
346 auto status = mSession->setThreads(kEmptyTids);
347 ASSERT_FALSE(status.isOk());
348 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
349
350 ASSERT_TRUE(mSession->setThreads(kSelfTids).isOk());
351 }
352
TEST_P(HintSessionAidl,setSessionMode)353 TEST_P(HintSessionAidl, setSessionMode) {
354 if (!mSessionSupport) {
355 GTEST_SKIP() << "DEVICE not support Hint Session.";
356 }
357 if (mServiceVersion < 5) {
358 GTEST_SKIP() << "DEVICE not launching with Power V5 and beyond.";
359 }
360
361 for (const auto& sessionMode : kSessionModes) {
362 ASSERT_TRUE(mSession->setMode(sessionMode, true).isOk());
363 ASSERT_TRUE(mSession->setMode(sessionMode, false).isOk());
364 }
365 for (const auto& sessionMode : kInvalidSessionModes) {
366 ASSERT_TRUE(mSession->setMode(sessionMode, true).isOk());
367 ASSERT_TRUE(mSession->setMode(sessionMode, false).isOk());
368 }
369 }
370
TEST_P(HintSessionAidl,getSessionConfig)371 TEST_P(HintSessionAidl, getSessionConfig) {
372 if (!mSessionSupport) {
373 GTEST_SKIP() << "DEVICE not support Hint Session.";
374 }
375 if (mServiceVersion < 5) {
376 GTEST_SKIP() << "DEVICE not launching with Power V5 and beyond.";
377 }
378 SessionConfig config;
379 ASSERT_TRUE(mSession->getSessionConfig(&config).isOk());
380 }
381
TEST_P(FMQAidl,getAndCloseSessionChannel)382 TEST_P(FMQAidl, getAndCloseSessionChannel) {}
383
TEST_P(FMQAidl,writeItems)384 TEST_P(FMQAidl, writeItems) {
385 std::vector<ChannelMessage> messages{
386 {.sessionID = static_cast<int32_t>(mSessionConfig.id),
387 .timeStampNanos = 1000,
388 .data = ChannelMessageContents::make<MessageTag::workDuration, WorkDurationFixedV1>(
389 {.durationNanos = 1000,
390 .workPeriodStartTimestampNanos = 10,
391 .cpuDurationNanos = 900,
392 .gpuDurationNanos = 100})},
393 {.sessionID = static_cast<int32_t>(mSessionConfig.id),
394 .timeStampNanos = 1000,
395 .data = ChannelMessageContents::make<MessageTag::mode, ModeSetter>(
396 {.modeInt = SessionMode::POWER_EFFICIENCY, .enabled = true})},
397 {.sessionID = static_cast<int32_t>(mSessionConfig.id),
398 .timeStampNanos = 1000,
399 .data = ChannelMessageContents::make<MessageTag::hint, SessionHint>(
400 SessionHint::CPU_LOAD_UP)},
401 {.sessionID = static_cast<int32_t>(mSessionConfig.id),
402 .timeStampNanos = 1000,
403 .data = ChannelMessageContents::make<MessageTag::targetDuration, int64_t>(
404 10000000 /* 10ms */)},
405 };
406 for (auto& message : messages) {
407 ASSERT_TRUE(mChannel->writeBlocking(&message, 1, mChannelConfig.readFlagBitmask,
408 mChannelConfig.writeFlagBitmask, 100000000,
409 mEventFlag));
410 }
411 // Make sure this still works after everything else is done to check crash
412 ASSERT_TRUE(mSession->setThreads(kSelfTids).isOk());
413 }
414
TEST_P(FMQAidl,writeExcess)415 TEST_P(FMQAidl, writeExcess) {
416 std::vector<ChannelMessage> messages;
417 size_t channelSize = mChannel->getQuantumCount();
418 for (size_t i = 0; i < channelSize; ++i) {
419 messages.push_back({.sessionID = static_cast<int32_t>(mSessionConfig.id),
420 .timeStampNanos = 1000,
421 .data = ChannelMessageContents::make<MessageTag::hint, SessionHint>(
422 SessionHint::CPU_LOAD_UP)});
423 }
424 ASSERT_TRUE(mChannel->writeBlocking(messages.data(), messages.size(),
425 mChannelConfig.readFlagBitmask,
426 mChannelConfig.writeFlagBitmask, 100000000, mEventFlag));
427 ASSERT_TRUE(mChannel->writeBlocking(messages.data(), messages.size(),
428 mChannelConfig.readFlagBitmask,
429 mChannelConfig.writeFlagBitmask, 1000000000, mEventFlag));
430 // Make sure this still works after everything else is done to check crash
431 ASSERT_TRUE(mSession->setThreads(kSelfTids).isOk());
432 }
433
434 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerAidl);
435 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HintSessionAidl);
436 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(FMQAidl);
437
438 INSTANTIATE_TEST_SUITE_P(Power, PowerAidl,
439 testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)),
440 ::android::PrintInstanceNameToString);
441 INSTANTIATE_TEST_SUITE_P(Power, HintSessionAidl,
442 testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)),
443 ::android::PrintInstanceNameToString);
444 INSTANTIATE_TEST_SUITE_P(Power, FMQAidl,
445 testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)),
446 ::android::PrintInstanceNameToString);
447
448 } // namespace
449 } // namespace aidl::android::hardware::power
450
main(int argc,char ** argv)451 int main(int argc, char** argv) {
452 ::testing::InitGoogleTest(&argc, argv);
453 ABinderProcess_setThreadPoolMaxThreadCount(1);
454 ABinderProcess_startThreadPool();
455 return RUN_ALL_TESTS();
456 }
457