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