• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
21 #include <benchmark/benchmark.h>
22 #include <powermanager/PowerHalController.h>
23 #include <testUtil.h>
24 #include <chrono>
25 
26 using android::hardware::power::Boost;
27 using android::hardware::power::Mode;
28 using android::power::HalResult;
29 using android::power::PowerHalController;
30 
31 using namespace android;
32 using namespace std::chrono_literals;
33 
34 // Values from Boost.aidl and Mode.aidl.
35 static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION);
36 static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT);
37 static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE);
38 static 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.
41 static constexpr std::chrono::microseconds ONEWAY_API_DELAY = 100us;
42 
43 template <typename T, class... Args0, class... Args1>
runBenchmark(benchmark::State & state,HalResult<T> (PowerHalController::* fn)(Args0...),Args1 &&...args1)44 static void runBenchmark(benchmark::State& state, HalResult<T> (PowerHalController::*fn)(Args0...),
45                          Args1&&... args1) {
46     while (state.KeepRunning()) {
47         PowerHalController controller;
48         HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
49         state.PauseTiming();
50         if (ret.isFailed()) state.SkipWithError("Power HAL request failed");
51         state.ResumeTiming();
52     }
53 }
54 
55 template <typename T, class... Args0, class... Args1>
runCachedBenchmark(benchmark::State & state,HalResult<T> (PowerHalController::* fn)(Args0...),Args1 &&...args1)56 static void runCachedBenchmark(benchmark::State& state,
57                                HalResult<T> (PowerHalController::*fn)(Args0...), Args1&&... args1) {
58     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()) {
63         HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
64         state.PauseTiming();
65         if (ret.isFailed()) {
66             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 }
73 
BM_PowerHalControllerBenchmarks_init(benchmark::State & state)74 static void BM_PowerHalControllerBenchmarks_init(benchmark::State& state) {
75     while (state.KeepRunning()) {
76         PowerHalController controller;
77         controller.init();
78     }
79 }
80 
BM_PowerHalControllerBenchmarks_initCached(benchmark::State & state)81 static 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 
BM_PowerHalControllerBenchmarks_setBoost(benchmark::State & state)91 static void BM_PowerHalControllerBenchmarks_setBoost(benchmark::State& state) {
92     Boost boost = static_cast<Boost>(state.range(0));
93     runBenchmark(state, &PowerHalController::setBoost, boost, 0);
94 }
95 
BM_PowerHalControllerBenchmarks_setBoostCached(benchmark::State & state)96 static void BM_PowerHalControllerBenchmarks_setBoostCached(benchmark::State& state) {
97     Boost boost = static_cast<Boost>(state.range(0));
98     runCachedBenchmark(state, &PowerHalController::setBoost, boost, 0);
99 }
100 
BM_PowerHalControllerBenchmarks_setMode(benchmark::State & state)101 static void BM_PowerHalControllerBenchmarks_setMode(benchmark::State& state) {
102     Mode mode = static_cast<Mode>(state.range(0));
103     runBenchmark(state, &PowerHalController::setMode, mode, false);
104 }
105 
BM_PowerHalControllerBenchmarks_setModeCached(benchmark::State & state)106 static void BM_PowerHalControllerBenchmarks_setModeCached(benchmark::State& state) {
107     Mode mode = static_cast<Mode>(state.range(0));
108     runCachedBenchmark(state, &PowerHalController::setMode, mode, false);
109 }
110 
111 BENCHMARK(BM_PowerHalControllerBenchmarks_init);
112 BENCHMARK(BM_PowerHalControllerBenchmarks_initCached);
113 BENCHMARK(BM_PowerHalControllerBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
114 BENCHMARK(BM_PowerHalControllerBenchmarks_setBoostCached)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
115 BENCHMARK(BM_PowerHalControllerBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1);
116 BENCHMARK(BM_PowerHalControllerBenchmarks_setModeCached)->DenseRange(FIRST_MODE, LAST_MODE, 1);
117