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" |
| 27 | #include "thread/CommonPool.h" |
| 28 | |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 29 | using namespace std::chrono_literals; |
| 30 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | namespace renderthread { |
| 34 | |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 35 | #define BIND_APH_METHOD(name) \ |
| 36 | name = (decltype(name))dlsym(handle_, "APerformanceHint_" #name); \ |
| 37 | 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] | 38 | |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 39 | void HintSessionWrapper::HintSessionBinding::init() { |
| 40 | if (mInitialized) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 41 | |
| 42 | void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE); |
| 43 | LOG_ALWAYS_FATAL_IF(handle_ == nullptr, "Failed to dlopen libandroid.so!"); |
| 44 | |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 45 | BIND_APH_METHOD(getManager); |
| 46 | BIND_APH_METHOD(createSession); |
| 47 | BIND_APH_METHOD(closeSession); |
| 48 | BIND_APH_METHOD(updateTargetWorkDuration); |
| 49 | BIND_APH_METHOD(reportActualWorkDuration); |
| 50 | BIND_APH_METHOD(sendHint); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 51 | |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 52 | mInitialized = true; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 55 | HintSessionWrapper::HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId) |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 56 | : mUiThreadId(uiThreadId) |
| 57 | , mRenderThreadId(renderThreadId) |
| 58 | , mBinding(std::make_shared<HintSessionBinding>()) {} |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 59 | |
| 60 | HintSessionWrapper::~HintSessionWrapper() { |
Matt Buckley | 10d75fc | 2023-08-24 15:56:46 +0000 | [diff] [blame] | 61 | destroy(); |
| 62 | } |
| 63 | |
| 64 | void HintSessionWrapper::destroy() { |
| 65 | if (mHintSessionFuture.valid()) { |
| 66 | mHintSession = mHintSessionFuture.get(); |
| 67 | } |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 68 | if (mHintSession) { |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 69 | mBinding->closeSession(mHintSession); |
Matt Buckley | 10d75fc | 2023-08-24 15:56:46 +0000 | [diff] [blame] | 70 | mSessionValid = true; |
| 71 | mHintSession = nullptr; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 75 | bool HintSessionWrapper::init() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 76 | if (mHintSession != nullptr) return true; |
| 77 | |
| 78 | // If we're waiting for the session |
| 79 | if (mHintSessionFuture.valid()) { |
| 80 | // If the session is here |
| 81 | if (mHintSessionFuture.wait_for(0s) == std::future_status::ready) { |
| 82 | mHintSession = mHintSessionFuture.get(); |
| 83 | if (mHintSession != nullptr) { |
| 84 | mSessionValid = true; |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | // 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] | 92 | // has bad argument values, don't even bother |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 93 | if (!mSessionValid || !Properties::useHintManager || !Properties::isDrawingEnabled() || |
| 94 | mUiThreadId < 0 || mRenderThreadId < 0) { |
Matt Buckley | 124d0c67 | 2023-01-19 03:04:19 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 97 | |
| 98 | // Assume that if we return before the end, it broke |
| 99 | mSessionValid = false; |
| 100 | |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 101 | mBinding->init(); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 102 | |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 103 | APerformanceHintManager* manager = mBinding->getManager(); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 104 | if (!manager) return false; |
| 105 | |
| 106 | std::vector<pid_t> tids = CommonPool::getThreadIds(); |
| 107 | tids.push_back(mUiThreadId); |
| 108 | tids.push_back(mRenderThreadId); |
| 109 | |
| 110 | // Use a placeholder target value to initialize, |
| 111 | // this will always be replaced elsewhere before it gets used |
| 112 | int64_t defaultTargetDurationNanos = 16666667; |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 113 | mHintSessionFuture = CommonPool::async([=, this, tids = std::move(tids)] { |
| 114 | return mBinding->createSession(manager, tids.data(), tids.size(), |
| 115 | defaultTargetDurationNanos); |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 116 | }); |
| 117 | return false; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void HintSessionWrapper::updateTargetWorkDuration(long targetWorkDurationNanos) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 121 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 122 | targetWorkDurationNanos = targetWorkDurationNanos * Properties::targetCpuTimePercentage / 100; |
| 123 | if (targetWorkDurationNanos != mLastTargetWorkDuration && |
| 124 | targetWorkDurationNanos > kSanityCheckLowerBound && |
| 125 | targetWorkDurationNanos < kSanityCheckUpperBound) { |
| 126 | mLastTargetWorkDuration = targetWorkDurationNanos; |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 127 | mBinding->updateTargetWorkDuration(mHintSession, targetWorkDurationNanos); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 128 | } |
| 129 | mLastFrameNotification = systemTime(); |
| 130 | } |
| 131 | |
| 132 | void HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 133 | if (!init()) return; |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 134 | mResetsSinceLastReport = 0; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 135 | if (actualDurationNanos > kSanityCheckLowerBound && |
| 136 | actualDurationNanos < kSanityCheckUpperBound) { |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 137 | mBinding->reportActualWorkDuration(mHintSession, actualDurationNanos); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | void HintSessionWrapper::sendLoadResetHint() { |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 142 | static constexpr int kMaxResetsSinceLastReport = 2; |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 143 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 144 | nsecs_t now = systemTime(); |
Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 145 | if (now - mLastFrameNotification > kResetHintTimeout && |
| 146 | mResetsSinceLastReport <= kMaxResetsSinceLastReport) { |
| 147 | ++mResetsSinceLastReport; |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 148 | mBinding->sendHint(mHintSession, static_cast<int32_t>(SessionHint::CPU_LOAD_RESET)); |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 149 | } |
| 150 | mLastFrameNotification = now; |
| 151 | } |
| 152 | |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 153 | void HintSessionWrapper::sendLoadIncreaseHint() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 154 | if (!init()) return; |
Matt Buckley | 8776557 | 2023-09-07 05:52:07 +0000 | [diff] [blame^] | 155 | mBinding->sendHint(mHintSession, static_cast<int32_t>(SessionHint::CPU_LOAD_UP)); |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 158 | } /* namespace renderthread */ |
| 159 | } /* namespace uirenderer */ |
| 160 | } /* namespace android */ |