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 | |
| 19 | #include <utility> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <android/os/IHintManager.h> |
| 23 | #include <android/os/IHintSession.h> |
| 24 | #include <binder/Binder.h> |
| 25 | #include <binder/IBinder.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <performance_hint_private.h> |
| 28 | #include <utils/SystemClock.h> |
| 29 | |
| 30 | using namespace android; |
| 31 | using namespace android::os; |
| 32 | |
| 33 | struct APerformanceHintSession; |
| 34 | |
| 35 | struct APerformanceHintManager { |
| 36 | public: |
| 37 | static APerformanceHintManager* getInstance(); |
| 38 | APerformanceHintManager(sp<IHintManager> service, int64_t preferredRateNanos); |
| 39 | APerformanceHintManager() = delete; |
| 40 | ~APerformanceHintManager() = default; |
| 41 | |
| 42 | APerformanceHintSession* createSession(const int32_t* threadIds, size_t size, |
| 43 | int64_t initialTargetWorkDurationNanos); |
| 44 | int64_t getPreferredRateNanos() const; |
| 45 | |
| 46 | private: |
| 47 | static APerformanceHintManager* create(sp<IHintManager> iHintManager); |
| 48 | |
| 49 | sp<IHintManager> mHintManager; |
| 50 | const int64_t mPreferredRateNanos; |
| 51 | }; |
| 52 | |
| 53 | struct APerformanceHintSession { |
| 54 | public: |
| 55 | APerformanceHintSession(sp<IHintSession> session, int64_t preferredRateNanos, |
| 56 | int64_t targetDurationNanos); |
| 57 | APerformanceHintSession() = delete; |
| 58 | ~APerformanceHintSession(); |
| 59 | |
| 60 | int updateTargetWorkDuration(int64_t targetDurationNanos); |
| 61 | int reportActualWorkDuration(int64_t actualDurationNanos); |
| 62 | |
| 63 | private: |
| 64 | friend struct APerformanceHintManager; |
| 65 | |
| 66 | sp<IHintSession> mHintSession; |
| 67 | // HAL preferred update rate |
| 68 | const int64_t mPreferredRateNanos; |
| 69 | // Target duration for choosing update rate |
| 70 | int64_t mTargetDurationNanos; |
| 71 | // Last update timestamp |
| 72 | int64_t mLastUpdateTimestamp; |
| 73 | // Cached samples |
| 74 | std::vector<int64_t> mActualDurationsNanos; |
| 75 | std::vector<int64_t> mTimestampsNanos; |
| 76 | }; |
| 77 | |
| 78 | static IHintManager* gIHintManagerForTesting = nullptr; |
| 79 | static APerformanceHintManager* gHintManagerForTesting = nullptr; |
| 80 | |
| 81 | // ===================================== APerformanceHintManager implementation |
| 82 | APerformanceHintManager::APerformanceHintManager(sp<IHintManager> manager, |
| 83 | int64_t preferredRateNanos) |
| 84 | : mHintManager(std::move(manager)), mPreferredRateNanos(preferredRateNanos) {} |
| 85 | |
| 86 | APerformanceHintManager* APerformanceHintManager::getInstance() { |
| 87 | if (gHintManagerForTesting) return gHintManagerForTesting; |
| 88 | if (gIHintManagerForTesting) { |
| 89 | APerformanceHintManager* manager = create(gIHintManagerForTesting); |
| 90 | gIHintManagerForTesting = nullptr; |
| 91 | return manager; |
| 92 | } |
| 93 | static APerformanceHintManager* instance = create(nullptr); |
| 94 | return instance; |
| 95 | } |
| 96 | |
| 97 | APerformanceHintManager* APerformanceHintManager::create(sp<IHintManager> manager) { |
| 98 | if (!manager) { |
| 99 | manager = interface_cast<IHintManager>( |
| 100 | defaultServiceManager()->checkService(String16("performance_hint"))); |
| 101 | } |
| 102 | if (manager == nullptr) { |
| 103 | ALOGE("%s: PerformanceHint service is not ready ", __FUNCTION__); |
| 104 | return nullptr; |
| 105 | } |
| 106 | int64_t preferredRateNanos = -1L; |
| 107 | binder::Status ret = manager->getHintSessionPreferredRate(&preferredRateNanos); |
| 108 | if (!ret.isOk()) { |
| 109 | ALOGE("%s: PerformanceHint cannot get preferred rate. %s", __FUNCTION__, |
| 110 | ret.exceptionMessage().c_str()); |
| 111 | return nullptr; |
| 112 | } |
| 113 | if (preferredRateNanos <= 0) { |
| 114 | ALOGE("%s: PerformanceHint invalid preferred rate.", __FUNCTION__); |
| 115 | return nullptr; |
| 116 | } |
| 117 | return new APerformanceHintManager(std::move(manager), preferredRateNanos); |
| 118 | } |
| 119 | |
| 120 | APerformanceHintSession* APerformanceHintManager::createSession( |
| 121 | const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos) { |
| 122 | sp<IBinder> token = sp<BBinder>::make(); |
| 123 | std::vector<int32_t> tids(threadIds, threadIds + size); |
| 124 | sp<IHintSession> session; |
| 125 | binder::Status ret = |
| 126 | mHintManager->createHintSession(token, tids, initialTargetWorkDurationNanos, &session); |
| 127 | if (!ret.isOk() || !session) { |
| 128 | return nullptr; |
| 129 | } |
| 130 | return new APerformanceHintSession(std::move(session), mPreferredRateNanos, |
| 131 | initialTargetWorkDurationNanos); |
| 132 | } |
| 133 | |
| 134 | int64_t APerformanceHintManager::getPreferredRateNanos() const { |
| 135 | return mPreferredRateNanos; |
| 136 | } |
| 137 | |
| 138 | // ===================================== APerformanceHintSession implementation |
| 139 | |
| 140 | APerformanceHintSession::APerformanceHintSession(sp<IHintSession> session, |
| 141 | int64_t preferredRateNanos, |
| 142 | int64_t targetDurationNanos) |
| 143 | : mHintSession(std::move(session)), |
| 144 | mPreferredRateNanos(preferredRateNanos), |
| 145 | mTargetDurationNanos(targetDurationNanos), |
| 146 | mLastUpdateTimestamp(elapsedRealtimeNano()) {} |
| 147 | |
| 148 | APerformanceHintSession::~APerformanceHintSession() { |
| 149 | binder::Status ret = mHintSession->close(); |
| 150 | if (!ret.isOk()) { |
| 151 | ALOGE("%s: HintSession close failed: %s", __FUNCTION__, ret.exceptionMessage().c_str()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | int APerformanceHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) { |
| 156 | if (targetDurationNanos <= 0) { |
| 157 | ALOGE("%s: targetDurationNanos must be positive", __FUNCTION__); |
| 158 | return EINVAL; |
| 159 | } |
| 160 | binder::Status ret = mHintSession->updateTargetWorkDuration(targetDurationNanos); |
| 161 | if (!ret.isOk()) { |
| 162 | ALOGE("%s: HintSessionn updateTargetWorkDuration failed: %s", __FUNCTION__, |
| 163 | ret.exceptionMessage().c_str()); |
| 164 | return EPIPE; |
| 165 | } |
| 166 | mTargetDurationNanos = targetDurationNanos; |
| 167 | /** |
| 168 | * Most of the workload is target_duration dependent, so now clear the cached samples |
| 169 | * as they are most likely obsolete. |
| 170 | */ |
| 171 | mActualDurationsNanos.clear(); |
| 172 | mTimestampsNanos.clear(); |
| 173 | mLastUpdateTimestamp = elapsedRealtimeNano(); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int APerformanceHintSession::reportActualWorkDuration(int64_t actualDurationNanos) { |
| 178 | if (actualDurationNanos <= 0) { |
| 179 | ALOGE("%s: actualDurationNanos must be positive", __FUNCTION__); |
| 180 | return EINVAL; |
| 181 | } |
| 182 | int64_t now = elapsedRealtimeNano(); |
| 183 | mActualDurationsNanos.push_back(actualDurationNanos); |
| 184 | mTimestampsNanos.push_back(now); |
| 185 | |
| 186 | /** |
| 187 | * Use current sample to determine the rate limit. We can pick a shorter rate limit |
| 188 | * if any sample underperformed, however, it could be the lower level system is slow |
| 189 | * to react. So here we explicitly choose the rate limit with the latest sample. |
| 190 | */ |
| 191 | int64_t rateLimit = actualDurationNanos > mTargetDurationNanos ? mPreferredRateNanos |
| 192 | : 10 * mPreferredRateNanos; |
| 193 | if (now - mLastUpdateTimestamp <= rateLimit) return 0; |
| 194 | |
| 195 | binder::Status ret = |
| 196 | mHintSession->reportActualWorkDuration(mActualDurationsNanos, mTimestampsNanos); |
| 197 | mActualDurationsNanos.clear(); |
| 198 | mTimestampsNanos.clear(); |
| 199 | if (!ret.isOk()) { |
| 200 | ALOGE("%s: HintSession reportActualWorkDuration failed: %s", __FUNCTION__, |
| 201 | ret.exceptionMessage().c_str()); |
| 202 | return EPIPE; |
| 203 | } |
| 204 | mLastUpdateTimestamp = now; |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | // ===================================== C API |
| 209 | APerformanceHintManager* APerformanceHint_getManager() { |
| 210 | return APerformanceHintManager::getInstance(); |
| 211 | } |
| 212 | |
| 213 | APerformanceHintSession* APerformanceHint_createSession(APerformanceHintManager* manager, |
| 214 | const int32_t* threadIds, size_t size, |
| 215 | int64_t initialTargetWorkDurationNanos) { |
| 216 | return manager->createSession(threadIds, size, initialTargetWorkDurationNanos); |
| 217 | } |
| 218 | |
| 219 | int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) { |
| 220 | return manager->getPreferredRateNanos(); |
| 221 | } |
| 222 | |
| 223 | int APerformanceHint_updateTargetWorkDuration(APerformanceHintSession* session, |
| 224 | int64_t targetDurationNanos) { |
| 225 | return session->updateTargetWorkDuration(targetDurationNanos); |
| 226 | } |
| 227 | |
| 228 | int APerformanceHint_reportActualWorkDuration(APerformanceHintSession* session, |
| 229 | int64_t actualDurationNanos) { |
| 230 | return session->reportActualWorkDuration(actualDurationNanos); |
| 231 | } |
| 232 | |
| 233 | void APerformanceHint_closeSession(APerformanceHintSession* session) { |
| 234 | delete session; |
| 235 | } |
| 236 | |
| 237 | void APerformanceHint_setIHintManagerForTesting(void* iManager) { |
| 238 | delete gHintManagerForTesting; |
| 239 | gHintManagerForTesting = nullptr; |
| 240 | gIHintManagerForTesting = static_cast<IHintManager*>(iManager); |
| 241 | } |