blob: 10048286b947a11b221348c56753d8b7d7de1cd9 [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
19#include <android/hardware/power/Boost.h>
20#include <android/hardware/power/IPower.h>
21#include <android/hardware/power/Mode.h>
Lais Andrade159eb6a2020-06-24 15:11:05 +000022#include <benchmark/benchmark.h>
Lais Andrade159eb6a2020-06-24 15:11:05 +000023#include <binder/IServiceManager.h>
Lais Andrade14e97b72020-07-14 12:27:44 +000024#include <testUtil.h>
25#include <chrono>
Lais Andrade159eb6a2020-06-24 15:11:05 +000026
27using android::hardware::power::Boost;
28using android::hardware::power::IPower;
29using android::hardware::power::Mode;
Lais Andrade14e97b72020-07-14 12:27:44 +000030using std::chrono::microseconds;
Lais Andrade159eb6a2020-06-24 15:11:05 +000031
32using namespace android;
Lais Andrade14e97b72020-07-14 12:27:44 +000033using namespace std::chrono_literals;
34
35// Values from Boost.aidl and Mode.aidl.
36static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION);
37static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT);
38static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE);
39static constexpr int64_t LAST_MODE = static_cast<int64_t>(Mode::CAMERA_STREAMING_HIGH);
40
41// Delay between oneway method calls to avoid overflowing the binder buffers.
42static constexpr microseconds ONEWAY_API_DELAY = 100us;
Lais Andrade159eb6a2020-06-24 15:11:05 +000043
44template <class R, class... Args0, class... Args1>
Lais Andrade14e97b72020-07-14 12:27:44 +000045static void runBenchmark(benchmark::State& state, microseconds delay, R (IPower::*fn)(Args0...),
46 Args1&&... args1) {
Lais Andrade159eb6a2020-06-24 15:11:05 +000047 sp<IPower> hal = waitForVintfService<IPower>();
48
49 if (hal == nullptr) {
Lais Andrade14e97b72020-07-14 12:27:44 +000050 ALOGI("Power HAL not available, skipping test...");
51 return;
52 }
53
54 binder::Status ret = (*hal.*fn)(std::forward<Args1>(args1)...);
55 if (ret.exceptionCode() == binder::Status::Exception::EX_UNSUPPORTED_OPERATION) {
56 ALOGI("Power HAL does not support this operation, skipping test...");
Lais Andrade159eb6a2020-06-24 15:11:05 +000057 return;
58 }
59
60 while (state.KeepRunning()) {
Lais Andrade14e97b72020-07-14 12:27:44 +000061 ret = (*hal.*fn)(std::forward<Args1>(args1)...);
62 state.PauseTiming();
63 if (!ret.isOk()) state.SkipWithError(ret.toString8().c_str());
64 if (delay > 0us) {
65 testDelaySpin(std::chrono::duration_cast<std::chrono::duration<float>>(delay).count());
66 }
67 state.ResumeTiming();
Lais Andrade159eb6a2020-06-24 15:11:05 +000068 }
69}
70
71static void BM_PowerHalAidlBenchmarks_isBoostSupported(benchmark::State& state) {
72 bool isSupported;
Lais Andrade14e97b72020-07-14 12:27:44 +000073 Boost boost = static_cast<Boost>(state.range(0));
74 runBenchmark(state, 0us, &IPower::isBoostSupported, boost, &isSupported);
Lais Andrade159eb6a2020-06-24 15:11:05 +000075}
76
77static void BM_PowerHalAidlBenchmarks_isModeSupported(benchmark::State& state) {
78 bool isSupported;
Lais Andrade14e97b72020-07-14 12:27:44 +000079 Mode mode = static_cast<Mode>(state.range(0));
80 runBenchmark(state, 0us, &IPower::isModeSupported, mode, &isSupported);
Lais Andrade159eb6a2020-06-24 15:11:05 +000081}
82
83static void BM_PowerHalAidlBenchmarks_setBoost(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000084 Boost boost = static_cast<Boost>(state.range(0));
85 runBenchmark(state, ONEWAY_API_DELAY, &IPower::setBoost, boost, 1);
Lais Andrade159eb6a2020-06-24 15:11:05 +000086}
87
88static void BM_PowerHalAidlBenchmarks_setMode(benchmark::State& state) {
Lais Andrade14e97b72020-07-14 12:27:44 +000089 Mode mode = static_cast<Mode>(state.range(0));
90 runBenchmark(state, ONEWAY_API_DELAY, &IPower::setMode, mode, false);
Lais Andrade159eb6a2020-06-24 15:11:05 +000091}
92
Lais Andrade14e97b72020-07-14 12:27:44 +000093BENCHMARK(BM_PowerHalAidlBenchmarks_isBoostSupported)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
94BENCHMARK(BM_PowerHalAidlBenchmarks_isModeSupported)->DenseRange(FIRST_MODE, LAST_MODE, 1);
95BENCHMARK(BM_PowerHalAidlBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1);
96BENCHMARK(BM_PowerHalAidlBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1);