Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
| 17 | #define LOG_TAG "perf_hint" |
| 18 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 19 | #include <aidl/android/hardware/power/SessionHint.h> |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 20 | #include <android/os/IHintManager.h> |
| 21 | #include <android/os/IHintSession.h> |
Bo Liu | 2b739bb | 2021-11-10 19:20:03 -0500 | [diff] [blame] | 22 | #include <android/performance_hint.h> |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 23 | #include <binder/Binder.h> |
| 24 | #include <binder/IBinder.h> |
| 25 | #include <binder/IServiceManager.h> |
| 26 | #include <performance_hint_private.h> |
| 27 | #include <utils/SystemClock.h> |
| 28 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 29 | #include <chrono> |
Bo Liu | 2b739bb | 2021-11-10 19:20:03 -0500 | [diff] [blame] | 30 | #include <utility> |
| 31 | #include <vector> |
| 32 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 33 | using namespace android; |
| 34 | using namespace android::os; |
| 35 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 36 | using namespace std::chrono_literals; |
| 37 | |
| 38 | using AidlSessionHint = aidl::android::hardware::power::SessionHint; |
| 39 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 40 | struct APerformanceHintSession; |
| 41 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 42 | constexpr int64_t SEND_HINT_TIMEOUT = std::chrono::nanoseconds(100ms).count(); |
| 43 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 44 | struct APerformanceHintManager { |
| 45 | public: |
| 46 | static APerformanceHintManager* getInstance(); |
| 47 | APerformanceHintManager(sp<IHintManager> service, int64_t preferredRateNanos); |
| 48 | APerformanceHintManager() = delete; |
| 49 | ~APerformanceHintManager() = default; |
| 50 | |
| 51 | APerformanceHintSession* createSession(const int32_t* threadIds, size_t size, |
| 52 | int64_t initialTargetWorkDurationNanos); |
| 53 | int64_t getPreferredRateNanos() const; |
| 54 | |
| 55 | private: |
| 56 | static APerformanceHintManager* create(sp<IHintManager> iHintManager); |
| 57 | |
| 58 | sp<IHintManager> mHintManager; |
Bo Liu | 9acc558 | 2022-02-17 16:47:32 -0500 | [diff] [blame] | 59 | const sp<IBinder> mToken = sp<BBinder>::make(); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 60 | const int64_t mPreferredRateNanos; |
| 61 | }; |
| 62 | |
| 63 | struct APerformanceHintSession { |
| 64 | public: |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 65 | APerformanceHintSession(sp<IHintManager> hintManager, sp<IHintSession> session, |
| 66 | int64_t preferredRateNanos, int64_t targetDurationNanos); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 67 | APerformanceHintSession() = delete; |
| 68 | ~APerformanceHintSession(); |
| 69 | |
| 70 | int updateTargetWorkDuration(int64_t targetDurationNanos); |
| 71 | int reportActualWorkDuration(int64_t actualDurationNanos); |
Matt Buckley | 712f58a | 2023-02-07 02:10:58 +0000 | [diff] [blame] | 72 | int sendHint(int32_t hint); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 73 | int setThreads(const int32_t* threadIds, size_t size); |
| 74 | int getThreadIds(int32_t* const threadIds, size_t* size); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | friend struct APerformanceHintManager; |
| 78 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 79 | sp<IHintManager> mHintManager; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 80 | sp<IHintSession> mHintSession; |
| 81 | // HAL preferred update rate |
| 82 | const int64_t mPreferredRateNanos; |
| 83 | // Target duration for choosing update rate |
| 84 | int64_t mTargetDurationNanos; |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 85 | // First target hit timestamp |
| 86 | int64_t mFirstTargetMetTimestamp; |
| 87 | // Last target hit timestamp |
| 88 | int64_t mLastTargetMetTimestamp; |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 89 | // Last hint reported from sendHint indexed by hint value |
| 90 | std::vector<int64_t> mLastHintSentTimestamp; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 91 | // Cached samples |
| 92 | std::vector<int64_t> mActualDurationsNanos; |
| 93 | std::vector<int64_t> mTimestampsNanos; |
| 94 | }; |
| 95 | |
| 96 | static IHintManager* gIHintManagerForTesting = nullptr; |
| 97 | static APerformanceHintManager* gHintManagerForTesting = nullptr; |
| 98 | |
| 99 | // ===================================== APerformanceHintManager implementation |
| 100 | APerformanceHintManager::APerformanceHintManager(sp<IHintManager> manager, |
| 101 | int64_t preferredRateNanos) |
| 102 | : mHintManager(std::move(manager)), mPreferredRateNanos(preferredRateNanos) {} |
| 103 | |
| 104 | APerformanceHintManager* APerformanceHintManager::getInstance() { |
| 105 | if (gHintManagerForTesting) return gHintManagerForTesting; |
| 106 | if (gIHintManagerForTesting) { |
| 107 | APerformanceHintManager* manager = create(gIHintManagerForTesting); |
| 108 | gIHintManagerForTesting = nullptr; |
| 109 | return manager; |
| 110 | } |
| 111 | static APerformanceHintManager* instance = create(nullptr); |
| 112 | return instance; |
| 113 | } |
| 114 | |
| 115 | APerformanceHintManager* APerformanceHintManager::create(sp<IHintManager> manager) { |
| 116 | if (!manager) { |
| 117 | manager = interface_cast<IHintManager>( |
| 118 | defaultServiceManager()->checkService(String16("performance_hint"))); |
| 119 | } |
| 120 | if (manager == nullptr) { |
| 121 | ALOGE("%s: PerformanceHint service is not ready ", __FUNCTION__); |
| 122 | return nullptr; |
| 123 | } |
| 124 | int64_t preferredRateNanos = -1L; |
| 125 | binder::Status ret = manager->getHintSessionPreferredRate(&preferredRateNanos); |
| 126 | if (!ret.isOk()) { |
| 127 | ALOGE("%s: PerformanceHint cannot get preferred rate. %s", __FUNCTION__, |
| 128 | ret.exceptionMessage().c_str()); |
| 129 | return nullptr; |
| 130 | } |
| 131 | if (preferredRateNanos <= 0) { |
Bo Liu | d6a0960 | 2021-07-26 14:48:41 -0400 | [diff] [blame] | 132 | preferredRateNanos = -1L; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 133 | } |
| 134 | return new APerformanceHintManager(std::move(manager), preferredRateNanos); |
| 135 | } |
| 136 | |
| 137 | APerformanceHintSession* APerformanceHintManager::createSession( |
| 138 | const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos) { |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 139 | std::vector<int32_t> tids(threadIds, threadIds + size); |
| 140 | sp<IHintSession> session; |
| 141 | binder::Status ret = |
Bo Liu | 9acc558 | 2022-02-17 16:47:32 -0500 | [diff] [blame] | 142 | mHintManager->createHintSession(mToken, tids, initialTargetWorkDurationNanos, &session); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 143 | if (!ret.isOk() || !session) { |
| 144 | return nullptr; |
| 145 | } |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 146 | return new APerformanceHintSession(mHintManager, std::move(session), mPreferredRateNanos, |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 147 | initialTargetWorkDurationNanos); |
| 148 | } |
| 149 | |
| 150 | int64_t APerformanceHintManager::getPreferredRateNanos() const { |
| 151 | return mPreferredRateNanos; |
| 152 | } |
| 153 | |
| 154 | // ===================================== APerformanceHintSession implementation |
| 155 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 156 | APerformanceHintSession::APerformanceHintSession(sp<IHintManager> hintManager, |
| 157 | sp<IHintSession> session, |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 158 | int64_t preferredRateNanos, |
| 159 | int64_t targetDurationNanos) |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 160 | : mHintManager(hintManager), |
| 161 | mHintSession(std::move(session)), |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 162 | mPreferredRateNanos(preferredRateNanos), |
| 163 | mTargetDurationNanos(targetDurationNanos), |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 164 | mFirstTargetMetTimestamp(0), |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 165 | mLastTargetMetTimestamp(0) { |
| 166 | const std::vector<AidlSessionHint> sessionHintRange{ndk::enum_range<AidlSessionHint>().begin(), |
| 167 | ndk::enum_range<AidlSessionHint>().end()}; |
| 168 | |
| 169 | mLastHintSentTimestamp = std::vector<int64_t>(sessionHintRange.size(), 0); |
| 170 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 171 | |
| 172 | APerformanceHintSession::~APerformanceHintSession() { |
| 173 | binder::Status ret = mHintSession->close(); |
| 174 | if (!ret.isOk()) { |
| 175 | ALOGE("%s: HintSession close failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | int APerformanceHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) { |
| 180 | if (targetDurationNanos <= 0) { |
| 181 | ALOGE("%s: targetDurationNanos must be positive", __FUNCTION__); |
| 182 | return EINVAL; |
| 183 | } |
| 184 | binder::Status ret = mHintSession->updateTargetWorkDuration(targetDurationNanos); |
| 185 | if (!ret.isOk()) { |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 186 | ALOGE("%s: HintSession updateTargetWorkDuration failed: %s", __FUNCTION__, |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 187 | ret.exceptionMessage().c_str()); |
| 188 | return EPIPE; |
| 189 | } |
| 190 | mTargetDurationNanos = targetDurationNanos; |
| 191 | /** |
| 192 | * Most of the workload is target_duration dependent, so now clear the cached samples |
| 193 | * as they are most likely obsolete. |
| 194 | */ |
| 195 | mActualDurationsNanos.clear(); |
| 196 | mTimestampsNanos.clear(); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 197 | mFirstTargetMetTimestamp = 0; |
| 198 | mLastTargetMetTimestamp = 0; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | int APerformanceHintSession::reportActualWorkDuration(int64_t actualDurationNanos) { |
| 203 | if (actualDurationNanos <= 0) { |
| 204 | ALOGE("%s: actualDurationNanos must be positive", __FUNCTION__); |
| 205 | return EINVAL; |
| 206 | } |
| 207 | int64_t now = elapsedRealtimeNano(); |
| 208 | mActualDurationsNanos.push_back(actualDurationNanos); |
| 209 | mTimestampsNanos.push_back(now); |
| 210 | |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 211 | if (actualDurationNanos >= mTargetDurationNanos) { |
| 212 | // Reset timestamps if we are equal or over the target. |
| 213 | mFirstTargetMetTimestamp = 0; |
| 214 | } else { |
| 215 | // Set mFirstTargetMetTimestamp for first time meeting target. |
| 216 | if (!mFirstTargetMetTimestamp || !mLastTargetMetTimestamp || |
| 217 | (now - mLastTargetMetTimestamp > 2 * mPreferredRateNanos)) { |
| 218 | mFirstTargetMetTimestamp = now; |
| 219 | } |
| 220 | /** |
| 221 | * Rate limit the change if the update is over mPreferredRateNanos since first |
| 222 | * meeting target and less than mPreferredRateNanos since last meeting target. |
| 223 | */ |
| 224 | if (now - mFirstTargetMetTimestamp > mPreferredRateNanos && |
| 225 | now - mLastTargetMetTimestamp <= mPreferredRateNanos) { |
| 226 | return 0; |
| 227 | } |
| 228 | mLastTargetMetTimestamp = now; |
Wei Wang | 9f174f4 | 2021-11-11 22:56:05 -0800 | [diff] [blame] | 229 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 230 | |
| 231 | binder::Status ret = |
| 232 | mHintSession->reportActualWorkDuration(mActualDurationsNanos, mTimestampsNanos); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 233 | if (!ret.isOk()) { |
| 234 | ALOGE("%s: HintSession reportActualWorkDuration failed: %s", __FUNCTION__, |
| 235 | ret.exceptionMessage().c_str()); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 236 | mFirstTargetMetTimestamp = 0; |
| 237 | mLastTargetMetTimestamp = 0; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 238 | return EPIPE; |
| 239 | } |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 240 | mActualDurationsNanos.clear(); |
| 241 | mTimestampsNanos.clear(); |
| 242 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Matt Buckley | 712f58a | 2023-02-07 02:10:58 +0000 | [diff] [blame] | 246 | int APerformanceHintSession::sendHint(int32_t hint) { |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 247 | if (hint < 0 || hint >= static_cast<int32_t>(mLastHintSentTimestamp.size())) { |
| 248 | ALOGE("%s: invalid session hint %d", __FUNCTION__, hint); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 249 | return EINVAL; |
| 250 | } |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 251 | int64_t now = elapsedRealtimeNano(); |
| 252 | |
| 253 | // Limit sendHint to a pre-detemined rate for safety |
| 254 | if (now < (mLastHintSentTimestamp[hint] + SEND_HINT_TIMEOUT)) { |
| 255 | return 0; |
| 256 | } |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 257 | |
| 258 | binder::Status ret = mHintSession->sendHint(hint); |
| 259 | |
| 260 | if (!ret.isOk()) { |
| 261 | ALOGE("%s: HintSession sendHint failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 262 | return EPIPE; |
| 263 | } |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 264 | mLastHintSentTimestamp[hint] = now; |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 268 | int APerformanceHintSession::setThreads(const int32_t* threadIds, size_t size) { |
| 269 | if (size == 0) { |
| 270 | ALOGE("%s: the list of thread ids must not be empty.", __FUNCTION__); |
| 271 | return EINVAL; |
| 272 | } |
| 273 | std::vector<int32_t> tids(threadIds, threadIds + size); |
| 274 | binder::Status ret = mHintManager->setHintSessionThreads(mHintSession, tids); |
| 275 | if (!ret.isOk()) { |
| 276 | ALOGE("%s: failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 277 | if (ret.exceptionCode() == binder::Status::Exception::EX_SECURITY || |
| 278 | ret.exceptionCode() == binder::Status::Exception::EX_ILLEGAL_ARGUMENT) { |
| 279 | return EINVAL; |
| 280 | } |
| 281 | return EPIPE; |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | int APerformanceHintSession::getThreadIds(int32_t* const threadIds, size_t* size) { |
| 287 | std::vector<int32_t> tids; |
| 288 | binder::Status ret = mHintManager->getHintSessionThreadIds(mHintSession, &tids); |
| 289 | if (!ret.isOk()) { |
| 290 | ALOGE("%s: failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 291 | return EPIPE; |
| 292 | } |
| 293 | |
| 294 | // When threadIds is nullptr, this is the first call to determine the size |
| 295 | // of the thread ids list. |
| 296 | if (threadIds == nullptr) { |
| 297 | *size = tids.size(); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | // Second call to return the actual list of thread ids. |
| 302 | *size = tids.size(); |
| 303 | for (size_t i = 0; i < *size; ++i) { |
| 304 | threadIds[i] = tids[i]; |
| 305 | } |
| 306 | return 0; |
| 307 | } |
| 308 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 309 | // ===================================== C API |
| 310 | APerformanceHintManager* APerformanceHint_getManager() { |
| 311 | return APerformanceHintManager::getInstance(); |
| 312 | } |
| 313 | |
| 314 | APerformanceHintSession* APerformanceHint_createSession(APerformanceHintManager* manager, |
| 315 | const int32_t* threadIds, size_t size, |
| 316 | int64_t initialTargetWorkDurationNanos) { |
| 317 | return manager->createSession(threadIds, size, initialTargetWorkDurationNanos); |
| 318 | } |
| 319 | |
| 320 | int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) { |
| 321 | return manager->getPreferredRateNanos(); |
| 322 | } |
| 323 | |
| 324 | int APerformanceHint_updateTargetWorkDuration(APerformanceHintSession* session, |
| 325 | int64_t targetDurationNanos) { |
| 326 | return session->updateTargetWorkDuration(targetDurationNanos); |
| 327 | } |
| 328 | |
| 329 | int APerformanceHint_reportActualWorkDuration(APerformanceHintSession* session, |
| 330 | int64_t actualDurationNanos) { |
| 331 | return session->reportActualWorkDuration(actualDurationNanos); |
| 332 | } |
| 333 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 334 | void APerformanceHint_closeSession(APerformanceHintSession* session) { |
| 335 | delete session; |
| 336 | } |
| 337 | |
Matt Buckley | 712f58a | 2023-02-07 02:10:58 +0000 | [diff] [blame] | 338 | int APerformanceHint_sendHint(void* session, int32_t hint) { |
Matt Buckley | 61726a3 | 2022-12-06 23:44:45 +0000 | [diff] [blame] | 339 | return reinterpret_cast<APerformanceHintSession*>(session)->sendHint(hint); |
| 340 | } |
| 341 | |
Peiyong Lin | 7ed6de3 | 2023-01-26 00:52:54 +0000 | [diff] [blame] | 342 | int APerformanceHint_setThreads(APerformanceHintSession* session, const pid_t* threadIds, |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 343 | size_t size) { |
| 344 | if (session == nullptr) { |
| 345 | return EINVAL; |
| 346 | } |
| 347 | return session->setThreads(threadIds, size); |
| 348 | } |
| 349 | |
| 350 | int APerformanceHint_getThreadIds(void* aPerformanceHintSession, int32_t* const threadIds, |
| 351 | size_t* const size) { |
| 352 | if (aPerformanceHintSession == nullptr) { |
| 353 | return EINVAL; |
| 354 | } |
| 355 | return static_cast<APerformanceHintSession*>(aPerformanceHintSession) |
| 356 | ->getThreadIds(threadIds, size); |
| 357 | } |
| 358 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 359 | void APerformanceHint_setIHintManagerForTesting(void* iManager) { |
| 360 | delete gHintManagerForTesting; |
| 361 | gHintManagerForTesting = nullptr; |
| 362 | gIHintManagerForTesting = static_cast<IHintManager*>(iManager); |
| 363 | } |