xref: /linux/tools/testing/selftests/bpf/bench_bpf_timing.h (revision a0fa68d8ce759dbf6aaf19a043ddd77a2128c26c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3 
4 #ifndef __BENCH_BPF_TIMING_H__
5 #define __BENCH_BPF_TIMING_H__
6 
7 #include <stdbool.h>
8 #include <linux/types.h>
9 #include "bench.h"
10 
11 #ifndef BENCH_NR_SAMPLES
12 #define BENCH_NR_SAMPLES	4096
13 #endif
14 #ifndef BENCH_NR_CPUS
15 #define BENCH_NR_CPUS		256
16 #endif
17 
18 typedef void (*bpf_bench_run_fn)(void *ctx);
19 
20 struct bpf_bench_timing {
21 	__u64 (*samples)[BENCH_NR_SAMPLES];	/* skel->bss->timing_samples */
22 	__u32 *idx;				/* skel->bss->timing_idx */
23 	volatile __u32 *timing_enabled;		/* &skel->bss->timing_enabled */
24 	volatile __u32 *batch_iters_bss;	/* &skel->bss->batch_iters */
25 	__u32 batch_iters;
26 	__u32 target_samples;
27 	__u32 nr_cpus;
28 	int warmup_ticks;
29 	bool done;
30 	bool machine_readable;
31 };
32 
33 #define BENCH_TIMING_INIT(t, skel, iters) do {				\
34 	(t)->samples = (skel)->bss->timing_samples;			\
35 	(t)->idx = (skel)->bss->timing_idx;				\
36 	(t)->timing_enabled = &(skel)->bss->timing_enabled;		\
37 	(t)->batch_iters_bss = &(skel)->bss->batch_iters;		\
38 	(t)->batch_iters = (iters);					\
39 	(t)->target_samples = 200;					\
40 	(t)->nr_cpus = env.nr_cpus;					\
41 	(t)->warmup_ticks = 0;						\
42 	(t)->done = false;						\
43 	(t)->machine_readable = false;					\
44 } while (0)
45 
46 void bpf_bench_timing_measure(struct bpf_bench_timing *t, struct bench_res *res);
47 void bpf_bench_timing_report(struct bpf_bench_timing *t, const char *name, const char *desc);
48 void bpf_bench_calibrate(struct bpf_bench_timing *t, bpf_bench_run_fn run_fn, void *ctx);
49 
50 #endif /* __BENCH_BPF_TIMING_H__ */
51