1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "PowerHalHidlBenchmarks"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <aidl/android/hardware/power/Boost.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <aidl/android/hardware/power/IPower.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <aidl/android/hardware/power/Mode.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <android/hardware/power/1.1/IPower.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <hardware/power.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <hardware_legacy/power.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <testUtil.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
28*38e8c45fSAndroid Build Coastguard Worker
29*38e8c45fSAndroid Build Coastguard Worker using android::hardware::Return;
30*38e8c45fSAndroid Build Coastguard Worker using android::hardware::power::V1_0::Feature;
31*38e8c45fSAndroid Build Coastguard Worker using android::hardware::power::V1_0::PowerHint;
32*38e8c45fSAndroid Build Coastguard Worker using std::chrono::microseconds;
33*38e8c45fSAndroid Build Coastguard Worker using IPower1_0 = android::hardware::power::V1_0::IPower;
34*38e8c45fSAndroid Build Coastguard Worker using IPower1_1 = android::hardware::power::V1_1::IPower;
35*38e8c45fSAndroid Build Coastguard Worker
36*38e8c45fSAndroid Build Coastguard Worker using namespace android;
37*38e8c45fSAndroid Build Coastguard Worker using namespace std::chrono_literals;
38*38e8c45fSAndroid Build Coastguard Worker
39*38e8c45fSAndroid Build Coastguard Worker // Values from types.hal from versions 1.0 to 1.3.
40*38e8c45fSAndroid Build Coastguard Worker static constexpr int64_t FIRST_POWER_HINT = static_cast<int64_t>(PowerHint::VSYNC);
41*38e8c45fSAndroid Build Coastguard Worker static constexpr int64_t LAST_POWER_HINT = static_cast<int64_t>(PowerHint::LAUNCH);
42*38e8c45fSAndroid Build Coastguard Worker
43*38e8c45fSAndroid Build Coastguard Worker // Delay between oneway method calls to avoid overflowing the binder buffers.
44*38e8c45fSAndroid Build Coastguard Worker static constexpr microseconds ONEWAY_API_DELAY = 100us;
45*38e8c45fSAndroid Build Coastguard Worker
46*38e8c45fSAndroid Build Coastguard Worker template <class R, class I, class... Args0, class... Args1>
runBenchmark(benchmark::State & state,microseconds delay,Return<R> (I::* fn)(Args0...),Args1 &&...args1)47*38e8c45fSAndroid Build Coastguard Worker static void runBenchmark(benchmark::State& state, microseconds delay, Return<R> (I::*fn)(Args0...),
48*38e8c45fSAndroid Build Coastguard Worker Args1&&... args1) {
49*38e8c45fSAndroid Build Coastguard Worker sp<I> hal = I::getService();
50*38e8c45fSAndroid Build Coastguard Worker
51*38e8c45fSAndroid Build Coastguard Worker if (hal == nullptr) {
52*38e8c45fSAndroid Build Coastguard Worker ALOGV("Power HAL HIDL not available, skipping test...");
53*38e8c45fSAndroid Build Coastguard Worker state.SkipWithMessage("Power HAL unavailable");
54*38e8c45fSAndroid Build Coastguard Worker return;
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker
57*38e8c45fSAndroid Build Coastguard Worker while (state.KeepRunning()) {
58*38e8c45fSAndroid Build Coastguard Worker Return<R> ret = (*hal.*fn)(std::forward<Args1>(args1)...);
59*38e8c45fSAndroid Build Coastguard Worker state.PauseTiming();
60*38e8c45fSAndroid Build Coastguard Worker if (!ret.isOk()) state.SkipWithError(ret.description().c_str());
61*38e8c45fSAndroid Build Coastguard Worker if (delay > 0us) {
62*38e8c45fSAndroid Build Coastguard Worker testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count());
63*38e8c45fSAndroid Build Coastguard Worker }
64*38e8c45fSAndroid Build Coastguard Worker state.ResumeTiming();
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker }
67*38e8c45fSAndroid Build Coastguard Worker
BM_PowerHalHidlBenchmarks_setFeature(benchmark::State & state)68*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalHidlBenchmarks_setFeature(benchmark::State& state) {
69*38e8c45fSAndroid Build Coastguard Worker runBenchmark(state, 0us, &IPower1_0::setFeature, Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE,
70*38e8c45fSAndroid Build Coastguard Worker false);
71*38e8c45fSAndroid Build Coastguard Worker }
72*38e8c45fSAndroid Build Coastguard Worker
BM_PowerHalHidlBenchmarks_setInteractive(benchmark::State & state)73*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalHidlBenchmarks_setInteractive(benchmark::State& state) {
74*38e8c45fSAndroid Build Coastguard Worker runBenchmark(state, 0us, &IPower1_0::setInteractive, false);
75*38e8c45fSAndroid Build Coastguard Worker }
76*38e8c45fSAndroid Build Coastguard Worker
BM_PowerHalHidlBenchmarks_powerHint(benchmark::State & state)77*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalHidlBenchmarks_powerHint(benchmark::State& state) {
78*38e8c45fSAndroid Build Coastguard Worker PowerHint powerHint = static_cast<PowerHint>(state.range(0));
79*38e8c45fSAndroid Build Coastguard Worker runBenchmark(state, 0us, &IPower1_0::powerHint, powerHint, 0);
80*38e8c45fSAndroid Build Coastguard Worker }
81*38e8c45fSAndroid Build Coastguard Worker
BM_PowerHalHidlBenchmarks_powerHintAsync(benchmark::State & state)82*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalHidlBenchmarks_powerHintAsync(benchmark::State& state) {
83*38e8c45fSAndroid Build Coastguard Worker PowerHint powerHint = static_cast<PowerHint>(state.range(0));
84*38e8c45fSAndroid Build Coastguard Worker runBenchmark(state, ONEWAY_API_DELAY, &IPower1_1::powerHintAsync, powerHint, 0);
85*38e8c45fSAndroid Build Coastguard Worker }
86*38e8c45fSAndroid Build Coastguard Worker
87*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalHidlBenchmarks_setFeature);
88*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalHidlBenchmarks_setInteractive);
89*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalHidlBenchmarks_powerHint)->DenseRange(FIRST_POWER_HINT, LAST_POWER_HINT, 1);
90*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalHidlBenchmarks_powerHintAsync)
91*38e8c45fSAndroid Build Coastguard Worker ->DenseRange(FIRST_POWER_HINT, LAST_POWER_HINT, 1);
92