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