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 | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 19 | #include <aidl/android/hardware/power/ChannelConfig.h> |
| 20 | #include <aidl/android/hardware/power/ChannelMessage.h> |
| 21 | #include <aidl/android/hardware/power/SessionConfig.h> |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 22 | #include <aidl/android/hardware/power/SessionHint.h> |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 23 | #include <aidl/android/hardware/power/SessionMode.h> |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 24 | #include <aidl/android/hardware/power/SessionTag.h> |
| 25 | #include <aidl/android/hardware/power/WorkDuration.h> |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 26 | #include <aidl/android/hardware/power/WorkDurationFixedV1.h> |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 27 | #include <aidl/android/os/IHintManager.h> |
| 28 | #include <aidl/android/os/IHintSession.h> |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 30 | #include <android-base/thread_annotations.h> |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 31 | #include <android/binder_manager.h> |
| 32 | #include <android/binder_status.h> |
Bo Liu | 2b739bb | 2021-11-10 19:20:03 -0500 | [diff] [blame] | 33 | #include <android/performance_hint.h> |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 34 | #include <android/trace.h> |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 35 | #include <android_os.h> |
| 36 | #include <fmq/AidlMessageQueue.h> |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 37 | #include <inttypes.h> |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 38 | #include <performance_hint_private.h> |
| 39 | #include <utils/SystemClock.h> |
| 40 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 41 | #include <chrono> |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 42 | #include <set> |
Bo Liu | 2b739bb | 2021-11-10 19:20:03 -0500 | [diff] [blame] | 43 | #include <utility> |
| 44 | #include <vector> |
| 45 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 46 | using namespace android; |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 47 | using namespace aidl::android::os; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 48 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 49 | using namespace std::chrono_literals; |
| 50 | |
Matt Buckley | 7c2de58 | 2024-04-05 08:41:35 +0000 | [diff] [blame] | 51 | // Namespace for AIDL types coming from the PowerHAL |
| 52 | namespace hal = aidl::android::hardware::power; |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 53 | |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 54 | using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; |
| 55 | using HalChannelMessageContents = hal::ChannelMessage::ChannelMessageContents; |
| 56 | using HalMessageQueue = ::android::AidlMessageQueue<hal::ChannelMessage, SynchronizedReadWrite>; |
| 57 | using HalFlagQueue = ::android::AidlMessageQueue<int8_t, SynchronizedReadWrite>; |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 58 | using android::base::StringPrintf; |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 59 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 60 | struct APerformanceHintSession; |
| 61 | |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 62 | constexpr int64_t SEND_HINT_TIMEOUT = std::chrono::nanoseconds(100ms).count(); |
Matt Buckley | 7c2de58 | 2024-04-05 08:41:35 +0000 | [diff] [blame] | 63 | struct AWorkDuration : public hal::WorkDuration {}; |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 64 | |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 65 | // Shared lock for the whole PerformanceHintManager and sessions |
| 66 | static std::mutex sHintMutex = std::mutex{}; |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 67 | class FMQWrapper { |
| 68 | public: |
| 69 | bool isActive(); |
| 70 | bool isSupported(); |
| 71 | bool startChannel(IHintManager* manager); |
| 72 | void stopChannel(IHintManager* manager); |
| 73 | // Number of elements the FMQ can hold |
| 74 | bool reportActualWorkDurations(std::optional<hal::SessionConfig>& config, |
| 75 | hal::WorkDuration* durations, size_t count) REQUIRES(sHintMutex); |
| 76 | bool updateTargetWorkDuration(std::optional<hal::SessionConfig>& config, |
| 77 | int64_t targetDurationNanos) REQUIRES(sHintMutex); |
| 78 | bool sendHint(std::optional<hal::SessionConfig>& config, SessionHint hint) REQUIRES(sHintMutex); |
| 79 | bool setMode(std::optional<hal::SessionConfig>& config, hal::SessionMode, bool enabled) |
| 80 | REQUIRES(sHintMutex); |
| 81 | void setToken(ndk::SpAIBinder& token); |
| 82 | void attemptWake(); |
| 83 | void setUnsupported(); |
| 84 | |
| 85 | private: |
| 86 | template <HalChannelMessageContents::Tag T, bool urgent = false, |
| 87 | class C = HalChannelMessageContents::_at<T>> |
| 88 | bool sendMessages(std::optional<hal::SessionConfig>& config, C* message, size_t count = 1) |
| 89 | REQUIRES(sHintMutex); |
| 90 | template <HalChannelMessageContents::Tag T, class C = HalChannelMessageContents::_at<T>> |
| 91 | void writeBuffer(C* message, hal::SessionConfig& config, size_t count) REQUIRES(sHintMutex); |
| 92 | |
| 93 | bool isActiveLocked() REQUIRES(sHintMutex); |
| 94 | bool updatePersistentTransaction() REQUIRES(sHintMutex); |
| 95 | std::shared_ptr<HalMessageQueue> mQueue GUARDED_BY(sHintMutex) = nullptr; |
| 96 | std::shared_ptr<HalFlagQueue> mFlagQueue GUARDED_BY(sHintMutex) = nullptr; |
| 97 | // android::hardware::EventFlag* mEventFlag GUARDED_BY(sHintMutex) = nullptr; |
| 98 | android::hardware::EventFlag* mEventFlag = nullptr; |
| 99 | int32_t mWriteMask; |
| 100 | ndk::SpAIBinder mToken = nullptr; |
| 101 | // Used to track if operating on the fmq consistently fails |
| 102 | bool mCorrupted = false; |
| 103 | // Used to keep a persistent transaction open with FMQ to reduce latency a bit |
| 104 | size_t mAvailableSlots GUARDED_BY(sHintMutex) = 0; |
| 105 | bool mHalSupported = true; |
| 106 | HalMessageQueue::MemTransaction mFmqTransaction GUARDED_BY(sHintMutex); |
| 107 | }; |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 108 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 109 | struct APerformanceHintManager { |
| 110 | public: |
| 111 | static APerformanceHintManager* getInstance(); |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 112 | APerformanceHintManager(std::shared_ptr<IHintManager>& service, int64_t preferredRateNanos); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 113 | APerformanceHintManager() = delete; |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 114 | ~APerformanceHintManager(); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 115 | |
| 116 | APerformanceHintSession* createSession(const int32_t* threadIds, size_t size, |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 117 | int64_t initialTargetWorkDurationNanos, |
Matt Buckley | 3197413 | 2024-05-21 19:54:39 +0000 | [diff] [blame] | 118 | hal::SessionTag tag = hal::SessionTag::APP); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 119 | int64_t getPreferredRateNanos() const; |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 120 | FMQWrapper& getFMQWrapper(); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 121 | |
| 122 | private: |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 123 | // Necessary to create an empty binder object |
| 124 | static void* tokenStubOnCreate(void*) { |
| 125 | return nullptr; |
| 126 | } |
| 127 | static void tokenStubOnDestroy(void*) {} |
| 128 | static binder_status_t tokenStubOnTransact(AIBinder*, transaction_code_t, const AParcel*, |
| 129 | AParcel*) { |
| 130 | return STATUS_OK; |
| 131 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 132 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 133 | static APerformanceHintManager* create(std::shared_ptr<IHintManager> iHintManager); |
| 134 | |
| 135 | std::shared_ptr<IHintManager> mHintManager; |
| 136 | ndk::SpAIBinder mToken; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 137 | const int64_t mPreferredRateNanos; |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 138 | FMQWrapper mFMQWrapper; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | struct APerformanceHintSession { |
| 142 | public: |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 143 | APerformanceHintSession(std::shared_ptr<IHintManager> hintManager, |
| 144 | std::shared_ptr<IHintSession> session, int64_t preferredRateNanos, |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 145 | int64_t targetDurationNanos, |
| 146 | std::optional<hal::SessionConfig> sessionConfig); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 147 | APerformanceHintSession() = delete; |
| 148 | ~APerformanceHintSession(); |
| 149 | |
| 150 | int updateTargetWorkDuration(int64_t targetDurationNanos); |
| 151 | int reportActualWorkDuration(int64_t actualDurationNanos); |
Steven Moreland | 42a8cce | 2023-03-03 23:31:17 +0000 | [diff] [blame] | 152 | int sendHint(SessionHint hint); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 153 | int setThreads(const int32_t* threadIds, size_t size); |
| 154 | int getThreadIds(int32_t* const threadIds, size_t* size); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 155 | int setPreferPowerEfficiency(bool enabled); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 156 | int reportActualWorkDuration(AWorkDuration* workDuration); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 157 | |
| 158 | private: |
| 159 | friend struct APerformanceHintManager; |
| 160 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 161 | int reportActualWorkDurationInternal(AWorkDuration* workDuration); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 162 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 163 | std::shared_ptr<IHintManager> mHintManager; |
| 164 | std::shared_ptr<IHintSession> mHintSession; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 165 | // HAL preferred update rate |
| 166 | const int64_t mPreferredRateNanos; |
| 167 | // Target duration for choosing update rate |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 168 | int64_t mTargetDurationNanos GUARDED_BY(sHintMutex); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 169 | // First target hit timestamp |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 170 | int64_t mFirstTargetMetTimestamp GUARDED_BY(sHintMutex); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 171 | // Last target hit timestamp |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 172 | int64_t mLastTargetMetTimestamp GUARDED_BY(sHintMutex); |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 173 | // Last hint reported from sendHint indexed by hint value |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 174 | std::vector<int64_t> mLastHintSentTimestamp GUARDED_BY(sHintMutex); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 175 | // Cached samples |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 176 | std::vector<hal::WorkDuration> mActualWorkDurations GUARDED_BY(sHintMutex); |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 177 | std::string mSessionName; |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 178 | static int64_t sIDCounter GUARDED_BY(sHintMutex); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 179 | // The most recent set of thread IDs |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 180 | std::vector<int32_t> mLastThreadIDs GUARDED_BY(sHintMutex); |
| 181 | std::optional<hal::SessionConfig> mSessionConfig GUARDED_BY(sHintMutex); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 182 | // Tracing helpers |
Matt Buckley | 053a1df | 2024-06-07 02:37:59 +0000 | [diff] [blame] | 183 | void traceThreads(std::vector<int32_t>& tids) REQUIRES(sHintMutex); |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 184 | void tracePowerEfficient(bool powerEfficient); |
| 185 | void traceActualDuration(int64_t actualDuration); |
| 186 | void traceBatchSize(size_t batchSize); |
| 187 | void traceTargetDuration(int64_t targetDuration); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 188 | }; |
| 189 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 190 | static std::shared_ptr<IHintManager>* gIHintManagerForTesting = nullptr; |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 191 | static std::shared_ptr<APerformanceHintManager> gHintManagerForTesting = nullptr; |
| 192 | |
| 193 | static std::optional<bool> gForceFMQEnabled = std::nullopt; |
| 194 | |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 195 | // Start above the int32 range so we don't collide with config sessions |
| 196 | int64_t APerformanceHintSession::sIDCounter = INT32_MAX; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 197 | |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 198 | static FMQWrapper& getFMQ() { |
| 199 | return APerformanceHintManager::getInstance()->getFMQWrapper(); |
| 200 | } |
| 201 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 202 | // ===================================== APerformanceHintManager implementation |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 203 | APerformanceHintManager::APerformanceHintManager(std::shared_ptr<IHintManager>& manager, |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 204 | int64_t preferredRateNanos) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 205 | : mHintManager(std::move(manager)), mPreferredRateNanos(preferredRateNanos) { |
| 206 | static AIBinder_Class* tokenBinderClass = |
| 207 | AIBinder_Class_define("phm_token", tokenStubOnCreate, tokenStubOnDestroy, |
| 208 | tokenStubOnTransact); |
| 209 | mToken = ndk::SpAIBinder(AIBinder_new(tokenBinderClass, nullptr)); |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 210 | if (mFMQWrapper.isSupported()) { |
| 211 | mFMQWrapper.setToken(mToken); |
| 212 | mFMQWrapper.startChannel(mHintManager.get()); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | APerformanceHintManager::~APerformanceHintManager() { |
| 217 | mFMQWrapper.stopChannel(mHintManager.get()); |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 218 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 219 | |
| 220 | APerformanceHintManager* APerformanceHintManager::getInstance() { |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 221 | if (gHintManagerForTesting) { |
| 222 | return gHintManagerForTesting.get(); |
| 223 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 224 | if (gIHintManagerForTesting) { |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 225 | gHintManagerForTesting = |
| 226 | std::shared_ptr<APerformanceHintManager>(create(*gIHintManagerForTesting)); |
| 227 | return gHintManagerForTesting.get(); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 228 | } |
| 229 | static APerformanceHintManager* instance = create(nullptr); |
| 230 | return instance; |
| 231 | } |
| 232 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 233 | APerformanceHintManager* APerformanceHintManager::create(std::shared_ptr<IHintManager> manager) { |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 234 | if (!manager) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 235 | manager = IHintManager::fromBinder( |
| 236 | ndk::SpAIBinder(AServiceManager_waitForService("performance_hint"))); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 237 | } |
| 238 | if (manager == nullptr) { |
| 239 | ALOGE("%s: PerformanceHint service is not ready ", __FUNCTION__); |
| 240 | return nullptr; |
| 241 | } |
| 242 | int64_t preferredRateNanos = -1L; |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 243 | ndk::ScopedAStatus ret = manager->getHintSessionPreferredRate(&preferredRateNanos); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 244 | if (!ret.isOk()) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 245 | ALOGE("%s: PerformanceHint cannot get preferred rate. %s", __FUNCTION__, ret.getMessage()); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 246 | return nullptr; |
| 247 | } |
| 248 | if (preferredRateNanos <= 0) { |
Bo Liu | d6a0960 | 2021-07-26 14:48:41 -0400 | [diff] [blame] | 249 | preferredRateNanos = -1L; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 250 | } |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 251 | return new APerformanceHintManager(manager, preferredRateNanos); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | APerformanceHintSession* APerformanceHintManager::createSession( |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 255 | const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos, |
| 256 | hal::SessionTag tag) { |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 257 | std::vector<int32_t> tids(threadIds, threadIds + size); |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 258 | std::shared_ptr<IHintSession> session; |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 259 | ndk::ScopedAStatus ret; |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 260 | hal::SessionConfig sessionConfig{.id = -1}; |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 261 | ret = mHintManager->createHintSessionWithConfig(mToken, tids, initialTargetWorkDurationNanos, |
| 262 | tag, &sessionConfig, &session); |
| 263 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 264 | if (!ret.isOk() || !session) { |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 265 | ALOGE("%s: PerformanceHint cannot create session. %s", __FUNCTION__, ret.getMessage()); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 266 | return nullptr; |
| 267 | } |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 268 | auto out = new APerformanceHintSession(mHintManager, std::move(session), mPreferredRateNanos, |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 269 | initialTargetWorkDurationNanos, |
| 270 | sessionConfig.id == -1 |
| 271 | ? std::nullopt |
| 272 | : std::make_optional<hal::SessionConfig>( |
| 273 | std::move(sessionConfig))); |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 274 | std::scoped_lock lock(sHintMutex); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 275 | out->traceThreads(tids); |
| 276 | out->traceTargetDuration(initialTargetWorkDurationNanos); |
| 277 | out->tracePowerEfficient(false); |
| 278 | return out; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | int64_t APerformanceHintManager::getPreferredRateNanos() const { |
| 282 | return mPreferredRateNanos; |
| 283 | } |
| 284 | |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 285 | FMQWrapper& APerformanceHintManager::getFMQWrapper() { |
| 286 | return mFMQWrapper; |
| 287 | } |
| 288 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 289 | // ===================================== APerformanceHintSession implementation |
| 290 | |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 291 | constexpr int kNumEnums = |
| 292 | ndk::enum_range<hal::SessionHint>().end() - ndk::enum_range<hal::SessionHint>().begin(); |
| 293 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 294 | APerformanceHintSession::APerformanceHintSession(std::shared_ptr<IHintManager> hintManager, |
| 295 | std::shared_ptr<IHintSession> session, |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 296 | int64_t preferredRateNanos, |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 297 | int64_t targetDurationNanos, |
| 298 | std::optional<hal::SessionConfig> sessionConfig) |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 299 | : mHintManager(hintManager), |
| 300 | mHintSession(std::move(session)), |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 301 | mPreferredRateNanos(preferredRateNanos), |
| 302 | mTargetDurationNanos(targetDurationNanos), |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 303 | mFirstTargetMetTimestamp(0), |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 304 | mLastTargetMetTimestamp(0), |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 305 | mLastHintSentTimestamp(std::vector<int64_t>(kNumEnums, 0)), |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 306 | mSessionConfig(sessionConfig) { |
| 307 | if (sessionConfig->id > INT32_MAX) { |
| 308 | ALOGE("Session ID too large, must fit 32-bit integer"); |
| 309 | } |
Matt Buckley | e073c73 | 2024-04-05 22:32:31 +0000 | [diff] [blame] | 310 | int64_t traceId = sessionConfig.has_value() ? sessionConfig->id : ++sIDCounter; |
| 311 | mSessionName = android::base::StringPrintf("ADPF Session %" PRId64, traceId); |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 312 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 313 | |
| 314 | APerformanceHintSession::~APerformanceHintSession() { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 315 | ndk::ScopedAStatus ret = mHintSession->close(); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 316 | if (!ret.isOk()) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 317 | ALOGE("%s: HintSession close failed: %s", __FUNCTION__, ret.getMessage()); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | int APerformanceHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) { |
| 322 | if (targetDurationNanos <= 0) { |
| 323 | ALOGE("%s: targetDurationNanos must be positive", __FUNCTION__); |
| 324 | return EINVAL; |
| 325 | } |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 326 | std::scoped_lock lock(sHintMutex); |
| 327 | if (mTargetDurationNanos == targetDurationNanos) { |
| 328 | return 0; |
| 329 | } |
| 330 | if (!getFMQ().updateTargetWorkDuration(mSessionConfig, targetDurationNanos)) { |
| 331 | ndk::ScopedAStatus ret = mHintSession->updateTargetWorkDuration(targetDurationNanos); |
| 332 | if (!ret.isOk()) { |
| 333 | ALOGE("%s: HintSession updateTargetWorkDuration failed: %s", __FUNCTION__, |
| 334 | ret.getMessage()); |
| 335 | return EPIPE; |
Xiang Wang | e16ad28 | 2024-06-26 14:31:34 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 338 | mTargetDurationNanos = targetDurationNanos; |
| 339 | /** |
| 340 | * Most of the workload is target_duration dependent, so now clear the cached samples |
| 341 | * as they are most likely obsolete. |
| 342 | */ |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 343 | mActualWorkDurations.clear(); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 344 | traceBatchSize(0); |
| 345 | traceTargetDuration(targetDurationNanos); |
Wei Wang | 00feb50 | 2022-10-18 10:56:59 -0700 | [diff] [blame] | 346 | mFirstTargetMetTimestamp = 0; |
| 347 | mLastTargetMetTimestamp = 0; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | int APerformanceHintSession::reportActualWorkDuration(int64_t actualDurationNanos) { |
Matt Buckley | 7c2de58 | 2024-04-05 08:41:35 +0000 | [diff] [blame] | 352 | hal::WorkDuration workDuration{.durationNanos = actualDurationNanos, |
| 353 | .workPeriodStartTimestampNanos = 0, |
| 354 | .cpuDurationNanos = actualDurationNanos, |
| 355 | .gpuDurationNanos = 0}; |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 356 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 357 | return reportActualWorkDurationInternal(static_cast<AWorkDuration*>(&workDuration)); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 358 | } |
| 359 | |
Steven Moreland | 42a8cce | 2023-03-03 23:31:17 +0000 | [diff] [blame] | 360 | int APerformanceHintSession::sendHint(SessionHint hint) { |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 361 | std::scoped_lock lock(sHintMutex); |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 362 | if (hint < 0 || hint >= static_cast<int32_t>(mLastHintSentTimestamp.size())) { |
| 363 | ALOGE("%s: invalid session hint %d", __FUNCTION__, hint); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 364 | return EINVAL; |
| 365 | } |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 366 | int64_t now = uptimeNanos(); |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 367 | |
| 368 | // Limit sendHint to a pre-detemined rate for safety |
| 369 | if (now < (mLastHintSentTimestamp[hint] + SEND_HINT_TIMEOUT)) { |
| 370 | return 0; |
| 371 | } |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 372 | |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 373 | if (!getFMQ().sendHint(mSessionConfig, hint)) { |
| 374 | ndk::ScopedAStatus ret = mHintSession->sendHint(hint); |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 375 | |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 376 | if (!ret.isOk()) { |
| 377 | ALOGE("%s: HintSession sendHint failed: %s", __FUNCTION__, ret.getMessage()); |
| 378 | return EPIPE; |
| 379 | } |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 380 | } |
Matt Buckley | 56093a7 | 2022-11-07 21:50:50 +0000 | [diff] [blame] | 381 | mLastHintSentTimestamp[hint] = now; |
Matt Buckley | 354cc0a | 2022-09-28 20:54:46 +0000 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 385 | int APerformanceHintSession::setThreads(const int32_t* threadIds, size_t size) { |
| 386 | if (size == 0) { |
| 387 | ALOGE("%s: the list of thread ids must not be empty.", __FUNCTION__); |
| 388 | return EINVAL; |
| 389 | } |
| 390 | std::vector<int32_t> tids(threadIds, threadIds + size); |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 391 | ndk::ScopedAStatus ret = mHintManager->setHintSessionThreads(mHintSession, tids); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 392 | if (!ret.isOk()) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 393 | ALOGE("%s: failed: %s", __FUNCTION__, ret.getMessage()); |
| 394 | if (ret.getExceptionCode() == EX_ILLEGAL_ARGUMENT) { |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 395 | return EINVAL; |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 396 | } else if (ret.getExceptionCode() == EX_SECURITY) { |
Xiang Wang | bee6f16 | 2023-07-18 17:58:10 -0700 | [diff] [blame] | 397 | return EPERM; |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 398 | } |
| 399 | return EPIPE; |
| 400 | } |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 401 | |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 402 | std::scoped_lock lock(sHintMutex); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 403 | traceThreads(tids); |
| 404 | |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | int APerformanceHintSession::getThreadIds(int32_t* const threadIds, size_t* size) { |
| 409 | std::vector<int32_t> tids; |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 410 | ndk::ScopedAStatus ret = mHintManager->getHintSessionThreadIds(mHintSession, &tids); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 411 | if (!ret.isOk()) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 412 | ALOGE("%s: failed: %s", __FUNCTION__, ret.getMessage()); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 413 | return EPIPE; |
| 414 | } |
| 415 | |
| 416 | // When threadIds is nullptr, this is the first call to determine the size |
| 417 | // of the thread ids list. |
| 418 | if (threadIds == nullptr) { |
| 419 | *size = tids.size(); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | // Second call to return the actual list of thread ids. |
| 424 | *size = tids.size(); |
| 425 | for (size_t i = 0; i < *size; ++i) { |
| 426 | threadIds[i] = tids[i]; |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 431 | int APerformanceHintSession::setPreferPowerEfficiency(bool enabled) { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 432 | ndk::ScopedAStatus ret = |
Matt Buckley | 7c2de58 | 2024-04-05 08:41:35 +0000 | [diff] [blame] | 433 | mHintSession->setMode(static_cast<int32_t>(hal::SessionMode::POWER_EFFICIENCY), |
| 434 | enabled); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 435 | |
| 436 | if (!ret.isOk()) { |
| 437 | ALOGE("%s: HintSession setPreferPowerEfficiency failed: %s", __FUNCTION__, |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 438 | ret.getMessage()); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 439 | return EPIPE; |
| 440 | } |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 441 | std::scoped_lock lock(sHintMutex); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 442 | tracePowerEfficient(enabled); |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 443 | return OK; |
| 444 | } |
| 445 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 446 | int APerformanceHintSession::reportActualWorkDuration(AWorkDuration* workDuration) { |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 447 | return reportActualWorkDurationInternal(workDuration); |
| 448 | } |
| 449 | |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 450 | int APerformanceHintSession::reportActualWorkDurationInternal(AWorkDuration* workDuration) { |
| 451 | int64_t actualTotalDurationNanos = workDuration->durationNanos; |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 452 | traceActualDuration(workDuration->durationNanos); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 453 | int64_t now = uptimeNanos(); |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 454 | workDuration->timeStampNanos = now; |
Matt Buckley | 1f5b95f | 2024-06-07 02:36:56 +0000 | [diff] [blame] | 455 | std::scoped_lock lock(sHintMutex); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 456 | mActualWorkDurations.push_back(std::move(*workDuration)); |
| 457 | |
| 458 | if (actualTotalDurationNanos >= mTargetDurationNanos) { |
| 459 | // Reset timestamps if we are equal or over the target. |
| 460 | mFirstTargetMetTimestamp = 0; |
| 461 | } else { |
| 462 | // Set mFirstTargetMetTimestamp for first time meeting target. |
| 463 | if (!mFirstTargetMetTimestamp || !mLastTargetMetTimestamp || |
| 464 | (now - mLastTargetMetTimestamp > 2 * mPreferredRateNanos)) { |
| 465 | mFirstTargetMetTimestamp = now; |
| 466 | } |
| 467 | /** |
| 468 | * Rate limit the change if the update is over mPreferredRateNanos since first |
| 469 | * meeting target and less than mPreferredRateNanos since last meeting target. |
| 470 | */ |
| 471 | if (now - mFirstTargetMetTimestamp > mPreferredRateNanos && |
| 472 | now - mLastTargetMetTimestamp <= mPreferredRateNanos) { |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 473 | traceBatchSize(mActualWorkDurations.size()); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | mLastTargetMetTimestamp = now; |
| 477 | } |
| 478 | |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 479 | if (!getFMQ().reportActualWorkDurations(mSessionConfig, mActualWorkDurations.data(), |
| 480 | mActualWorkDurations.size())) { |
| 481 | ndk::ScopedAStatus ret = mHintSession->reportActualWorkDuration2(mActualWorkDurations); |
| 482 | if (!ret.isOk()) { |
| 483 | ALOGE("%s: HintSession reportActualWorkDuration failed: %s", __FUNCTION__, |
| 484 | ret.getMessage()); |
| 485 | mFirstTargetMetTimestamp = 0; |
| 486 | mLastTargetMetTimestamp = 0; |
| 487 | traceBatchSize(mActualWorkDurations.size()); |
| 488 | return ret.getExceptionCode() == EX_ILLEGAL_ARGUMENT ? EINVAL : EPIPE; |
| 489 | } |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 490 | } |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 491 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 492 | mActualWorkDurations.clear(); |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 493 | traceBatchSize(0); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 494 | |
| 495 | return 0; |
| 496 | } |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 497 | |
| 498 | // ===================================== FMQ wrapper implementation |
| 499 | |
| 500 | bool FMQWrapper::isActive() { |
| 501 | std::scoped_lock lock{sHintMutex}; |
| 502 | return isActiveLocked(); |
| 503 | } |
| 504 | |
| 505 | bool FMQWrapper::isActiveLocked() { |
| 506 | return mQueue != nullptr; |
| 507 | } |
| 508 | |
| 509 | void FMQWrapper::setUnsupported() { |
| 510 | mHalSupported = false; |
| 511 | } |
| 512 | |
| 513 | bool FMQWrapper::isSupported() { |
| 514 | if (!mHalSupported) { |
| 515 | return false; |
| 516 | } |
| 517 | // Used for testing |
| 518 | if (gForceFMQEnabled.has_value()) { |
| 519 | return *gForceFMQEnabled; |
| 520 | } |
| 521 | return android::os::adpf_use_fmq_channel_fixed(); |
| 522 | } |
| 523 | |
| 524 | bool FMQWrapper::startChannel(IHintManager* manager) { |
| 525 | if (isSupported() && !isActive()) { |
| 526 | std::optional<hal::ChannelConfig> config; |
| 527 | auto ret = manager->getSessionChannel(mToken, &config); |
| 528 | if (ret.isOk() && config.has_value()) { |
| 529 | std::scoped_lock lock{sHintMutex}; |
| 530 | mQueue = std::make_shared<HalMessageQueue>(config->channelDescriptor, true); |
| 531 | if (config->eventFlagDescriptor.has_value()) { |
| 532 | mFlagQueue = std::make_shared<HalFlagQueue>(*config->eventFlagDescriptor, true); |
| 533 | android::hardware::EventFlag::createEventFlag(mFlagQueue->getEventFlagWord(), |
| 534 | &mEventFlag); |
| 535 | mWriteMask = config->writeFlagBitmask; |
| 536 | } |
| 537 | updatePersistentTransaction(); |
| 538 | } else if (ret.isOk() && !config.has_value()) { |
| 539 | ALOGV("FMQ channel enabled but unsupported."); |
| 540 | setUnsupported(); |
| 541 | } else { |
| 542 | ALOGE("%s: FMQ channel initialization failed: %s", __FUNCTION__, ret.getMessage()); |
| 543 | } |
| 544 | } |
| 545 | return isActive(); |
| 546 | } |
| 547 | |
| 548 | void FMQWrapper::stopChannel(IHintManager* manager) { |
| 549 | { |
| 550 | std::scoped_lock lock{sHintMutex}; |
| 551 | if (!isActiveLocked()) { |
| 552 | return; |
| 553 | } |
| 554 | mFlagQueue = nullptr; |
| 555 | mQueue = nullptr; |
| 556 | } |
| 557 | manager->closeSessionChannel(); |
| 558 | } |
| 559 | |
| 560 | template <HalChannelMessageContents::Tag T, class C> |
| 561 | void FMQWrapper::writeBuffer(C* message, hal::SessionConfig& config, size_t) { |
| 562 | new (mFmqTransaction.getSlot(0)) hal::ChannelMessage{ |
| 563 | .sessionID = static_cast<int32_t>(config.id), |
| 564 | .timeStampNanos = ::android::uptimeNanos(), |
| 565 | .data = HalChannelMessageContents::make<T, C>(std::move(*message)), |
| 566 | }; |
| 567 | } |
| 568 | |
| 569 | template <> |
| 570 | void FMQWrapper::writeBuffer<HalChannelMessageContents::workDuration>(hal::WorkDuration* messages, |
| 571 | hal::SessionConfig& config, |
| 572 | size_t count) { |
| 573 | for (size_t i = 0; i < count; ++i) { |
| 574 | hal::WorkDuration& message = messages[i]; |
| 575 | new (mFmqTransaction.getSlot(i)) hal::ChannelMessage{ |
| 576 | .sessionID = static_cast<int32_t>(config.id), |
| 577 | .timeStampNanos = |
| 578 | (i == count - 1) ? ::android::uptimeNanos() : message.timeStampNanos, |
| 579 | .data = HalChannelMessageContents::make<HalChannelMessageContents::workDuration, |
| 580 | hal::WorkDurationFixedV1>({ |
| 581 | .durationNanos = message.cpuDurationNanos, |
| 582 | .workPeriodStartTimestampNanos = message.workPeriodStartTimestampNanos, |
| 583 | .cpuDurationNanos = message.cpuDurationNanos, |
| 584 | .gpuDurationNanos = message.gpuDurationNanos, |
| 585 | }), |
| 586 | }; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | template <HalChannelMessageContents::Tag T, bool urgent, class C> |
| 591 | bool FMQWrapper::sendMessages(std::optional<hal::SessionConfig>& config, C* message, size_t count) { |
| 592 | if (!isActiveLocked() || !config.has_value() || mCorrupted) { |
| 593 | return false; |
| 594 | } |
| 595 | // If we didn't reserve enough space, try re-creating the transaction |
| 596 | if (count > mAvailableSlots) { |
| 597 | if (!updatePersistentTransaction()) { |
| 598 | return false; |
| 599 | } |
| 600 | // If we actually don't have enough space, give up |
| 601 | if (count > mAvailableSlots) { |
| 602 | return false; |
| 603 | } |
| 604 | } |
| 605 | writeBuffer<T, C>(message, *config, count); |
| 606 | mQueue->commitWrite(count); |
| 607 | mEventFlag->wake(mWriteMask); |
| 608 | // Re-create the persistent transaction after writing |
| 609 | updatePersistentTransaction(); |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | void FMQWrapper::setToken(ndk::SpAIBinder& token) { |
| 614 | mToken = token; |
| 615 | } |
| 616 | |
| 617 | bool FMQWrapper::updatePersistentTransaction() { |
| 618 | mAvailableSlots = mQueue->availableToWrite(); |
| 619 | if (mAvailableSlots > 0 && !mQueue->beginWrite(mAvailableSlots, &mFmqTransaction)) { |
| 620 | ALOGE("ADPF FMQ became corrupted, falling back to binder calls!"); |
| 621 | mCorrupted = true; |
| 622 | return false; |
| 623 | } |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | bool FMQWrapper::reportActualWorkDurations(std::optional<hal::SessionConfig>& config, |
| 628 | hal::WorkDuration* durations, size_t count) { |
| 629 | return sendMessages<HalChannelMessageContents::workDuration>(config, durations, count); |
| 630 | } |
| 631 | |
| 632 | bool FMQWrapper::updateTargetWorkDuration(std::optional<hal::SessionConfig>& config, |
| 633 | int64_t targetDurationNanos) { |
| 634 | return sendMessages<HalChannelMessageContents::targetDuration>(config, &targetDurationNanos); |
| 635 | } |
| 636 | |
| 637 | bool FMQWrapper::sendHint(std::optional<hal::SessionConfig>& config, SessionHint hint) { |
| 638 | return sendMessages<HalChannelMessageContents::hint>(config, |
| 639 | reinterpret_cast<hal::SessionHint*>( |
| 640 | &hint)); |
| 641 | } |
| 642 | |
| 643 | bool FMQWrapper::setMode(std::optional<hal::SessionConfig>& config, hal::SessionMode mode, |
| 644 | bool enabled) { |
| 645 | hal::ChannelMessage::ChannelMessageContents::SessionModeSetter modeObj{.modeInt = mode, |
| 646 | .enabled = enabled}; |
| 647 | return sendMessages<HalChannelMessageContents::mode, true>(config, &modeObj); |
| 648 | } |
| 649 | |
Matt Buckley | e3d5c3a | 2023-12-01 23:10:32 +0000 | [diff] [blame] | 650 | // ===================================== Tracing helpers |
| 651 | |
| 652 | void APerformanceHintSession::traceThreads(std::vector<int32_t>& tids) { |
| 653 | std::set<int32_t> tidSet{tids.begin(), tids.end()}; |
| 654 | |
| 655 | // Disable old TID tracing |
| 656 | for (int32_t tid : mLastThreadIDs) { |
| 657 | if (!tidSet.count(tid)) { |
| 658 | std::string traceName = |
| 659 | android::base::StringPrintf("%s TID: %" PRId32, mSessionName.c_str(), tid); |
| 660 | ATrace_setCounter(traceName.c_str(), 0); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // Add new TID tracing |
| 665 | for (int32_t tid : tids) { |
| 666 | std::string traceName = |
| 667 | android::base::StringPrintf("%s TID: %" PRId32, mSessionName.c_str(), tid); |
| 668 | ATrace_setCounter(traceName.c_str(), 1); |
| 669 | } |
| 670 | |
| 671 | mLastThreadIDs = std::move(tids); |
| 672 | } |
| 673 | |
| 674 | void APerformanceHintSession::tracePowerEfficient(bool powerEfficient) { |
| 675 | ATrace_setCounter((mSessionName + " power efficiency mode").c_str(), powerEfficient); |
| 676 | } |
| 677 | |
| 678 | void APerformanceHintSession::traceActualDuration(int64_t actualDuration) { |
| 679 | ATrace_setCounter((mSessionName + " actual duration").c_str(), actualDuration); |
| 680 | } |
| 681 | |
| 682 | void APerformanceHintSession::traceBatchSize(size_t batchSize) { |
| 683 | std::string traceName = StringPrintf("%s batch size", mSessionName.c_str()); |
| 684 | ATrace_setCounter((mSessionName + " batch size").c_str(), batchSize); |
| 685 | } |
| 686 | |
| 687 | void APerformanceHintSession::traceTargetDuration(int64_t targetDuration) { |
| 688 | ATrace_setCounter((mSessionName + " target duration").c_str(), targetDuration); |
| 689 | } |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 690 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 691 | // ===================================== C API |
| 692 | APerformanceHintManager* APerformanceHint_getManager() { |
| 693 | return APerformanceHintManager::getInstance(); |
| 694 | } |
| 695 | |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 696 | #define VALIDATE_PTR(ptr) \ |
| 697 | LOG_ALWAYS_FATAL_IF(ptr == nullptr, "%s: " #ptr " is nullptr", __FUNCTION__); |
| 698 | |
| 699 | #define VALIDATE_INT(value, cmp) \ |
| 700 | if (!(value cmp)) { \ |
| 701 | ALOGE("%s: Invalid value. Check failed: (" #value " " #cmp ") with value: %" PRIi64, \ |
| 702 | __FUNCTION__, value); \ |
| 703 | return EINVAL; \ |
| 704 | } |
| 705 | |
| 706 | #define WARN_INT(value, cmp) \ |
| 707 | if (!(value cmp)) { \ |
| 708 | ALOGE("%s: Invalid value. Check failed: (" #value " " #cmp ") with value: %" PRIi64, \ |
| 709 | __FUNCTION__, value); \ |
| 710 | } |
| 711 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 712 | APerformanceHintSession* APerformanceHint_createSession(APerformanceHintManager* manager, |
| 713 | const int32_t* threadIds, size_t size, |
| 714 | int64_t initialTargetWorkDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 715 | VALIDATE_PTR(manager) |
| 716 | VALIDATE_PTR(threadIds) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 717 | return manager->createSession(threadIds, size, initialTargetWorkDurationNanos); |
| 718 | } |
| 719 | |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 720 | APerformanceHintSession* APerformanceHint_createSessionInternal( |
| 721 | APerformanceHintManager* manager, const int32_t* threadIds, size_t size, |
| 722 | int64_t initialTargetWorkDurationNanos, SessionTag tag) { |
| 723 | VALIDATE_PTR(manager) |
| 724 | VALIDATE_PTR(threadIds) |
| 725 | return manager->createSession(threadIds, size, initialTargetWorkDurationNanos, |
| 726 | static_cast<hal::SessionTag>(tag)); |
| 727 | } |
| 728 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 729 | int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 730 | VALIDATE_PTR(manager) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 731 | return manager->getPreferredRateNanos(); |
| 732 | } |
| 733 | |
| 734 | int APerformanceHint_updateTargetWorkDuration(APerformanceHintSession* session, |
| 735 | int64_t targetDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 736 | VALIDATE_PTR(session) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 737 | return session->updateTargetWorkDuration(targetDurationNanos); |
| 738 | } |
| 739 | |
| 740 | int APerformanceHint_reportActualWorkDuration(APerformanceHintSession* session, |
| 741 | int64_t actualDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 742 | VALIDATE_PTR(session) |
| 743 | VALIDATE_INT(actualDurationNanos, > 0) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 744 | return session->reportActualWorkDuration(actualDurationNanos); |
| 745 | } |
| 746 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 747 | void APerformanceHint_closeSession(APerformanceHintSession* session) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 748 | VALIDATE_PTR(session) |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 749 | delete session; |
| 750 | } |
| 751 | |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 752 | int APerformanceHint_sendHint(APerformanceHintSession* session, SessionHint hint) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 753 | VALIDATE_PTR(session) |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 754 | return session->sendHint(hint); |
Matt Buckley | 61726a3 | 2022-12-06 23:44:45 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Peiyong Lin | 7ed6de3 | 2023-01-26 00:52:54 +0000 | [diff] [blame] | 757 | int APerformanceHint_setThreads(APerformanceHintSession* session, const pid_t* threadIds, |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 758 | size_t size) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 759 | VALIDATE_PTR(session) |
| 760 | VALIDATE_PTR(threadIds) |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 761 | return session->setThreads(threadIds, size); |
| 762 | } |
| 763 | |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 764 | int APerformanceHint_getThreadIds(APerformanceHintSession* session, int32_t* const threadIds, |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 765 | size_t* const size) { |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 766 | VALIDATE_PTR(session) |
| 767 | return session->getThreadIds(threadIds, size); |
Peiyong Lin | 095de76 | 2022-11-11 18:28:12 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 770 | int APerformanceHint_setPreferPowerEfficiency(APerformanceHintSession* session, bool enabled) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 771 | VALIDATE_PTR(session) |
Matt Buckley | 423c1b3 | 2023-06-28 19:13:42 +0000 | [diff] [blame] | 772 | return session->setPreferPowerEfficiency(enabled); |
| 773 | } |
| 774 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 775 | int APerformanceHint_reportActualWorkDuration2(APerformanceHintSession* session, |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 776 | AWorkDuration* workDurationPtr) { |
| 777 | VALIDATE_PTR(session) |
| 778 | VALIDATE_PTR(workDurationPtr) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 779 | VALIDATE_INT(workDurationPtr->durationNanos, > 0) |
| 780 | VALIDATE_INT(workDurationPtr->workPeriodStartTimestampNanos, > 0) |
| 781 | VALIDATE_INT(workDurationPtr->cpuDurationNanos, >= 0) |
| 782 | VALIDATE_INT(workDurationPtr->gpuDurationNanos, >= 0) |
| 783 | VALIDATE_INT(workDurationPtr->gpuDurationNanos + workDurationPtr->cpuDurationNanos, > 0) |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 784 | return session->reportActualWorkDuration(workDurationPtr); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | AWorkDuration* AWorkDuration_create() { |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 788 | return new AWorkDuration(); |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | void AWorkDuration_release(AWorkDuration* aWorkDuration) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 792 | VALIDATE_PTR(aWorkDuration) |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 793 | delete aWorkDuration; |
| 794 | } |
| 795 | |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 796 | void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* aWorkDuration, |
| 797 | int64_t actualTotalDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 798 | VALIDATE_PTR(aWorkDuration) |
| 799 | WARN_INT(actualTotalDurationNanos, > 0) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 800 | aWorkDuration->durationNanos = actualTotalDurationNanos; |
| 801 | } |
| 802 | |
| 803 | void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* aWorkDuration, |
| 804 | int64_t workPeriodStartTimestampNanos) { |
| 805 | VALIDATE_PTR(aWorkDuration) |
| 806 | WARN_INT(workPeriodStartTimestampNanos, > 0) |
| 807 | aWorkDuration->workPeriodStartTimestampNanos = workPeriodStartTimestampNanos; |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* aWorkDuration, |
| 811 | int64_t actualCpuDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 812 | VALIDATE_PTR(aWorkDuration) |
Matt Buckley | a115b12 | 2024-01-31 20:57:49 +0000 | [diff] [blame] | 813 | WARN_INT(actualCpuDurationNanos, >= 0) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 814 | aWorkDuration->cpuDurationNanos = actualCpuDurationNanos; |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* aWorkDuration, |
| 818 | int64_t actualGpuDurationNanos) { |
Matt Buckley | 83f7709 | 2024-01-18 19:57:29 +0000 | [diff] [blame] | 819 | VALIDATE_PTR(aWorkDuration) |
| 820 | WARN_INT(actualGpuDurationNanos, >= 0) |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 821 | aWorkDuration->gpuDurationNanos = actualGpuDurationNanos; |
Peiyong Lin | 70de085 | 2023-10-25 21:12:35 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 824 | void APerformanceHint_setIHintManagerForTesting(void* iManager) { |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 825 | if (iManager == nullptr) { |
| 826 | gHintManagerForTesting = nullptr; |
| 827 | } |
Matt Buckley | 5897772 | 2024-03-11 23:32:09 +0000 | [diff] [blame] | 828 | gIHintManagerForTesting = static_cast<std::shared_ptr<IHintManager>*>(iManager); |
Bo Liu | 4426772 | 2021-07-16 17:03:20 -0400 | [diff] [blame] | 829 | } |
Matt Buckley | 854947f | 2024-02-07 17:30:01 +0000 | [diff] [blame^] | 830 | |
| 831 | void APerformanceHint_setUseFMQForTesting(bool enabled) { |
| 832 | gForceFMQEnabled = enabled; |
| 833 | } |