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, |
Matt Buckley | 3197413 | 2024-05-21 19:54:39 +0000 | [diff] [blame] | 67 | hal::SessionTag tag = hal::SessionTag::APP); |
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 | } |
Xiang Wang | e16ad28 | 2024-06-26 14:31:34 -0700 | [diff] [blame] | 247 | { |
| 248 | std::scoped_lock lock(sHintMutex); |
| 249 | if (mTargetDurationNanos == targetDurationNanos) { |
| 250 | return 0; |
| 251 | } |
| 252 | } |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 253 | ndk::ScopedAStatus ret = mHintSession->updateTargetWorkDuration(targetDurationNanos); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 254 | if (!ret.isOk()) { |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 255 | ALOGE("%s: HintSession updateTargetWorkDuration failed: %s", __FUNCTION__, |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 256 | ret.getMessage()); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 257 | return EPIPE; |
| 258 | } |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 259 | std::scoped_lock lock(sHintMutex); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 260 | mTargetDurationNanos = targetDurationNanos; |
| 261 | /** |
| 262 | * Most of the workload is target_duration dependent, so now clear the cached samples |
| 263 | * as they are most likely obsolete. |
| 264 | */ |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 265 | mActualWorkDurations.clear(); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 266 | traceBatchSize(0); |
| 267 | traceTargetDuration(targetDurationNanos); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 268 | mFirstTargetMetTimestamp = 0; |
| 269 | mLastTargetMetTimestamp = 0; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int APerformanceHintSession::reportActualWorkDuration(int64_t actualDurationNanos) { |
Matt Buckley | 7c2de58 | 2024-04-05 08:41:35 +0000 | [diff] [blame] | 274 | hal::WorkDuration workDuration{.durationNanos = actualDurationNanos, |
| 275 | .workPeriodStartTimestampNanos = 0, |
| 276 | .cpuDurationNanos = actualDurationNanos, |
| 277 | .gpuDurationNanos = 0}; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 278 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 279 | return reportActualWorkDurationInternal(static_cast<AWorkDuration*>(&workDuration)); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Steven Moreland | 42a8cce | 2023-03-03 23:31:17 +0000 | [diff] [blame] | 282 | int APerformanceHintSession::sendHint(SessionHint hint) { |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 283 | std::scoped_lock lock(sHintMutex); |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 284 | if (hint < 0 || hint >= static_cast<int32_t>(mLastHintSentTimestamp.size())) { |
| 285 | ALOGE("%s: invalid session hint %d", __FUNCTION__, hint); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 286 | return EINVAL; |
| 287 | } |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 288 | int64_t now = uptimeNanos(); |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 289 | |
| 290 | // Limit sendHint to a pre-detemined rate for safety |
| 291 | if (now < (mLastHintSentTimestamp[hint] + SEND_HINT_TIMEOUT)) { |
| 292 | return 0; |
| 293 | } |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 294 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 295 | ndk::ScopedAStatus ret = mHintSession->sendHint(hint); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 296 | |
| 297 | if (!ret.isOk()) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 298 | ALOGE("%s: HintSession sendHint failed: %s", __FUNCTION__, ret.getMessage()); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 299 | return EPIPE; |
| 300 | } |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 301 | mLastHintSentTimestamp[hint] = now; |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 305 | int APerformanceHintSession::setThreads(const int32_t* threadIds, size_t size) { |
| 306 | if (size == 0) { |
| 307 | ALOGE("%s: the list of thread ids must not be empty.", __FUNCTION__); |
| 308 | return EINVAL; |
| 309 | } |
| 310 | std::vector<int32_t> tids(threadIds, threadIds + size); |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 311 | ndk::ScopedAStatus ret = mHintManager->setHintSessionThreads(mHintSession, tids); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 312 | if (!ret.isOk()) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 313 | ALOGE("%s: failed: %s", __FUNCTION__, ret.getMessage()); |
| 314 | if (ret.getExceptionCode() == EX_ILLEGAL_ARGUMENT) { |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 315 | return EINVAL; |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 316 | } else if (ret.getExceptionCode() == EX_SECURITY) { |
Xiang Wang | bee6f16 | 2023-07-18 17:58:10 -0700 | [diff] [blame] | 317 | return EPERM; |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 318 | } |
| 319 | return EPIPE; |
| 320 | } |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 321 | |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 322 | std::scoped_lock lock(sHintMutex); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 323 | traceThreads(tids); |
| 324 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | int APerformanceHintSession::getThreadIds(int32_t* const threadIds, size_t* size) { |
| 329 | std::vector<int32_t> tids; |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 330 | ndk::ScopedAStatus ret = mHintManager->getHintSessionThreadIds(mHintSession, &tids); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 331 | if (!ret.isOk()) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 332 | ALOGE("%s: failed: %s", __FUNCTION__, ret.getMessage()); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 333 | return EPIPE; |
| 334 | } |
| 335 | |
| 336 | // When threadIds is nullptr, this is the first call to determine the size |
| 337 | // of the thread ids list. |
| 338 | if (threadIds == nullptr) { |
| 339 | *size = tids.size(); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | // Second call to return the actual list of thread ids. |
| 344 | *size = tids.size(); |
| 345 | for (size_t i = 0; i < *size; ++i) { |
| 346 | threadIds[i] = tids[i]; |
| 347 | } |
| 348 | return 0; |
| 349 | } |
| 350 | |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 351 | int APerformanceHintSession::setPreferPowerEfficiency(bool enabled) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 352 | ndk::ScopedAStatus ret = |
Matt Buckley | 7c2de58 | 2024-04-05 08:41:35 +0000 | [diff] [blame] | 353 | mHintSession->setMode(static_cast<int32_t>(hal::SessionMode::POWER_EFFICIENCY), |
| 354 | enabled); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 355 | |
| 356 | if (!ret.isOk()) { |
| 357 | ALOGE("%s: HintSession setPreferPowerEfficiency failed: %s", __FUNCTION__, |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 358 | ret.getMessage()); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 359 | return EPIPE; |
| 360 | } |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 361 | std::scoped_lock lock(sHintMutex); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 362 | tracePowerEfficient(enabled); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 363 | return OK; |
| 364 | } |
| 365 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 366 | int APerformanceHintSession::reportActualWorkDuration(AWorkDuration* workDuration) { |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 367 | return reportActualWorkDurationInternal(workDuration); |
| 368 | } |
| 369 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 370 | int APerformanceHintSession::reportActualWorkDurationInternal(AWorkDuration* workDuration) { |
| 371 | int64_t actualTotalDurationNanos = workDuration->durationNanos; |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 372 | int64_t now = uptimeNanos(); |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 373 | workDuration->timeStampNanos = now; |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 374 | std::scoped_lock lock(sHintMutex); |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 375 | traceActualDuration(workDuration->durationNanos); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 376 | mActualWorkDurations.push_back(std::move(*workDuration)); |
| 377 | |
| 378 | if (actualTotalDurationNanos >= mTargetDurationNanos) { |
| 379 | // Reset timestamps if we are equal or over the target. |
| 380 | mFirstTargetMetTimestamp = 0; |
| 381 | } else { |
| 382 | // Set mFirstTargetMetTimestamp for first time meeting target. |
| 383 | if (!mFirstTargetMetTimestamp || !mLastTargetMetTimestamp || |
| 384 | (now - mLastTargetMetTimestamp > 2 * mPreferredRateNanos)) { |
| 385 | mFirstTargetMetTimestamp = now; |
| 386 | } |
| 387 | /** |
| 388 | * Rate limit the change if the update is over mPreferredRateNanos since first |
| 389 | * meeting target and less than mPreferredRateNanos since last meeting target. |
| 390 | */ |
| 391 | if (now - mFirstTargetMetTimestamp > mPreferredRateNanos && |
| 392 | now - mLastTargetMetTimestamp <= mPreferredRateNanos) { |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 393 | traceBatchSize(mActualWorkDurations.size()); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | mLastTargetMetTimestamp = now; |
| 397 | } |
| 398 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 399 | ndk::ScopedAStatus ret = mHintSession->reportActualWorkDuration2(mActualWorkDurations); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 400 | if (!ret.isOk()) { |
| 401 | ALOGE("%s: HintSession reportActualWorkDuration failed: %s", __FUNCTION__, |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 402 | ret.getMessage()); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 403 | mFirstTargetMetTimestamp = 0; |
| 404 | mLastTargetMetTimestamp = 0; |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 405 | traceBatchSize(mActualWorkDurations.size()); |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 406 | return ret.getExceptionCode() == EX_ILLEGAL_ARGUMENT ? EINVAL : EPIPE; |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 407 | } |
| 408 | mActualWorkDurations.clear(); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 409 | traceBatchSize(0); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 413 | // ===================================== Tracing helpers |
| 414 | |
| 415 | void APerformanceHintSession::traceThreads(std::vector<int32_t>& tids) { |
| 416 | std::set<int32_t> tidSet{tids.begin(), tids.end()}; |
| 417 | |
| 418 | // Disable old TID tracing |
| 419 | for (int32_t tid : mLastThreadIDs) { |
| 420 | if (!tidSet.count(tid)) { |
| 421 | std::string traceName = |
| 422 | android::base::StringPrintf("%s TID: %" PRId32, mSessionName.c_str(), tid); |
| 423 | ATrace_setCounter(traceName.c_str(), 0); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // Add new TID tracing |
| 428 | for (int32_t tid : tids) { |
| 429 | std::string traceName = |
| 430 | android::base::StringPrintf("%s TID: %" PRId32, mSessionName.c_str(), tid); |
| 431 | ATrace_setCounter(traceName.c_str(), 1); |
| 432 | } |
| 433 | |
| 434 | mLastThreadIDs = std::move(tids); |
| 435 | } |
| 436 | |
| 437 | void APerformanceHintSession::tracePowerEfficient(bool powerEfficient) { |
| 438 | ATrace_setCounter((mSessionName + " power efficiency mode").c_str(), powerEfficient); |
| 439 | } |
| 440 | |
| 441 | void APerformanceHintSession::traceActualDuration(int64_t actualDuration) { |
| 442 | ATrace_setCounter((mSessionName + " actual duration").c_str(), actualDuration); |
| 443 | } |
| 444 | |
| 445 | void APerformanceHintSession::traceBatchSize(size_t batchSize) { |
| 446 | std::string traceName = StringPrintf("%s batch size", mSessionName.c_str()); |
| 447 | ATrace_setCounter((mSessionName + " batch size").c_str(), batchSize); |
| 448 | } |
| 449 | |
| 450 | void APerformanceHintSession::traceTargetDuration(int64_t targetDuration) { |
| 451 | ATrace_setCounter((mSessionName + " target duration").c_str(), targetDuration); |
| 452 | } |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 453 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 454 | // ===================================== C API |
| 455 | APerformanceHintManager* APerformanceHint_getManager() { |
| 456 | return APerformanceHintManager::getInstance(); |
| 457 | } |
| 458 | |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 459 | #define VALIDATE_PTR(ptr) \ |
| 460 | LOG_ALWAYS_FATAL_IF(ptr == nullptr, "%s: " #ptr " is nullptr", __FUNCTION__); |
| 461 | |
| 462 | #define VALIDATE_INT(value, cmp) \ |
| 463 | if (!(value cmp)) { \ |
| 464 | ALOGE("%s: Invalid value. Check failed: (" #value " " #cmp ") with value: %" PRIi64, \ |
| 465 | __FUNCTION__, value); \ |
| 466 | return EINVAL; \ |
| 467 | } |
| 468 | |
| 469 | #define WARN_INT(value, cmp) \ |
| 470 | if (!(value cmp)) { \ |
| 471 | ALOGE("%s: Invalid value. Check failed: (" #value " " #cmp ") with value: %" PRIi64, \ |
| 472 | __FUNCTION__, value); \ |
| 473 | } |
| 474 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 475 | APerformanceHintSession* APerformanceHint_createSession(APerformanceHintManager* manager, |
| 476 | const int32_t* threadIds, size_t size, |
| 477 | int64_t initialTargetWorkDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 478 | VALIDATE_PTR(manager) |
| 479 | VALIDATE_PTR(threadIds) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 480 | return manager->createSession(threadIds, size, initialTargetWorkDurationNanos); |
| 481 | } |
| 482 | |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 483 | APerformanceHintSession* APerformanceHint_createSessionInternal( |
| 484 | APerformanceHintManager* manager, const int32_t* threadIds, size_t size, |
| 485 | int64_t initialTargetWorkDurationNanos, SessionTag tag) { |
| 486 | VALIDATE_PTR(manager) |
| 487 | VALIDATE_PTR(threadIds) |
| 488 | return manager->createSession(threadIds, size, initialTargetWorkDurationNanos, |
| 489 | static_cast<hal::SessionTag>(tag)); |
| 490 | } |
| 491 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 492 | int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 493 | VALIDATE_PTR(manager) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 494 | return manager->getPreferredRateNanos(); |
| 495 | } |
| 496 | |
| 497 | int APerformanceHint_updateTargetWorkDuration(APerformanceHintSession* session, |
| 498 | int64_t targetDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 499 | VALIDATE_PTR(session) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 500 | return session->updateTargetWorkDuration(targetDurationNanos); |
| 501 | } |
| 502 | |
| 503 | int APerformanceHint_reportActualWorkDuration(APerformanceHintSession* session, |
| 504 | int64_t actualDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 505 | VALIDATE_PTR(session) |
| 506 | VALIDATE_INT(actualDurationNanos, > 0) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 507 | return session->reportActualWorkDuration(actualDurationNanos); |
| 508 | } |
| 509 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 510 | void APerformanceHint_closeSession(APerformanceHintSession* session) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 511 | VALIDATE_PTR(session) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 512 | delete session; |
| 513 | } |
| 514 | |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 515 | int APerformanceHint_sendHint(APerformanceHintSession* session, SessionHint hint) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 516 | VALIDATE_PTR(session) |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 517 | return session->sendHint(hint); |
Matt Buckley | 61726a3 | 2022-12-06 23:44:45 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Peiyong Lin | 7ed6de3 | 2023-01-26 00:52:54 +0000 | [diff] [blame] | 520 | int APerformanceHint_setThreads(APerformanceHintSession* session, const pid_t* threadIds, |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 521 | size_t size) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 522 | VALIDATE_PTR(session) |
| 523 | VALIDATE_PTR(threadIds) |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 524 | return session->setThreads(threadIds, size); |
| 525 | } |
| 526 | |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 527 | int APerformanceHint_getThreadIds(APerformanceHintSession* session, int32_t* const threadIds, |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 528 | size_t* const size) { |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 529 | VALIDATE_PTR(session) |
| 530 | return session->getThreadIds(threadIds, size); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 533 | int APerformanceHint_setPreferPowerEfficiency(APerformanceHintSession* session, bool enabled) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 534 | VALIDATE_PTR(session) |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 535 | return session->setPreferPowerEfficiency(enabled); |
| 536 | } |
| 537 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 538 | int APerformanceHint_reportActualWorkDuration2(APerformanceHintSession* session, |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 539 | AWorkDuration* workDurationPtr) { |
| 540 | VALIDATE_PTR(session) |
| 541 | VALIDATE_PTR(workDurationPtr) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 542 | VALIDATE_INT(workDurationPtr->durationNanos, > 0) |
| 543 | VALIDATE_INT(workDurationPtr->workPeriodStartTimestampNanos, > 0) |
| 544 | VALIDATE_INT(workDurationPtr->cpuDurationNanos, >= 0) |
| 545 | VALIDATE_INT(workDurationPtr->gpuDurationNanos, >= 0) |
| 546 | VALIDATE_INT(workDurationPtr->gpuDurationNanos + workDurationPtr->cpuDurationNanos, > 0) |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 547 | return session->reportActualWorkDuration(workDurationPtr); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | AWorkDuration* AWorkDuration_create() { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 551 | return new AWorkDuration(); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void AWorkDuration_release(AWorkDuration* aWorkDuration) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 555 | VALIDATE_PTR(aWorkDuration) |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 556 | delete aWorkDuration; |
| 557 | } |
| 558 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 559 | void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* aWorkDuration, |
| 560 | int64_t actualTotalDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 561 | VALIDATE_PTR(aWorkDuration) |
| 562 | WARN_INT(actualTotalDurationNanos, > 0) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 563 | aWorkDuration->durationNanos = actualTotalDurationNanos; |
| 564 | } |
| 565 | |
| 566 | void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* aWorkDuration, |
| 567 | int64_t workPeriodStartTimestampNanos) { |
| 568 | VALIDATE_PTR(aWorkDuration) |
| 569 | WARN_INT(workPeriodStartTimestampNanos, > 0) |
| 570 | aWorkDuration->workPeriodStartTimestampNanos = workPeriodStartTimestampNanos; |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* aWorkDuration, |
| 574 | int64_t actualCpuDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 575 | VALIDATE_PTR(aWorkDuration) |
Matt Buckley | a115b12 | 2024-01-31 20:57:49 +0000 | [diff] [blame] | 576 | WARN_INT(actualCpuDurationNanos, >= 0) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 577 | aWorkDuration->cpuDurationNanos = actualCpuDurationNanos; |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* aWorkDuration, |
| 581 | int64_t actualGpuDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 582 | VALIDATE_PTR(aWorkDuration) |
| 583 | WARN_INT(actualGpuDurationNanos, >= 0) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 584 | aWorkDuration->gpuDurationNanos = actualGpuDurationNanos; |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 587 | void APerformanceHint_setIHintManagerForTesting(void* iManager) { |
| 588 | delete gHintManagerForTesting; |
| 589 | gHintManagerForTesting = nullptr; |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 590 | gIHintManagerForTesting = static_cast<std::shared_ptr<IHintManager>*>(iManager); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 591 | } |