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