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
validate(void)15 static void validate(void)
16 {
17 if (env.consumer_cnt != 1) {
18 fprintf(stderr, "benchmark doesn't support multi-consumer!\n");
19 exit(1);
20 }
21 }
22
producer(void * input)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
consumer(void * input)33 static void *consumer(void *input)
34 {
35 return NULL;
36 }
37
measure(struct bench_res * res)38 static void measure(struct bench_res *res)
39 {
40 }
41
setup(void)42 static void setup(void)
43 {
44 struct bpf_link *link;
45 int map_fd, i, max_entries;
46
47 setup_libbpf();
48
49 ctx.skel = bpf_hashmap_full_update_bench__open_and_load();
50 if (!ctx.skel) {
51 fprintf(stderr, "failed to open skeleton\n");
52 exit(1);
53 }
54
55 ctx.skel->bss->nr_loops = MAX_LOOP_NUM;
56
57 link = bpf_program__attach(ctx.skel->progs.benchmark);
58 if (!link) {
59 fprintf(stderr, "failed to attach program!\n");
60 exit(1);
61 }
62
63 /* fill hash_map */
64 map_fd = bpf_map__fd(ctx.skel->maps.hash_map_bench);
65 max_entries = bpf_map__max_entries(ctx.skel->maps.hash_map_bench);
66 for (i = 0; i < max_entries; i++)
67 bpf_map_update_elem(map_fd, &i, &i, BPF_ANY);
68 }
69
hashmap_report_final(struct bench_res res[],int res_cnt)70 static void hashmap_report_final(struct bench_res res[], int res_cnt)
71 {
72 unsigned int nr_cpus = bpf_num_possible_cpus();
73 int i;
74
75 for (i = 0; i < nr_cpus; i++) {
76 u64 time = ctx.skel->bss->percpu_time[i];
77
78 if (!time)
79 continue;
80
81 printf("%d:hash_map_full_perf %lld events per sec\n",
82 i, ctx.skel->bss->nr_loops * 1000000000ll / time);
83 }
84 }
85
86 const struct bench bench_bpf_hashmap_full_update = {
87 .name = "bpf-hashmap-full-update",
88 .validate = validate,
89 .setup = setup,
90 .producer_thread = producer,
91 .consumer_thread = consumer,
92 .measure = measure,
93 .report_progress = NULL,
94 .report_final = hashmap_report_final,
95 };
96