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