Add an x86-64 one-shot generic benchmark comparing 1000 individual fentry link creations with one fentry.multi link creation for the same 1000 bpf prog targets.
fentry.multi is much faster than fentry when attaching to 1000 targets on a 16c16g QEMU VM on an AMD EPYC 9745 server. ./bench tracing-multi-attach-progs Setting up benchmark 'tracing-multi-attach-progs'... tracing-multi-attach-progs: prepared 1000 identical BPF program targets tracing-multi-attach-progs: fentry created and attached 1000 programs/links in 338.171ms tracing-multi-attach-progs: fentry.multi created one program and attached one 1000-target link in 47.345ms tracing-multi-attach-progs: fentry.multi creation/attachment speedup is 7.14x Signed-off-by: Leon Hwang <[email protected]> --- tools/testing/selftests/bpf/Makefile | 1 + tools/testing/selftests/bpf/bench.c | 5 + .../benchs/bench_tracing_multi_attach_progs.c | 193 ++++++++++++++++++ .../bpf/progs/tracing_multi_attach_progs.c | 24 +++ 4 files changed, 223 insertions(+) create mode 100644 tools/testing/selftests/bpf/benchs/bench_tracing_multi_attach_progs.c create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_attach_progs.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 80d8bbbc3084..23bed732d1bd 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -625,6 +625,7 @@ $(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h $(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h $(OUTPUT)/bench_bpf_for.o: $(OUTPUT)/bpf_for_bench.skel.h $(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h +$(OUTPUT)/bench_tracing_multi_attach_progs.o: $(OUTPUT)/tracing_multi_attach_progs.skel.h $(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h $(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c index d5f494184f9d..c7502187e654 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -529,6 +529,9 @@ extern const struct bench bench_trig_fmodret; extern const struct bench bench_trig_tp; extern const struct bench bench_trig_rawtp; +/* tracing_multi link attachment benchmark */ +extern const struct bench bench_tracing_multi_attach_progs; + /* uprobe/uretprobe benchmarks */ extern const struct bench bench_trig_uprobe_nop; extern const struct bench bench_trig_uretprobe_nop; @@ -615,6 +618,8 @@ static const struct bench *benchs[] = { &bench_trig_fmodret, &bench_trig_tp, &bench_trig_rawtp, + /* tracing_multi */ + &bench_tracing_multi_attach_progs, /* uprobes */ &bench_trig_uprobe_nop, &bench_trig_uretprobe_nop, diff --git a/tools/testing/selftests/bpf/benchs/bench_tracing_multi_attach_progs.c b/tools/testing/selftests/bpf/benchs/bench_tracing_multi_attach_progs.c new file mode 100644 index 000000000000..6cc6851eb488 --- /dev/null +++ b/tools/testing/selftests/bpf/benchs/bench_tracing_multi_attach_progs.c @@ -0,0 +1,193 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <stdio.h> +#include <string.h> +#include "bench.h" +#include "testing_helpers.h" +#include "tracing_multi_attach_progs.skel.h" +#include "bpf/libbpf_internal.h" + +#define TARGET_CNT 1000 + +static struct ctx { + struct tracing_multi_attach_progs *targets[TARGET_CNT]; + struct tracing_multi_attach_progs *fentries[TARGET_CNT]; + struct tracing_multi_attach_progs *fentry_multi; + int target_fds[TARGET_CNT]; + __u32 target_btf_ids[TARGET_CNT]; + int fentry_prog_fds[TARGET_CNT]; + int fentry_multi_prog_fd; +} ctx; + +static void prepare(void) +{ + int err, i; + + setup_libbpf(); + + for (i = 0; i < TARGET_CNT; i++) { + struct tracing_multi_attach_progs *target; + int btf_id, fd, obj_token_fd; + + target = tracing_multi_attach_progs__open(); + if (!target) { + fprintf(stderr, "failed to open target program %d\n", i); + exit(1); + } + + bpf_program__set_autoload(target->progs.dummy_fentry, false); + bpf_program__set_autoload(target->progs.dummy_fentry_multi, false); + err = tracing_multi_attach_progs__load(target); + if (err) { + fprintf(stderr, "failed to load target program %d: %s\n", + i, strerror(-err)); + exit(1); + } + + fd = bpf_program__fd(target->progs.target); + obj_token_fd = bpf_object__token_fd(target->obj); + obj_token_fd = obj_token_fd < 0 ? 0 : obj_token_fd; + btf_id = libbpf_find_prog_btf_id("target", fd, obj_token_fd); + if (btf_id <= 0) { + fprintf(stderr, "failed to find target BTF ID: %s\n", + strerror(-btf_id)); + exit(1); + } + + ctx.targets[i] = target; + ctx.target_fds[i] = fd; + ctx.target_btf_ids[i] = btf_id; + } +} + +static long create_and_attach_fentry(void) +{ + int link_fds[TARGET_CNT]; + long start_ns, end_ns; + int err, i, j; + + start_ns = get_time_ns(); + + /* An ordinary fentry program is bound to one target when it is loaded. */ + for (i = 0; i < TARGET_CNT; i++) { + struct tracing_multi_attach_progs *fentry; + + fentry = tracing_multi_attach_progs__open(); + if (!fentry) { + fprintf(stderr, "failed to open fentry program %d\n", i); + exit(1); + } + + bpf_program__set_autoload(fentry->progs.target, false); + bpf_program__set_autoload(fentry->progs.dummy_fentry_multi, false); + err = bpf_program__set_attach_target(fentry->progs.dummy_fentry, ctx.target_fds[i], + "target"); + err = err ?: tracing_multi_attach_progs__load(fentry); + if (err) { + fprintf(stderr, "failed to load fentry program %d: %s\n", + i, strerror(-err)); + exit(1); + } + + ctx.fentries[i] = fentry; + ctx.fentry_prog_fds[i] = bpf_program__fd(fentry->progs.dummy_fentry); + } + + for (i = 0; i < TARGET_CNT; i++) { + link_fds[i] = bpf_link_create(ctx.fentry_prog_fds[i], 0, BPF_TRACE_FENTRY, NULL); + if (link_fds[i] < 0) { + err = errno; + for (j = 0; j < i; j++) + close(link_fds[j]); + fprintf(stderr, "failed to create fentry link %d: %s\n", + i, strerror(err)); + exit(1); + } + } + end_ns = get_time_ns(); + + for (i = 0; i < TARGET_CNT; i++) + close(link_fds[i]); + + return end_ns - start_ns; +} + +static long create_and_attach_fentry_multi(void) +{ + LIBBPF_OPTS(bpf_link_create_opts, opts); + long start_ns, end_ns; + int err, link_fd; + + opts.tracing_multi.ids = ctx.target_btf_ids; + opts.tracing_multi.fds = ctx.target_fds; + opts.tracing_multi.cnt = TARGET_CNT; + + start_ns = get_time_ns(); + + ctx.fentry_multi = tracing_multi_attach_progs__open(); + if (!ctx.fentry_multi) { + fprintf(stderr, "failed to open fentry.multi program\n"); + exit(1); + } + + bpf_program__set_autoload(ctx.fentry_multi->progs.target, false); + bpf_program__set_autoload(ctx.fentry_multi->progs.dummy_fentry, false); + err = tracing_multi_attach_progs__load(ctx.fentry_multi); + if (err) { + fprintf(stderr, "failed to load fentry.multi program: %s\n", + strerror(-err)); + exit(1); + } + + ctx.fentry_multi_prog_fd = bpf_program__fd(ctx.fentry_multi->progs.dummy_fentry_multi); + link_fd = bpf_link_create(ctx.fentry_multi_prog_fd, 0, BPF_TRACE_FENTRY_MULTI, &opts); + end_ns = get_time_ns(); + if (link_fd < 0) { + err = errno; + fprintf(stderr, "failed to create fentry.multi link: %s\n", + strerror(err)); + exit(1); + } + + close(link_fd); + return end_ns - start_ns; +} + +static void cleanup(void) +{ + int i; + + tracing_multi_attach_progs__destroy(ctx.fentry_multi); + for (i = 0; i < TARGET_CNT; i++) { + tracing_multi_attach_progs__destroy(ctx.fentries[i]); + tracing_multi_attach_progs__destroy(ctx.targets[i]); + } +} + +static void setup(void) +{ + double fentry_multi_ms, fentry_ms; + long fentry_multi_ns, fentry_ns; + + prepare(); + fentry_ns = create_and_attach_fentry(); + fentry_multi_ns = create_and_attach_fentry_multi(); + fentry_ms = fentry_ns / 1000000.0; + fentry_multi_ms = fentry_multi_ns / 1000000.0; + + printf("%s: prepared %d identical BPF program targets\n", + bench->name, TARGET_CNT); + printf("%s: fentry created and attached %d programs/links in %-.3lfms\n", + bench->name, TARGET_CNT, fentry_ms); + printf("%s: fentry.multi created one program and attached one %d-target link in %-.3lfms\n", + bench->name, TARGET_CNT, fentry_multi_ms); + printf("%s: fentry.multi creation/attachment speedup is %-.2lfx\n", + bench->name, fentry_ms / fentry_multi_ms); + + cleanup(); + exit(0); +} + +const struct bench bench_tracing_multi_attach_progs = { + .name = "tracing-multi-attach-progs", + .setup = setup, +}; diff --git a/tools/testing/selftests/bpf/progs/tracing_multi_attach_progs.c b/tools/testing/selftests/bpf/progs/tracing_multi_attach_progs.c new file mode 100644 index 000000000000..0ea4513904cd --- /dev/null +++ b/tools/testing/selftests/bpf/progs/tracing_multi_attach_progs.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +SEC("syscall") +int target(void *ctx) +{ + return 0; +} + +SEC("fentry") +int BPF_PROG(dummy_fentry) +{ + return 0; +} + +SEC("fentry.multi") +int BPF_PROG(dummy_fentry_multi) +{ + return 0; +} -- 2.55.0
