Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftest/bpf/benchs: Add bpf_map benchmark

Add benchmark for hash_map to reproduce the worst case
that non-stop update when map's free is zero.

Just like this:
./run_bench_bpf_hashmap_full_update.sh
Setting up benchmark 'bpf-hashmap-ful-update'...
Benchmark 'bpf-hashmap-ful-update' started.
1:hash_map_full_perf 555830 events per sec
...

Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
Link: https://lore.kernel.org/r/20220610023308.93798-3-zhoufeng.zf@bytedance.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Feng Zhou and committed by
Alexei Starovoitov
89eda984 54a9c3a4

+152 -1
+3 -1
tools/testing/selftests/bpf/Makefile
··· 560 560 $(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h 561 561 $(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h 562 562 $(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h 563 + $(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h 563 564 $(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) 564 565 $(OUTPUT)/bench: LDLIBS += -lm 565 566 $(OUTPUT)/bench: $(OUTPUT)/bench.o \ ··· 572 571 $(OUTPUT)/bench_ringbufs.o \ 573 572 $(OUTPUT)/bench_bloom_filter_map.o \ 574 573 $(OUTPUT)/bench_bpf_loop.o \ 575 - $(OUTPUT)/bench_strncmp.o 574 + $(OUTPUT)/bench_strncmp.o \ 575 + $(OUTPUT)/bench_bpf_hashmap_full_update.o 576 576 $(call msg,BINARY,,$@) 577 577 $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ 578 578
+2
tools/testing/selftests/bpf/bench.c
··· 396 396 extern const struct bench bench_bpf_loop; 397 397 extern const struct bench bench_strncmp_no_helper; 398 398 extern const struct bench bench_strncmp_helper; 399 + extern const struct bench bench_bpf_hashmap_full_update; 399 400 400 401 static const struct bench *benchs[] = { 401 402 &bench_count_global, ··· 431 430 &bench_bpf_loop, 432 431 &bench_strncmp_no_helper, 433 432 &bench_strncmp_helper, 433 + &bench_bpf_hashmap_full_update, 434 434 }; 435 435 436 436 static void setup_benchmark()
+96
tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2022 Bytedance */ 3 + 4 + #include <argp.h> 5 + #include "bench.h" 6 + #include "bpf_hashmap_full_update_bench.skel.h" 7 + #include "bpf_util.h" 8 + 9 + /* BPF triggering benchmarks */ 10 + static struct ctx { 11 + struct bpf_hashmap_full_update_bench *skel; 12 + } ctx; 13 + 14 + #define MAX_LOOP_NUM 10000 15 + 16 + static void validate(void) 17 + { 18 + if (env.consumer_cnt != 1) { 19 + fprintf(stderr, "benchmark doesn't support multi-consumer!\n"); 20 + exit(1); 21 + } 22 + } 23 + 24 + static void *producer(void *input) 25 + { 26 + while (true) { 27 + /* trigger the bpf program */ 28 + syscall(__NR_getpgid); 29 + } 30 + 31 + return NULL; 32 + } 33 + 34 + static void *consumer(void *input) 35 + { 36 + return NULL; 37 + } 38 + 39 + static void measure(struct bench_res *res) 40 + { 41 + } 42 + 43 + static void setup(void) 44 + { 45 + struct bpf_link *link; 46 + int map_fd, i, max_entries; 47 + 48 + setup_libbpf(); 49 + 50 + ctx.skel = bpf_hashmap_full_update_bench__open_and_load(); 51 + if (!ctx.skel) { 52 + fprintf(stderr, "failed to open skeleton\n"); 53 + exit(1); 54 + } 55 + 56 + ctx.skel->bss->nr_loops = MAX_LOOP_NUM; 57 + 58 + link = bpf_program__attach(ctx.skel->progs.benchmark); 59 + if (!link) { 60 + fprintf(stderr, "failed to attach program!\n"); 61 + exit(1); 62 + } 63 + 64 + /* fill hash_map */ 65 + map_fd = bpf_map__fd(ctx.skel->maps.hash_map_bench); 66 + max_entries = bpf_map__max_entries(ctx.skel->maps.hash_map_bench); 67 + for (i = 0; i < max_entries; i++) 68 + bpf_map_update_elem(map_fd, &i, &i, BPF_ANY); 69 + } 70 + 71 + void hashmap_report_final(struct bench_res res[], int res_cnt) 72 + { 73 + unsigned int nr_cpus = bpf_num_possible_cpus(); 74 + int i; 75 + 76 + for (i = 0; i < nr_cpus; i++) { 77 + u64 time = ctx.skel->bss->percpu_time[i]; 78 + 79 + if (!time) 80 + continue; 81 + 82 + printf("%d:hash_map_full_perf %lld events per sec\n", 83 + i, ctx.skel->bss->nr_loops * 1000000000ll / time); 84 + } 85 + } 86 + 87 + const struct bench bench_bpf_hashmap_full_update = { 88 + .name = "bpf-hashmap-ful-update", 89 + .validate = validate, 90 + .setup = setup, 91 + .producer_thread = producer, 92 + .consumer_thread = consumer, 93 + .measure = measure, 94 + .report_progress = NULL, 95 + .report_final = hashmap_report_final, 96 + };
+11
tools/testing/selftests/bpf/benchs/run_bench_bpf_hashmap_full_update.sh
··· 1 + #!/bin/bash 2 + # SPDX-License-Identifier: GPL-2.0 3 + 4 + source ./benchs/run_common.sh 5 + 6 + set -eufo pipefail 7 + 8 + nr_threads=`expr $(cat /proc/cpuinfo | grep "processor"| wc -l) - 1` 9 + summary=$($RUN_BENCH -p $nr_threads bpf-hashmap-ful-update) 10 + printf "$summary" 11 + printf "\n"
+40
tools/testing/selftests/bpf/progs/bpf_hashmap_full_update_bench.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2022 Bytedance */ 3 + 4 + #include "vmlinux.h" 5 + #include <bpf/bpf_helpers.h> 6 + #include "bpf_misc.h" 7 + 8 + char _license[] SEC("license") = "GPL"; 9 + 10 + #define MAX_ENTRIES 1000 11 + 12 + struct { 13 + __uint(type, BPF_MAP_TYPE_HASH); 14 + __type(key, u32); 15 + __type(value, u64); 16 + __uint(max_entries, MAX_ENTRIES); 17 + } hash_map_bench SEC(".maps"); 18 + 19 + u64 __attribute__((__aligned__(256))) percpu_time[256]; 20 + u64 nr_loops; 21 + 22 + static int loop_update_callback(__u32 index, u32 *key) 23 + { 24 + u64 init_val = 1; 25 + 26 + bpf_map_update_elem(&hash_map_bench, key, &init_val, BPF_ANY); 27 + return 0; 28 + } 29 + 30 + SEC("fentry/" SYS_PREFIX "sys_getpgid") 31 + int benchmark(void *ctx) 32 + { 33 + u32 cpu = bpf_get_smp_processor_id(); 34 + u32 key = cpu + MAX_ENTRIES; 35 + u64 start_time = bpf_ktime_get_ns(); 36 + 37 + bpf_loop(nr_loops, loop_update_callback, &key, 0); 38 + percpu_time[cpu & 255] = bpf_ktime_get_ns() - start_time; 39 + return 0; 40 + }