Files
linux/tools/testing/selftests/bpf/benchs/bench_bpf_nop.c
Puranjay Mohan dcf11479c2 selftests/bpf: Add bpf-nop benchmark for timing overhead baseline
Add a minimal benchmark that measures the overhead of the batch-timing
infrastructure itself. The BPF program runs an empty BENCH_BPF_LOOP body
(~1.5-2 ns/op), establishing the floor cost that all timing-library
benchmarks include.

[root@virtme-ng tools/testing/selftests/bpf]# sudo ./bench -a -p8 bpf-nop
Setting up benchmark 'bpf-nop'...
Benchmark 'bpf-nop' started.
bpf-nop: median 1.82 ns/op, stddev 0.01, p99 1.86 (1754 samples)

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20260427232313.1582588-4-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2026-05-11 15:25:24 -07:00

85 lines
1.6 KiB
C

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include "bench.h"
#include "bench_bpf_timing.h"
#include "bpf_nop_bench.skel.h"
#include "bpf_util.h"
static struct ctx {
struct bpf_nop_bench *skel;
struct bpf_bench_timing timing;
int prog_fd;
} ctx;
static void nop_validate(void)
{
if (env.consumer_cnt != 0) {
fprintf(stderr, "benchmark doesn't support consumers\n");
exit(1);
}
}
static void nop_run_once(void *unused __always_unused)
{
LIBBPF_OPTS(bpf_test_run_opts, topts);
bpf_prog_test_run_opts(ctx.prog_fd, &topts);
}
static void nop_setup(void)
{
struct bpf_nop_bench *skel;
int err;
setup_libbpf();
skel = bpf_nop_bench__open();
if (!skel) {
fprintf(stderr, "failed to open skeleton\n");
exit(1);
}
err = bpf_nop_bench__load(skel);
if (err) {
fprintf(stderr, "failed to load skeleton: %s\n", strerror(-err));
bpf_nop_bench__destroy(skel);
exit(1);
}
ctx.skel = skel;
ctx.prog_fd = bpf_program__fd(skel->progs.bench_nop);
BENCH_TIMING_INIT(&ctx.timing, skel, 0);
bpf_bench_calibrate(&ctx.timing, nop_run_once, NULL);
env.duration_sec = 600;
}
static void *nop_producer(void *input)
{
while (true)
nop_run_once(NULL);
return NULL;
}
static void nop_measure(struct bench_res *res)
{
bpf_bench_timing_measure(&ctx.timing, res);
}
static void nop_report_final(struct bench_res res[], int res_cnt)
{
bpf_bench_timing_report(&ctx.timing, "bpf-nop", NULL);
}
const struct bench bench_bpf_nop = {
.name = "bpf-nop",
.validate = nop_validate,
.setup = nop_setup,
.producer_thread = nop_producer,
.measure = nop_measure,
.report_final = nop_report_final,
};