xref: /aosp_15_r20/frameworks/native/services/powermanager/benchmarks/PowerHalControllerBenchmarks.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 "PowerHalControllerBenchmarks"
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/Mode.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <powermanager/PowerHalController.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <testUtil.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::power::Boost;
27*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::power::Mode;
28*38e8c45fSAndroid Build Coastguard Worker using android::power::HalResult;
29*38e8c45fSAndroid Build Coastguard Worker using android::power::PowerHalController;
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker using namespace android;
32*38e8c45fSAndroid Build Coastguard Worker using namespace std::chrono_literals;
33*38e8c45fSAndroid Build Coastguard Worker 
34*38e8c45fSAndroid Build Coastguard Worker // Values from Boost.aidl and Mode.aidl.
35*38e8c45fSAndroid Build Coastguard Worker static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION);
36*38e8c45fSAndroid Build Coastguard Worker static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT);
37*38e8c45fSAndroid Build Coastguard Worker static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE);
38*38e8c45fSAndroid Build Coastguard Worker static constexpr int64_t LAST_MODE = static_cast<int64_t>(Mode::CAMERA_STREAMING_HIGH);
39*38e8c45fSAndroid Build Coastguard Worker 
40*38e8c45fSAndroid Build Coastguard Worker // Delay between oneway method calls to avoid overflowing the binder buffers.
41*38e8c45fSAndroid Build Coastguard Worker static constexpr std::chrono::microseconds ONEWAY_API_DELAY = 100us;
42*38e8c45fSAndroid Build Coastguard Worker 
43*38e8c45fSAndroid Build Coastguard Worker template <typename T, class... Args0, class... Args1>
runBenchmark(benchmark::State & state,HalResult<T> (PowerHalController::* fn)(Args0...),Args1 &&...args1)44*38e8c45fSAndroid Build Coastguard Worker static void runBenchmark(benchmark::State& state, HalResult<T> (PowerHalController::*fn)(Args0...),
45*38e8c45fSAndroid Build Coastguard Worker                          Args1&&... args1) {
46*38e8c45fSAndroid Build Coastguard Worker     while (state.KeepRunning()) {
47*38e8c45fSAndroid Build Coastguard Worker         PowerHalController controller;
48*38e8c45fSAndroid Build Coastguard Worker         HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
49*38e8c45fSAndroid Build Coastguard Worker         state.PauseTiming();
50*38e8c45fSAndroid Build Coastguard Worker         if (ret.isFailed()) state.SkipWithError("Power HAL request failed");
51*38e8c45fSAndroid Build Coastguard Worker         state.ResumeTiming();
52*38e8c45fSAndroid Build Coastguard Worker     }
53*38e8c45fSAndroid Build Coastguard Worker }
54*38e8c45fSAndroid Build Coastguard Worker 
55*38e8c45fSAndroid Build Coastguard Worker template <typename T, class... Args0, class... Args1>
runCachedBenchmark(benchmark::State & state,HalResult<T> (PowerHalController::* fn)(Args0...),Args1 &&...args1)56*38e8c45fSAndroid Build Coastguard Worker static void runCachedBenchmark(benchmark::State& state,
57*38e8c45fSAndroid Build Coastguard Worker                                HalResult<T> (PowerHalController::*fn)(Args0...), Args1&&... args1) {
58*38e8c45fSAndroid Build Coastguard Worker     PowerHalController controller;
59*38e8c45fSAndroid Build Coastguard Worker     // First call out of test, to cache HAL service and isSupported result.
60*38e8c45fSAndroid Build Coastguard Worker     (controller.*fn)(std::forward<Args1>(args1)...);
61*38e8c45fSAndroid Build Coastguard Worker 
62*38e8c45fSAndroid Build Coastguard Worker     while (state.KeepRunning()) {
63*38e8c45fSAndroid Build Coastguard Worker         HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
64*38e8c45fSAndroid Build Coastguard Worker         state.PauseTiming();
65*38e8c45fSAndroid Build Coastguard Worker         if (ret.isFailed()) {
66*38e8c45fSAndroid Build Coastguard Worker             state.SkipWithError("Power HAL request failed");
67*38e8c45fSAndroid Build Coastguard Worker         }
68*38e8c45fSAndroid Build Coastguard Worker         testDelaySpin(
69*38e8c45fSAndroid Build Coastguard Worker                 std::chrono::duration_cast<std::chrono::duration<float>>(ONEWAY_API_DELAY).count());
70*38e8c45fSAndroid Build Coastguard Worker         state.ResumeTiming();
71*38e8c45fSAndroid Build Coastguard Worker     }
72*38e8c45fSAndroid Build Coastguard Worker }
73*38e8c45fSAndroid Build Coastguard Worker 
BM_PowerHalControllerBenchmarks_init(benchmark::State & state)74*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalControllerBenchmarks_init(benchmark::State& state) {
75*38e8c45fSAndroid Build Coastguard Worker     while (state.KeepRunning()) {
76*38e8c45fSAndroid Build Coastguard Worker         PowerHalController controller;
77*38e8c45fSAndroid Build Coastguard Worker         controller.init();
78*38e8c45fSAndroid Build Coastguard Worker     }
79*38e8c45fSAndroid Build Coastguard Worker }
80*38e8c45fSAndroid Build Coastguard Worker 
BM_PowerHalControllerBenchmarks_initCached(benchmark::State & state)81*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalControllerBenchmarks_initCached(benchmark::State& state) {
82*38e8c45fSAndroid Build Coastguard Worker     PowerHalController controller;
83*38e8c45fSAndroid Build Coastguard Worker     // First connection out of test.
84*38e8c45fSAndroid Build Coastguard Worker     controller.init();
85*38e8c45fSAndroid Build Coastguard Worker 
86*38e8c45fSAndroid Build Coastguard Worker     while (state.KeepRunning()) {
87*38e8c45fSAndroid Build Coastguard Worker         controller.init();
88*38e8c45fSAndroid Build Coastguard Worker     }
89*38e8c45fSAndroid Build Coastguard Worker }
90*38e8c45fSAndroid Build Coastguard Worker 
BM_PowerHalControllerBenchmarks_setBoost(benchmark::State & state)91*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalControllerBenchmarks_setBoost(benchmark::State& state) {
92*38e8c45fSAndroid Build Coastguard Worker     Boost boost = static_cast<Boost>(state.range(0));
93*38e8c45fSAndroid Build Coastguard Worker     runBenchmark(state, &PowerHalController::setBoost, boost, 0);
94*38e8c45fSAndroid Build Coastguard Worker }
95*38e8c45fSAndroid Build Coastguard Worker 
BM_PowerHalControllerBenchmarks_setBoostCached(benchmark::State & state)96*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalControllerBenchmarks_setBoostCached(benchmark::State& state) {
97*38e8c45fSAndroid Build Coastguard Worker     Boost boost = static_cast<Boost>(state.range(0));
98*38e8c45fSAndroid Build Coastguard Worker     runCachedBenchmark(state, &PowerHalController::setBoost, boost, 0);
99*38e8c45fSAndroid Build Coastguard Worker }
100*38e8c45fSAndroid Build Coastguard Worker 
BM_PowerHalControllerBenchmarks_setMode(benchmark::State & state)101*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalControllerBenchmarks_setMode(benchmark::State& state) {
102*38e8c45fSAndroid Build Coastguard Worker     Mode mode = static_cast<Mode>(state.range(0));
103*38e8c45fSAndroid Build Coastguard Worker     runBenchmark(state, &PowerHalController::setMode, mode, false);
104*38e8c45fSAndroid Build Coastguard Worker }
105*38e8c45fSAndroid Build Coastguard Worker 
BM_PowerHalControllerBenchmarks_setModeCached(benchmark::State & state)106*38e8c45fSAndroid Build Coastguard Worker static void BM_PowerHalControllerBenchmarks_setModeCached(benchmark::State& state) {
107*38e8c45fSAndroid Build Coastguard Worker     Mode mode = static_cast<Mode>(state.range(0));
108*38e8c45fSAndroid Build Coastguard Worker     runCachedBenchmark(state, &PowerHalController::setMode, mode, false);
109*38e8c45fSAndroid Build Coastguard Worker }
110*38e8c45fSAndroid Build Coastguard Worker 
111*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalControllerBenchmarks_init);
112*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalControllerBenchmarks_initCached);
113*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalControllerBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
114*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalControllerBenchmarks_setBoostCached)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
115*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalControllerBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1);
116*38e8c45fSAndroid Build Coastguard Worker BENCHMARK(BM_PowerHalControllerBenchmarks_setModeCached)->DenseRange(FIRST_MODE, LAST_MODE, 1);
117