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 | } |
| 75 | } |
| 76 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 77 | bool HintSessionWrapper::init() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 78 | if (mHintSession != nullptr) return true; |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 79 | // If we're waiting for the session |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame^] | 80 | if (mHintSessionFuture.has_value()) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 81 | // If the session is here |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame^] | 82 | if (mHintSessionFuture->wait_for(0s) == std::future_status::ready) { |
| 83 | mHintSession = mHintSessionFuture->get(); |
| 84 | mHintSessionFuture = std::nullopt; |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 85 | if (mHintSession != nullptr) { |
| 86 | mSessionValid = true; |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // 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] | 94 | // has bad argument values, don't even bother |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 95 | if (!mSessionValid || !Properties::useHintManager || !Properties::isDrawingEnabled() || |
| 96 | mUiThreadId < 0 || mRenderThreadId < 0) { |
Matt Buckley | 124d0c67 | 2023-01-19 03:04:19 +0000 | [diff] [blame] | 97 | return false; |
| 98 | } |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 99 | |
| 100 | // Assume that if we return before the end, it broke |
| 101 | mSessionValid = false; |
| 102 | |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 103 | mBinding->init(); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 104 | |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 105 | APerformanceHintManager* manager = mBinding->getManager(); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 106 | if (!manager) return false; |
| 107 | |
| 108 | std::vector<pid_t> tids = CommonPool::getThreadIds(); |
| 109 | tids.push_back(mUiThreadId); |
| 110 | tids.push_back(mRenderThreadId); |
| 111 | |
| 112 | // Use a placeholder target value to initialize, |
| 113 | // this will always be replaced elsewhere before it gets used |
| 114 | int64_t defaultTargetDurationNanos = 16666667; |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 115 | mHintSessionFuture = CommonPool::async([=, this, tids = std::move(tids)] { |
| 116 | return mBinding->createSession(manager, tids.data(), tids.size(), |
| 117 | defaultTargetDurationNanos); |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 118 | }); |
| 119 | return false; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void HintSessionWrapper::updateTargetWorkDuration(long targetWorkDurationNanos) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 123 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 124 | targetWorkDurationNanos = targetWorkDurationNanos * Properties::targetCpuTimePercentage / 100; |
| 125 | if (targetWorkDurationNanos != mLastTargetWorkDuration && |
| 126 | targetWorkDurationNanos > kSanityCheckLowerBound && |
| 127 | targetWorkDurationNanos < kSanityCheckUpperBound) { |
| 128 | mLastTargetWorkDuration = targetWorkDurationNanos; |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 129 | mBinding->updateTargetWorkDuration(mHintSession, targetWorkDurationNanos); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 130 | } |
| 131 | mLastFrameNotification = systemTime(); |
| 132 | } |
| 133 | |
| 134 | void HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 135 | if (!init()) return; |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 136 | mResetsSinceLastReport = 0; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 137 | if (actualDurationNanos > kSanityCheckLowerBound && |
| 138 | actualDurationNanos < kSanityCheckUpperBound) { |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 139 | mBinding->reportActualWorkDuration(mHintSession, actualDurationNanos); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 140 | } |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame^] | 141 | mLastFrameNotification = systemTime(); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void HintSessionWrapper::sendLoadResetHint() { |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 145 | static constexpr int kMaxResetsSinceLastReport = 2; |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 146 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 147 | nsecs_t now = systemTime(); |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 148 | if (now - mLastFrameNotification > kResetHintTimeout && |
| 149 | mResetsSinceLastReport <= kMaxResetsSinceLastReport) { |
| 150 | ++mResetsSinceLastReport; |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 151 | mBinding->sendHint(mHintSession, static_cast<int32_t>(SessionHint::CPU_LOAD_RESET)); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 152 | } |
| 153 | mLastFrameNotification = now; |
| 154 | } |
| 155 | |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 156 | void HintSessionWrapper::sendLoadIncreaseHint() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 157 | if (!init()) return; |
Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 158 | mBinding->sendHint(mHintSession, static_cast<int32_t>(SessionHint::CPU_LOAD_UP)); |
Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame^] | 159 | mLastFrameNotification = systemTime(); |
| 160 | } |
| 161 | |
| 162 | bool HintSessionWrapper::alive() { |
| 163 | return mHintSession != nullptr; |
| 164 | } |
| 165 | |
| 166 | nsecs_t HintSessionWrapper::getLastUpdate() { |
| 167 | return mLastFrameNotification; |
| 168 | } |
| 169 | |
| 170 | // Requires passing in its shared_ptr since it shouldn't own a shared_ptr to itself |
| 171 | void HintSessionWrapper::delayedDestroy(RenderThread& rt, nsecs_t delay, |
| 172 | std::shared_ptr<HintSessionWrapper> wrapperPtr) { |
| 173 | nsecs_t lastUpdate = wrapperPtr->getLastUpdate(); |
| 174 | rt.queue().postDelayed(delay, [lastUpdate = lastUpdate, wrapper = wrapperPtr]() mutable { |
| 175 | if (wrapper->getLastUpdate() == lastUpdate) { |
| 176 | wrapper->destroy(); |
| 177 | } |
| 178 | // Ensure the shared_ptr is killed at the end of the method |
| 179 | wrapper = nullptr; |
| 180 | }); |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 183 | } /* namespace renderthread */ |
| 184 | } /* namespace uirenderer */ |
| 185 | } /* namespace android */ |