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