Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 "PowerHalAidlBenchmarks" |
| 18 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 19 | #include <aidl/android/hardware/power/Boost.h> |
| 20 | #include <aidl/android/hardware/power/IPower.h> |
| 21 | #include <aidl/android/hardware/power/IPowerHintSession.h> |
| 22 | #include <aidl/android/hardware/power/Mode.h> |
| 23 | #include <aidl/android/hardware/power/WorkDuration.h> |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 24 | #include <benchmark/benchmark.h> |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 26 | #include <binder/Status.h> |
| 27 | #include <powermanager/PowerHalLoader.h> |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 28 | #include <testUtil.h> |
| 29 | #include <chrono> |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 30 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 31 | using aidl::android::hardware::power::Boost; |
| 32 | using aidl::android::hardware::power::IPower; |
| 33 | using aidl::android::hardware::power::IPowerHintSession; |
| 34 | using aidl::android::hardware::power::Mode; |
| 35 | using aidl::android::hardware::power::WorkDuration; |
| 36 | using android::power::PowerHalLoader; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 37 | using std::chrono::microseconds; |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace android; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 40 | using namespace std::chrono_literals; |
| 41 | |
| 42 | // Values from Boost.aidl and Mode.aidl. |
| 43 | static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION); |
| 44 | static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT); |
| 45 | static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE); |
| 46 | static constexpr int64_t LAST_MODE = static_cast<int64_t>(Mode::CAMERA_STREAMING_HIGH); |
| 47 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 48 | class DurationWrapper : public WorkDuration { |
| 49 | public: |
| 50 | DurationWrapper(int64_t dur, int64_t time) { |
| 51 | durationNanos = dur; |
| 52 | timeStampNanos = time; |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | static const std::vector<WorkDuration> DURATIONS = { |
| 57 | DurationWrapper(1L, 1L), |
| 58 | DurationWrapper(1000L, 2L), |
| 59 | DurationWrapper(1000000L, 3L), |
| 60 | DurationWrapper(1000000000L, 4L), |
| 61 | }; |
| 62 | |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 63 | // Delay between oneway method calls to avoid overflowing the binder buffers. |
| 64 | static constexpr microseconds ONEWAY_API_DELAY = 100us; |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 65 | |
| 66 | template <class R, class... Args0, class... Args1> |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 67 | static void runBenchmark(benchmark::State& state, microseconds delay, R (IPower::*fn)(Args0...), |
| 68 | Args1&&... args1) { |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 69 | std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl(); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 70 | |
| 71 | if (hal == nullptr) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 72 | ALOGV("Power HAL not available, skipping test..."); |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 76 | ndk::ScopedAStatus ret = (*hal.*fn)(std::forward<Args1>(args1)...); |
| 77 | if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 78 | ALOGV("Power HAL does not support this operation, skipping test..."); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
| 82 | while (state.KeepRunning()) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 83 | ret = (*hal.*fn)(std::forward<Args1>(args1)...); |
| 84 | state.PauseTiming(); |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 85 | if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str()); |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 86 | if (delay > 0us) { |
| 87 | testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count()); |
| 88 | } |
| 89 | state.ResumeTiming(); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 93 | template <class R, class... Args0, class... Args1> |
| 94 | static void runSessionBenchmark(benchmark::State& state, R (IPowerHintSession::*fn)(Args0...), |
| 95 | Args1&&... args1) { |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 96 | std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl(); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 97 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 98 | if (hal == nullptr) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 99 | ALOGV("Power HAL not available, skipping test..."); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | // do not use tid from the benchmark process, use 1 for init |
| 104 | std::vector<int32_t> threadIds{1}; |
| 105 | int64_t durationNanos = 16666666L; |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 106 | std::shared_ptr<IPowerHintSession> session; |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 107 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 108 | auto status = hal->createHintSession(1, 0, threadIds, durationNanos, &session); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 109 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 110 | if (session == nullptr) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 111 | ALOGV("Power HAL doesn't support session, skipping test..."); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 115 | ndk::ScopedAStatus ret = (*session.*fn)(std::forward<Args1>(args1)...); |
| 116 | if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 117 | ALOGV("Power HAL does not support this operation, skipping test..."); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
| 121 | while (state.KeepRunning()) { |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 122 | ret = (*session.*fn)(std::forward<Args1>(args1)...); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 123 | state.PauseTiming(); |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 124 | if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str()); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 125 | if (ONEWAY_API_DELAY > 0us) { |
| 126 | testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(ONEWAY_API_DELAY) |
| 127 | .count()); |
| 128 | } |
| 129 | state.ResumeTiming(); |
| 130 | } |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 131 | session->close(); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 134 | static void BM_PowerHalAidlBenchmarks_isBoostSupported(benchmark::State& state) { |
| 135 | bool isSupported; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 136 | Boost boost = static_cast<Boost>(state.range(0)); |
| 137 | runBenchmark(state, 0us, &IPower::isBoostSupported, boost, &isSupported); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void BM_PowerHalAidlBenchmarks_isModeSupported(benchmark::State& state) { |
| 141 | bool isSupported; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 142 | Mode mode = static_cast<Mode>(state.range(0)); |
| 143 | runBenchmark(state, 0us, &IPower::isModeSupported, mode, &isSupported); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void BM_PowerHalAidlBenchmarks_setBoost(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 147 | Boost boost = static_cast<Boost>(state.range(0)); |
| 148 | runBenchmark(state, ONEWAY_API_DELAY, &IPower::setBoost, boost, 1); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void BM_PowerHalAidlBenchmarks_setMode(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 152 | Mode mode = static_cast<Mode>(state.range(0)); |
| 153 | runBenchmark(state, ONEWAY_API_DELAY, &IPower::setMode, mode, false); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 156 | static void BM_PowerHalAidlBenchmarks_createHintSession(benchmark::State& state) { |
| 157 | std::vector<int32_t> threadIds{static_cast<int32_t>(state.range(0))}; |
| 158 | int64_t durationNanos = 16666666L; |
| 159 | int32_t tgid = 999; |
| 160 | int32_t uid = 1001; |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 161 | std::shared_ptr<IPowerHintSession> appSession; |
| 162 | std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl(); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 163 | |
| 164 | if (hal == nullptr) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 165 | ALOGV("Power HAL not available, skipping test..."); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 169 | ndk::ScopedAStatus ret = |
| 170 | hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession); |
| 171 | if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 172 | ALOGV("Power HAL does not support this operation, skipping test..."); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
| 176 | while (state.KeepRunning()) { |
| 177 | ret = hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession); |
| 178 | state.PauseTiming(); |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame^] | 179 | if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str()); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 180 | appSession->close(); |
| 181 | state.ResumeTiming(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static void BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate(benchmark::State& state) { |
| 186 | int64_t rate; |
| 187 | runBenchmark(state, 0us, &IPower::getHintSessionPreferredRate, &rate); |
| 188 | } |
| 189 | |
| 190 | static void BM_PowerHalAidlBenchmarks_updateTargetWorkDuration(benchmark::State& state) { |
| 191 | int64_t duration = 1000; |
| 192 | runSessionBenchmark(state, &IPowerHintSession::updateTargetWorkDuration, duration); |
| 193 | } |
| 194 | |
| 195 | static void BM_PowerHalAidlBenchmarks_reportActualWorkDuration(benchmark::State& state) { |
| 196 | runSessionBenchmark(state, &IPowerHintSession::reportActualWorkDuration, DURATIONS); |
| 197 | } |
| 198 | |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 199 | BENCHMARK(BM_PowerHalAidlBenchmarks_isBoostSupported)->DenseRange(FIRST_BOOST, LAST_BOOST, 1); |
| 200 | BENCHMARK(BM_PowerHalAidlBenchmarks_isModeSupported)->DenseRange(FIRST_MODE, LAST_MODE, 1); |
| 201 | BENCHMARK(BM_PowerHalAidlBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1); |
| 202 | BENCHMARK(BM_PowerHalAidlBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 203 | BENCHMARK(BM_PowerHalAidlBenchmarks_createHintSession)->Arg(1); |
| 204 | BENCHMARK(BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate); |
| 205 | BENCHMARK(BM_PowerHalAidlBenchmarks_updateTargetWorkDuration); |
| 206 | BENCHMARK(BM_PowerHalAidlBenchmarks_reportActualWorkDuration); |