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..."); |
Matt Buckley | eefb6a4 | 2024-02-15 18:19:27 +0000 | [diff] [blame^] | 73 | state.SkipWithMessage("Power HAL unavailable"); |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 77 | ndk::ScopedAStatus ret = (*hal.*fn)(std::forward<Args1>(args1)...); |
| 78 | if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 79 | ALOGV("Power HAL does not support this operation, skipping test..."); |
Matt Buckley | eefb6a4 | 2024-02-15 18:19:27 +0000 | [diff] [blame^] | 80 | state.SkipWithMessage("operation unsupported"); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
| 84 | while (state.KeepRunning()) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 85 | ret = (*hal.*fn)(std::forward<Args1>(args1)...); |
| 86 | state.PauseTiming(); |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 87 | if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str()); |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 88 | if (delay > 0us) { |
| 89 | testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count()); |
| 90 | } |
| 91 | state.ResumeTiming(); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 95 | template <class R, class... Args0, class... Args1> |
| 96 | static void runSessionBenchmark(benchmark::State& state, R (IPowerHintSession::*fn)(Args0...), |
| 97 | Args1&&... args1) { |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 98 | std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl(); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 99 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 100 | if (hal == nullptr) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 101 | ALOGV("Power HAL not available, skipping test..."); |
Matt Buckley | eefb6a4 | 2024-02-15 18:19:27 +0000 | [diff] [blame^] | 102 | state.SkipWithMessage("Power HAL unavailable"); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
| 106 | // do not use tid from the benchmark process, use 1 for init |
| 107 | std::vector<int32_t> threadIds{1}; |
| 108 | int64_t durationNanos = 16666666L; |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 109 | std::shared_ptr<IPowerHintSession> session; |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 110 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 111 | auto status = hal->createHintSession(1, 0, threadIds, durationNanos, &session); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 112 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 113 | if (session == nullptr) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 114 | ALOGV("Power HAL doesn't support session, skipping test..."); |
Matt Buckley | eefb6a4 | 2024-02-15 18:19:27 +0000 | [diff] [blame^] | 115 | state.SkipWithMessage("operation unsupported"); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 119 | ndk::ScopedAStatus ret = (*session.*fn)(std::forward<Args1>(args1)...); |
| 120 | if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 121 | ALOGV("Power HAL does not support this operation, skipping test..."); |
Matt Buckley | eefb6a4 | 2024-02-15 18:19:27 +0000 | [diff] [blame^] | 122 | state.SkipWithMessage("operation unsupported"); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
| 126 | while (state.KeepRunning()) { |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 127 | ret = (*session.*fn)(std::forward<Args1>(args1)...); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 128 | state.PauseTiming(); |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 129 | if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str()); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 130 | if (ONEWAY_API_DELAY > 0us) { |
| 131 | testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(ONEWAY_API_DELAY) |
| 132 | .count()); |
| 133 | } |
| 134 | state.ResumeTiming(); |
| 135 | } |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 136 | session->close(); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 137 | } |
| 138 | |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 139 | static void BM_PowerHalAidlBenchmarks_isBoostSupported(benchmark::State& state) { |
| 140 | bool isSupported; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 141 | Boost boost = static_cast<Boost>(state.range(0)); |
| 142 | runBenchmark(state, 0us, &IPower::isBoostSupported, boost, &isSupported); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void BM_PowerHalAidlBenchmarks_isModeSupported(benchmark::State& state) { |
| 146 | bool isSupported; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 147 | Mode mode = static_cast<Mode>(state.range(0)); |
| 148 | runBenchmark(state, 0us, &IPower::isModeSupported, mode, &isSupported); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void BM_PowerHalAidlBenchmarks_setBoost(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 152 | Boost boost = static_cast<Boost>(state.range(0)); |
| 153 | runBenchmark(state, ONEWAY_API_DELAY, &IPower::setBoost, boost, 1); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void BM_PowerHalAidlBenchmarks_setMode(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 157 | Mode mode = static_cast<Mode>(state.range(0)); |
| 158 | runBenchmark(state, ONEWAY_API_DELAY, &IPower::setMode, mode, false); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 161 | static void BM_PowerHalAidlBenchmarks_createHintSession(benchmark::State& state) { |
| 162 | std::vector<int32_t> threadIds{static_cast<int32_t>(state.range(0))}; |
| 163 | int64_t durationNanos = 16666666L; |
| 164 | int32_t tgid = 999; |
| 165 | int32_t uid = 1001; |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 166 | std::shared_ptr<IPowerHintSession> appSession; |
| 167 | std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl(); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 168 | |
| 169 | if (hal == nullptr) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 170 | ALOGV("Power HAL not available, skipping test..."); |
Matt Buckley | eefb6a4 | 2024-02-15 18:19:27 +0000 | [diff] [blame^] | 171 | state.SkipWithMessage("Power HAL unavailable"); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 175 | ndk::ScopedAStatus ret = |
| 176 | hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession); |
| 177 | if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
Lais Andrade | deacc12 | 2022-03-10 18:47:37 +0000 | [diff] [blame] | 178 | ALOGV("Power HAL does not support this operation, skipping test..."); |
Matt Buckley | eefb6a4 | 2024-02-15 18:19:27 +0000 | [diff] [blame^] | 179 | state.SkipWithMessage("operation unsupported"); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | |
| 183 | while (state.KeepRunning()) { |
| 184 | ret = hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession); |
| 185 | state.PauseTiming(); |
Xiang Wang | a525c45 | 2023-05-22 13:13:26 -0700 | [diff] [blame] | 186 | if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str()); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 187 | appSession->close(); |
| 188 | state.ResumeTiming(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate(benchmark::State& state) { |
| 193 | int64_t rate; |
| 194 | runBenchmark(state, 0us, &IPower::getHintSessionPreferredRate, &rate); |
| 195 | } |
| 196 | |
| 197 | static void BM_PowerHalAidlBenchmarks_updateTargetWorkDuration(benchmark::State& state) { |
| 198 | int64_t duration = 1000; |
| 199 | runSessionBenchmark(state, &IPowerHintSession::updateTargetWorkDuration, duration); |
| 200 | } |
| 201 | |
| 202 | static void BM_PowerHalAidlBenchmarks_reportActualWorkDuration(benchmark::State& state) { |
| 203 | runSessionBenchmark(state, &IPowerHintSession::reportActualWorkDuration, DURATIONS); |
| 204 | } |
| 205 | |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 206 | BENCHMARK(BM_PowerHalAidlBenchmarks_isBoostSupported)->DenseRange(FIRST_BOOST, LAST_BOOST, 1); |
| 207 | BENCHMARK(BM_PowerHalAidlBenchmarks_isModeSupported)->DenseRange(FIRST_MODE, LAST_MODE, 1); |
| 208 | BENCHMARK(BM_PowerHalAidlBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1); |
| 209 | BENCHMARK(BM_PowerHalAidlBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 210 | BENCHMARK(BM_PowerHalAidlBenchmarks_createHintSession)->Arg(1); |
| 211 | BENCHMARK(BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate); |
| 212 | BENCHMARK(BM_PowerHalAidlBenchmarks_updateTargetWorkDuration); |
| 213 | BENCHMARK(BM_PowerHalAidlBenchmarks_reportActualWorkDuration); |