Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 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/Mode.h> |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 22 | #include <benchmark/benchmark.h> |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 24 | #include <testUtil.h> |
| 25 | #include <chrono> |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 26 | |
| 27 | using android::hardware::power::Boost; |
| 28 | using android::hardware::power::IPower; |
| 29 | using android::hardware::power::Mode; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 30 | using std::chrono::microseconds; |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace android; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 33 | using namespace std::chrono_literals; |
| 34 | |
| 35 | // Values from Boost.aidl and Mode.aidl. |
| 36 | static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION); |
| 37 | static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT); |
| 38 | static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE); |
| 39 | static 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. |
| 42 | static constexpr microseconds ONEWAY_API_DELAY = 100us; |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 43 | |
| 44 | template <class R, class... Args0, class... Args1> |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 45 | static void runBenchmark(benchmark::State& state, microseconds delay, R (IPower::*fn)(Args0...), |
| 46 | Args1&&... args1) { |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 47 | sp<IPower> hal = waitForVintfService<IPower>(); |
| 48 | |
| 49 | if (hal == nullptr) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 50 | 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 Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | while (state.KeepRunning()) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 61 | 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 Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | static void BM_PowerHalAidlBenchmarks_isBoostSupported(benchmark::State& state) { |
| 72 | bool isSupported; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 73 | Boost boost = static_cast<Boost>(state.range(0)); |
| 74 | runBenchmark(state, 0us, &IPower::isBoostSupported, boost, &isSupported); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void BM_PowerHalAidlBenchmarks_isModeSupported(benchmark::State& state) { |
| 78 | bool isSupported; |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 79 | Mode mode = static_cast<Mode>(state.range(0)); |
| 80 | runBenchmark(state, 0us, &IPower::isModeSupported, mode, &isSupported); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void BM_PowerHalAidlBenchmarks_setBoost(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 84 | Boost boost = static_cast<Boost>(state.range(0)); |
| 85 | runBenchmark(state, ONEWAY_API_DELAY, &IPower::setBoost, boost, 1); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void BM_PowerHalAidlBenchmarks_setMode(benchmark::State& state) { |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 89 | Mode mode = static_cast<Mode>(state.range(0)); |
| 90 | runBenchmark(state, ONEWAY_API_DELAY, &IPower::setMode, mode, false); |
Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 93 | BENCHMARK(BM_PowerHalAidlBenchmarks_isBoostSupported)->DenseRange(FIRST_BOOST, LAST_BOOST, 1); |
| 94 | BENCHMARK(BM_PowerHalAidlBenchmarks_isModeSupported)->DenseRange(FIRST_MODE, LAST_MODE, 1); |
| 95 | BENCHMARK(BM_PowerHalAidlBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1); |
| 96 | BENCHMARK(BM_PowerHalAidlBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1); |