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> |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 20 | #include <aidl/android/hardware/power/SessionMode.h> |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 22 | #include <android/WorkDuration.h> |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 23 | #include <android/os/IHintManager.h> |
| 24 | #include <android/os/IHintSession.h> |
Bo Liu | 2b739bb | 2021-11-10 19:20:03 -0500 | [diff] [blame] | 25 | #include <android/performance_hint.h> |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 26 | #include <android/trace.h> |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 27 | #include <binder/Binder.h> |
| 28 | #include <binder/IBinder.h> |
| 29 | #include <binder/IServiceManager.h> |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 30 | #include <inttypes.h> |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 31 | #include <performance_hint_private.h> |
| 32 | #include <utils/SystemClock.h> |
| 33 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 34 | #include <chrono> |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 35 | #include <set> |
Bo Liu | 2b739bb | 2021-11-10 19:20:03 -0500 | [diff] [blame] | 36 | #include <utility> |
| 37 | #include <vector> |
| 38 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 39 | using namespace android; |
| 40 | using namespace android::os; |
| 41 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 42 | using namespace std::chrono_literals; |
| 43 | |
| 44 | using AidlSessionHint = aidl::android::hardware::power::SessionHint; |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 45 | using AidlSessionMode = aidl::android::hardware::power::SessionMode; |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 46 | using android::base::StringPrintf; |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 47 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 48 | struct APerformanceHintSession; |
| 49 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 50 | constexpr int64_t SEND_HINT_TIMEOUT = std::chrono::nanoseconds(100ms).count(); |
| 51 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 52 | struct APerformanceHintManager { |
| 53 | public: |
| 54 | static APerformanceHintManager* getInstance(); |
| 55 | APerformanceHintManager(sp<IHintManager> service, int64_t preferredRateNanos); |
| 56 | APerformanceHintManager() = delete; |
| 57 | ~APerformanceHintManager() = default; |
| 58 | |
| 59 | APerformanceHintSession* createSession(const int32_t* threadIds, size_t size, |
| 60 | int64_t initialTargetWorkDurationNanos); |
| 61 | int64_t getPreferredRateNanos() const; |
| 62 | |
| 63 | private: |
| 64 | static APerformanceHintManager* create(sp<IHintManager> iHintManager); |
| 65 | |
| 66 | sp<IHintManager> mHintManager; |
Bo Liu | 9acc558 | 2022-02-17 16:47:32 -0500 | [diff] [blame] | 67 | const sp<IBinder> mToken = sp<BBinder>::make(); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 68 | const int64_t mPreferredRateNanos; |
| 69 | }; |
| 70 | |
| 71 | struct APerformanceHintSession { |
| 72 | public: |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 73 | APerformanceHintSession(sp<IHintManager> hintManager, sp<IHintSession> session, |
| 74 | int64_t preferredRateNanos, int64_t targetDurationNanos); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 75 | APerformanceHintSession() = delete; |
| 76 | ~APerformanceHintSession(); |
| 77 | |
| 78 | int updateTargetWorkDuration(int64_t targetDurationNanos); |
| 79 | int reportActualWorkDuration(int64_t actualDurationNanos); |
Steven Moreland | 42a8cce | 2023-03-03 23:31:17 +0000 | [diff] [blame] | 80 | int sendHint(SessionHint hint); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 81 | int setThreads(const int32_t* threadIds, size_t size); |
| 82 | int getThreadIds(int32_t* const threadIds, size_t* size); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 83 | int setPreferPowerEfficiency(bool enabled); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 84 | int reportActualWorkDuration(AWorkDuration* workDuration); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | friend struct APerformanceHintManager; |
| 88 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 89 | int reportActualWorkDurationInternal(WorkDuration* workDuration); |
| 90 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 91 | sp<IHintManager> mHintManager; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 92 | sp<IHintSession> mHintSession; |
| 93 | // HAL preferred update rate |
| 94 | const int64_t mPreferredRateNanos; |
| 95 | // Target duration for choosing update rate |
| 96 | int64_t mTargetDurationNanos; |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 97 | // First target hit timestamp |
| 98 | int64_t mFirstTargetMetTimestamp; |
| 99 | // Last target hit timestamp |
| 100 | int64_t mLastTargetMetTimestamp; |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 101 | // Last hint reported from sendHint indexed by hint value |
| 102 | std::vector<int64_t> mLastHintSentTimestamp; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 103 | // Cached samples |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 104 | std::vector<WorkDuration> mActualWorkDurations; |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 105 | std::string mSessionName; |
| 106 | static int32_t sIDCounter; |
| 107 | // The most recent set of thread IDs |
| 108 | std::vector<int32_t> mLastThreadIDs; |
| 109 | // Tracing helpers |
| 110 | void traceThreads(std::vector<int32_t>& tids); |
| 111 | void tracePowerEfficient(bool powerEfficient); |
| 112 | void traceActualDuration(int64_t actualDuration); |
| 113 | void traceBatchSize(size_t batchSize); |
| 114 | void traceTargetDuration(int64_t targetDuration); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static IHintManager* gIHintManagerForTesting = nullptr; |
| 118 | static APerformanceHintManager* gHintManagerForTesting = nullptr; |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 119 | int32_t APerformanceHintSession::sIDCounter = 0; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 120 | |
| 121 | // ===================================== APerformanceHintManager implementation |
| 122 | APerformanceHintManager::APerformanceHintManager(sp<IHintManager> manager, |
| 123 | int64_t preferredRateNanos) |
| 124 | : mHintManager(std::move(manager)), mPreferredRateNanos(preferredRateNanos) {} |
| 125 | |
| 126 | APerformanceHintManager* APerformanceHintManager::getInstance() { |
| 127 | if (gHintManagerForTesting) return gHintManagerForTesting; |
| 128 | if (gIHintManagerForTesting) { |
| 129 | APerformanceHintManager* manager = create(gIHintManagerForTesting); |
| 130 | gIHintManagerForTesting = nullptr; |
| 131 | return manager; |
| 132 | } |
| 133 | static APerformanceHintManager* instance = create(nullptr); |
| 134 | return instance; |
| 135 | } |
| 136 | |
| 137 | APerformanceHintManager* APerformanceHintManager::create(sp<IHintManager> manager) { |
| 138 | if (!manager) { |
| 139 | manager = interface_cast<IHintManager>( |
| 140 | defaultServiceManager()->checkService(String16("performance_hint"))); |
| 141 | } |
| 142 | if (manager == nullptr) { |
| 143 | ALOGE("%s: PerformanceHint service is not ready ", __FUNCTION__); |
| 144 | return nullptr; |
| 145 | } |
| 146 | int64_t preferredRateNanos = -1L; |
| 147 | binder::Status ret = manager->getHintSessionPreferredRate(&preferredRateNanos); |
| 148 | if (!ret.isOk()) { |
| 149 | ALOGE("%s: PerformanceHint cannot get preferred rate. %s", __FUNCTION__, |
| 150 | ret.exceptionMessage().c_str()); |
| 151 | return nullptr; |
| 152 | } |
| 153 | if (preferredRateNanos <= 0) { |
Bo Liu | d6a0960 | 2021-07-26 14:48:41 -0400 | [diff] [blame] | 154 | preferredRateNanos = -1L; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 155 | } |
| 156 | return new APerformanceHintManager(std::move(manager), preferredRateNanos); |
| 157 | } |
| 158 | |
| 159 | APerformanceHintSession* APerformanceHintManager::createSession( |
| 160 | const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos) { |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 161 | std::vector<int32_t> tids(threadIds, threadIds + size); |
| 162 | sp<IHintSession> session; |
| 163 | binder::Status ret = |
Bo Liu | 9acc558 | 2022-02-17 16:47:32 -0500 | [diff] [blame] | 164 | mHintManager->createHintSession(mToken, tids, initialTargetWorkDurationNanos, &session); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 165 | if (!ret.isOk() || !session) { |
| 166 | return nullptr; |
| 167 | } |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 168 | auto out = new APerformanceHintSession(mHintManager, std::move(session), mPreferredRateNanos, |
| 169 | initialTargetWorkDurationNanos); |
| 170 | out->traceThreads(tids); |
| 171 | out->traceTargetDuration(initialTargetWorkDurationNanos); |
| 172 | out->tracePowerEfficient(false); |
| 173 | return out; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int64_t APerformanceHintManager::getPreferredRateNanos() const { |
| 177 | return mPreferredRateNanos; |
| 178 | } |
| 179 | |
| 180 | // ===================================== APerformanceHintSession implementation |
| 181 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 182 | APerformanceHintSession::APerformanceHintSession(sp<IHintManager> hintManager, |
| 183 | sp<IHintSession> session, |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 184 | int64_t preferredRateNanos, |
| 185 | int64_t targetDurationNanos) |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 186 | : mHintManager(hintManager), |
| 187 | mHintSession(std::move(session)), |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 188 | mPreferredRateNanos(preferredRateNanos), |
| 189 | mTargetDurationNanos(targetDurationNanos), |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 190 | mFirstTargetMetTimestamp(0), |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 191 | mLastTargetMetTimestamp(0) { |
| 192 | const std::vector<AidlSessionHint> sessionHintRange{ndk::enum_range<AidlSessionHint>().begin(), |
| 193 | ndk::enum_range<AidlSessionHint>().end()}; |
| 194 | |
| 195 | mLastHintSentTimestamp = std::vector<int64_t>(sessionHintRange.size(), 0); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 196 | mSessionName = android::base::StringPrintf("ADPF Session %" PRId32, ++sIDCounter); |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 197 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 198 | |
| 199 | APerformanceHintSession::~APerformanceHintSession() { |
| 200 | binder::Status ret = mHintSession->close(); |
| 201 | if (!ret.isOk()) { |
| 202 | ALOGE("%s: HintSession close failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | int APerformanceHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) { |
| 207 | if (targetDurationNanos <= 0) { |
| 208 | ALOGE("%s: targetDurationNanos must be positive", __FUNCTION__); |
| 209 | return EINVAL; |
| 210 | } |
| 211 | binder::Status ret = mHintSession->updateTargetWorkDuration(targetDurationNanos); |
| 212 | if (!ret.isOk()) { |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 213 | ALOGE("%s: HintSession updateTargetWorkDuration failed: %s", __FUNCTION__, |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 214 | ret.exceptionMessage().c_str()); |
| 215 | return EPIPE; |
| 216 | } |
| 217 | mTargetDurationNanos = targetDurationNanos; |
| 218 | /** |
| 219 | * Most of the workload is target_duration dependent, so now clear the cached samples |
| 220 | * as they are most likely obsolete. |
| 221 | */ |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 222 | mActualWorkDurations.clear(); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 223 | traceBatchSize(0); |
| 224 | traceTargetDuration(targetDurationNanos); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 225 | mFirstTargetMetTimestamp = 0; |
| 226 | mLastTargetMetTimestamp = 0; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int APerformanceHintSession::reportActualWorkDuration(int64_t actualDurationNanos) { |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 231 | WorkDuration workDuration(0, actualDurationNanos, actualDurationNanos, 0); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 232 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 233 | return reportActualWorkDurationInternal(&workDuration); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Steven Moreland | 42a8cce | 2023-03-03 23:31:17 +0000 | [diff] [blame] | 236 | int APerformanceHintSession::sendHint(SessionHint hint) { |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 237 | if (hint < 0 || hint >= static_cast<int32_t>(mLastHintSentTimestamp.size())) { |
| 238 | ALOGE("%s: invalid session hint %d", __FUNCTION__, hint); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 239 | return EINVAL; |
| 240 | } |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 241 | int64_t now = elapsedRealtimeNano(); |
| 242 | |
| 243 | // Limit sendHint to a pre-detemined rate for safety |
| 244 | if (now < (mLastHintSentTimestamp[hint] + SEND_HINT_TIMEOUT)) { |
| 245 | return 0; |
| 246 | } |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 247 | |
| 248 | binder::Status ret = mHintSession->sendHint(hint); |
| 249 | |
| 250 | if (!ret.isOk()) { |
| 251 | ALOGE("%s: HintSession sendHint failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 252 | return EPIPE; |
| 253 | } |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 254 | mLastHintSentTimestamp[hint] = now; |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 258 | int APerformanceHintSession::setThreads(const int32_t* threadIds, size_t size) { |
| 259 | if (size == 0) { |
| 260 | ALOGE("%s: the list of thread ids must not be empty.", __FUNCTION__); |
| 261 | return EINVAL; |
| 262 | } |
| 263 | std::vector<int32_t> tids(threadIds, threadIds + size); |
| 264 | binder::Status ret = mHintManager->setHintSessionThreads(mHintSession, tids); |
| 265 | if (!ret.isOk()) { |
| 266 | ALOGE("%s: failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
Xiang Wang | bee6f16 | 2023-07-18 17:58:10 -0700 | [diff] [blame] | 267 | if (ret.exceptionCode() == binder::Status::Exception::EX_ILLEGAL_ARGUMENT) { |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 268 | return EINVAL; |
Xiang Wang | bee6f16 | 2023-07-18 17:58:10 -0700 | [diff] [blame] | 269 | } else if (ret.exceptionCode() == binder::Status::Exception::EX_SECURITY) { |
| 270 | return EPERM; |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 271 | } |
| 272 | return EPIPE; |
| 273 | } |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 274 | |
| 275 | traceThreads(tids); |
| 276 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | int APerformanceHintSession::getThreadIds(int32_t* const threadIds, size_t* size) { |
| 281 | std::vector<int32_t> tids; |
| 282 | binder::Status ret = mHintManager->getHintSessionThreadIds(mHintSession, &tids); |
| 283 | if (!ret.isOk()) { |
| 284 | ALOGE("%s: failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 285 | return EPIPE; |
| 286 | } |
| 287 | |
| 288 | // When threadIds is nullptr, this is the first call to determine the size |
| 289 | // of the thread ids list. |
| 290 | if (threadIds == nullptr) { |
| 291 | *size = tids.size(); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | // Second call to return the actual list of thread ids. |
| 296 | *size = tids.size(); |
| 297 | for (size_t i = 0; i < *size; ++i) { |
| 298 | threadIds[i] = tids[i]; |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 303 | int APerformanceHintSession::setPreferPowerEfficiency(bool enabled) { |
| 304 | binder::Status ret = |
| 305 | mHintSession->setMode(static_cast<int32_t>(AidlSessionMode::POWER_EFFICIENCY), enabled); |
| 306 | |
| 307 | if (!ret.isOk()) { |
| 308 | ALOGE("%s: HintSession setPreferPowerEfficiency failed: %s", __FUNCTION__, |
| 309 | ret.exceptionMessage().c_str()); |
| 310 | return EPIPE; |
| 311 | } |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 312 | tracePowerEfficient(enabled); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 313 | return OK; |
| 314 | } |
| 315 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 316 | int APerformanceHintSession::reportActualWorkDuration(AWorkDuration* aWorkDuration) { |
| 317 | WorkDuration* workDuration = static_cast<WorkDuration*>(aWorkDuration); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 318 | return reportActualWorkDurationInternal(workDuration); |
| 319 | } |
| 320 | |
| 321 | int APerformanceHintSession::reportActualWorkDurationInternal(WorkDuration* workDuration) { |
| 322 | int64_t actualTotalDurationNanos = workDuration->actualTotalDurationNanos; |
| 323 | int64_t now = uptimeNanos(); |
| 324 | workDuration->timestampNanos = now; |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 325 | traceActualDuration(workDuration->actualTotalDurationNanos); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 326 | mActualWorkDurations.push_back(std::move(*workDuration)); |
| 327 | |
| 328 | if (actualTotalDurationNanos >= mTargetDurationNanos) { |
| 329 | // Reset timestamps if we are equal or over the target. |
| 330 | mFirstTargetMetTimestamp = 0; |
| 331 | } else { |
| 332 | // Set mFirstTargetMetTimestamp for first time meeting target. |
| 333 | if (!mFirstTargetMetTimestamp || !mLastTargetMetTimestamp || |
| 334 | (now - mLastTargetMetTimestamp > 2 * mPreferredRateNanos)) { |
| 335 | mFirstTargetMetTimestamp = now; |
| 336 | } |
| 337 | /** |
| 338 | * Rate limit the change if the update is over mPreferredRateNanos since first |
| 339 | * meeting target and less than mPreferredRateNanos since last meeting target. |
| 340 | */ |
| 341 | if (now - mFirstTargetMetTimestamp > mPreferredRateNanos && |
| 342 | now - mLastTargetMetTimestamp <= mPreferredRateNanos) { |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 343 | traceBatchSize(mActualWorkDurations.size()); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | mLastTargetMetTimestamp = now; |
| 347 | } |
| 348 | |
| 349 | binder::Status ret = mHintSession->reportActualWorkDuration2(mActualWorkDurations); |
| 350 | if (!ret.isOk()) { |
| 351 | ALOGE("%s: HintSession reportActualWorkDuration failed: %s", __FUNCTION__, |
| 352 | ret.exceptionMessage().c_str()); |
| 353 | mFirstTargetMetTimestamp = 0; |
| 354 | mLastTargetMetTimestamp = 0; |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 355 | traceBatchSize(mActualWorkDurations.size()); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 356 | return ret.exceptionCode() == binder::Status::EX_ILLEGAL_ARGUMENT ? EINVAL : EPIPE; |
| 357 | } |
| 358 | mActualWorkDurations.clear(); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 359 | traceBatchSize(0); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 360 | |
| 361 | return 0; |
| 362 | } |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 363 | // ===================================== Tracing helpers |
| 364 | |
| 365 | void APerformanceHintSession::traceThreads(std::vector<int32_t>& tids) { |
| 366 | std::set<int32_t> tidSet{tids.begin(), tids.end()}; |
| 367 | |
| 368 | // Disable old TID tracing |
| 369 | for (int32_t tid : mLastThreadIDs) { |
| 370 | if (!tidSet.count(tid)) { |
| 371 | std::string traceName = |
| 372 | android::base::StringPrintf("%s TID: %" PRId32, mSessionName.c_str(), tid); |
| 373 | ATrace_setCounter(traceName.c_str(), 0); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // Add new TID tracing |
| 378 | for (int32_t tid : tids) { |
| 379 | std::string traceName = |
| 380 | android::base::StringPrintf("%s TID: %" PRId32, mSessionName.c_str(), tid); |
| 381 | ATrace_setCounter(traceName.c_str(), 1); |
| 382 | } |
| 383 | |
| 384 | mLastThreadIDs = std::move(tids); |
| 385 | } |
| 386 | |
| 387 | void APerformanceHintSession::tracePowerEfficient(bool powerEfficient) { |
| 388 | ATrace_setCounter((mSessionName + " power efficiency mode").c_str(), powerEfficient); |
| 389 | } |
| 390 | |
| 391 | void APerformanceHintSession::traceActualDuration(int64_t actualDuration) { |
| 392 | ATrace_setCounter((mSessionName + " actual duration").c_str(), actualDuration); |
| 393 | } |
| 394 | |
| 395 | void APerformanceHintSession::traceBatchSize(size_t batchSize) { |
| 396 | std::string traceName = StringPrintf("%s batch size", mSessionName.c_str()); |
| 397 | ATrace_setCounter((mSessionName + " batch size").c_str(), batchSize); |
| 398 | } |
| 399 | |
| 400 | void APerformanceHintSession::traceTargetDuration(int64_t targetDuration) { |
| 401 | ATrace_setCounter((mSessionName + " target duration").c_str(), targetDuration); |
| 402 | } |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 403 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 404 | // ===================================== C API |
| 405 | APerformanceHintManager* APerformanceHint_getManager() { |
| 406 | return APerformanceHintManager::getInstance(); |
| 407 | } |
| 408 | |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 409 | #define VALIDATE_PTR(ptr) \ |
| 410 | LOG_ALWAYS_FATAL_IF(ptr == nullptr, "%s: " #ptr " is nullptr", __FUNCTION__); |
| 411 | |
| 412 | #define VALIDATE_INT(value, cmp) \ |
| 413 | if (!(value cmp)) { \ |
| 414 | ALOGE("%s: Invalid value. Check failed: (" #value " " #cmp ") with value: %" PRIi64, \ |
| 415 | __FUNCTION__, value); \ |
| 416 | return EINVAL; \ |
| 417 | } |
| 418 | |
| 419 | #define WARN_INT(value, cmp) \ |
| 420 | if (!(value cmp)) { \ |
| 421 | ALOGE("%s: Invalid value. Check failed: (" #value " " #cmp ") with value: %" PRIi64, \ |
| 422 | __FUNCTION__, value); \ |
| 423 | } |
| 424 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 425 | APerformanceHintSession* APerformanceHint_createSession(APerformanceHintManager* manager, |
| 426 | const int32_t* threadIds, size_t size, |
| 427 | int64_t initialTargetWorkDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 428 | VALIDATE_PTR(manager) |
| 429 | VALIDATE_PTR(threadIds) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 430 | return manager->createSession(threadIds, size, initialTargetWorkDurationNanos); |
| 431 | } |
| 432 | |
| 433 | int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 434 | VALIDATE_PTR(manager) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 435 | return manager->getPreferredRateNanos(); |
| 436 | } |
| 437 | |
| 438 | int APerformanceHint_updateTargetWorkDuration(APerformanceHintSession* session, |
| 439 | int64_t targetDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 440 | VALIDATE_PTR(session) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 441 | return session->updateTargetWorkDuration(targetDurationNanos); |
| 442 | } |
| 443 | |
| 444 | int APerformanceHint_reportActualWorkDuration(APerformanceHintSession* session, |
| 445 | int64_t actualDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 446 | VALIDATE_PTR(session) |
| 447 | VALIDATE_INT(actualDurationNanos, > 0) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 448 | return session->reportActualWorkDuration(actualDurationNanos); |
| 449 | } |
| 450 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 451 | void APerformanceHint_closeSession(APerformanceHintSession* session) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 452 | VALIDATE_PTR(session) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 453 | delete session; |
| 454 | } |
| 455 | |
Steven Moreland | 42a8cce | 2023-03-03 23:31:17 +0000 | [diff] [blame] | 456 | int APerformanceHint_sendHint(void* session, SessionHint hint) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 457 | VALIDATE_PTR(session) |
Matt Buckley | 61726a3 | 2022-12-06 23:44:45 +0000 | [diff] [blame] | 458 | return reinterpret_cast<APerformanceHintSession*>(session)->sendHint(hint); |
| 459 | } |
| 460 | |
Peiyong Lin | 7ed6de3 | 2023-01-26 00:52:54 +0000 | [diff] [blame] | 461 | int APerformanceHint_setThreads(APerformanceHintSession* session, const pid_t* threadIds, |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 462 | size_t size) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 463 | VALIDATE_PTR(session) |
| 464 | VALIDATE_PTR(threadIds) |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 465 | return session->setThreads(threadIds, size); |
| 466 | } |
| 467 | |
| 468 | int APerformanceHint_getThreadIds(void* aPerformanceHintSession, int32_t* const threadIds, |
| 469 | size_t* const size) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 470 | VALIDATE_PTR(aPerformanceHintSession) |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 471 | return static_cast<APerformanceHintSession*>(aPerformanceHintSession) |
| 472 | ->getThreadIds(threadIds, size); |
| 473 | } |
| 474 | |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 475 | int APerformanceHint_setPreferPowerEfficiency(APerformanceHintSession* session, bool enabled) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 476 | VALIDATE_PTR(session) |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 477 | return session->setPreferPowerEfficiency(enabled); |
| 478 | } |
| 479 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 480 | int APerformanceHint_reportActualWorkDuration2(APerformanceHintSession* session, |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 481 | AWorkDuration* workDurationPtr) { |
| 482 | VALIDATE_PTR(session) |
| 483 | VALIDATE_PTR(workDurationPtr) |
| 484 | WorkDuration& workDuration = *static_cast<WorkDuration*>(workDurationPtr); |
| 485 | VALIDATE_INT(workDuration.workPeriodStartTimestampNanos, > 0) |
| 486 | VALIDATE_INT(workDuration.actualTotalDurationNanos, > 0) |
| 487 | VALIDATE_INT(workDuration.actualCpuDurationNanos, > 0) |
| 488 | VALIDATE_INT(workDuration.actualGpuDurationNanos, >= 0) |
| 489 | return session->reportActualWorkDuration(workDurationPtr); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | AWorkDuration* AWorkDuration_create() { |
| 493 | WorkDuration* workDuration = new WorkDuration(); |
| 494 | return static_cast<AWorkDuration*>(workDuration); |
| 495 | } |
| 496 | |
| 497 | void AWorkDuration_release(AWorkDuration* aWorkDuration) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 498 | VALIDATE_PTR(aWorkDuration) |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 499 | delete aWorkDuration; |
| 500 | } |
| 501 | |
| 502 | void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* aWorkDuration, |
| 503 | int64_t workPeriodStartTimestampNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 504 | VALIDATE_PTR(aWorkDuration) |
| 505 | WARN_INT(workPeriodStartTimestampNanos, > 0) |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 506 | static_cast<WorkDuration*>(aWorkDuration)->workPeriodStartTimestampNanos = |
| 507 | workPeriodStartTimestampNanos; |
| 508 | } |
| 509 | |
| 510 | void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* aWorkDuration, |
| 511 | int64_t actualTotalDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 512 | VALIDATE_PTR(aWorkDuration) |
| 513 | WARN_INT(actualTotalDurationNanos, > 0) |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 514 | static_cast<WorkDuration*>(aWorkDuration)->actualTotalDurationNanos = actualTotalDurationNanos; |
| 515 | } |
| 516 | |
| 517 | void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* aWorkDuration, |
| 518 | int64_t actualCpuDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 519 | VALIDATE_PTR(aWorkDuration) |
| 520 | WARN_INT(actualCpuDurationNanos, > 0) |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 521 | static_cast<WorkDuration*>(aWorkDuration)->actualCpuDurationNanos = actualCpuDurationNanos; |
| 522 | } |
| 523 | |
| 524 | void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* aWorkDuration, |
| 525 | int64_t actualGpuDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 526 | VALIDATE_PTR(aWorkDuration) |
| 527 | WARN_INT(actualGpuDurationNanos, >= 0) |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 528 | static_cast<WorkDuration*>(aWorkDuration)->actualGpuDurationNanos = actualGpuDurationNanos; |
| 529 | } |
| 530 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 531 | void APerformanceHint_setIHintManagerForTesting(void* iManager) { |
| 532 | delete gHintManagerForTesting; |
| 533 | gHintManagerForTesting = nullptr; |
| 534 | gIHintManagerForTesting = static_cast<IHintManager*>(iManager); |
| 535 | } |