Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #include "HintSessionWrapper.h" |
| 18 | |
| 19 | #include <dlfcn.h> |
Matt Buckley | 61726a3 | 2022-12-06 23:44:45 +0000 | [diff] [blame] | 20 | #include <private/performance_hint_private.h> |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 24 | #include <chrono> |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
| 27 | #include "../Properties.h" |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 28 | #include "RenderThread.h" |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 29 | #include "thread/CommonPool.h" |
| 30 | |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 31 | using namespace std::chrono_literals; |
| 32 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | namespace renderthread { |
| 36 | |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 37 | #define BIND_APH_METHOD(name) \ |
| 38 | name = (decltype(name))dlsym(handle_, "APerformanceHint_" #name); \ |
| 39 | LOG_ALWAYS_FATAL_IF(name == nullptr, "Failed to find required symbol APerformanceHint_" #name) |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 40 | |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 41 | void HintSessionWrapper::HintSessionBinding::init() { |
| 42 | if (mInitialized) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 43 | |
| 44 | void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE); |
| 45 | LOG_ALWAYS_FATAL_IF(handle_ == nullptr, "Failed to dlopen libandroid.so!"); |
| 46 | |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 47 | BIND_APH_METHOD(getManager); |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 48 | BIND_APH_METHOD(createSessionInternal); |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 49 | BIND_APH_METHOD(closeSession); |
| 50 | BIND_APH_METHOD(updateTargetWorkDuration); |
| 51 | BIND_APH_METHOD(reportActualWorkDuration); |
| 52 | BIND_APH_METHOD(sendHint); |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 53 | BIND_APH_METHOD(setThreads); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 54 | |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 55 | mInitialized = true; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 58 | HintSessionWrapper::HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId) |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 59 | : mUiThreadId(uiThreadId) |
| 60 | , mRenderThreadId(renderThreadId) |
| 61 | , mBinding(std::make_shared<HintSessionBinding>()) {} |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 62 | |
| 63 | HintSessionWrapper::~HintSessionWrapper() { |
Matt Buckley | ac620f6 | 2023-08-24 15:56:46 +0000 | [diff] [blame] | 64 | destroy(); |
| 65 | } |
| 66 | |
| 67 | void HintSessionWrapper::destroy() { |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 68 | if (mHintSessionFuture.has_value()) { |
| 69 | mHintSession = mHintSessionFuture->get(); |
| 70 | mHintSessionFuture = std::nullopt; |
Matt Buckley | ac620f6 | 2023-08-24 15:56:46 +0000 | [diff] [blame] | 71 | } |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 72 | if (mSetThreadsFuture.has_value()) { |
| 73 | mSetThreadsFuture->wait(); |
| 74 | mSetThreadsFuture = std::nullopt; |
| 75 | } |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 76 | if (mHintSession) { |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 77 | mBinding->closeSession(mHintSession); |
Matt Buckley | ac620f6 | 2023-08-24 15:56:46 +0000 | [diff] [blame] | 78 | mSessionValid = true; |
| 79 | mHintSession = nullptr; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 80 | } |
Matt Buckley | 1b99d78 | 2023-09-26 19:30:25 +0000 | [diff] [blame] | 81 | mResetsSinceLastReport = 0; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 84 | bool HintSessionWrapper::init() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 85 | if (mHintSession != nullptr) return true; |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 86 | // If we're waiting for the session |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 87 | if (mHintSessionFuture.has_value()) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 88 | // If the session is here |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 89 | if (mHintSessionFuture->wait_for(0s) == std::future_status::ready) { |
| 90 | mHintSession = mHintSessionFuture->get(); |
| 91 | mHintSessionFuture = std::nullopt; |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 92 | if (mHintSession != nullptr) { |
| 93 | mSessionValid = true; |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // If it broke last time we tried this, shouldn't be running, or |
Matt Buckley | 124d0c67 | 2023-01-19 03:04:19 +0000 | [diff] [blame] | 101 | // has bad argument values, don't even bother |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 102 | if (!mSessionValid || !Properties::useHintManager || !Properties::isDrawingEnabled() || |
| 103 | mUiThreadId < 0 || mRenderThreadId < 0) { |
Matt Buckley | 124d0c67 | 2023-01-19 03:04:19 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 106 | |
| 107 | // Assume that if we return before the end, it broke |
| 108 | mSessionValid = false; |
| 109 | |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 110 | mBinding->init(); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 111 | |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 112 | APerformanceHintManager* manager = mBinding->getManager(); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 113 | if (!manager) return false; |
| 114 | |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 115 | mPermanentSessionTids = CommonPool::getThreadIds(); |
| 116 | mPermanentSessionTids.push_back(mUiThreadId); |
| 117 | mPermanentSessionTids.push_back(mRenderThreadId); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 118 | |
Matt Buckley | 1b99d78 | 2023-09-26 19:30:25 +0000 | [diff] [blame] | 119 | // Use the cached target value if there is one, otherwise use a default. This is to ensure |
| 120 | // the cached target and target in PowerHAL are consistent, and that it updates correctly |
| 121 | // whenever there is a change. |
| 122 | int64_t targetDurationNanos = |
| 123 | mLastTargetWorkDuration == 0 ? kDefaultTargetDuration : mLastTargetWorkDuration; |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 124 | mHintSessionFuture = CommonPool::async([=, this, tids = mPermanentSessionTids] { |
Matt Buckley | 27da134 | 2024-04-05 23:10:48 +0000 | [diff] [blame] | 125 | return mBinding->createSessionInternal(manager, tids.data(), tids.size(), |
| 126 | targetDurationNanos, SessionTag::HWUI); |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 127 | }); |
| 128 | return false; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void HintSessionWrapper::updateTargetWorkDuration(long targetWorkDurationNanos) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 132 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 133 | targetWorkDurationNanos = targetWorkDurationNanos * Properties::targetCpuTimePercentage / 100; |
| 134 | if (targetWorkDurationNanos != mLastTargetWorkDuration && |
| 135 | targetWorkDurationNanos > kSanityCheckLowerBound && |
| 136 | targetWorkDurationNanos < kSanityCheckUpperBound) { |
| 137 | mLastTargetWorkDuration = targetWorkDurationNanos; |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 138 | mBinding->updateTargetWorkDuration(mHintSession, targetWorkDurationNanos); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 139 | } |
| 140 | mLastFrameNotification = systemTime(); |
| 141 | } |
| 142 | |
| 143 | void HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 144 | if (!init()) return; |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 145 | mResetsSinceLastReport = 0; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 146 | if (actualDurationNanos > kSanityCheckLowerBound && |
| 147 | actualDurationNanos < kSanityCheckUpperBound) { |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 148 | mBinding->reportActualWorkDuration(mHintSession, actualDurationNanos); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 149 | } |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 150 | mLastFrameNotification = systemTime(); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Igor Kraskevich | 2cec158 | 2024-03-13 11:23:40 +0000 | [diff] [blame] | 153 | void HintSessionWrapper::setActiveFunctorThreads(std::vector<pid_t> threadIds) { |
| 154 | if (!init()) return; |
| 155 | if (!mBinding || !mHintSession) return; |
| 156 | // Sort the vector to make sure they're compared as sets. |
| 157 | std::sort(threadIds.begin(), threadIds.end()); |
| 158 | if (threadIds == mActiveFunctorTids) return; |
| 159 | mActiveFunctorTids = std::move(threadIds); |
| 160 | std::vector<pid_t> combinedTids = mPermanentSessionTids; |
| 161 | std::copy(mActiveFunctorTids.begin(), mActiveFunctorTids.end(), |
| 162 | std::back_inserter(combinedTids)); |
| 163 | mSetThreadsFuture = CommonPool::async([this, tids = std::move(combinedTids)] { |
| 164 | int ret = mBinding->setThreads(mHintSession, tids.data(), tids.size()); |
| 165 | ALOGE_IF(ret != 0, "APerformaceHint_setThreads failed: %d", ret); |
| 166 | return ret; |
| 167 | }); |
| 168 | } |
| 169 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 170 | void HintSessionWrapper::sendLoadResetHint() { |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 171 | static constexpr int kMaxResetsSinceLastReport = 2; |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 172 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 173 | nsecs_t now = systemTime(); |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 174 | if (now - mLastFrameNotification > kResetHintTimeout && |
| 175 | mResetsSinceLastReport <= kMaxResetsSinceLastReport) { |
| 176 | ++mResetsSinceLastReport; |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 177 | mBinding->sendHint(mHintSession, static_cast<int32_t>(SessionHint::CPU_LOAD_RESET)); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 178 | } |
| 179 | mLastFrameNotification = now; |
| 180 | } |
| 181 | |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 182 | void HintSessionWrapper::sendLoadIncreaseHint() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 183 | if (!init()) return; |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 184 | mBinding->sendHint(mHintSession, static_cast<int32_t>(SessionHint::CPU_LOAD_UP)); |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | bool HintSessionWrapper::alive() { |
| 188 | return mHintSession != nullptr; |
| 189 | } |
| 190 | |
| 191 | nsecs_t HintSessionWrapper::getLastUpdate() { |
| 192 | return mLastFrameNotification; |
| 193 | } |
| 194 | |
| 195 | // Requires passing in its shared_ptr since it shouldn't own a shared_ptr to itself |
| 196 | void HintSessionWrapper::delayedDestroy(RenderThread& rt, nsecs_t delay, |
| 197 | std::shared_ptr<HintSessionWrapper> wrapperPtr) { |
| 198 | nsecs_t lastUpdate = wrapperPtr->getLastUpdate(); |
| 199 | rt.queue().postDelayed(delay, [lastUpdate = lastUpdate, wrapper = wrapperPtr]() mutable { |
| 200 | if (wrapper->getLastUpdate() == lastUpdate) { |
| 201 | wrapper->destroy(); |
| 202 | } |
| 203 | // Ensure the shared_ptr is killed at the end of the method |
| 204 | wrapper = nullptr; |
| 205 | }); |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 208 | } /* namespace renderthread */ |
| 209 | } /* namespace uirenderer */ |
| 210 | } /* namespace android */ |