• 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 <aidl/android/hardware/power/Boost.h>
20 #include <aidl/android/hardware/power/Mode.h>
21 #include <benchmark/benchmark.h>
22 #include <chrono>
23 #include <powermanager/PowerHalController.h>
24 #include <testUtil.h>
25 
26 using aidl::android::hardware::power::Boost;
27 using aidl::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>(*ndk::enum_range<Boost>().begin());
36 static constexpr int64_t LAST_BOOST = static_cast<int64_t>(*(ndk::enum_range<Boost>().end()-1));
37 static constexpr int64_t FIRST_MODE = static_cast<int64_t>(*ndk::enum_range<Mode>().begin());
38 static constexpr int64_t LAST_MODE = static_cast<int64_t>(*(ndk::enum_range<Mode>().end()-1));
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     PowerHalController initController;
47     HalResult<T> result = (initController.*fn)(std::forward<Args1>(args1)...);
48     if (result.isFailed()) {
49         state.SkipWithError(result.errorMessage());
50         return;
51     } else if (result.isUnsupported()) {
52         ALOGV("Power HAL does not support this operation, skipping test...");
53         state.SkipWithMessage("operation unsupported");
54         return;
55     }
56 
57     for (auto _ : state) {
58         PowerHalController controller; // new controller to avoid caching
59         HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
60         if (ret.isFailed()) {
61             state.SkipWithError(ret.errorMessage());
62             break;
63         }
64         state.PauseTiming();
65         testDelaySpin(
66                 std::chrono::duration_cast<std::chrono::duration<float>>(ONEWAY_API_DELAY).count());
67         state.ResumeTiming();
68     }
69 }
70 
71 template <typename T, class... Args0, class... Args1>
runCachedBenchmark(benchmark::State & state,HalResult<T> (PowerHalController::* fn)(Args0...),Args1 &&...args1)72 static void runCachedBenchmark(benchmark::State& state,
73                                HalResult<T> (PowerHalController::*fn)(Args0...), Args1&&... args1) {
74     PowerHalController controller;
75     // First call out of test, to cache HAL service and isSupported result.
76     HalResult<T> result = (controller.*fn)(std::forward<Args1>(args1)...);
77     if (result.isFailed()) {
78         state.SkipWithError(result.errorMessage());
79         return;
80     } else if (result.isUnsupported()) {
81         ALOGV("Power HAL does not support this operation, skipping test...");
82         state.SkipWithMessage("operation unsupported");
83         return;
84     }
85 
86     for (auto _ : state) {
87         HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
88         if (ret.isFailed()) {
89             state.SkipWithError(ret.errorMessage());
90             break;
91         }
92     }
93 }
94 
BM_PowerHalControllerBenchmarks_init(benchmark::State & state)95 static void BM_PowerHalControllerBenchmarks_init(benchmark::State& state) {
96     for (auto _ : state) {
97         PowerHalController controller;
98         controller.init();
99     }
100 }
101 
BM_PowerHalControllerBenchmarks_initCached(benchmark::State & state)102 static void BM_PowerHalControllerBenchmarks_initCached(benchmark::State& state) {
103     PowerHalController controller;
104     // First connection out of test.
105     controller.init();
106 
107     while (state.KeepRunning()) {
108         controller.init();
109     }
110 }
111 
BM_PowerHalControllerBenchmarks_setBoost(benchmark::State & state)112 static void BM_PowerHalControllerBenchmarks_setBoost(benchmark::State& state) {
113     Boost boost = static_cast<Boost>(state.range(0));
114     runBenchmark(state, &PowerHalController::setBoost, boost, 1);
115 }
116 
BM_PowerHalControllerBenchmarks_setBoostCached(benchmark::State & state)117 static void BM_PowerHalControllerBenchmarks_setBoostCached(benchmark::State& state) {
118     Boost boost = static_cast<Boost>(state.range(0));
119     runCachedBenchmark(state, &PowerHalController::setBoost, boost, 1);
120 }
121 
BM_PowerHalControllerBenchmarks_setMode(benchmark::State & state)122 static void BM_PowerHalControllerBenchmarks_setMode(benchmark::State& state) {
123     Mode mode = static_cast<Mode>(state.range(0));
124     runBenchmark(state, &PowerHalController::setMode, mode, false);
125 }
126 
BM_PowerHalControllerBenchmarks_setModeCached(benchmark::State & state)127 static void BM_PowerHalControllerBenchmarks_setModeCached(benchmark::State& state) {
128     Mode mode = static_cast<Mode>(state.range(0));
129     runCachedBenchmark(state, &PowerHalController::setMode, mode, false);
130 }
131 
132 BENCHMARK(BM_PowerHalControllerBenchmarks_init);
133 BENCHMARK(BM_PowerHalControllerBenchmarks_initCached);
134 BENCHMARK(BM_PowerHalControllerBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
135 BENCHMARK(BM_PowerHalControllerBenchmarks_setBoostCached)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
136 BENCHMARK(BM_PowerHalControllerBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1);
137 BENCHMARK(BM_PowerHalControllerBenchmarks_setModeCached)->DenseRange(FIRST_MODE, LAST_MODE, 1);
138