blob: 97e026bd25bfbb527feeb7d760dbd130cb5d4699 [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
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 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::Boost;
31using android::hardware::power::Mode;
32using android::hardware::power::V1_0::Feature;
33using android::hardware::power::V1_0::PowerHint;
Lais Andrade14e97b72020-07-14 12:27:44 +000034using std::chrono::microseconds;
Lais Andrade159eb6a2020-06-24 15:11:05 +000035using IPower1_0 = android::hardware::power::V1_0::IPower;
36using IPower1_1 = android::hardware::power::V1_1::IPower;
37
38using namespace android;
Lais Andrade14e97b72020-07-14 12:27:44 +000039using namespace std::chrono_literals;
40
41// Values from types.hal from versions 1.0 to 1.3.
42static constexpr int64_t FIRST_POWER_HINT = static_cast<int64_t>(PowerHint::VSYNC);
43static 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.
46static constexpr microseconds ONEWAY_API_DELAY = 100us;
Lais Andrade159eb6a2020-06-24 15:11:05 +000047
48template <class R, class I, class... Args0, class... Args1>
Lais Andrade14e97b72020-07-14 12:27:44 +000049static void runBenchmark(benchmark::State& state, microseconds delay, Return<R> (I::*fn)(Args0...),
50 Args1&&... args1) {
Lais Andrade159eb6a2020-06-24 15:11:05 +000051 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 Andrade14e97b72020-07-14 12:27:44 +000059 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 Andrade159eb6a2020-06-24 15:11:05 +000066 }
67}
68
69static void BM_PowerHalHidlBenchmarks_setFeature(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000070 runBenchmark(state, 0us, &IPower1_0::setFeature, Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE,
71 false);
Lais Andrade159eb6a2020-06-24 15:11:05 +000072}
73
74static void BM_PowerHalHidlBenchmarks_setInteractive(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000075 runBenchmark(state, 0us, &IPower1_0::setInteractive, false);
Lais Andrade159eb6a2020-06-24 15:11:05 +000076}
77
78static void BM_PowerHalHidlBenchmarks_powerHint(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000079 PowerHint powerHint = static_cast<PowerHint>(state.range(0));
80 runBenchmark(state, 0us, &IPower1_0::powerHint, powerHint, 0);
Lais Andrade159eb6a2020-06-24 15:11:05 +000081}
82
83static void BM_PowerHalHidlBenchmarks_powerHintAsync(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000084 PowerHint powerHint = static_cast<PowerHint>(state.range(0));
85 runBenchmark(state, ONEWAY_API_DELAY, &IPower1_1::powerHintAsync, powerHint, 0);
Lais Andrade159eb6a2020-06-24 15:11:05 +000086}
87
88BENCHMARK(BM_PowerHalHidlBenchmarks_setFeature);
89BENCHMARK(BM_PowerHalHidlBenchmarks_setInteractive);
Lais Andrade14e97b72020-07-14 12:27:44 +000090BENCHMARK(BM_PowerHalHidlBenchmarks_powerHint)->DenseRange(FIRST_POWER_HINT, LAST_POWER_HINT, 1);
91BENCHMARK(BM_PowerHalHidlBenchmarks_powerHintAsync)
92 ->DenseRange(FIRST_POWER_HINT, LAST_POWER_HINT, 1);