| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright (c) 2022 Bytedance */ |
| 3 | |
| 4 | #include "bench.h" |
| 5 | #include "bpf_hashmap_full_update_bench.skel.h" |
| 6 | #include "bpf_util.h" |
| 7 | |
| 8 | /* BPF triggering benchmarks */ |
| 9 | static struct ctx { |
| 10 | struct bpf_hashmap_full_update_bench *skel; |
| 11 | } ctx; |
| 12 | |
| 13 | #define MAX_LOOP_NUM 10000 |
| 14 | |
| 15 | static void validate(void) |
| 16 | { |
| 17 | if (env.consumer_cnt != 0) { |
| 18 | fprintf(stderr, "benchmark doesn't support consumer!\n" ); |
| 19 | exit(1); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | static void *producer(void *input) |
| 24 | { |
| 25 | while (true) { |
| 26 | /* trigger the bpf program */ |
| 27 | syscall(__NR_getpgid); |
| 28 | } |
| 29 | |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | static void measure(struct bench_res *res) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | static void setup(void) |
| 38 | { |
| 39 | struct bpf_link *link; |
| 40 | int map_fd, i, max_entries; |
| 41 | |
| 42 | setup_libbpf(); |
| 43 | |
| 44 | ctx.skel = bpf_hashmap_full_update_bench__open_and_load(); |
| 45 | if (!ctx.skel) { |
| 46 | fprintf(stderr, "failed to open skeleton\n" ); |
| 47 | exit(1); |
| 48 | } |
| 49 | |
| 50 | ctx.skel->bss->nr_loops = MAX_LOOP_NUM; |
| 51 | |
| 52 | link = bpf_program__attach(ctx.skel->progs.benchmark); |
| 53 | if (!link) { |
| 54 | fprintf(stderr, "failed to attach program!\n" ); |
| 55 | exit(1); |
| 56 | } |
| 57 | |
| 58 | /* fill hash_map */ |
| 59 | map_fd = bpf_map__fd(ctx.skel->maps.hash_map_bench); |
| 60 | max_entries = bpf_map__max_entries(ctx.skel->maps.hash_map_bench); |
| 61 | for (i = 0; i < max_entries; i++) |
| 62 | bpf_map_update_elem(map_fd, &i, &i, BPF_ANY); |
| 63 | } |
| 64 | |
| 65 | static void hashmap_report_final(struct bench_res res[], int res_cnt) |
| 66 | { |
| 67 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < nr_cpus; i++) { |
| 71 | u64 time = ctx.skel->bss->percpu_time[i]; |
| 72 | |
| 73 | if (!time) |
| 74 | continue; |
| 75 | |
| 76 | printf("%d:hash_map_full_perf %lld events per sec\n" , |
| 77 | i, ctx.skel->bss->nr_loops * 1000000000ll / time); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | const struct bench bench_bpf_hashmap_full_update = { |
| 82 | .name = "bpf-hashmap-full-update" , |
| 83 | .validate = validate, |
| 84 | .setup = setup, |
| 85 | .producer_thread = producer, |
| 86 | .measure = measure, |
| 87 | .report_progress = NULL, |
| 88 | .report_final = hashmap_report_final, |
| 89 | }; |
| 90 | |