Skip to content

Commit 11cf628

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
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: Jiri Olsa <jolsa@kernel.org>
1 parent b4f3dc9 commit 11cf628

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

tools/testing/selftests/bpf/bench.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ static const struct argp_option opts[] = {
265265
{ "verbose", 'v', NULL, 0, "Verbose debug output"},
266266
{ "affinity", 'a', NULL, 0, "Set consumer/producer thread affinity"},
267267
{ "quiet", 'q', NULL, 0, "Be more quiet"},
268+
{ "stacktrace", 's', NULL, 0, "Get stack trace"},
268269
{ "prod-affinity", ARG_PROD_AFFINITY_SET, "CPUSET", 0,
269270
"Set of CPUs for producer threads; implies --affinity"},
270271
{ "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)
350351
case 'q':
351352
env.quiet = true;
352353
break;
354+
case 's':
355+
env.stacktrace = true;
356+
break;
353357
case ARG_PROD_AFFINITY_SET:
354358
env.affinity = true;
355359
if (parse_num_list(arg, &env.prod_cpus.cpus,

tools/testing/selftests/bpf/bench.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct env {
2626
bool list;
2727
bool affinity;
2828
bool quiet;
29+
bool stacktrace;
2930
int consumer_cnt;
3031
int producer_cnt;
3132
int nr_cpus;

tools/testing/selftests/bpf/benchs/bench_trigger.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static void setup_ctx(void)
146146
bpf_program__set_autoload(ctx.skel->progs.trigger_driver, true);
147147

148148
ctx.skel->rodata->batch_iters = args.batch_iters;
149+
ctx.skel->rodata->stacktrace = env.stacktrace;
149150
}
150151

151152
static void load_ctx(void)

tools/testing/selftests/bpf/progs/trigger_bench.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,34 @@ static __always_inline void inc_counter(void)
2525
__sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1);
2626
}
2727

28+
volatile const int stacktrace;
29+
30+
typedef __u64 stack_trace_t[128];
31+
32+
struct {
33+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
34+
__uint(max_entries, 1);
35+
__type(key, __u32);
36+
__type(value, stack_trace_t);
37+
} stack_heap SEC(".maps");
38+
39+
static __always_inline void do_stacktrace(void *ctx)
40+
{
41+
if (!stacktrace)
42+
return;
43+
44+
__u64 *ptr = bpf_map_lookup_elem(&stack_heap, &(__u32){0});
45+
46+
if (ptr)
47+
bpf_get_stack(ctx, ptr, sizeof(stack_trace_t), 0);
48+
}
49+
50+
static __always_inline void handle(void *ctx)
51+
{
52+
inc_counter();
53+
do_stacktrace(ctx);
54+
}
55+
2856
SEC("?uprobe")
2957
int bench_trigger_uprobe(void *ctx)
3058
{
@@ -81,21 +109,21 @@ int trigger_driver_kfunc(void *ctx)
81109
SEC("?kprobe/bpf_get_numa_node_id")
82110
int bench_trigger_kprobe(void *ctx)
83111
{
84-
inc_counter();
112+
handle(ctx);
85113
return 0;
86114
}
87115

88116
SEC("?kretprobe/bpf_get_numa_node_id")
89117
int bench_trigger_kretprobe(void *ctx)
90118
{
91-
inc_counter();
119+
handle(ctx);
92120
return 0;
93121
}
94122

95123
SEC("?kprobe.multi/bpf_get_numa_node_id")
96124
int bench_trigger_kprobe_multi(void *ctx)
97125
{
98-
inc_counter();
126+
handle(ctx);
99127
return 0;
100128
}
101129

@@ -108,7 +136,7 @@ int bench_kprobe_multi_empty(void *ctx)
108136
SEC("?kretprobe.multi/bpf_get_numa_node_id")
109137
int bench_trigger_kretprobe_multi(void *ctx)
110138
{
111-
inc_counter();
139+
handle(ctx);
112140
return 0;
113141
}
114142

@@ -121,34 +149,34 @@ int bench_kretprobe_multi_empty(void *ctx)
121149
SEC("?fentry/bpf_get_numa_node_id")
122150
int bench_trigger_fentry(void *ctx)
123151
{
124-
inc_counter();
152+
handle(ctx);
125153
return 0;
126154
}
127155

128156
SEC("?fexit/bpf_get_numa_node_id")
129157
int bench_trigger_fexit(void *ctx)
130158
{
131-
inc_counter();
159+
handle(ctx);
132160
return 0;
133161
}
134162

135163
SEC("?fmod_ret/bpf_modify_return_test_tp")
136164
int bench_trigger_fmodret(void *ctx)
137165
{
138-
inc_counter();
166+
handle(ctx);
139167
return -22;
140168
}
141169

142170
SEC("?tp/bpf_test_run/bpf_trigger_tp")
143171
int bench_trigger_tp(void *ctx)
144172
{
145-
inc_counter();
173+
handle(ctx);
146174
return 0;
147175
}
148176

149177
SEC("?raw_tp/bpf_trigger_tp")
150178
int bench_trigger_rawtp(void *ctx)
151179
{
152-
inc_counter();
180+
handle(ctx);
153181
return 0;
154182
}

0 commit comments

Comments
 (0)