Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 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 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/power/BnPower.h> |
| 20 | #include <aidl/android/hardware/power/BnPowerHintSession.h> |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 22 | #include <android/binder_ibinder.h> |
| 23 | #include <android/binder_manager.h> |
| 24 | #include <android/binder_process.h> |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 25 | #include <android/binder_status.h> |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 26 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 28 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 29 | namespace aidl::android::hardware::power { |
| 30 | namespace { |
| 31 | |
| 32 | using ::android::base::GetUintProperty; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 33 | using android::hardware::power::Boost; |
| 34 | using android::hardware::power::IPower; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 35 | using android::hardware::power::IPowerHintSession; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 36 | using android::hardware::power::Mode; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 37 | using android::hardware::power::WorkDuration; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 38 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 39 | const std::vector<Boost> kBoosts{ndk::enum_range<Boost>().begin(), ndk::enum_range<Boost>().end()}; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 40 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 41 | const std::vector<Mode> kModes{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()}; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 42 | |
| 43 | const std::vector<Boost> kInvalidBoosts = { |
| 44 | static_cast<Boost>(static_cast<int32_t>(kBoosts.front()) - 1), |
| 45 | static_cast<Boost>(static_cast<int32_t>(kBoosts.back()) + 1), |
| 46 | }; |
| 47 | |
| 48 | const std::vector<Mode> kInvalidModes = { |
| 49 | static_cast<Mode>(static_cast<int32_t>(kModes.front()) - 1), |
| 50 | static_cast<Mode>(static_cast<int32_t>(kModes.back()) + 1), |
| 51 | }; |
| 52 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 53 | class DurationWrapper : public WorkDuration { |
| 54 | public: |
| 55 | DurationWrapper(int64_t dur, int64_t time) { |
| 56 | durationNanos = dur; |
| 57 | timeStampNanos = time; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | const std::vector<int32_t> kSelfTids = { |
| 62 | gettid(), |
| 63 | }; |
| 64 | |
| 65 | const std::vector<int32_t> kEmptyTids = {}; |
| 66 | |
| 67 | const std::vector<WorkDuration> kNoDurations = {}; |
| 68 | |
| 69 | const std::vector<WorkDuration> kDurationsWithZero = { |
| 70 | DurationWrapper(1000L, 1L), |
| 71 | DurationWrapper(0L, 2L), |
| 72 | }; |
| 73 | |
| 74 | const std::vector<WorkDuration> kDurationsWithNegative = { |
| 75 | DurationWrapper(1000L, 1L), |
| 76 | DurationWrapper(-1000L, 2L), |
| 77 | }; |
| 78 | |
| 79 | const std::vector<WorkDuration> kDurations = { |
| 80 | DurationWrapper(1L, 1L), |
| 81 | DurationWrapper(1000L, 2L), |
| 82 | DurationWrapper(1000000L, 3L), |
| 83 | DurationWrapper(1000000000L, 4L), |
| 84 | }; |
| 85 | |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 86 | // DEVICEs launching with Android 11 MUST meet the requirements for the |
| 87 | // target-level=5 compatibility_matrix file. |
| 88 | const uint64_t kCompatibilityMatrix5ApiLevel = 30; |
| 89 | |
| 90 | // DEVICEs launching with Android 13 MUST meet the requirements for the |
| 91 | // target-level=7 compatibility_matrix file. |
| 92 | const uint64_t kCompatibilityMatrix7ApiLevel = 33; |
| 93 | |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 94 | inline bool isUnknownOrUnsupported(const ndk::ScopedAStatus& status) { |
| 95 | return status.getStatus() == STATUS_UNKNOWN_TRANSACTION || |
| 96 | status.getExceptionCode() == EX_UNSUPPORTED_OPERATION; |
| 97 | } |
| 98 | |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 99 | class PowerAidl : public testing::TestWithParam<std::string> { |
| 100 | public: |
| 101 | virtual void SetUp() override { |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 102 | AIBinder* binder = AServiceManager_waitForService(GetParam().c_str()); |
| 103 | ASSERT_NE(binder, nullptr); |
| 104 | power = IPower::fromBinder(ndk::SpAIBinder(binder)); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 105 | |
| 106 | mApiLevel = GetUintProperty<uint64_t>("ro.vendor.api_level", 0); |
| 107 | ASSERT_NE(mApiLevel, 0); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 110 | std::shared_ptr<IPower> power; |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 111 | uint64_t mApiLevel; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | TEST_P(PowerAidl, setMode) { |
| 115 | for (const auto& mode : kModes) { |
| 116 | ASSERT_TRUE(power->setMode(mode, true).isOk()); |
| 117 | ASSERT_TRUE(power->setMode(mode, false).isOk()); |
| 118 | } |
| 119 | for (const auto& mode : kInvalidModes) { |
| 120 | ASSERT_TRUE(power->setMode(mode, true).isOk()); |
| 121 | ASSERT_TRUE(power->setMode(mode, false).isOk()); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | TEST_P(PowerAidl, isModeSupported) { |
| 126 | for (const auto& mode : kModes) { |
| 127 | bool supported; |
| 128 | ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk()); |
| 129 | } |
| 130 | for (const auto& mode : kInvalidModes) { |
| 131 | bool supported; |
| 132 | ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk()); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 133 | // Should return false for values outside enum |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 134 | ASSERT_FALSE(supported); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | TEST_P(PowerAidl, setBoost) { |
| 139 | for (const auto& boost : kBoosts) { |
| 140 | ASSERT_TRUE(power->setBoost(boost, 0).isOk()); |
| 141 | ASSERT_TRUE(power->setBoost(boost, 1000).isOk()); |
| 142 | ASSERT_TRUE(power->setBoost(boost, -1).isOk()); |
| 143 | } |
| 144 | for (const auto& boost : kInvalidBoosts) { |
| 145 | ASSERT_TRUE(power->setBoost(boost, 0).isOk()); |
| 146 | ASSERT_TRUE(power->setBoost(boost, 1000).isOk()); |
| 147 | ASSERT_TRUE(power->setBoost(boost, -1).isOk()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | TEST_P(PowerAidl, isBoostSupported) { |
| 152 | for (const auto& boost : kBoosts) { |
| 153 | bool supported; |
| 154 | ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk()); |
| 155 | } |
| 156 | for (const auto& boost : kInvalidBoosts) { |
| 157 | bool supported; |
| 158 | ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk()); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 159 | // Should return false for values outside enum |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 160 | ASSERT_FALSE(supported); |
| 161 | } |
| 162 | } |
| 163 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 164 | TEST_P(PowerAidl, getHintSessionPreferredRate) { |
| 165 | int64_t rate = -1; |
| 166 | auto status = power->getHintSessionPreferredRate(&rate); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 167 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 169 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 170 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 171 | ASSERT_TRUE(status.isOk()); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 172 | // At least 1ms rate limit from HAL |
| 173 | ASSERT_GE(rate, 1000000); |
| 174 | } |
| 175 | |
| 176 | TEST_P(PowerAidl, createAndCloseHintSession) { |
| 177 | std::shared_ptr<IPowerHintSession> session; |
| 178 | auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 179 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 180 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 181 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 182 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 183 | ASSERT_TRUE(status.isOk()); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 184 | ASSERT_NE(nullptr, session); |
| 185 | ASSERT_TRUE(session->pause().isOk()); |
| 186 | ASSERT_TRUE(session->resume().isOk()); |
| 187 | // Test normal destroy operation |
| 188 | ASSERT_TRUE(session->close().isOk()); |
| 189 | session.reset(); |
| 190 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 191 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 192 | TEST_P(PowerAidl, createHintSessionFailed) { |
| 193 | std::shared_ptr<IPowerHintSession> session; |
| 194 | auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 195 | |
| 196 | // Regardless of whether V2 and beyond is supported, the status is always not STATUS_OK. |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 197 | ASSERT_FALSE(status.isOk()); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 198 | |
| 199 | // If device not launching with Android 13 and beyond, check whether it's supported, |
| 200 | // if not, skip the test. |
| 201 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && isUnknownOrUnsupported(status)) { |
| 202 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 203 | } |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 204 | ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode()); |
| 205 | } |
| 206 | |
| 207 | TEST_P(PowerAidl, updateAndReportDurations) { |
| 208 | std::shared_ptr<IPowerHintSession> session; |
| 209 | auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 210 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 211 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 212 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 213 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 214 | ASSERT_TRUE(status.isOk()); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 215 | ASSERT_NE(nullptr, session); |
| 216 | |
| 217 | ASSERT_TRUE(session->updateTargetWorkDuration(16666667LL).isOk()); |
| 218 | ASSERT_TRUE(session->reportActualWorkDuration(kDurations).isOk()); |
| 219 | } |
| 220 | |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 221 | // FIXED_PERFORMANCE mode is required for all devices which ship on Android 11 |
| 222 | // or later |
| 223 | TEST_P(PowerAidl, hasFixedPerformance) { |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 224 | if (mApiLevel < kCompatibilityMatrix5ApiLevel) { |
| 225 | GTEST_SKIP() << "FIXED_PERFORMANCE mode is only required for all devices launching Android " |
| 226 | "11 or later."; |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 227 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame^] | 228 | bool supported; |
| 229 | ASSERT_TRUE(power->isModeSupported(Mode::FIXED_PERFORMANCE, &supported).isOk()); |
| 230 | ASSERT_TRUE(supported); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 233 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerAidl); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 234 | INSTANTIATE_TEST_SUITE_P(Power, PowerAidl, |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 235 | testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)), |
| 236 | ::android::PrintInstanceNameToString); |
| 237 | |
| 238 | } // namespace |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 239 | |
| 240 | int main(int argc, char** argv) { |
| 241 | ::testing::InitGoogleTest(&argc, argv); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 242 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 243 | ABinderProcess_startThreadPool(); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 244 | return RUN_ALL_TESTS(); |
| 245 | } |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 246 | |
| 247 | } // namespace aidl::android::hardware::power |