blob: f8abc7aba6ecf91817946f5b21686a6bd3fc286d [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 "PowerHalControllerBenchmarks"
18
19#include <android/hardware/power/Boost.h>
20#include <android/hardware/power/Mode.h>
Lais Andrade159eb6a2020-06-24 15:11:05 +000021#include <benchmark/benchmark.h>
Lais Andrade159eb6a2020-06-24 15:11:05 +000022#include <powermanager/PowerHalController.h>
Lais Andrade14e97b72020-07-14 12:27:44 +000023#include <testUtil.h>
24#include <chrono>
Lais Andrade159eb6a2020-06-24 15:11:05 +000025
26using android::hardware::power::Boost;
27using android::hardware::power::Mode;
Lais Andrade14e97b72020-07-14 12:27:44 +000028using android::power::HalResult;
Lais Andrade159eb6a2020-06-24 15:11:05 +000029using android::power::PowerHalController;
30
31using namespace android;
Lais Andrade14e97b72020-07-14 12:27:44 +000032using namespace std::chrono_literals;
33
34// Values from Boost.aidl and Mode.aidl.
35static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION);
36static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT);
37static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE);
38static constexpr int64_t LAST_MODE = static_cast<int64_t>(Mode::CAMERA_STREAMING_HIGH);
39
40// Delay between oneway method calls to avoid overflowing the binder buffers.
41static constexpr std::chrono::microseconds ONEWAY_API_DELAY = 100us;
42
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080043template <typename T, class... Args0, class... Args1>
44static void runBenchmark(benchmark::State& state, HalResult<T> (PowerHalController::*fn)(Args0...),
Lais Andrade14e97b72020-07-14 12:27:44 +000045 Args1&&... args1) {
46 while (state.KeepRunning()) {
47 PowerHalController controller;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080048 HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
Lais Andrade14e97b72020-07-14 12:27:44 +000049 state.PauseTiming();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080050 if (ret.isFailed()) state.SkipWithError("Power HAL request failed");
Lais Andrade14e97b72020-07-14 12:27:44 +000051 state.ResumeTiming();
52 }
53}
54
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080055template <typename T, class... Args0, class... Args1>
Lais Andrade14e97b72020-07-14 12:27:44 +000056static void runCachedBenchmark(benchmark::State& state,
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080057 HalResult<T> (PowerHalController::*fn)(Args0...), Args1&&... args1) {
Lais Andrade14e97b72020-07-14 12:27:44 +000058 PowerHalController controller;
59 // First call out of test, to cache HAL service and isSupported result.
60 (controller.*fn)(std::forward<Args1>(args1)...);
61
62 while (state.KeepRunning()) {
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080063 HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
Lais Andrade14e97b72020-07-14 12:27:44 +000064 state.PauseTiming();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080065 if (ret.isFailed()) {
Lais Andrade14e97b72020-07-14 12:27:44 +000066 state.SkipWithError("Power HAL request failed");
67 }
68 testDelaySpin(
69 std::chrono::duration_cast<std::chrono::duration<float>>(ONEWAY_API_DELAY).count());
70 state.ResumeTiming();
71 }
72}
Lais Andrade159eb6a2020-06-24 15:11:05 +000073
74static void BM_PowerHalControllerBenchmarks_init(benchmark::State& state) {
75 while (state.KeepRunning()) {
76 PowerHalController controller;
77 controller.init();
78 }
79}
80
81static void BM_PowerHalControllerBenchmarks_initCached(benchmark::State& state) {
82 PowerHalController controller;
83 // First connection out of test.
84 controller.init();
85
86 while (state.KeepRunning()) {
87 controller.init();
88 }
89}
90
91static void BM_PowerHalControllerBenchmarks_setBoost(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000092 Boost boost = static_cast<Boost>(state.range(0));
93 runBenchmark(state, &PowerHalController::setBoost, boost, 0);
Lais Andrade159eb6a2020-06-24 15:11:05 +000094}
95
96static void BM_PowerHalControllerBenchmarks_setBoostCached(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000097 Boost boost = static_cast<Boost>(state.range(0));
98 runCachedBenchmark(state, &PowerHalController::setBoost, boost, 0);
Lais Andrade159eb6a2020-06-24 15:11:05 +000099}
100
101static void BM_PowerHalControllerBenchmarks_setMode(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000102 Mode mode = static_cast<Mode>(state.range(0));
103 runBenchmark(state, &PowerHalController::setMode, mode, false);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000104}
105
106static void BM_PowerHalControllerBenchmarks_setModeCached(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000107 Mode mode = static_cast<Mode>(state.range(0));
108 runCachedBenchmark(state, &PowerHalController::setMode, mode, false);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000109}
110
111BENCHMARK(BM_PowerHalControllerBenchmarks_init);
112BENCHMARK(BM_PowerHalControllerBenchmarks_initCached);
Lais Andrade14e97b72020-07-14 12:27:44 +0000113BENCHMARK(BM_PowerHalControllerBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
114BENCHMARK(BM_PowerHalControllerBenchmarks_setBoostCached)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
115BENCHMARK(BM_PowerHalControllerBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1);
116BENCHMARK(BM_PowerHalControllerBenchmarks_setModeCached)->DenseRange(FIRST_MODE, LAST_MODE, 1);