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 
validate(void)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 
producer(void * input)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 
consumer(void * input)34 static void *consumer(void *input)
35 {
36 	return NULL;
37 }
38 
measure(struct bench_res * res)39 static void measure(struct bench_res *res)
40 {
41 }
42 
setup(void)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 
hashmap_report_final(struct bench_res res[],int res_cnt)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 };
97