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 | |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 86 | inline bool isUnknownOrUnsupported(const ndk::ScopedAStatus& status) { |
| 87 | return status.getStatus() == STATUS_UNKNOWN_TRANSACTION || |
| 88 | status.getExceptionCode() == EX_UNSUPPORTED_OPERATION; |
| 89 | } |
| 90 | |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 91 | class PowerAidl : public testing::TestWithParam<std::string> { |
| 92 | public: |
| 93 | virtual void SetUp() override { |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 94 | AIBinder* binder = AServiceManager_waitForService(GetParam().c_str()); |
| 95 | ASSERT_NE(binder, nullptr); |
| 96 | power = IPower::fromBinder(ndk::SpAIBinder(binder)); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 99 | std::shared_ptr<IPower> power; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | TEST_P(PowerAidl, setMode) { |
| 103 | for (const auto& mode : kModes) { |
| 104 | ASSERT_TRUE(power->setMode(mode, true).isOk()); |
| 105 | ASSERT_TRUE(power->setMode(mode, false).isOk()); |
| 106 | } |
| 107 | for (const auto& mode : kInvalidModes) { |
| 108 | ASSERT_TRUE(power->setMode(mode, true).isOk()); |
| 109 | ASSERT_TRUE(power->setMode(mode, false).isOk()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | TEST_P(PowerAidl, isModeSupported) { |
| 114 | for (const auto& mode : kModes) { |
| 115 | bool supported; |
| 116 | ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk()); |
| 117 | } |
| 118 | for (const auto& mode : kInvalidModes) { |
| 119 | bool supported; |
| 120 | ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk()); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 121 | // Should return false for values outside enum |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 122 | ASSERT_FALSE(supported); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | TEST_P(PowerAidl, setBoost) { |
| 127 | for (const auto& boost : kBoosts) { |
| 128 | ASSERT_TRUE(power->setBoost(boost, 0).isOk()); |
| 129 | ASSERT_TRUE(power->setBoost(boost, 1000).isOk()); |
| 130 | ASSERT_TRUE(power->setBoost(boost, -1).isOk()); |
| 131 | } |
| 132 | for (const auto& boost : kInvalidBoosts) { |
| 133 | ASSERT_TRUE(power->setBoost(boost, 0).isOk()); |
| 134 | ASSERT_TRUE(power->setBoost(boost, 1000).isOk()); |
| 135 | ASSERT_TRUE(power->setBoost(boost, -1).isOk()); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | TEST_P(PowerAidl, isBoostSupported) { |
| 140 | for (const auto& boost : kBoosts) { |
| 141 | bool supported; |
| 142 | ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk()); |
| 143 | } |
| 144 | for (const auto& boost : kInvalidBoosts) { |
| 145 | bool supported; |
| 146 | ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk()); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 147 | // Should return false for values outside enum |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 148 | ASSERT_FALSE(supported); |
| 149 | } |
| 150 | } |
| 151 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 152 | TEST_P(PowerAidl, getHintSessionPreferredRate) { |
| 153 | int64_t rate = -1; |
| 154 | auto status = power->getHintSessionPreferredRate(&rate); |
| 155 | if (!status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 156 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
| 160 | // At least 1ms rate limit from HAL |
| 161 | ASSERT_GE(rate, 1000000); |
| 162 | } |
| 163 | |
| 164 | TEST_P(PowerAidl, createAndCloseHintSession) { |
| 165 | std::shared_ptr<IPowerHintSession> session; |
| 166 | auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session); |
| 167 | if (!status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | ASSERT_NE(nullptr, session); |
| 172 | ASSERT_TRUE(session->pause().isOk()); |
| 173 | ASSERT_TRUE(session->resume().isOk()); |
| 174 | // Test normal destroy operation |
| 175 | ASSERT_TRUE(session->close().isOk()); |
| 176 | session.reset(); |
| 177 | } |
| 178 | TEST_P(PowerAidl, createHintSessionFailed) { |
| 179 | std::shared_ptr<IPowerHintSession> session; |
| 180 | auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session); |
| 181 | ASSERT_FALSE(status.isOk()); |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 182 | if (isUnknownOrUnsupported(status)) { |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 183 | return; |
| 184 | } |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 185 | ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode()); |
| 186 | } |
| 187 | |
| 188 | TEST_P(PowerAidl, updateAndReportDurations) { |
| 189 | std::shared_ptr<IPowerHintSession> session; |
| 190 | auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session); |
| 191 | if (!status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 192 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | ASSERT_NE(nullptr, session); |
| 196 | |
| 197 | ASSERT_TRUE(session->updateTargetWorkDuration(16666667LL).isOk()); |
| 198 | ASSERT_TRUE(session->reportActualWorkDuration(kDurations).isOk()); |
| 199 | } |
| 200 | |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 201 | // FIXED_PERFORMANCE mode is required for all devices which ship on Android 11 |
| 202 | // or later |
| 203 | TEST_P(PowerAidl, hasFixedPerformance) { |
jimblackler | 425df50 | 2022-01-19 15:45:11 +0000 | [diff] [blame] | 204 | auto apiLevel = GetUintProperty<uint64_t>("ro.vendor.api_level", 0); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 205 | ASSERT_NE(apiLevel, 0); |
| 206 | |
| 207 | if (apiLevel >= 30) { |
| 208 | bool supported; |
| 209 | ASSERT_TRUE(power->isModeSupported(Mode::FIXED_PERFORMANCE, &supported).isOk()); |
| 210 | ASSERT_TRUE(supported); |
| 211 | } |
| 212 | } |
| 213 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 214 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerAidl); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 215 | INSTANTIATE_TEST_SUITE_P(Power, PowerAidl, |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 216 | testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)), |
| 217 | ::android::PrintInstanceNameToString); |
| 218 | |
| 219 | } // namespace |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 220 | |
| 221 | int main(int argc, char** argv) { |
| 222 | ::testing::InitGoogleTest(&argc, argv); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 223 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 224 | ABinderProcess_startThreadPool(); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 225 | return RUN_ALL_TESTS(); |
| 226 | } |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 227 | |
| 228 | } // namespace aidl::android::hardware::power |