blob: 914692366e0d73dd985c08239083d2ad9bc85cfc [file] [log] [blame]
Chenbo Fengd3e29562018-08-24 15:05:01 -07001/*
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 Fengf434e862018-06-27 14:08:39 -070017#include <android-base/stringprintf.h>
Chenbo Fengd3e29562018-08-24 15:05:01 -070018#include <benchmark/benchmark.h>
19
20#include "bpf/BpfMap.h"
21#include "bpf/BpfUtils.h"
Chenbo Fengf434e862018-06-27 14:08:39 -070022
Chenbo Fengd3e29562018-08-24 15:05:01 -070023constexpr uint32_t TEST_MAP_SIZE = 10000;
24
Chenbo Fengf434e862018-06-27 14:08:39 -070025using android::base::StringPrintf;
Chenbo Fengd3e29562018-08-24 15:05:01 -070026using android::bpf::BpfMap;
27
28class 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
34BENCHMARK_DEFINE_F(BpfBenchMark, MapWriteNewEntry)(benchmark::State& state) {
35 for (auto _ : state) mBpfTestMap.writeValue(state.range(0), state.range(0), BPF_NOEXIST);
36}
37
38BENCHMARK_DEFINE_F(BpfBenchMark, MapUpdateEntry)(benchmark::State& state) {
Sehee Park8659b8d2018-11-16 10:53:16 +090039 for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090040 expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST));
41 }
42 for (auto _ : state) {
43 expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_EXIST));
44 }
Chenbo Fengd3e29562018-08-24 15:05:01 -070045}
46
47BENCHMARK_DEFINE_F(BpfBenchMark, MapDeleteAddEntry)(benchmark::State& state) {
Sehee Park8659b8d2018-11-16 10:53:16 +090048 for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090049 expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST));
50 }
Chenbo Fengd3e29562018-08-24 15:05:01 -070051 for (auto _ : state) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090052 expectOk(mBpfTestMap.deleteValue(state.range(0)));
53 expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_NOEXIST));
Chenbo Fengd3e29562018-08-24 15:05:01 -070054 }
55}
56
Chenbo Fengf434e862018-06-27 14:08:39 -070057BENCHMARK_DEFINE_F(BpfBenchMark, WaitForRcu)(benchmark::State& state) {
58 for (auto _ : state) {
59 int ret = android::bpf::synchronizeKernelRCU();
60 if (ret) {
61 state.SkipWithError(
62 StringPrintf("synchronizeKernelRCU() failed with errno=%d", -ret).c_str());
63 }
64 }
65}
66
Chenbo Fengd3e29562018-08-24 15:05:01 -070067BENCHMARK_REGISTER_F(BpfBenchMark, MapUpdateEntry)->Arg(1);
68BENCHMARK_REGISTER_F(BpfBenchMark, MapWriteNewEntry)->Arg(1);
69BENCHMARK_REGISTER_F(BpfBenchMark, MapDeleteAddEntry)->Arg(1);
Chenbo Fengf434e862018-06-27 14:08:39 -070070BENCHMARK_REGISTER_F(BpfBenchMark, WaitForRcu)->Arg(1);
Chenbo Fengd3e29562018-08-24 15:05:01 -070071BENCHMARK_MAIN();