1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker *
4*8542734aSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker *
8*8542734aSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker *
10*8542734aSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker */
16*8542734aSAndroid Build Coastguard Worker
17*8542734aSAndroid Build Coastguard Worker #include <android-base/logging.h>
18*8542734aSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
19*8542734aSAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
20*8542734aSAndroid Build Coastguard Worker
21*8542734aSAndroid Build Coastguard Worker #define BPF_MAP_MAKE_VISIBLE_FOR_TESTING
22*8542734aSAndroid Build Coastguard Worker #include "bpf/BpfMap.h"
23*8542734aSAndroid Build Coastguard Worker
24*8542734aSAndroid Build Coastguard Worker constexpr uint32_t TEST_MAP_SIZE = 10000;
25*8542734aSAndroid Build Coastguard Worker
26*8542734aSAndroid Build Coastguard Worker using android::base::Result;
27*8542734aSAndroid Build Coastguard Worker using android::base::StringPrintf;
28*8542734aSAndroid Build Coastguard Worker using android::bpf::BpfMap;
29*8542734aSAndroid Build Coastguard Worker
30*8542734aSAndroid Build Coastguard Worker class BpfBenchMark : public ::benchmark::Fixture {
31*8542734aSAndroid Build Coastguard Worker public:
BpfBenchMark()32*8542734aSAndroid Build Coastguard Worker BpfBenchMark() {
33*8542734aSAndroid Build Coastguard Worker mBpfTestMap.resetMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
34*8542734aSAndroid Build Coastguard Worker }
35*8542734aSAndroid Build Coastguard Worker
36*8542734aSAndroid Build Coastguard Worker BpfMap<uint32_t, uint32_t> mBpfTestMap;
37*8542734aSAndroid Build Coastguard Worker };
38*8542734aSAndroid Build Coastguard Worker
BENCHMARK_DEFINE_F(BpfBenchMark,MapWriteNewEntry)39*8542734aSAndroid Build Coastguard Worker BENCHMARK_DEFINE_F(BpfBenchMark, MapWriteNewEntry)(benchmark::State& state) {
40*8542734aSAndroid Build Coastguard Worker for (auto _ : state) { // NOLINT(clang-analyzer-deadcode.DeadStores)
41*8542734aSAndroid Build Coastguard Worker // TODO(b/147676069) assert
42*8542734aSAndroid Build Coastguard Worker mBpfTestMap.writeValue(state.range(0), state.range(0), BPF_NOEXIST);
43*8542734aSAndroid Build Coastguard Worker }
44*8542734aSAndroid Build Coastguard Worker }
45*8542734aSAndroid Build Coastguard Worker
BENCHMARK_DEFINE_F(BpfBenchMark,MapUpdateEntry)46*8542734aSAndroid Build Coastguard Worker BENCHMARK_DEFINE_F(BpfBenchMark, MapUpdateEntry)(benchmark::State& state) {
47*8542734aSAndroid Build Coastguard Worker for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) {
48*8542734aSAndroid Build Coastguard Worker // TODO(b/147676069) assert
49*8542734aSAndroid Build Coastguard Worker mBpfTestMap.writeValue(i, i, BPF_NOEXIST);
50*8542734aSAndroid Build Coastguard Worker }
51*8542734aSAndroid Build Coastguard Worker for (auto _ : state) { // NOLINT(clang-analyzer-deadcode.DeadStores)
52*8542734aSAndroid Build Coastguard Worker // TODO(b/147676069) assert
53*8542734aSAndroid Build Coastguard Worker mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_EXIST);
54*8542734aSAndroid Build Coastguard Worker }
55*8542734aSAndroid Build Coastguard Worker }
56*8542734aSAndroid Build Coastguard Worker
BENCHMARK_DEFINE_F(BpfBenchMark,MapDeleteAddEntry)57*8542734aSAndroid Build Coastguard Worker BENCHMARK_DEFINE_F(BpfBenchMark, MapDeleteAddEntry)(benchmark::State& state) {
58*8542734aSAndroid Build Coastguard Worker for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) {
59*8542734aSAndroid Build Coastguard Worker // TODO(b/147676069) assert
60*8542734aSAndroid Build Coastguard Worker mBpfTestMap.writeValue(i, i, BPF_NOEXIST);
61*8542734aSAndroid Build Coastguard Worker }
62*8542734aSAndroid Build Coastguard Worker for (auto _ : state) { // NOLINT(clang-analyzer-deadcode.DeadStores)
63*8542734aSAndroid Build Coastguard Worker // TODO(b/147676069) assert
64*8542734aSAndroid Build Coastguard Worker mBpfTestMap.deleteValue(state.range(0));
65*8542734aSAndroid Build Coastguard Worker // TODO(b/147676069) assert
66*8542734aSAndroid Build Coastguard Worker mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_NOEXIST);
67*8542734aSAndroid Build Coastguard Worker }
68*8542734aSAndroid Build Coastguard Worker }
69*8542734aSAndroid Build Coastguard Worker
BENCHMARK_DEFINE_F(BpfBenchMark,WaitForRcu)70*8542734aSAndroid Build Coastguard Worker BENCHMARK_DEFINE_F(BpfBenchMark, WaitForRcu)(benchmark::State& state) {
71*8542734aSAndroid Build Coastguard Worker for (auto _ : state) { // NOLINT(clang-analyzer-deadcode.DeadStores)
72*8542734aSAndroid Build Coastguard Worker int ret = android::bpf::synchronizeKernelRCU();
73*8542734aSAndroid Build Coastguard Worker if (ret) {
74*8542734aSAndroid Build Coastguard Worker state.SkipWithError(
75*8542734aSAndroid Build Coastguard Worker StringPrintf("synchronizeKernelRCU() failed with errno=%d", -ret).c_str());
76*8542734aSAndroid Build Coastguard Worker }
77*8542734aSAndroid Build Coastguard Worker }
78*8542734aSAndroid Build Coastguard Worker }
79*8542734aSAndroid Build Coastguard Worker
80*8542734aSAndroid Build Coastguard Worker namespace {
81*8542734aSAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(BpfBenchMark, MapUpdateEntry)->Arg(1);
82*8542734aSAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(BpfBenchMark, MapWriteNewEntry)->Arg(1);
83*8542734aSAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(BpfBenchMark, MapDeleteAddEntry)->Arg(1);
84*8542734aSAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(BpfBenchMark, WaitForRcu)->Arg(1);
85*8542734aSAndroid Build Coastguard Worker } // namespace
86*8542734aSAndroid Build Coastguard Worker
87*8542734aSAndroid Build Coastguard Worker BENCHMARK_MAIN();
88