blob: 111b5d70bc5eafd7d5f43931e6eee6b4c4cea660 [file] [log] [blame]
Lais Andrade159eb6a2020-06-24 15:11:05 +00001/*
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
Xiang Wanga525c452023-05-22 13:13:26 -070019#include <aidl/android/hardware/power/Boost.h>
20#include <aidl/android/hardware/power/IPower.h>
21#include <aidl/android/hardware/power/Mode.h>
Lais Andrade159eb6a2020-06-24 15:11:05 +000022#include <android/hardware/power/1.1/IPower.h>
Lais Andrade159eb6a2020-06-24 15:11:05 +000023#include <benchmark/benchmark.h>
Lais Andrade159eb6a2020-06-24 15:11:05 +000024#include <hardware/power.h>
25#include <hardware_legacy/power.h>
Lais Andrade14e97b72020-07-14 12:27:44 +000026#include <testUtil.h>
27#include <chrono>
Lais Andrade159eb6a2020-06-24 15:11:05 +000028
Lais Andrade14e97b72020-07-14 12:27:44 +000029using android::hardware::Return;
Lais Andrade159eb6a2020-06-24 15:11:05 +000030using android::hardware::power::V1_0::Feature;
31using android::hardware::power::V1_0::PowerHint;
Lais Andrade14e97b72020-07-14 12:27:44 +000032using std::chrono::microseconds;
Lais Andrade159eb6a2020-06-24 15:11:05 +000033using IPower1_0 = android::hardware::power::V1_0::IPower;
34using IPower1_1 = android::hardware::power::V1_1::IPower;
35
36using namespace android;
Lais Andrade14e97b72020-07-14 12:27:44 +000037using namespace std::chrono_literals;
38
39// Values from types.hal from versions 1.0 to 1.3.
40static constexpr int64_t FIRST_POWER_HINT = static_cast<int64_t>(PowerHint::VSYNC);
41static 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.
44static constexpr microseconds ONEWAY_API_DELAY = 100us;
Lais Andrade159eb6a2020-06-24 15:11:05 +000045
46template <class R, class I, class... Args0, class... Args1>
Lais Andrade14e97b72020-07-14 12:27:44 +000047static void runBenchmark(benchmark::State& state, microseconds delay, Return<R> (I::*fn)(Args0...),
48 Args1&&... args1) {
Lais Andrade159eb6a2020-06-24 15:11:05 +000049 sp<I> hal = I::getService();
50
51 if (hal == nullptr) {
Lais Andradedeacc122022-03-10 18:47:37 +000052 ALOGV("Power HAL HIDL not available, skipping test...");
Lais Andrade159eb6a2020-06-24 15:11:05 +000053 return;
54 }
55
56 while (state.KeepRunning()) {
Lais Andrade14e97b72020-07-14 12:27:44 +000057 Return<R> ret = (*hal.*fn)(std::forward<Args1>(args1)...);
58 state.PauseTiming();
59 if (!ret.isOk()) state.SkipWithError(ret.description().c_str());
60 if (delay > 0us) {
61 testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count());
62 }
63 state.ResumeTiming();
Lais Andrade159eb6a2020-06-24 15:11:05 +000064 }
65}
66
67static void BM_PowerHalHidlBenchmarks_setFeature(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000068 runBenchmark(state, 0us, &IPower1_0::setFeature, Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE,
69 false);
Lais Andrade159eb6a2020-06-24 15:11:05 +000070}
71
72static void BM_PowerHalHidlBenchmarks_setInteractive(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000073 runBenchmark(state, 0us, &IPower1_0::setInteractive, false);
Lais Andrade159eb6a2020-06-24 15:11:05 +000074}
75
76static void BM_PowerHalHidlBenchmarks_powerHint(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000077 PowerHint powerHint = static_cast<PowerHint>(state.range(0));
78 runBenchmark(state, 0us, &IPower1_0::powerHint, powerHint, 0);
Lais Andrade159eb6a2020-06-24 15:11:05 +000079}
80
81static void BM_PowerHalHidlBenchmarks_powerHintAsync(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000082 PowerHint powerHint = static_cast<PowerHint>(state.range(0));
83 runBenchmark(state, ONEWAY_API_DELAY, &IPower1_1::powerHintAsync, powerHint, 0);
Lais Andrade159eb6a2020-06-24 15:11:05 +000084}
85
86BENCHMARK(BM_PowerHalHidlBenchmarks_setFeature);
87BENCHMARK(BM_PowerHalHidlBenchmarks_setInteractive);
Lais Andrade14e97b72020-07-14 12:27:44 +000088BENCHMARK(BM_PowerHalHidlBenchmarks_powerHint)->DenseRange(FIRST_POWER_HINT, LAST_POWER_HINT, 1);
89BENCHMARK(BM_PowerHalHidlBenchmarks_powerHintAsync)
90 ->DenseRange(FIRST_POWER_HINT, LAST_POWER_HINT, 1);