blob: 4de806610539f09cdd8d3889d960ed7c3d50ea4e [file] [log] [blame]
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <benchmark/benchmark.h>
#define BPF_MAP_MAKE_VISIBLE_FOR_TESTING
#include "bpf/BpfMap.h"
#include "bpf/BpfUtils.h"
constexpr uint32_t TEST_MAP_SIZE = 10000;
using android::base::Result;
using android::base::StringPrintf;
using android::bpf::BpfMap;
class BpfBenchMark : public ::benchmark::Fixture {
public:
BpfBenchMark() : mBpfTestMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC) {}
BpfMap<uint32_t, uint32_t> mBpfTestMap;
};
BENCHMARK_DEFINE_F(BpfBenchMark, MapWriteNewEntry)(benchmark::State& state) {
for (auto _ : state) { // NOLINT(clang-analyzer-deadcode.DeadStores)
// TODO(b/147676069) assert
mBpfTestMap.writeValue(state.range(0), state.range(0), BPF_NOEXIST);
}
}
BENCHMARK_DEFINE_F(BpfBenchMark, MapUpdateEntry)(benchmark::State& state) {
for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) {
// TODO(b/147676069) assert
mBpfTestMap.writeValue(i, i, BPF_NOEXIST);
}
for (auto _ : state) { // NOLINT(clang-analyzer-deadcode.DeadStores)
// TODO(b/147676069) assert
mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_EXIST);
}
}
BENCHMARK_DEFINE_F(BpfBenchMark, MapDeleteAddEntry)(benchmark::State& state) {
for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) {
// TODO(b/147676069) assert
mBpfTestMap.writeValue(i, i, BPF_NOEXIST);
}
for (auto _ : state) { // NOLINT(clang-analyzer-deadcode.DeadStores)
// TODO(b/147676069) assert
mBpfTestMap.deleteValue(state.range(0));
// TODO(b/147676069) assert
mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_NOEXIST);
}
}
BENCHMARK_DEFINE_F(BpfBenchMark, WaitForRcu)(benchmark::State& state) {
for (auto _ : state) { // NOLINT(clang-analyzer-deadcode.DeadStores)
int ret = android::bpf::synchronizeKernelRCU();
if (ret) {
state.SkipWithError(
StringPrintf("synchronizeKernelRCU() failed with errno=%d", -ret).c_str());
}
}
}
BENCHMARK_REGISTER_F(BpfBenchMark, MapUpdateEntry)->Arg(1);
BENCHMARK_REGISTER_F(BpfBenchMark, MapWriteNewEntry)->Arg(1);
BENCHMARK_REGISTER_F(BpfBenchMark, MapDeleteAddEntry)->Arg(1);
BENCHMARK_REGISTER_F(BpfBenchMark, WaitForRcu)->Arg(1);
BENCHMARK_MAIN();