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