Skip to content

Commit 8dec900

Browse files
committed
[WIP] bench: benchmark per-CPU array/hashmap lookups
Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent 8b06116 commit 8dec900

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

tools/testing/selftests/bpf/bench.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ extern const struct bench bench_trig_fmodret;
510510
extern const struct bench bench_trig_tp;
511511
extern const struct bench bench_trig_rawtp;
512512

513+
extern const struct bench bench_trig_arr_inc;
514+
extern const struct bench bench_trig_hash_inc;
515+
extern const struct bench bench_trig_glob_arr_inc;
516+
513517
/* uprobe/uretprobe benchmarks */
514518
extern const struct bench bench_trig_uprobe_nop;
515519
extern const struct bench bench_trig_uretprobe_nop;
@@ -562,6 +566,11 @@ static const struct bench *benchs[] = {
562566
&bench_trig_fmodret,
563567
&bench_trig_tp,
564568
&bench_trig_rawtp,
569+
570+
&bench_trig_arr_inc,
571+
&bench_trig_hash_inc,
572+
&bench_trig_glob_arr_inc,
573+
565574
/* uprobes */
566575
&bench_trig_uprobe_nop,
567576
&bench_trig_uretprobe_nop,

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,33 @@ static void trigger_fentry_setup(void)
225225
attach_bpf(ctx.skel->progs.bench_trigger_fentry);
226226
}
227227

228+
static void trigger_arr_inc_setup(void)
229+
{
230+
setup_ctx();
231+
bpf_program__set_autoload(ctx.skel->progs.trigger_driver, false);
232+
bpf_program__set_autoload(ctx.skel->progs.trigger_arr_inc, true);
233+
load_ctx();
234+
ctx.driver_prog_fd = bpf_program__fd(ctx.skel->progs.trigger_arr_inc);
235+
}
236+
237+
static void trigger_hash_inc_setup(void)
238+
{
239+
setup_ctx();
240+
bpf_program__set_autoload(ctx.skel->progs.trigger_driver, false);
241+
bpf_program__set_autoload(ctx.skel->progs.trigger_hash_inc, true);
242+
load_ctx();
243+
ctx.driver_prog_fd = bpf_program__fd(ctx.skel->progs.trigger_hash_inc);
244+
}
245+
246+
static void trigger_glob_arr_inc_setup(void)
247+
{
248+
setup_ctx();
249+
bpf_program__set_autoload(ctx.skel->progs.trigger_driver, false);
250+
bpf_program__set_autoload(ctx.skel->progs.trigger_glob_arr_inc, true);
251+
load_ctx();
252+
ctx.driver_prog_fd = bpf_program__fd(ctx.skel->progs.trigger_glob_arr_inc);
253+
}
254+
228255
static void trigger_fexit_setup(void)
229256
{
230257
setup_ctx();
@@ -435,6 +462,10 @@ BENCH_TRIG_KERNEL(fmodret, "fmodret");
435462
BENCH_TRIG_KERNEL(tp, "tp");
436463
BENCH_TRIG_KERNEL(rawtp, "rawtp");
437464

465+
BENCH_TRIG_KERNEL(arr_inc, "arr-inc");
466+
BENCH_TRIG_KERNEL(hash_inc, "hash-inc");
467+
BENCH_TRIG_KERNEL(glob_arr_inc, "glob-arr-inc");
468+
438469
/* uprobe benchmarks */
439470
#define BENCH_TRIG_USERMODE(KIND, PRODUCER, NAME) \
440471
const struct bench bench_trig_##KIND = { \

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

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

28+
static __always_inline void inc_counter2(int amount)
29+
{
30+
int cpu = bpf_get_smp_processor_id();
31+
32+
__sync_add_and_fetch(&hits[cpu & CPU_MASK].value, amount);
33+
}
34+
35+
struct {
36+
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
37+
__type(key, int);
38+
__type(value, int);
39+
__uint(max_entries, 1);
40+
} hash_map SEC(".maps");
41+
42+
struct {
43+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
44+
__type(key, int);
45+
__type(value, int);
46+
__uint(max_entries, 1);
47+
} array_map SEC(".maps");
48+
49+
static int zero = 0;
50+
51+
static void __always_inline hash_inc(void *map) {
52+
int *p;
53+
54+
p = bpf_map_lookup_elem(map, &zero);
55+
if (!p) {
56+
bpf_map_update_elem(map, &zero, &zero, BPF_ANY);
57+
p = bpf_map_lookup_elem(map, &zero);
58+
if (!p)
59+
return;
60+
}
61+
*p += 1;
62+
}
63+
64+
struct counter arr[256];
65+
66+
static void __always_inline glob_arr_inc(void) {
67+
int cpu = bpf_get_smp_processor_id();
68+
69+
arr[cpu].value += 1;
70+
}
71+
2872
SEC("?uprobe")
2973
int bench_trigger_uprobe(void *ctx)
3074
{
@@ -34,6 +78,45 @@ int bench_trigger_uprobe(void *ctx)
3478

3579
const volatile int batch_iters = 0;
3680

81+
SEC("?raw_tp")
82+
int trigger_arr_inc(void *ctx)
83+
{
84+
int i;
85+
86+
for (i = 0; i < batch_iters; i++)
87+
hash_inc(&array_map);
88+
89+
inc_counter2(batch_iters);
90+
91+
return 0;
92+
}
93+
94+
SEC("?raw_tp")
95+
int trigger_hash_inc(void *ctx)
96+
{
97+
int i;
98+
99+
for (i = 0; i < batch_iters; i++)
100+
hash_inc(&hash_map);
101+
102+
inc_counter2(batch_iters);
103+
104+
return 0;
105+
}
106+
107+
SEC("?raw_tp")
108+
int trigger_glob_arr_inc(void *ctx)
109+
{
110+
int i;
111+
112+
for (i = 0; i < batch_iters; i++)
113+
glob_arr_inc();
114+
115+
inc_counter2(batch_iters);
116+
117+
return 0;
118+
}
119+
37120
SEC("?raw_tp")
38121
int trigger_count(void *ctx)
39122
{

0 commit comments

Comments
 (0)