mirror of
https://github.com/torvalds/linux.git
synced 2026-07-27 09:36:22 +02:00
Add a reusable timing library for BPF benchmarks that need to measure BPF program execution time. The BPF side (progs/bench_bpf_timing.bpf.h) provides per-CPU sample arrays and BENCH_BPF_LOOP(), a macro that brackets batch_iters iterations with bpf_ktime_get_ns() reads and records the elapsed time. One extra untimed iteration runs afterward for output validation. The userspace side (benchs/bench_bpf_timing.c) collects samples from the skeleton BSS, computes percentile statistics, and auto-calibrates batch_iters to target ~10 ms per batch. Signed-off-by: Puranjay Mohan <puranjay@kernel.org> Link: https://lore.kernel.org/r/20260427232313.1582588-3-puranjay@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
51 lines
1.5 KiB
C
51 lines
1.5 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
|
|
|
|
#ifndef __BENCH_BPF_TIMING_H__
|
|
#define __BENCH_BPF_TIMING_H__
|
|
|
|
#include <stdbool.h>
|
|
#include <linux/types.h>
|
|
#include "bench.h"
|
|
|
|
#ifndef BENCH_NR_SAMPLES
|
|
#define BENCH_NR_SAMPLES 4096
|
|
#endif
|
|
#ifndef BENCH_NR_CPUS
|
|
#define BENCH_NR_CPUS 256
|
|
#endif
|
|
|
|
typedef void (*bpf_bench_run_fn)(void *ctx);
|
|
|
|
struct bpf_bench_timing {
|
|
__u64 (*samples)[BENCH_NR_SAMPLES]; /* skel->bss->timing_samples */
|
|
__u32 *idx; /* skel->bss->timing_idx */
|
|
volatile __u32 *timing_enabled; /* &skel->bss->timing_enabled */
|
|
volatile __u32 *batch_iters_bss; /* &skel->bss->batch_iters */
|
|
__u32 batch_iters;
|
|
__u32 target_samples;
|
|
__u32 nr_cpus;
|
|
int warmup_ticks;
|
|
bool done;
|
|
bool machine_readable;
|
|
};
|
|
|
|
#define BENCH_TIMING_INIT(t, skel, iters) do { \
|
|
(t)->samples = (skel)->bss->timing_samples; \
|
|
(t)->idx = (skel)->bss->timing_idx; \
|
|
(t)->timing_enabled = &(skel)->bss->timing_enabled; \
|
|
(t)->batch_iters_bss = &(skel)->bss->batch_iters; \
|
|
(t)->batch_iters = (iters); \
|
|
(t)->target_samples = 200; \
|
|
(t)->nr_cpus = env.nr_cpus; \
|
|
(t)->warmup_ticks = 0; \
|
|
(t)->done = false; \
|
|
(t)->machine_readable = false; \
|
|
} while (0)
|
|
|
|
void bpf_bench_timing_measure(struct bpf_bench_timing *t, struct bench_res *res);
|
|
void bpf_bench_timing_report(struct bpf_bench_timing *t, const char *name, const char *desc);
|
|
void bpf_bench_calibrate(struct bpf_bench_timing *t, bpf_bench_run_fn run_fn, void *ctx);
|
|
|
|
#endif /* __BENCH_BPF_TIMING_H__ */
|