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