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: |
| 65 | APerformanceHintSession(sp<IHintSession> session, int64_t preferredRateNanos, |
| 66 | int64_t targetDurationNanos); |
| 67 | APerformanceHintSession() = delete; |
| 68 | ~APerformanceHintSession(); |
| 69 | |
| 70 | int updateTargetWorkDuration(int64_t targetDurationNanos); |
| 71 | int reportActualWorkDuration(int64_t actualDurationNanos); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 72 | int sendHint(int32_t hint); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | friend struct APerformanceHintManager; |
| 76 | |
| 77 | sp<IHintSession> mHintSession; |
| 78 | // HAL preferred update rate |
| 79 | const int64_t mPreferredRateNanos; |
| 80 | // Target duration for choosing update rate |
| 81 | int64_t mTargetDurationNanos; |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 82 | // First target hit timestamp |
| 83 | int64_t mFirstTargetMetTimestamp; |
| 84 | // Last target hit timestamp |
| 85 | int64_t mLastTargetMetTimestamp; |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame^] | 86 | // Last hint reported from sendHint indexed by hint value |
| 87 | std::vector<int64_t> mLastHintSentTimestamp; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 88 | // Cached samples |
| 89 | std::vector<int64_t> mActualDurationsNanos; |
| 90 | std::vector<int64_t> mTimestampsNanos; |
| 91 | }; |
| 92 | |
| 93 | static IHintManager* gIHintManagerForTesting = nullptr; |
| 94 | static APerformanceHintManager* gHintManagerForTesting = nullptr; |
| 95 | |
| 96 | // ===================================== APerformanceHintManager implementation |
| 97 | APerformanceHintManager::APerformanceHintManager(sp<IHintManager> manager, |
| 98 | int64_t preferredRateNanos) |
| 99 | : mHintManager(std::move(manager)), mPreferredRateNanos(preferredRateNanos) {} |
| 100 | |
| 101 | APerformanceHintManager* APerformanceHintManager::getInstance() { |
| 102 | if (gHintManagerForTesting) return gHintManagerForTesting; |
| 103 | if (gIHintManagerForTesting) { |
| 104 | APerformanceHintManager* manager = create(gIHintManagerForTesting); |
| 105 | gIHintManagerForTesting = nullptr; |
| 106 | return manager; |
| 107 | } |
| 108 | static APerformanceHintManager* instance = create(nullptr); |
| 109 | return instance; |
| 110 | } |
| 111 | |
| 112 | APerformanceHintManager* APerformanceHintManager::create(sp<IHintManager> manager) { |
| 113 | if (!manager) { |
| 114 | manager = interface_cast<IHintManager>( |
| 115 | defaultServiceManager()->checkService(String16("performance_hint"))); |
| 116 | } |
| 117 | if (manager == nullptr) { |
| 118 | ALOGE("%s: PerformanceHint service is not ready ", __FUNCTION__); |
| 119 | return nullptr; |
| 120 | } |
| 121 | int64_t preferredRateNanos = -1L; |
| 122 | binder::Status ret = manager->getHintSessionPreferredRate(&preferredRateNanos); |
| 123 | if (!ret.isOk()) { |
| 124 | ALOGE("%s: PerformanceHint cannot get preferred rate. %s", __FUNCTION__, |
| 125 | ret.exceptionMessage().c_str()); |
| 126 | return nullptr; |
| 127 | } |
| 128 | if (preferredRateNanos <= 0) { |
Bo Liu | d6a0960 | 2021-07-26 14:48:41 -0400 | [diff] [blame] | 129 | preferredRateNanos = -1L; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 130 | } |
| 131 | return new APerformanceHintManager(std::move(manager), preferredRateNanos); |
| 132 | } |
| 133 | |
| 134 | APerformanceHintSession* APerformanceHintManager::createSession( |
| 135 | const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos) { |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 136 | std::vector<int32_t> tids(threadIds, threadIds + size); |
| 137 | sp<IHintSession> session; |
| 138 | binder::Status ret = |
Bo Liu | 9acc558 | 2022-02-17 16:47:32 -0500 | [diff] [blame] | 139 | mHintManager->createHintSession(mToken, tids, initialTargetWorkDurationNanos, &session); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 140 | if (!ret.isOk() || !session) { |
| 141 | return nullptr; |
| 142 | } |
| 143 | return new APerformanceHintSession(std::move(session), mPreferredRateNanos, |
| 144 | initialTargetWorkDurationNanos); |
| 145 | } |
| 146 | |
| 147 | int64_t APerformanceHintManager::getPreferredRateNanos() const { |
| 148 | return mPreferredRateNanos; |
| 149 | } |
| 150 | |
| 151 | // ===================================== APerformanceHintSession implementation |
| 152 | |
| 153 | APerformanceHintSession::APerformanceHintSession(sp<IHintSession> session, |
| 154 | int64_t preferredRateNanos, |
| 155 | int64_t targetDurationNanos) |
| 156 | : mHintSession(std::move(session)), |
| 157 | mPreferredRateNanos(preferredRateNanos), |
| 158 | mTargetDurationNanos(targetDurationNanos), |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 159 | mFirstTargetMetTimestamp(0), |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame^] | 160 | mLastTargetMetTimestamp(0) { |
| 161 | const std::vector<AidlSessionHint> sessionHintRange{ndk::enum_range<AidlSessionHint>().begin(), |
| 162 | ndk::enum_range<AidlSessionHint>().end()}; |
| 163 | |
| 164 | mLastHintSentTimestamp = std::vector<int64_t>(sessionHintRange.size(), 0); |
| 165 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 166 | |
| 167 | APerformanceHintSession::~APerformanceHintSession() { |
| 168 | binder::Status ret = mHintSession->close(); |
| 169 | if (!ret.isOk()) { |
| 170 | ALOGE("%s: HintSession close failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | int APerformanceHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) { |
| 175 | if (targetDurationNanos <= 0) { |
| 176 | ALOGE("%s: targetDurationNanos must be positive", __FUNCTION__); |
| 177 | return EINVAL; |
| 178 | } |
| 179 | binder::Status ret = mHintSession->updateTargetWorkDuration(targetDurationNanos); |
| 180 | if (!ret.isOk()) { |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 181 | ALOGE("%s: HintSession updateTargetWorkDuration failed: %s", __FUNCTION__, |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 182 | ret.exceptionMessage().c_str()); |
| 183 | return EPIPE; |
| 184 | } |
| 185 | mTargetDurationNanos = targetDurationNanos; |
| 186 | /** |
| 187 | * Most of the workload is target_duration dependent, so now clear the cached samples |
| 188 | * as they are most likely obsolete. |
| 189 | */ |
| 190 | mActualDurationsNanos.clear(); |
| 191 | mTimestampsNanos.clear(); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 192 | mFirstTargetMetTimestamp = 0; |
| 193 | mLastTargetMetTimestamp = 0; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int APerformanceHintSession::reportActualWorkDuration(int64_t actualDurationNanos) { |
| 198 | if (actualDurationNanos <= 0) { |
| 199 | ALOGE("%s: actualDurationNanos must be positive", __FUNCTION__); |
| 200 | return EINVAL; |
| 201 | } |
| 202 | int64_t now = elapsedRealtimeNano(); |
| 203 | mActualDurationsNanos.push_back(actualDurationNanos); |
| 204 | mTimestampsNanos.push_back(now); |
| 205 | |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 206 | if (actualDurationNanos >= mTargetDurationNanos) { |
| 207 | // Reset timestamps if we are equal or over the target. |
| 208 | mFirstTargetMetTimestamp = 0; |
| 209 | } else { |
| 210 | // Set mFirstTargetMetTimestamp for first time meeting target. |
| 211 | if (!mFirstTargetMetTimestamp || !mLastTargetMetTimestamp || |
| 212 | (now - mLastTargetMetTimestamp > 2 * mPreferredRateNanos)) { |
| 213 | mFirstTargetMetTimestamp = now; |
| 214 | } |
| 215 | /** |
| 216 | * Rate limit the change if the update is over mPreferredRateNanos since first |
| 217 | * meeting target and less than mPreferredRateNanos since last meeting target. |
| 218 | */ |
| 219 | if (now - mFirstTargetMetTimestamp > mPreferredRateNanos && |
| 220 | now - mLastTargetMetTimestamp <= mPreferredRateNanos) { |
| 221 | return 0; |
| 222 | } |
| 223 | mLastTargetMetTimestamp = now; |
Wei Wang | 9f174f4 | 2021-11-11 22:56:05 -0800 | [diff] [blame] | 224 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 225 | |
| 226 | binder::Status ret = |
| 227 | mHintSession->reportActualWorkDuration(mActualDurationsNanos, mTimestampsNanos); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 228 | if (!ret.isOk()) { |
| 229 | ALOGE("%s: HintSession reportActualWorkDuration failed: %s", __FUNCTION__, |
| 230 | ret.exceptionMessage().c_str()); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 231 | mFirstTargetMetTimestamp = 0; |
| 232 | mLastTargetMetTimestamp = 0; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 233 | return EPIPE; |
| 234 | } |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 235 | mActualDurationsNanos.clear(); |
| 236 | mTimestampsNanos.clear(); |
| 237 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 241 | int APerformanceHintSession::sendHint(int32_t hint) { |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame^] | 242 | if (hint < 0 || hint >= static_cast<int32_t>(mLastHintSentTimestamp.size())) { |
| 243 | ALOGE("%s: invalid session hint %d", __FUNCTION__, hint); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 244 | return EINVAL; |
| 245 | } |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame^] | 246 | int64_t now = elapsedRealtimeNano(); |
| 247 | |
| 248 | // Limit sendHint to a pre-detemined rate for safety |
| 249 | if (now < (mLastHintSentTimestamp[hint] + SEND_HINT_TIMEOUT)) { |
| 250 | return 0; |
| 251 | } |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 252 | |
| 253 | binder::Status ret = mHintSession->sendHint(hint); |
| 254 | |
| 255 | if (!ret.isOk()) { |
| 256 | ALOGE("%s: HintSession sendHint failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 257 | return EPIPE; |
| 258 | } |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame^] | 259 | mLastHintSentTimestamp[hint] = now; |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 263 | // ===================================== C API |
| 264 | APerformanceHintManager* APerformanceHint_getManager() { |
| 265 | return APerformanceHintManager::getInstance(); |
| 266 | } |
| 267 | |
| 268 | APerformanceHintSession* APerformanceHint_createSession(APerformanceHintManager* manager, |
| 269 | const int32_t* threadIds, size_t size, |
| 270 | int64_t initialTargetWorkDurationNanos) { |
| 271 | return manager->createSession(threadIds, size, initialTargetWorkDurationNanos); |
| 272 | } |
| 273 | |
| 274 | int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) { |
| 275 | return manager->getPreferredRateNanos(); |
| 276 | } |
| 277 | |
| 278 | int APerformanceHint_updateTargetWorkDuration(APerformanceHintSession* session, |
| 279 | int64_t targetDurationNanos) { |
| 280 | return session->updateTargetWorkDuration(targetDurationNanos); |
| 281 | } |
| 282 | |
| 283 | int APerformanceHint_reportActualWorkDuration(APerformanceHintSession* session, |
| 284 | int64_t actualDurationNanos) { |
| 285 | return session->reportActualWorkDuration(actualDurationNanos); |
| 286 | } |
| 287 | |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 288 | int APerformanceHint_sendHint(APerformanceHintSession* session, int32_t hint) { |
| 289 | return session->sendHint(hint); |
| 290 | } |
| 291 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 292 | void APerformanceHint_closeSession(APerformanceHintSession* session) { |
| 293 | delete session; |
| 294 | } |
| 295 | |
| 296 | void APerformanceHint_setIHintManagerForTesting(void* iManager) { |
| 297 | delete gHintManagerForTesting; |
| 298 | gHintManagerForTesting = nullptr; |
| 299 | gIHintManagerForTesting = static_cast<IHintManager*>(iManager); |
| 300 | } |