Benchmark to measure execution time of a simple bpf_dynptr_slice() call, when dynptr type is known and buffer__opt parameter is null. Under such conditions verifier inline the kfunc call and delete a couple of conditionals in the body of the kfunc. Signed-off-by: Eduard Zingerman <eddyz87@xxxxxxxxx> --- tools/testing/selftests/bpf/Makefile | 2 + tools/testing/selftests/bpf/bench.c | 2 + .../selftests/bpf/benchs/bench_dynptr_slice.c | 76 +++++++++++++++++++ .../selftests/bpf/progs/dynptr_slice_bench.c | 29 +++++++ 4 files changed, 109 insertions(+) create mode 100644 tools/testing/selftests/bpf/benchs/bench_dynptr_slice.c create mode 100644 tools/testing/selftests/bpf/progs/dynptr_slice_bench.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index edef5df08cb2..3a938d5b295f 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -845,6 +845,7 @@ $(OUTPUT)/bench_local_storage_create.o: $(OUTPUT)/bench_local_storage_create.ske $(OUTPUT)/bench_bpf_hashmap_lookup.o: $(OUTPUT)/bpf_hashmap_lookup.skel.h $(OUTPUT)/bench_htab_mem.o: $(OUTPUT)/htab_mem_bench.skel.h $(OUTPUT)/bench_bpf_crypto.o: $(OUTPUT)/crypto_bench.skel.h +$(OUTPUT)/bench_dynptr_slice.o: $(OUTPUT)/dynptr_slice_bench.skel.h $(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) $(OUTPUT)/bench: LDLIBS += -lm $(OUTPUT)/bench: $(OUTPUT)/bench.o \ @@ -865,6 +866,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \ $(OUTPUT)/bench_local_storage_create.o \ $(OUTPUT)/bench_htab_mem.o \ $(OUTPUT)/bench_bpf_crypto.o \ + $(OUTPUT)/bench_dynptr_slice.o \ # $(call msg,BINARY,,$@) $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c index 1bd403a5ef7b..74053665465c 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -549,6 +549,7 @@ extern const struct bench bench_local_storage_create; extern const struct bench bench_htab_mem; extern const struct bench bench_crypto_encrypt; extern const struct bench bench_crypto_decrypt; +extern const struct bench bench_dynptr_slice; static const struct bench *benchs[] = { &bench_count_global, @@ -609,6 +610,7 @@ static const struct bench *benchs[] = { &bench_htab_mem, &bench_crypto_encrypt, &bench_crypto_decrypt, + &bench_dynptr_slice, }; static void find_benchmark(void) diff --git a/tools/testing/selftests/bpf/benchs/bench_dynptr_slice.c b/tools/testing/selftests/bpf/benchs/bench_dynptr_slice.c new file mode 100644 index 000000000000..957ecc6f1531 --- /dev/null +++ b/tools/testing/selftests/bpf/benchs/bench_dynptr_slice.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include <argp.h> +#include "bench.h" +#include "dynptr_slice_bench.skel.h" + +static struct dynptr_slice_ctx { + struct dynptr_slice_bench *skel; + int pfd; +} ctx; + +static void dynptr_slice_validate(void) +{ + if (env.consumer_cnt != 0) { + fprintf(stderr, "bpf dynptr_slice benchmark doesn't support consumer!\n"); + exit(1); + } +} + +static void dynptr_slice_setup(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts); + int err; + + setup_libbpf(); + ctx.skel = dynptr_slice_bench__open(); + if (!ctx.skel) { + fprintf(stderr, "failed to open skeleton\n"); + exit(1); + } + + ctx.skel->bss->hits = 0; + err = dynptr_slice_bench__load(ctx.skel); + if (err) { + fprintf(stderr, "failed to load skeleton\n"); + dynptr_slice_bench__destroy(ctx.skel); + exit(1); + } +} + +static void dynptr_slice_encrypt_setup(void) +{ + dynptr_slice_setup(); + ctx.pfd = bpf_program__fd(ctx.skel->progs.dynptr_slice); +} + + +static void dynptr_slice_measure(struct bench_res *res) +{ + res->hits = atomic_swap(&ctx.skel->bss->hits, 0); +} + +static void *dynptr_slice_producer(void *unused) +{ + static const char data_in[1000]; + LIBBPF_OPTS(bpf_test_run_opts, opts, + .repeat = 64, + .data_in = data_in, + .data_size_in = sizeof(data_in), + ); + + while (true) + (void)bpf_prog_test_run_opts(ctx.pfd, &opts); + return NULL; +} + +const struct bench bench_dynptr_slice = { + .name = "dynptr_slice", + .validate = dynptr_slice_validate, + .setup = dynptr_slice_encrypt_setup, + .producer_thread = dynptr_slice_producer, + .measure = dynptr_slice_measure, + .report_progress = hits_drops_report_progress, + .report_final = hits_drops_report_final, +}; diff --git a/tools/testing/selftests/bpf/progs/dynptr_slice_bench.c b/tools/testing/selftests/bpf/progs/dynptr_slice_bench.c new file mode 100644 index 000000000000..65a493426b5e --- /dev/null +++ b/tools/testing/selftests/bpf/progs/dynptr_slice_bench.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include "bpf_compiler.h" +#include "bpf_kfuncs.h" + +long hits = 0; + +SEC("tc") +int dynptr_slice(struct __sk_buff *skb) +{ + struct bpf_dynptr psrc; + const int N = 100; + int i; + + bpf_dynptr_from_skb(skb, 0, &psrc); +__pragma_loop_unroll_full + for (i = 0; i < N; ++i) { + bpf_dynptr_slice(&psrc, i, NULL, 1); + } + __sync_add_and_fetch(&hits, N); + + return 0; +} + +char __license[] SEC("license") = "GPL"; -- 2.47.0