Skip to content

Commit

Permalink
[WIP] bench: benchmark per-CPU array/hashmap lookups
Browse files Browse the repository at this point in the history
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
  • Loading branch information
anakryiko committed Apr 4, 2024
1 parent 8b06116 commit 8dec900
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tools/testing/selftests/bpf/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ extern const struct bench bench_trig_fmodret;
extern const struct bench bench_trig_tp;
extern const struct bench bench_trig_rawtp;

extern const struct bench bench_trig_arr_inc;
extern const struct bench bench_trig_hash_inc;
extern const struct bench bench_trig_glob_arr_inc;

/* uprobe/uretprobe benchmarks */
extern const struct bench bench_trig_uprobe_nop;
extern const struct bench bench_trig_uretprobe_nop;
Expand Down Expand Up @@ -562,6 +566,11 @@ static const struct bench *benchs[] = {
&bench_trig_fmodret,
&bench_trig_tp,
&bench_trig_rawtp,

&bench_trig_arr_inc,
&bench_trig_hash_inc,
&bench_trig_glob_arr_inc,

/* uprobes */
&bench_trig_uprobe_nop,
&bench_trig_uretprobe_nop,
Expand Down
31 changes: 31 additions & 0 deletions tools/testing/selftests/bpf/benchs/bench_trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,33 @@ static void trigger_fentry_setup(void)
attach_bpf(ctx.skel->progs.bench_trigger_fentry);
}

static void trigger_arr_inc_setup(void)
{
setup_ctx();
bpf_program__set_autoload(ctx.skel->progs.trigger_driver, false);
bpf_program__set_autoload(ctx.skel->progs.trigger_arr_inc, true);
load_ctx();
ctx.driver_prog_fd = bpf_program__fd(ctx.skel->progs.trigger_arr_inc);
}

static void trigger_hash_inc_setup(void)
{
setup_ctx();
bpf_program__set_autoload(ctx.skel->progs.trigger_driver, false);
bpf_program__set_autoload(ctx.skel->progs.trigger_hash_inc, true);
load_ctx();
ctx.driver_prog_fd = bpf_program__fd(ctx.skel->progs.trigger_hash_inc);
}

static void trigger_glob_arr_inc_setup(void)
{
setup_ctx();
bpf_program__set_autoload(ctx.skel->progs.trigger_driver, false);
bpf_program__set_autoload(ctx.skel->progs.trigger_glob_arr_inc, true);
load_ctx();
ctx.driver_prog_fd = bpf_program__fd(ctx.skel->progs.trigger_glob_arr_inc);
}

static void trigger_fexit_setup(void)
{
setup_ctx();
Expand Down Expand Up @@ -435,6 +462,10 @@ BENCH_TRIG_KERNEL(fmodret, "fmodret");
BENCH_TRIG_KERNEL(tp, "tp");
BENCH_TRIG_KERNEL(rawtp, "rawtp");

BENCH_TRIG_KERNEL(arr_inc, "arr-inc");
BENCH_TRIG_KERNEL(hash_inc, "hash-inc");
BENCH_TRIG_KERNEL(glob_arr_inc, "glob-arr-inc");

/* uprobe benchmarks */
#define BENCH_TRIG_USERMODE(KIND, PRODUCER, NAME) \
const struct bench bench_trig_##KIND = { \
Expand Down
83 changes: 83 additions & 0 deletions tools/testing/selftests/bpf/progs/trigger_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,50 @@ static __always_inline void inc_counter(void)
__sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1);
}

static __always_inline void inc_counter2(int amount)
{
int cpu = bpf_get_smp_processor_id();

__sync_add_and_fetch(&hits[cpu & CPU_MASK].value, amount);
}

struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__type(key, int);
__type(value, int);
__uint(max_entries, 1);
} hash_map SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__type(key, int);
__type(value, int);
__uint(max_entries, 1);
} array_map SEC(".maps");

static int zero = 0;

static void __always_inline hash_inc(void *map) {
int *p;

p = bpf_map_lookup_elem(map, &zero);
if (!p) {
bpf_map_update_elem(map, &zero, &zero, BPF_ANY);
p = bpf_map_lookup_elem(map, &zero);
if (!p)
return;
}
*p += 1;
}

struct counter arr[256];

static void __always_inline glob_arr_inc(void) {
int cpu = bpf_get_smp_processor_id();

arr[cpu].value += 1;
}

SEC("?uprobe")
int bench_trigger_uprobe(void *ctx)
{
Expand All @@ -34,6 +78,45 @@ int bench_trigger_uprobe(void *ctx)

const volatile int batch_iters = 0;

SEC("?raw_tp")
int trigger_arr_inc(void *ctx)
{
int i;

for (i = 0; i < batch_iters; i++)
hash_inc(&array_map);

inc_counter2(batch_iters);

return 0;
}

SEC("?raw_tp")
int trigger_hash_inc(void *ctx)
{
int i;

for (i = 0; i < batch_iters; i++)
hash_inc(&hash_map);

inc_counter2(batch_iters);

return 0;
}

SEC("?raw_tp")
int trigger_glob_arr_inc(void *ctx)
{
int i;

for (i = 0; i < batch_iters; i++)
glob_arr_inc();

inc_counter2(batch_iters);

return 0;
}

SEC("?raw_tp")
int trigger_count(void *ctx)
{
Expand Down

0 comments on commit 8dec900

Please sign in to comment.