]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Allow to benchmark trigger with stacktrace
authorJiri Olsa <jolsa@kernel.org>
Mon, 26 Jan 2026 21:18:37 +0000 (22:18 +0100)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 30 Jan 2026 21:40:09 +0000 (13:40 -0800)
Adding support to call bpf_get_stackid helper from trigger programs,
so far added for kprobe multi.

Adding the --stacktrace/-g option to enable it.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20260126211837.472802-7-jolsa@kernel.org
tools/testing/selftests/bpf/bench.c
tools/testing/selftests/bpf/bench.h
tools/testing/selftests/bpf/benchs/bench_trigger.c
tools/testing/selftests/bpf/progs/trigger_bench.c

index bd29bb2e6cb5294033e7e033d33fffba58e0bede..8368bd3a066516d1710514e921e74845bcb00ccb 100644 (file)
@@ -265,6 +265,7 @@ static const struct argp_option opts[] = {
        { "verbose", 'v', NULL, 0, "Verbose debug output"},
        { "affinity", 'a', NULL, 0, "Set consumer/producer thread affinity"},
        { "quiet", 'q', NULL, 0, "Be more quiet"},
+       { "stacktrace", 's', NULL, 0, "Get stack trace"},
        { "prod-affinity", ARG_PROD_AFFINITY_SET, "CPUSET", 0,
          "Set of CPUs for producer threads; implies --affinity"},
        { "cons-affinity", ARG_CONS_AFFINITY_SET, "CPUSET", 0,
@@ -350,6 +351,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
        case 'q':
                env.quiet = true;
                break;
+       case 's':
+               env.stacktrace = true;
+               break;
        case ARG_PROD_AFFINITY_SET:
                env.affinity = true;
                if (parse_num_list(arg, &env.prod_cpus.cpus,
index bea323820ffb8888318539f4b12142c4fe969575..7cf21936e7eda0c32dcb06e11adf23610738d1db 100644 (file)
@@ -26,6 +26,7 @@ struct env {
        bool list;
        bool affinity;
        bool quiet;
+       bool stacktrace;
        int consumer_cnt;
        int producer_cnt;
        int nr_cpus;
index 34018fc3927f299573735b36d85524038e0f4e46..aeec9edd3851dbf9ea8a87215df8558871904786 100644 (file)
@@ -146,6 +146,7 @@ static void setup_ctx(void)
        bpf_program__set_autoload(ctx.skel->progs.trigger_driver, true);
 
        ctx.skel->rodata->batch_iters = args.batch_iters;
+       ctx.skel->rodata->stacktrace = env.stacktrace;
 }
 
 static void load_ctx(void)
index 2898b3749d076d657f53ff46e575f6287aa18cc9..4ea0422d1042a3ad44a9b2c39a541c5d6649299f 100644 (file)
@@ -25,6 +25,34 @@ static __always_inline void inc_counter(void)
        __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1);
 }
 
+volatile const int stacktrace;
+
+typedef __u64 stack_trace_t[128];
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+        __uint(max_entries, 1);
+       __type(key, __u32);
+       __type(value, stack_trace_t);
+} stack_heap SEC(".maps");
+
+static __always_inline void do_stacktrace(void *ctx)
+{
+       if (!stacktrace)
+               return;
+
+       __u64 *ptr = bpf_map_lookup_elem(&stack_heap, &(__u32){0});
+
+       if (ptr)
+               bpf_get_stack(ctx, ptr, sizeof(stack_trace_t), 0);
+}
+
+static __always_inline void handle(void *ctx)
+{
+       inc_counter();
+       do_stacktrace(ctx);
+}
+
 SEC("?uprobe")
 int bench_trigger_uprobe(void *ctx)
 {
@@ -81,21 +109,21 @@ int trigger_driver_kfunc(void *ctx)
 SEC("?kprobe/bpf_get_numa_node_id")
 int bench_trigger_kprobe(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return 0;
 }
 
 SEC("?kretprobe/bpf_get_numa_node_id")
 int bench_trigger_kretprobe(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return 0;
 }
 
 SEC("?kprobe.multi/bpf_get_numa_node_id")
 int bench_trigger_kprobe_multi(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return 0;
 }
 
@@ -108,7 +136,7 @@ int bench_kprobe_multi_empty(void *ctx)
 SEC("?kretprobe.multi/bpf_get_numa_node_id")
 int bench_trigger_kretprobe_multi(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return 0;
 }
 
@@ -121,34 +149,34 @@ int bench_kretprobe_multi_empty(void *ctx)
 SEC("?fentry/bpf_get_numa_node_id")
 int bench_trigger_fentry(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return 0;
 }
 
 SEC("?fexit/bpf_get_numa_node_id")
 int bench_trigger_fexit(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return 0;
 }
 
 SEC("?fmod_ret/bpf_modify_return_test_tp")
 int bench_trigger_fmodret(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return -22;
 }
 
 SEC("?tp/bpf_test_run/bpf_trigger_tp")
 int bench_trigger_tp(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return 0;
 }
 
 SEC("?raw_tp/bpf_trigger_tp")
 int bench_trigger_rawtp(void *ctx)
 {
-       inc_counter();
+       handle(ctx);
        return 0;
 }