Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 17 | #include <android-base/stringprintf.h> |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 18 | #include <benchmark/benchmark.h> |
| 19 | |
| 20 | #include "bpf/BpfMap.h" |
| 21 | #include "bpf/BpfUtils.h" |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 22 | |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 23 | constexpr uint32_t TEST_MAP_SIZE = 10000; |
| 24 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 25 | using android::base::StringPrintf; |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 26 | using android::bpf::BpfMap; |
| 27 | |
| 28 | class BpfBenchMark : public ::benchmark::Fixture { |
| 29 | public: |
| 30 | BpfBenchMark() : mBpfTestMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC) {} |
| 31 | BpfMap<uint32_t, uint32_t> mBpfTestMap; |
| 32 | }; |
| 33 | |
| 34 | BENCHMARK_DEFINE_F(BpfBenchMark, MapWriteNewEntry)(benchmark::State& state) { |
Yi Kong | d49c83d | 2019-03-06 14:03:53 -0800 | [diff] [blame^] | 35 | for (auto _ : state) { |
| 36 | expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0), BPF_NOEXIST)); |
| 37 | } |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | BENCHMARK_DEFINE_F(BpfBenchMark, MapUpdateEntry)(benchmark::State& state) { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 41 | for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 42 | expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST)); |
| 43 | } |
| 44 | for (auto _ : state) { |
| 45 | expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_EXIST)); |
| 46 | } |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | BENCHMARK_DEFINE_F(BpfBenchMark, MapDeleteAddEntry)(benchmark::State& state) { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 50 | for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 51 | expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST)); |
| 52 | } |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 53 | for (auto _ : state) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 54 | expectOk(mBpfTestMap.deleteValue(state.range(0))); |
| 55 | expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_NOEXIST)); |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 59 | BENCHMARK_DEFINE_F(BpfBenchMark, WaitForRcu)(benchmark::State& state) { |
| 60 | for (auto _ : state) { |
| 61 | int ret = android::bpf::synchronizeKernelRCU(); |
| 62 | if (ret) { |
| 63 | state.SkipWithError( |
| 64 | StringPrintf("synchronizeKernelRCU() failed with errno=%d", -ret).c_str()); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 69 | BENCHMARK_REGISTER_F(BpfBenchMark, MapUpdateEntry)->Arg(1); |
| 70 | BENCHMARK_REGISTER_F(BpfBenchMark, MapWriteNewEntry)->Arg(1); |
| 71 | BENCHMARK_REGISTER_F(BpfBenchMark, MapDeleteAddEntry)->Arg(1); |
Chenbo Feng | f434e86 | 2018-06-27 14:08:39 -0700 | [diff] [blame] | 72 | BENCHMARK_REGISTER_F(BpfBenchMark, WaitForRcu)->Arg(1); |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 73 | BENCHMARK_MAIN(); |