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