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>
23 #include <benchmark/benchmark.h>
24 #include <hardware/power.h>
25 #include <hardware_legacy/power.h>
26 #include <testUtil.h>
27 #include <chrono>
28
29 using android::hardware::Return;
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;
34 using std::chrono::microseconds;
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;
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;
47
48 template <class R, class I, class... Args0, class... Args1>
runBenchmark(benchmark::State & state,microseconds delay,Return<R> (I::* fn)(Args0...),Args1 &&...args1)49 static void runBenchmark(benchmark::State& state, microseconds delay, Return<R> (I::*fn)(Args0...),
50 Args1&&... args1) {
51 sp<I> hal = I::getService();
52
53 if (hal == nullptr) {
54 ALOGV("Power HAL HIDL not available, skipping test...");
55 return;
56 }
57
58 while (state.KeepRunning()) {
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();
66 }
67 }
68
BM_PowerHalHidlBenchmarks_setFeature(benchmark::State & state)69 static void BM_PowerHalHidlBenchmarks_setFeature(benchmark::State& state) {
70 runBenchmark(state, 0us, &IPower1_0::setFeature, Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE,
71 false);
72 }
73
BM_PowerHalHidlBenchmarks_setInteractive(benchmark::State & state)74 static void BM_PowerHalHidlBenchmarks_setInteractive(benchmark::State& state) {
75 runBenchmark(state, 0us, &IPower1_0::setInteractive, false);
76 }
77
BM_PowerHalHidlBenchmarks_powerHint(benchmark::State & state)78 static void BM_PowerHalHidlBenchmarks_powerHint(benchmark::State& state) {
79 PowerHint powerHint = static_cast<PowerHint>(state.range(0));
80 runBenchmark(state, 0us, &IPower1_0::powerHint, powerHint, 0);
81 }
82
BM_PowerHalHidlBenchmarks_powerHintAsync(benchmark::State & state)83 static void BM_PowerHalHidlBenchmarks_powerHintAsync(benchmark::State& state) {
84 PowerHint powerHint = static_cast<PowerHint>(state.range(0));
85 runBenchmark(state, ONEWAY_API_DELAY, &IPower1_1::powerHintAsync, powerHint, 0);
86 }
87
88 BENCHMARK(BM_PowerHalHidlBenchmarks_setFeature);
89 BENCHMARK(BM_PowerHalHidlBenchmarks_setInteractive);
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);
93