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