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