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