blob: 61ab47a75a9a8811f93011246fb9b2fdee523104 [file] [log] [blame]
Lais Andrade159eb6a2020-06-24 15:11:05 +00001/*
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
Xiang Wanga525c452023-05-22 13:13:26 -070019#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>
Lais Andrade159eb6a2020-06-24 15:11:05 +000024#include <benchmark/benchmark.h>
Lais Andrade159eb6a2020-06-24 15:11:05 +000025#include <binder/IServiceManager.h>
Xiang Wanga525c452023-05-22 13:13:26 -070026#include <binder/Status.h>
27#include <powermanager/PowerHalLoader.h>
Lais Andrade14e97b72020-07-14 12:27:44 +000028#include <testUtil.h>
29#include <chrono>
Lais Andrade159eb6a2020-06-24 15:11:05 +000030
Xiang Wanga525c452023-05-22 13:13:26 -070031using aidl::android::hardware::power::Boost;
32using aidl::android::hardware::power::IPower;
33using aidl::android::hardware::power::IPowerHintSession;
34using aidl::android::hardware::power::Mode;
35using aidl::android::hardware::power::WorkDuration;
36using android::power::PowerHalLoader;
Lais Andrade14e97b72020-07-14 12:27:44 +000037using std::chrono::microseconds;
Lais Andrade159eb6a2020-06-24 15:11:05 +000038
39using namespace android;
Lais Andrade14e97b72020-07-14 12:27:44 +000040using namespace std::chrono_literals;
41
42// Values from Boost.aidl and Mode.aidl.
43static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION);
44static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT);
45static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE);
46static constexpr int64_t LAST_MODE = static_cast<int64_t>(Mode::CAMERA_STREAMING_HIGH);
47
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080048class DurationWrapper : public WorkDuration {
49public:
50 DurationWrapper(int64_t dur, int64_t time) {
51 durationNanos = dur;
52 timeStampNanos = time;
53 }
54};
55
56static const std::vector<WorkDuration> DURATIONS = {
57 DurationWrapper(1L, 1L),
58 DurationWrapper(1000L, 2L),
59 DurationWrapper(1000000L, 3L),
60 DurationWrapper(1000000000L, 4L),
61};
62
Lais Andrade14e97b72020-07-14 12:27:44 +000063// Delay between oneway method calls to avoid overflowing the binder buffers.
64static constexpr microseconds ONEWAY_API_DELAY = 100us;
Lais Andrade159eb6a2020-06-24 15:11:05 +000065
66template <class R, class... Args0, class... Args1>
Lais Andrade14e97b72020-07-14 12:27:44 +000067static void runBenchmark(benchmark::State& state, microseconds delay, R (IPower::*fn)(Args0...),
68 Args1&&... args1) {
Xiang Wanga525c452023-05-22 13:13:26 -070069 std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl();
Lais Andrade159eb6a2020-06-24 15:11:05 +000070
71 if (hal == nullptr) {
Lais Andradedeacc122022-03-10 18:47:37 +000072 ALOGV("Power HAL not available, skipping test...");
Matt Buckleyeefb6a42024-02-15 18:19:27 +000073 state.SkipWithMessage("Power HAL unavailable");
Lais Andrade14e97b72020-07-14 12:27:44 +000074 return;
75 }
76
Xiang Wanga525c452023-05-22 13:13:26 -070077 ndk::ScopedAStatus ret = (*hal.*fn)(std::forward<Args1>(args1)...);
78 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
Lais Andradedeacc122022-03-10 18:47:37 +000079 ALOGV("Power HAL does not support this operation, skipping test...");
Matt Buckleyeefb6a42024-02-15 18:19:27 +000080 state.SkipWithMessage("operation unsupported");
Lais Andrade159eb6a2020-06-24 15:11:05 +000081 return;
82 }
83
84 while (state.KeepRunning()) {
Lais Andrade14e97b72020-07-14 12:27:44 +000085 ret = (*hal.*fn)(std::forward<Args1>(args1)...);
86 state.PauseTiming();
Xiang Wanga525c452023-05-22 13:13:26 -070087 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
Lais Andrade14e97b72020-07-14 12:27:44 +000088 if (delay > 0us) {
89 testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count());
90 }
91 state.ResumeTiming();
Lais Andrade159eb6a2020-06-24 15:11:05 +000092 }
93}
94
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080095template <class R, class... Args0, class... Args1>
96static void runSessionBenchmark(benchmark::State& state, R (IPowerHintSession::*fn)(Args0...),
97 Args1&&... args1) {
Xiang Wanga525c452023-05-22 13:13:26 -070098 std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080099
Xiang Wanga525c452023-05-22 13:13:26 -0700100 if (hal == nullptr) {
Lais Andradedeacc122022-03-10 18:47:37 +0000101 ALOGV("Power HAL not available, skipping test...");
Matt Buckleyeefb6a42024-02-15 18:19:27 +0000102 state.SkipWithMessage("Power HAL unavailable");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800103 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;
Xiang Wanga525c452023-05-22 13:13:26 -0700109 std::shared_ptr<IPowerHintSession> session;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800110
Xiang Wanga525c452023-05-22 13:13:26 -0700111 auto status = hal->createHintSession(1, 0, threadIds, durationNanos, &session);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800112
Xiang Wanga525c452023-05-22 13:13:26 -0700113 if (session == nullptr) {
Lais Andradedeacc122022-03-10 18:47:37 +0000114 ALOGV("Power HAL doesn't support session, skipping test...");
Matt Buckleyeefb6a42024-02-15 18:19:27 +0000115 state.SkipWithMessage("operation unsupported");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800116 return;
117 }
118
Xiang Wanga525c452023-05-22 13:13:26 -0700119 ndk::ScopedAStatus ret = (*session.*fn)(std::forward<Args1>(args1)...);
120 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
Lais Andradedeacc122022-03-10 18:47:37 +0000121 ALOGV("Power HAL does not support this operation, skipping test...");
Matt Buckleyeefb6a42024-02-15 18:19:27 +0000122 state.SkipWithMessage("operation unsupported");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800123 return;
124 }
125
126 while (state.KeepRunning()) {
Xiang Wanga525c452023-05-22 13:13:26 -0700127 ret = (*session.*fn)(std::forward<Args1>(args1)...);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800128 state.PauseTiming();
Xiang Wanga525c452023-05-22 13:13:26 -0700129 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800130 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 }
Xiang Wanga525c452023-05-22 13:13:26 -0700136 session->close();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800137}
138
Lais Andrade159eb6a2020-06-24 15:11:05 +0000139static void BM_PowerHalAidlBenchmarks_isBoostSupported(benchmark::State& state) {
140 bool isSupported;
Lais Andrade14e97b72020-07-14 12:27:44 +0000141 Boost boost = static_cast<Boost>(state.range(0));
142 runBenchmark(state, 0us, &IPower::isBoostSupported, boost, &isSupported);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000143}
144
145static void BM_PowerHalAidlBenchmarks_isModeSupported(benchmark::State& state) {
146 bool isSupported;
Lais Andrade14e97b72020-07-14 12:27:44 +0000147 Mode mode = static_cast<Mode>(state.range(0));
148 runBenchmark(state, 0us, &IPower::isModeSupported, mode, &isSupported);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000149}
150
151static void BM_PowerHalAidlBenchmarks_setBoost(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000152 Boost boost = static_cast<Boost>(state.range(0));
153 runBenchmark(state, ONEWAY_API_DELAY, &IPower::setBoost, boost, 1);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000154}
155
156static void BM_PowerHalAidlBenchmarks_setMode(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000157 Mode mode = static_cast<Mode>(state.range(0));
158 runBenchmark(state, ONEWAY_API_DELAY, &IPower::setMode, mode, false);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000159}
160
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800161static 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;
Xiang Wanga525c452023-05-22 13:13:26 -0700166 std::shared_ptr<IPowerHintSession> appSession;
167 std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800168
169 if (hal == nullptr) {
Lais Andradedeacc122022-03-10 18:47:37 +0000170 ALOGV("Power HAL not available, skipping test...");
Matt Buckleyeefb6a42024-02-15 18:19:27 +0000171 state.SkipWithMessage("Power HAL unavailable");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800172 return;
173 }
174
Xiang Wanga525c452023-05-22 13:13:26 -0700175 ndk::ScopedAStatus ret =
176 hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession);
177 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
Lais Andradedeacc122022-03-10 18:47:37 +0000178 ALOGV("Power HAL does not support this operation, skipping test...");
Matt Buckleyeefb6a42024-02-15 18:19:27 +0000179 state.SkipWithMessage("operation unsupported");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800180 return;
181 }
182
183 while (state.KeepRunning()) {
184 ret = hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession);
185 state.PauseTiming();
Xiang Wanga525c452023-05-22 13:13:26 -0700186 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800187 appSession->close();
188 state.ResumeTiming();
189 }
190}
191
192static void BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate(benchmark::State& state) {
193 int64_t rate;
194 runBenchmark(state, 0us, &IPower::getHintSessionPreferredRate, &rate);
195}
196
197static void BM_PowerHalAidlBenchmarks_updateTargetWorkDuration(benchmark::State& state) {
198 int64_t duration = 1000;
199 runSessionBenchmark(state, &IPowerHintSession::updateTargetWorkDuration, duration);
200}
201
202static void BM_PowerHalAidlBenchmarks_reportActualWorkDuration(benchmark::State& state) {
203 runSessionBenchmark(state, &IPowerHintSession::reportActualWorkDuration, DURATIONS);
204}
205
Lais Andrade14e97b72020-07-14 12:27:44 +0000206BENCHMARK(BM_PowerHalAidlBenchmarks_isBoostSupported)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
207BENCHMARK(BM_PowerHalAidlBenchmarks_isModeSupported)->DenseRange(FIRST_MODE, LAST_MODE, 1);
208BENCHMARK(BM_PowerHalAidlBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
209BENCHMARK(BM_PowerHalAidlBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800210BENCHMARK(BM_PowerHalAidlBenchmarks_createHintSession)->Arg(1);
211BENCHMARK(BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate);
212BENCHMARK(BM_PowerHalAidlBenchmarks_updateTargetWorkDuration);
213BENCHMARK(BM_PowerHalAidlBenchmarks_reportActualWorkDuration);