| 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 "PowerHalControllerBenchmarks" | 
|  | 18 |  | 
|  | 19 | #include <android/hardware/power/Boost.h> | 
|  | 20 | #include <android/hardware/power/Mode.h> | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 21 | #include <benchmark/benchmark.h> | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 22 | #include <powermanager/PowerHalController.h> | 
| Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 23 | #include <testUtil.h> | 
|  | 24 | #include <chrono> | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | using android::hardware::power::Boost; | 
|  | 27 | using android::hardware::power::Mode; | 
| Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 28 | using android::power::HalResult; | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 29 | using android::power::PowerHalController; | 
|  | 30 |  | 
|  | 31 | using namespace android; | 
| Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 32 | using namespace std::chrono_literals; | 
|  | 33 |  | 
|  | 34 | // Values from Boost.aidl and Mode.aidl. | 
|  | 35 | static constexpr int64_t FIRST_BOOST = static_cast<int64_t>(Boost::INTERACTION); | 
|  | 36 | static constexpr int64_t LAST_BOOST = static_cast<int64_t>(Boost::CAMERA_SHOT); | 
|  | 37 | static constexpr int64_t FIRST_MODE = static_cast<int64_t>(Mode::DOUBLE_TAP_TO_WAKE); | 
|  | 38 | static constexpr int64_t LAST_MODE = static_cast<int64_t>(Mode::CAMERA_STREAMING_HIGH); | 
|  | 39 |  | 
|  | 40 | // Delay between oneway method calls to avoid overflowing the binder buffers. | 
|  | 41 | static constexpr std::chrono::microseconds ONEWAY_API_DELAY = 100us; | 
|  | 42 |  | 
|  | 43 | template <class... Args0, class... Args1> | 
|  | 44 | static void runBenchmark(benchmark::State& state, HalResult (PowerHalController::*fn)(Args0...), | 
|  | 45 | Args1&&... args1) { | 
|  | 46 | while (state.KeepRunning()) { | 
|  | 47 | PowerHalController controller; | 
|  | 48 | HalResult ret = (controller.*fn)(std::forward<Args1>(args1)...); | 
|  | 49 | state.PauseTiming(); | 
|  | 50 | if (ret == HalResult::FAILED) state.SkipWithError("Power HAL request failed"); | 
|  | 51 | state.ResumeTiming(); | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | template <class... Args0, class... Args1> | 
|  | 56 | static void runCachedBenchmark(benchmark::State& state, | 
|  | 57 | HalResult (PowerHalController::*fn)(Args0...), Args1&&... args1) { | 
|  | 58 | PowerHalController controller; | 
|  | 59 | // First call out of test, to cache HAL service and isSupported result. | 
|  | 60 | (controller.*fn)(std::forward<Args1>(args1)...); | 
|  | 61 |  | 
|  | 62 | while (state.KeepRunning()) { | 
|  | 63 | HalResult ret = (controller.*fn)(std::forward<Args1>(args1)...); | 
|  | 64 | state.PauseTiming(); | 
|  | 65 | if (ret == HalResult::FAILED) { | 
|  | 66 | state.SkipWithError("Power HAL request failed"); | 
|  | 67 | } | 
|  | 68 | testDelaySpin( | 
|  | 69 | std::chrono::duration_cast<std::chrono::duration<float>>(ONEWAY_API_DELAY).count()); | 
|  | 70 | state.ResumeTiming(); | 
|  | 71 | } | 
|  | 72 | } | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 73 |  | 
|  | 74 | static void BM_PowerHalControllerBenchmarks_init(benchmark::State& state) { | 
|  | 75 | while (state.KeepRunning()) { | 
|  | 76 | PowerHalController controller; | 
|  | 77 | controller.init(); | 
|  | 78 | } | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | static void BM_PowerHalControllerBenchmarks_initCached(benchmark::State& state) { | 
|  | 82 | PowerHalController controller; | 
|  | 83 | // First connection out of test. | 
|  | 84 | controller.init(); | 
|  | 85 |  | 
|  | 86 | while (state.KeepRunning()) { | 
|  | 87 | controller.init(); | 
|  | 88 | } | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | static void BM_PowerHalControllerBenchmarks_setBoost(benchmark::State& state) { | 
| Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 92 | Boost boost = static_cast<Boost>(state.range(0)); | 
|  | 93 | runBenchmark(state, &PowerHalController::setBoost, boost, 0); | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
|  | 96 | static void BM_PowerHalControllerBenchmarks_setBoostCached(benchmark::State& state) { | 
| Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 97 | Boost boost = static_cast<Boost>(state.range(0)); | 
|  | 98 | runCachedBenchmark(state, &PowerHalController::setBoost, boost, 0); | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
|  | 101 | static void BM_PowerHalControllerBenchmarks_setMode(benchmark::State& state) { | 
| Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 102 | Mode mode = static_cast<Mode>(state.range(0)); | 
|  | 103 | runBenchmark(state, &PowerHalController::setMode, mode, false); | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
|  | 106 | static void BM_PowerHalControllerBenchmarks_setModeCached(benchmark::State& state) { | 
| Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 107 | Mode mode = static_cast<Mode>(state.range(0)); | 
|  | 108 | runCachedBenchmark(state, &PowerHalController::setMode, mode, false); | 
| Lais Andrade | 159eb6a | 2020-06-24 15:11:05 +0000 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | BENCHMARK(BM_PowerHalControllerBenchmarks_init); | 
|  | 112 | BENCHMARK(BM_PowerHalControllerBenchmarks_initCached); | 
| Lais Andrade | 14e97b7 | 2020-07-14 12:27:44 +0000 | [diff] [blame] | 113 | BENCHMARK(BM_PowerHalControllerBenchmarks_setBoost)->DenseRange(FIRST_BOOST, LAST_BOOST, 1); | 
|  | 114 | BENCHMARK(BM_PowerHalControllerBenchmarks_setBoostCached)->DenseRange(FIRST_BOOST, LAST_BOOST, 1); | 
|  | 115 | BENCHMARK(BM_PowerHalControllerBenchmarks_setMode)->DenseRange(FIRST_MODE, LAST_MODE, 1); | 
|  | 116 | BENCHMARK(BM_PowerHalControllerBenchmarks_setModeCached)->DenseRange(FIRST_MODE, LAST_MODE, 1); |