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 "PowerHalAidlBenchmarks"
18
19 #include <aidl/android/hardware/power/Boost.h>
20 #include <aidl/android/hardware/power/IPower.h>
21 #include <aidl/android/hardware/power/IPowerHintSession.h>
22 #include <aidl/android/hardware/power/Mode.h>
23 #include <aidl/android/hardware/power/WorkDuration.h>
24 #include <benchmark/benchmark.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/Status.h>
27 #include <powermanager/PowerHalLoader.h>
28 #include <testUtil.h>
29 #include <chrono>
30
31 using aidl::android::hardware::power::Boost;
32 using aidl::android::hardware::power::IPower;
33 using aidl::android::hardware::power::IPowerHintSession;
34 using aidl::android::hardware::power::Mode;
35 using aidl::android::hardware::power::WorkDuration;
36 using android::power::PowerHalLoader;
37 using std::chrono::microseconds;
38
39 using namespace android;
40 using namespace std::chrono_literals;
41
42 // Values from Boost.aidl and Mode.aidl.
43 static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION);
44 static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT);
45 static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE);
46 static constexpr int64_t LAST_MODE = static_cast<int64_t>(Mode::CAMERA_STREAMING_HIGH);
47
48 class DurationWrapper : public WorkDuration {
49 public:
DurationWrapper(int64_t dur,int64_t time)50 DurationWrapper(int64_t dur, int64_t time) {
51 durationNanos = dur;
52 timeStampNanos = time;
53 }
54 };
55
56 static const std::vector<WorkDuration> DURATIONS = {
57 DurationWrapper(1L, 1L),
58 DurationWrapper(1000L, 2L),
59 DurationWrapper(1000000L, 3L),
60 DurationWrapper(1000000000L, 4L),
61 };
62
63 // Delay between oneway method calls to avoid overflowing the binder buffers.
64 static constexpr microseconds ONEWAY_API_DELAY = 100us;
65
66 template <class R, class... Args0, class... Args1>
runBenchmark(benchmark::State & state,microseconds delay,R (IPower::* fn)(Args0...),Args1 &&...args1)67 static void runBenchmark(benchmark::State& state, microseconds delay, R (IPower::*fn)(Args0...),
68 Args1&&... args1) {
69 std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl();
70
71 if (hal == nullptr) {
72 ALOGV("Power HAL not available, skipping test...");
73 state.SkipWithMessage("Power HAL unavailable");
74 return;
75 }
76
77 ndk::ScopedAStatus ret = (*hal.*fn)(std::forward<Args1>(args1)...);
78 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
79 ALOGV("Power HAL does not support this operation, skipping test...");
80 state.SkipWithMessage("operation unsupported");
81 return;
82 }
83
84 while (state.KeepRunning()) {
85 ret = (*hal.*fn)(std::forward<Args1>(args1)...);
86 state.PauseTiming();
87 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
88 if (delay > 0us) {
89 testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count());
90 }
91 state.ResumeTiming();
92 }
93 }
94
95 template <class R, class... Args0, class... Args1>
runSessionBenchmark(benchmark::State & state,R (IPowerHintSession::* fn)(Args0...),Args1 &&...args1)96 static void runSessionBenchmark(benchmark::State& state, R (IPowerHintSession::*fn)(Args0...),
97 Args1&&... args1) {
98 std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl();
99
100 if (hal == nullptr) {
101 ALOGV("Power HAL not available, skipping test...");
102 state.SkipWithMessage("Power HAL unavailable");
103 return;
104 }
105
106 // do not use tid from the benchmark process, use 1 for init
107 std::vector<int32_t> threadIds{1};
108 int64_t durationNanos = 16666666L;
109 std::shared_ptr<IPowerHintSession> session;
110
111 auto status = hal->createHintSession(1, 0, threadIds, durationNanos, &session);
112
113 if (session == nullptr) {
114 ALOGV("Power HAL doesn't support session, skipping test...");
115 state.SkipWithMessage("operation unsupported");
116 return;
117 }
118
119 ndk::ScopedAStatus ret = (*session.*fn)(std::forward<Args1>(args1)...);
120 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
121 ALOGV("Power HAL does not support this operation, skipping test...");
122 state.SkipWithMessage("operation unsupported");
123 return;
124 }
125
126 while (state.KeepRunning()) {
127 ret = (*session.*fn)(std::forward<Args1>(args1)...);
128 state.PauseTiming();
129 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
130 if (ONEWAY_API_DELAY > 0us) {
131 testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(ONEWAY_API_DELAY)
132 .count());
133 }
134 state.ResumeTiming();
135 }
136 session->close();
137 }
138
BM_PowerHalAidlBenchmarks_isBoostSupported(benchmark::State & state)139 static void BM_PowerHalAidlBenchmarks_isBoostSupported(benchmark::State& state) {
140 bool isSupported;
141 Boost boost = static_cast<Boost>(state.range(0));
142 runBenchmark(state, 0us, &IPower::isBoostSupported, boost, &isSupported);
143 }
144
BM_PowerHalAidlBenchmarks_isModeSupported(benchmark::State & state)145 static void BM_PowerHalAidlBenchmarks_isModeSupported(benchmark::State& state) {
146 bool isSupported;
147 Mode mode = static_cast<Mode>(state.range(0));
148 runBenchmark(state, 0us, &IPower::isModeSupported, mode, &isSupported);
149 }
150
BM_PowerHalAidlBenchmarks_setBoost(benchmark::State & state)151 static void BM_PowerHalAidlBenchmarks_setBoost(benchmark::State& state) {
152 Boost boost = static_cast<Boost>(state.range(0));
153 runBenchmark(state, ONEWAY_API_DELAY, &IPower::setBoost, boost, 1);
154 }
155
BM_PowerHalAidlBenchmarks_setMode(benchmark::State & state)156 static void BM_PowerHalAidlBenchmarks_setMode(benchmark::State& state) {
157 Mode mode = static_cast<Mode>(state.range(0));
158 runBenchmark(state, ONEWAY_API_DELAY, &IPower::setMode, mode, false);
159 }
160
BM_PowerHalAidlBenchmarks_createHintSession(benchmark::State & state)161 static void BM_PowerHalAidlBenchmarks_createHintSession(benchmark::State& state) {
162 std::vector<int32_t> threadIds{static_cast<int32_t>(state.range(0))};
163 int64_t durationNanos = 16666666L;
164 int32_t tgid = 999;
165 int32_t uid = 1001;
166 std::shared_ptr<IPowerHintSession> appSession;
167 std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl();
168
169 if (hal == nullptr) {
170 ALOGV("Power HAL not available, skipping test...");
171 state.SkipWithMessage("Power HAL unavailable");
172 return;
173 }
174
175 ndk::ScopedAStatus ret =
176 hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession);
177 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
178 ALOGV("Power HAL does not support this operation, skipping test...");
179 state.SkipWithMessage("operation unsupported");
180 return;
181 }
182
183 while (state.KeepRunning()) {
184 ret = hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession);
185 state.PauseTiming();
186 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
187 appSession->close();
188 state.ResumeTiming();
189 }
190 }
191
BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate(benchmark::State & state)192 static void BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate(benchmark::State& state) {
193 int64_t rate;
194 runBenchmark(state, 0us, &IPower::getHintSessionPreferredRate, &rate);
195 }
196
BM_PowerHalAidlBenchmarks_updateTargetWorkDuration(benchmark::State & state)197 static void BM_PowerHalAidlBenchmarks_updateTargetWorkDuration(benchmark::State& state) {
198 int64_t duration = 1000;
199 runSessionBenchmark(state, &IPowerHintSession::updateTargetWorkDuration, duration);
200 }
201
BM_PowerHalAidlBenchmarks_reportActualWorkDuration(benchmark::State & state)202 static void BM_PowerHalAidlBenchmarks_reportActualWorkDuration(benchmark::State& state) {
203 runSessionBenchmark(state, &IPowerHintSession::reportActualWorkDuration, DURATIONS);
204 }
205
206 BENCHMARK(BM_PowerHalAidlBenchmarks_isBoostSupported)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
207 BENCHMARK(BM_PowerHalAidlBenchmarks_isModeSupported)->DenseRange(FIRST_MODE, LAST_MODE, 1);
208 BENCHMARK(BM_PowerHalAidlBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
209 BENCHMARK(BM_PowerHalAidlBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1);
210 BENCHMARK(BM_PowerHalAidlBenchmarks_createHintSession)->Arg(1);
211 BENCHMARK(BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate);
212 BENCHMARK(BM_PowerHalAidlBenchmarks_updateTargetWorkDuration);
213 BENCHMARK(BM_PowerHalAidlBenchmarks_reportActualWorkDuration);
214