Commit 4173b494 authored by Jiri Olsa's avatar Jiri Olsa Committed by Andrii Nakryiko
Browse files

selftests/bpf: Allow to benchmark trigger with stacktrace



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: default avatarJiri Olsa <jolsa@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20260126211837.472802-7-jolsa@kernel.org
parent e5d532be
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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,
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ struct env {
	bool list;
	bool affinity;
	bool quiet;
	bool stacktrace;
	int consumer_cnt;
	int producer_cnt;
	int nr_cpus;
+1 −0
Original line number Diff line number Diff line
@@ -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)
+37 −9
Original line number Diff line number Diff line
@@ -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;
}