blob: 759485ffce6fe20c336ba77df3c190da83682b9d [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...");
Lais Andrade14e97b72020-07-14 12:27:44 +000073 return;
74 }
75
Xiang Wanga525c452023-05-22 13:13:26 -070076 ndk::ScopedAStatus ret = (*hal.*fn)(std::forward<Args1>(args1)...);
77 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
Lais Andradedeacc122022-03-10 18:47:37 +000078 ALOGV("Power HAL does not support this operation, skipping test...");
Lais Andrade159eb6a2020-06-24 15:11:05 +000079 return;
80 }
81
82 while (state.KeepRunning()) {
Lais Andrade14e97b72020-07-14 12:27:44 +000083 ret = (*hal.*fn)(std::forward<Args1>(args1)...);
84 state.PauseTiming();
Xiang Wanga525c452023-05-22 13:13:26 -070085 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
Lais Andrade14e97b72020-07-14 12:27:44 +000086 if (delay > 0us) {
87 testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count());
88 }
89 state.ResumeTiming();
Lais Andrade159eb6a2020-06-24 15:11:05 +000090 }
91}
92
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080093template <class R, class... Args0, class... Args1>
94static void runSessionBenchmark(benchmark::State& state, R (IPowerHintSession::*fn)(Args0...),
95 Args1&&... args1) {
Xiang Wanga525c452023-05-22 13:13:26 -070096 std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080097
Xiang Wanga525c452023-05-22 13:13:26 -070098 if (hal == nullptr) {
Lais Andradedeacc122022-03-10 18:47:37 +000099 ALOGV("Power HAL not available, skipping test...");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800100 return;
101 }
102
103 // do not use tid from the benchmark process, use 1 for init
104 std::vector<int32_t> threadIds{1};
105 int64_t durationNanos = 16666666L;
Xiang Wanga525c452023-05-22 13:13:26 -0700106 std::shared_ptr<IPowerHintSession> session;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800107
Xiang Wanga525c452023-05-22 13:13:26 -0700108 auto status = hal->createHintSession(1, 0, threadIds, durationNanos, &session);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800109
Xiang Wanga525c452023-05-22 13:13:26 -0700110 if (session == nullptr) {
Lais Andradedeacc122022-03-10 18:47:37 +0000111 ALOGV("Power HAL doesn't support session, skipping test...");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800112 return;
113 }
114
Xiang Wanga525c452023-05-22 13:13:26 -0700115 ndk::ScopedAStatus ret = (*session.*fn)(std::forward<Args1>(args1)...);
116 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
Lais Andradedeacc122022-03-10 18:47:37 +0000117 ALOGV("Power HAL does not support this operation, skipping test...");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800118 return;
119 }
120
121 while (state.KeepRunning()) {
Xiang Wanga525c452023-05-22 13:13:26 -0700122 ret = (*session.*fn)(std::forward<Args1>(args1)...);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800123 state.PauseTiming();
Xiang Wanga525c452023-05-22 13:13:26 -0700124 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800125 if (ONEWAY_API_DELAY > 0us) {
126 testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(ONEWAY_API_DELAY)
127 .count());
128 }
129 state.ResumeTiming();
130 }
Xiang Wanga525c452023-05-22 13:13:26 -0700131 session->close();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800132}
133
Lais Andrade159eb6a2020-06-24 15:11:05 +0000134static void BM_PowerHalAidlBenchmarks_isBoostSupported(benchmark::State& state) {
135 bool isSupported;
Lais Andrade14e97b72020-07-14 12:27:44 +0000136 Boost boost = static_cast<Boost>(state.range(0));
137 runBenchmark(state, 0us, &IPower::isBoostSupported, boost, &isSupported);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000138}
139
140static void BM_PowerHalAidlBenchmarks_isModeSupported(benchmark::State& state) {
141 bool isSupported;
Lais Andrade14e97b72020-07-14 12:27:44 +0000142 Mode mode = static_cast<Mode>(state.range(0));
143 runBenchmark(state, 0us, &IPower::isModeSupported, mode, &isSupported);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000144}
145
146static void BM_PowerHalAidlBenchmarks_setBoost(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000147 Boost boost = static_cast<Boost>(state.range(0));
148 runBenchmark(state, ONEWAY_API_DELAY, &IPower::setBoost, boost, 1);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000149}
150
151static void BM_PowerHalAidlBenchmarks_setMode(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000152 Mode mode = static_cast<Mode>(state.range(0));
153 runBenchmark(state, ONEWAY_API_DELAY, &IPower::setMode, mode, false);
Lais Andrade159eb6a2020-06-24 15:11:05 +0000154}
155
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800156static void BM_PowerHalAidlBenchmarks_createHintSession(benchmark::State& state) {
157 std::vector<int32_t> threadIds{static_cast<int32_t>(state.range(0))};
158 int64_t durationNanos = 16666666L;
159 int32_t tgid = 999;
160 int32_t uid = 1001;
Xiang Wanga525c452023-05-22 13:13:26 -0700161 std::shared_ptr<IPowerHintSession> appSession;
162 std::shared_ptr<IPower> hal = PowerHalLoader::loadAidl();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800163
164 if (hal == nullptr) {
Lais Andradedeacc122022-03-10 18:47:37 +0000165 ALOGV("Power HAL not available, skipping test...");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800166 return;
167 }
168
Xiang Wanga525c452023-05-22 13:13:26 -0700169 ndk::ScopedAStatus ret =
170 hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession);
171 if (ret.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
Lais Andradedeacc122022-03-10 18:47:37 +0000172 ALOGV("Power HAL does not support this operation, skipping test...");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800173 return;
174 }
175
176 while (state.KeepRunning()) {
177 ret = hal->createHintSession(tgid, uid, threadIds, durationNanos, &appSession);
178 state.PauseTiming();
Xiang Wanga525c452023-05-22 13:13:26 -0700179 if (!ret.isOk()) state.SkipWithError(ret.getDescription().c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800180 appSession->close();
181 state.ResumeTiming();
182 }
183}
184
185static void BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate(benchmark::State& state) {
186 int64_t rate;
187 runBenchmark(state, 0us, &IPower::getHintSessionPreferredRate, &rate);
188}
189
190static void BM_PowerHalAidlBenchmarks_updateTargetWorkDuration(benchmark::State& state) {
191 int64_t duration = 1000;
192 runSessionBenchmark(state, &IPowerHintSession::updateTargetWorkDuration, duration);
193}
194
195static void BM_PowerHalAidlBenchmarks_reportActualWorkDuration(benchmark::State& state) {
196 runSessionBenchmark(state, &IPowerHintSession::reportActualWorkDuration, DURATIONS);
197}
198
Lais Andrade14e97b72020-07-14 12:27:44 +0000199BENCHMARK(BM_PowerHalAidlBenchmarks_isBoostSupported)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
200BENCHMARK(BM_PowerHalAidlBenchmarks_isModeSupported)->DenseRange(FIRST_MODE, LAST_MODE, 1);
201BENCHMARK(BM_PowerHalAidlBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
202BENCHMARK(BM_PowerHalAidlBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800203BENCHMARK(BM_PowerHalAidlBenchmarks_createHintSession)->Arg(1);
204BENCHMARK(BM_PowerHalAidlBenchmarks_getHintSessionPreferredRate);
205BENCHMARK(BM_PowerHalAidlBenchmarks_updateTargetWorkDuration);
206BENCHMARK(BM_PowerHalAidlBenchmarks_reportActualWorkDuration);