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 "PowerHalHidlBenchmarks" |
| 18 | |
| 19 | #include <android/hardware/power/1.1/IPower.h> |
| 20 | #include <android/hardware/power/Boost.h> |
| 21 | #include <android/hardware/power/IPower.h> |
| 22 | #include <android/hardware/power/Mode.h> |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 23 | #include <benchmark/benchmark.h> |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 24 | #include <hardware/power.h> |
| 25 | #include <hardware_legacy/power.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 | |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 29 | using android::hardware::Return; |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 30 | using android::hardware::power::Boost; |
| 31 | using android::hardware::power::Mode; |
| 32 | using android::hardware::power::V1_0::Feature; |
| 33 | using android::hardware::power::V1_0::PowerHint; |
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 | using IPower1_0 = android::hardware::power::V1_0::IPower; |
| 36 | using IPower1_1 = android::hardware::power::V1_1::IPower; |
| 37 | |
| 38 | using namespace android; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 39 | using namespace std::chrono_literals; |
| 40 | |
| 41 | // Values from types.hal from versions 1.0 to 1.3. |
| 42 | static constexpr int64_t FIRST_POWER_HINT = static_cast<int64_t>(PowerHint::VSYNC); |
| 43 | static constexpr int64_t LAST_POWER_HINT = static_cast<int64_t>(PowerHint::LAUNCH); |
| 44 | |
| 45 | // Delay between oneway method calls to avoid overflowing the binder buffers. |
| 46 | static constexpr microseconds ONEWAY_API_DELAY = 100us; |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 47 | |
| 48 | template <class R, class I, class... Args0, class... Args1> |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 49 | static void runBenchmark(benchmark::State& state, microseconds delay, Return<R> (I::*fn)(Args0...), |
| 50 | Args1&&... args1) { |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 51 | sp<I> hal = I::getService(); |
| 52 | |
| 53 | if (hal == nullptr) { |
| 54 | ALOGI("Power HAL HIDL not available, skipping test..."); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | while (state.KeepRunning()) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 59 | Return<R> ret = (*hal.*fn)(std::forward<Args1>(args1)...); |
| 60 | state.PauseTiming(); |
| 61 | if (!ret.isOk()) state.SkipWithError(ret.description().c_str()); |
| 62 | if (delay > 0us) { |
| 63 | testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count()); |
| 64 | } |
| 65 | state.ResumeTiming(); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | static void BM_PowerHalHidlBenchmarks_setFeature(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 70 | runBenchmark(state, 0us, &IPower1_0::setFeature, Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, |
| 71 | false); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void BM_PowerHalHidlBenchmarks_setInteractive(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 75 | runBenchmark(state, 0us, &IPower1_0::setInteractive, false); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void BM_PowerHalHidlBenchmarks_powerHint(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 79 | PowerHint powerHint = static_cast<PowerHint>(state.range(0)); |
| 80 | runBenchmark(state, 0us, &IPower1_0::powerHint, powerHint, 0); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void BM_PowerHalHidlBenchmarks_powerHintAsync(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 84 | PowerHint powerHint = static_cast<PowerHint>(state.range(0)); |
| 85 | runBenchmark(state, ONEWAY_API_DELAY, &IPower1_1::powerHintAsync, powerHint, 0); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | BENCHMARK(BM_PowerHalHidlBenchmarks_setFeature); |
| 89 | BENCHMARK(BM_PowerHalHidlBenchmarks_setInteractive); |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame^] | 90 | BENCHMARK(BM_PowerHalHidlBenchmarks_powerHint)->DenseRange(FIRST_POWER_HINT, LAST_POWER_HINT, 1); |
| 91 | BENCHMARK(BM_PowerHalHidlBenchmarks_powerHintAsync) |
| 92 | ->DenseRange(FIRST_POWER_HINT, LAST_POWER_HINT, 1); |