xref: /linux/tools/testing/selftests/bpf/benchs/bench_htab_mem.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1fd283ab1SHou Tao // SPDX-License-Identifier: GPL-2.0
2fd283ab1SHou Tao /* Copyright (C) 2023. Huawei Technologies Co., Ltd */
3fd283ab1SHou Tao #include <argp.h>
4fd283ab1SHou Tao #include <stdbool.h>
5fd283ab1SHou Tao #include <pthread.h>
6fd283ab1SHou Tao #include <sys/types.h>
7fd283ab1SHou Tao #include <sys/stat.h>
8fd283ab1SHou Tao #include <sys/param.h>
9fd283ab1SHou Tao #include <fcntl.h>
10fd283ab1SHou Tao 
11fd283ab1SHou Tao #include "bench.h"
12fd283ab1SHou Tao #include "bpf_util.h"
13fd283ab1SHou Tao #include "cgroup_helpers.h"
14fd283ab1SHou Tao #include "htab_mem_bench.skel.h"
15fd283ab1SHou Tao 
16fd283ab1SHou Tao struct htab_mem_use_case {
17fd283ab1SHou Tao 	const char *name;
18fd283ab1SHou Tao 	const char **progs;
19fd283ab1SHou Tao 	/* Do synchronization between addition thread and deletion thread */
20fd283ab1SHou Tao 	bool need_sync;
21fd283ab1SHou Tao };
22fd283ab1SHou Tao 
23fd283ab1SHou Tao static struct htab_mem_ctx {
24fd283ab1SHou Tao 	const struct htab_mem_use_case *uc;
25fd283ab1SHou Tao 	struct htab_mem_bench *skel;
26fd283ab1SHou Tao 	pthread_barrier_t *notify;
27fd283ab1SHou Tao 	int fd;
28fd283ab1SHou Tao } ctx;
29fd283ab1SHou Tao 
30fd283ab1SHou Tao const char *ow_progs[] = {"overwrite", NULL};
31fd283ab1SHou Tao const char *batch_progs[] = {"batch_add_batch_del", NULL};
32fd283ab1SHou Tao const char *add_del_progs[] = {"add_only", "del_only", NULL};
33fd283ab1SHou Tao const static struct htab_mem_use_case use_cases[] = {
34fd283ab1SHou Tao 	{ .name = "overwrite", .progs = ow_progs },
35fd283ab1SHou Tao 	{ .name = "batch_add_batch_del", .progs = batch_progs },
36fd283ab1SHou Tao 	{ .name = "add_del_on_diff_cpu", .progs = add_del_progs, .need_sync = true },
37fd283ab1SHou Tao };
38fd283ab1SHou Tao 
39fd283ab1SHou Tao static struct htab_mem_args {
40fd283ab1SHou Tao 	u32 value_size;
41fd283ab1SHou Tao 	const char *use_case;
42fd283ab1SHou Tao 	bool preallocated;
43fd283ab1SHou Tao } args = {
44fd283ab1SHou Tao 	.value_size = 8,
45fd283ab1SHou Tao 	.use_case = "overwrite",
46fd283ab1SHou Tao 	.preallocated = false,
47fd283ab1SHou Tao };
48fd283ab1SHou Tao 
49fd283ab1SHou Tao enum {
50fd283ab1SHou Tao 	ARG_VALUE_SIZE = 10000,
51fd283ab1SHou Tao 	ARG_USE_CASE = 10001,
52fd283ab1SHou Tao 	ARG_PREALLOCATED = 10002,
53fd283ab1SHou Tao };
54fd283ab1SHou Tao 
55fd283ab1SHou Tao static const struct argp_option opts[] = {
56fd283ab1SHou Tao 	{ "value-size", ARG_VALUE_SIZE, "VALUE_SIZE", 0,
57fd283ab1SHou Tao 	  "Set the value size of hash map (default 8)" },
58fd283ab1SHou Tao 	{ "use-case", ARG_USE_CASE, "USE_CASE", 0,
59fd283ab1SHou Tao 	  "Set the use case of hash map: overwrite|batch_add_batch_del|add_del_on_diff_cpu" },
60fd283ab1SHou Tao 	{ "preallocated", ARG_PREALLOCATED, NULL, 0, "use preallocated hash map" },
61fd283ab1SHou Tao 	{},
62fd283ab1SHou Tao };
63fd283ab1SHou Tao 
htab_mem_parse_arg(int key,char * arg,struct argp_state * state)64fd283ab1SHou Tao static error_t htab_mem_parse_arg(int key, char *arg, struct argp_state *state)
65fd283ab1SHou Tao {
66fd283ab1SHou Tao 	switch (key) {
67fd283ab1SHou Tao 	case ARG_VALUE_SIZE:
68fd283ab1SHou Tao 		args.value_size = strtoul(arg, NULL, 10);
69fd283ab1SHou Tao 		if (args.value_size > 4096) {
70fd283ab1SHou Tao 			fprintf(stderr, "too big value size %u\n", args.value_size);
71fd283ab1SHou Tao 			argp_usage(state);
72fd283ab1SHou Tao 		}
73fd283ab1SHou Tao 		break;
74fd283ab1SHou Tao 	case ARG_USE_CASE:
75fd283ab1SHou Tao 		args.use_case = strdup(arg);
76fd283ab1SHou Tao 		if (!args.use_case) {
77fd283ab1SHou Tao 			fprintf(stderr, "no mem for use-case\n");
78fd283ab1SHou Tao 			argp_usage(state);
79fd283ab1SHou Tao 		}
80fd283ab1SHou Tao 		break;
81fd283ab1SHou Tao 	case ARG_PREALLOCATED:
82fd283ab1SHou Tao 		args.preallocated = true;
83fd283ab1SHou Tao 		break;
84fd283ab1SHou Tao 	default:
85fd283ab1SHou Tao 		return ARGP_ERR_UNKNOWN;
86fd283ab1SHou Tao 	}
87fd283ab1SHou Tao 
88fd283ab1SHou Tao 	return 0;
89fd283ab1SHou Tao }
90fd283ab1SHou Tao 
91fd283ab1SHou Tao const struct argp bench_htab_mem_argp = {
92fd283ab1SHou Tao 	.options = opts,
93fd283ab1SHou Tao 	.parser = htab_mem_parse_arg,
94fd283ab1SHou Tao };
95fd283ab1SHou Tao 
htab_mem_validate(void)96fd283ab1SHou Tao static void htab_mem_validate(void)
97fd283ab1SHou Tao {
98fd283ab1SHou Tao 	if (!strcmp(use_cases[2].name, args.use_case) && env.producer_cnt % 2) {
99fd283ab1SHou Tao 		fprintf(stderr, "%s needs an even number of producers\n", args.use_case);
100fd283ab1SHou Tao 		exit(1);
101fd283ab1SHou Tao 	}
102fd283ab1SHou Tao }
103fd283ab1SHou Tao 
htab_mem_bench_init_barriers(void)104fd283ab1SHou Tao static int htab_mem_bench_init_barriers(void)
105fd283ab1SHou Tao {
106fd283ab1SHou Tao 	pthread_barrier_t *barriers;
107fd283ab1SHou Tao 	unsigned int i, nr;
108fd283ab1SHou Tao 
109fd283ab1SHou Tao 	if (!ctx.uc->need_sync)
110fd283ab1SHou Tao 		return 0;
111fd283ab1SHou Tao 
112fd283ab1SHou Tao 	nr = (env.producer_cnt + 1) / 2;
113fd283ab1SHou Tao 	barriers = calloc(nr, sizeof(*barriers));
114fd283ab1SHou Tao 	if (!barriers)
115fd283ab1SHou Tao 		return -1;
116fd283ab1SHou Tao 
117fd283ab1SHou Tao 	/* Used for synchronization between two threads */
118fd283ab1SHou Tao 	for (i = 0; i < nr; i++)
119fd283ab1SHou Tao 		pthread_barrier_init(&barriers[i], NULL, 2);
120fd283ab1SHou Tao 
121fd283ab1SHou Tao 	ctx.notify = barriers;
122fd283ab1SHou Tao 	return 0;
123fd283ab1SHou Tao }
124fd283ab1SHou Tao 
htab_mem_bench_exit_barriers(void)125fd283ab1SHou Tao static void htab_mem_bench_exit_barriers(void)
126fd283ab1SHou Tao {
127fd283ab1SHou Tao 	unsigned int i, nr;
128fd283ab1SHou Tao 
129fd283ab1SHou Tao 	if (!ctx.notify)
130fd283ab1SHou Tao 		return;
131fd283ab1SHou Tao 
132fd283ab1SHou Tao 	nr = (env.producer_cnt + 1) / 2;
133fd283ab1SHou Tao 	for (i = 0; i < nr; i++)
134fd283ab1SHou Tao 		pthread_barrier_destroy(&ctx.notify[i]);
135fd283ab1SHou Tao 	free(ctx.notify);
136fd283ab1SHou Tao }
137fd283ab1SHou Tao 
htab_mem_find_use_case_or_exit(const char * name)138fd283ab1SHou Tao static const struct htab_mem_use_case *htab_mem_find_use_case_or_exit(const char *name)
139fd283ab1SHou Tao {
140fd283ab1SHou Tao 	unsigned int i;
141fd283ab1SHou Tao 
142fd283ab1SHou Tao 	for (i = 0; i < ARRAY_SIZE(use_cases); i++) {
143fd283ab1SHou Tao 		if (!strcmp(name, use_cases[i].name))
144fd283ab1SHou Tao 			return &use_cases[i];
145fd283ab1SHou Tao 	}
146fd283ab1SHou Tao 
147fd283ab1SHou Tao 	fprintf(stderr, "no such use-case: %s\n", name);
148fd283ab1SHou Tao 	fprintf(stderr, "available use case:");
149fd283ab1SHou Tao 	for (i = 0; i < ARRAY_SIZE(use_cases); i++)
150fd283ab1SHou Tao 		fprintf(stderr, " %s", use_cases[i].name);
151fd283ab1SHou Tao 	fprintf(stderr, "\n");
152fd283ab1SHou Tao 	exit(1);
153fd283ab1SHou Tao }
154fd283ab1SHou Tao 
htab_mem_setup(void)155fd283ab1SHou Tao static void htab_mem_setup(void)
156fd283ab1SHou Tao {
157fd283ab1SHou Tao 	struct bpf_map *map;
158fd283ab1SHou Tao 	const char **names;
159fd283ab1SHou Tao 	int err;
160fd283ab1SHou Tao 
161fd283ab1SHou Tao 	setup_libbpf();
162fd283ab1SHou Tao 
163fd283ab1SHou Tao 	ctx.uc = htab_mem_find_use_case_or_exit(args.use_case);
164fd283ab1SHou Tao 	err = htab_mem_bench_init_barriers();
165fd283ab1SHou Tao 	if (err) {
166fd283ab1SHou Tao 		fprintf(stderr, "failed to init barrier\n");
167fd283ab1SHou Tao 		exit(1);
168fd283ab1SHou Tao 	}
169fd283ab1SHou Tao 
170fd283ab1SHou Tao 	ctx.fd = cgroup_setup_and_join("/htab_mem");
171fd283ab1SHou Tao 	if (ctx.fd < 0)
172fd283ab1SHou Tao 		goto cleanup;
173fd283ab1SHou Tao 
174fd283ab1SHou Tao 	ctx.skel = htab_mem_bench__open();
175fd283ab1SHou Tao 	if (!ctx.skel) {
176fd283ab1SHou Tao 		fprintf(stderr, "failed to open skeleton\n");
177fd283ab1SHou Tao 		goto cleanup;
178fd283ab1SHou Tao 	}
179fd283ab1SHou Tao 
180fd283ab1SHou Tao 	map = ctx.skel->maps.htab;
181fd283ab1SHou Tao 	bpf_map__set_value_size(map, args.value_size);
182fd283ab1SHou Tao 	/* Ensure that different CPUs can operate on different subset */
183fd283ab1SHou Tao 	bpf_map__set_max_entries(map, MAX(8192, 64 * env.nr_cpus));
184fd283ab1SHou Tao 	if (args.preallocated)
185fd283ab1SHou Tao 		bpf_map__set_map_flags(map, bpf_map__map_flags(map) & ~BPF_F_NO_PREALLOC);
186fd283ab1SHou Tao 
187fd283ab1SHou Tao 	names = ctx.uc->progs;
188fd283ab1SHou Tao 	while (*names) {
189fd283ab1SHou Tao 		struct bpf_program *prog;
190fd283ab1SHou Tao 
191fd283ab1SHou Tao 		prog = bpf_object__find_program_by_name(ctx.skel->obj, *names);
192fd283ab1SHou Tao 		if (!prog) {
193fd283ab1SHou Tao 			fprintf(stderr, "no such program %s\n", *names);
194fd283ab1SHou Tao 			goto cleanup;
195fd283ab1SHou Tao 		}
196fd283ab1SHou Tao 		bpf_program__set_autoload(prog, true);
197fd283ab1SHou Tao 		names++;
198fd283ab1SHou Tao 	}
199fd283ab1SHou Tao 	ctx.skel->bss->nr_thread = env.producer_cnt;
200fd283ab1SHou Tao 
201fd283ab1SHou Tao 	err = htab_mem_bench__load(ctx.skel);
202fd283ab1SHou Tao 	if (err) {
203fd283ab1SHou Tao 		fprintf(stderr, "failed to load skeleton\n");
204fd283ab1SHou Tao 		goto cleanup;
205fd283ab1SHou Tao 	}
206fd283ab1SHou Tao 	err = htab_mem_bench__attach(ctx.skel);
207fd283ab1SHou Tao 	if (err) {
208fd283ab1SHou Tao 		fprintf(stderr, "failed to attach skeleton\n");
209fd283ab1SHou Tao 		goto cleanup;
210fd283ab1SHou Tao 	}
211fd283ab1SHou Tao 	return;
212fd283ab1SHou Tao 
213fd283ab1SHou Tao cleanup:
214fd283ab1SHou Tao 	htab_mem_bench__destroy(ctx.skel);
215fd283ab1SHou Tao 	htab_mem_bench_exit_barriers();
216fd283ab1SHou Tao 	if (ctx.fd >= 0) {
217fd283ab1SHou Tao 		close(ctx.fd);
218fd283ab1SHou Tao 		cleanup_cgroup_environment();
219fd283ab1SHou Tao 	}
220fd283ab1SHou Tao 	exit(1);
221fd283ab1SHou Tao }
222fd283ab1SHou Tao 
htab_mem_add_fn(pthread_barrier_t * notify)223fd283ab1SHou Tao static void htab_mem_add_fn(pthread_barrier_t *notify)
224fd283ab1SHou Tao {
225fd283ab1SHou Tao 	while (true) {
226fd283ab1SHou Tao 		/* Do addition */
227fd283ab1SHou Tao 		(void)syscall(__NR_getpgid, 0);
228fd283ab1SHou Tao 		/* Notify deletion thread to do deletion */
229fd283ab1SHou Tao 		pthread_barrier_wait(notify);
230fd283ab1SHou Tao 		/* Wait for deletion to complete */
231fd283ab1SHou Tao 		pthread_barrier_wait(notify);
232fd283ab1SHou Tao 	}
233fd283ab1SHou Tao }
234fd283ab1SHou Tao 
htab_mem_delete_fn(pthread_barrier_t * notify)235fd283ab1SHou Tao static void htab_mem_delete_fn(pthread_barrier_t *notify)
236fd283ab1SHou Tao {
237fd283ab1SHou Tao 	while (true) {
238fd283ab1SHou Tao 		/* Wait for addition to complete */
239fd283ab1SHou Tao 		pthread_barrier_wait(notify);
240fd283ab1SHou Tao 		/* Do deletion */
241fd283ab1SHou Tao 		(void)syscall(__NR_getppid);
242fd283ab1SHou Tao 		/* Notify addition thread to do addition */
243fd283ab1SHou Tao 		pthread_barrier_wait(notify);
244fd283ab1SHou Tao 	}
245fd283ab1SHou Tao }
246fd283ab1SHou Tao 
htab_mem_producer(void * arg)247fd283ab1SHou Tao static void *htab_mem_producer(void *arg)
248fd283ab1SHou Tao {
249fd283ab1SHou Tao 	pthread_barrier_t *notify;
250fd283ab1SHou Tao 	int seq;
251fd283ab1SHou Tao 
252fd283ab1SHou Tao 	if (!ctx.uc->need_sync) {
253fd283ab1SHou Tao 		while (true)
254fd283ab1SHou Tao 			(void)syscall(__NR_getpgid, 0);
255fd283ab1SHou Tao 		return NULL;
256fd283ab1SHou Tao 	}
257fd283ab1SHou Tao 
258fd283ab1SHou Tao 	seq = (long)arg;
259fd283ab1SHou Tao 	notify = &ctx.notify[seq / 2];
260fd283ab1SHou Tao 	if (seq & 1)
261fd283ab1SHou Tao 		htab_mem_delete_fn(notify);
262fd283ab1SHou Tao 	else
263fd283ab1SHou Tao 		htab_mem_add_fn(notify);
264fd283ab1SHou Tao 	return NULL;
265fd283ab1SHou Tao }
266fd283ab1SHou Tao 
htab_mem_read_mem_cgrp_file(const char * name,unsigned long * value)267fd283ab1SHou Tao static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
268fd283ab1SHou Tao {
269fd283ab1SHou Tao 	char buf[32];
270fd283ab1SHou Tao 	ssize_t got;
271fd283ab1SHou Tao 	int fd;
272fd283ab1SHou Tao 
273fd283ab1SHou Tao 	fd = openat(ctx.fd, name, O_RDONLY);
274fd283ab1SHou Tao 	if (fd < 0) {
275fd283ab1SHou Tao 		/* cgroup v1 ? */
276fd283ab1SHou Tao 		fprintf(stderr, "no %s\n", name);
277fd283ab1SHou Tao 		*value = 0;
278fd283ab1SHou Tao 		return;
279fd283ab1SHou Tao 	}
280fd283ab1SHou Tao 
281fd283ab1SHou Tao 	got = read(fd, buf, sizeof(buf) - 1);
282fd283ab1SHou Tao 	if (got <= 0) {
283fd283ab1SHou Tao 		*value = 0;
284fd283ab1SHou Tao 		return;
285fd283ab1SHou Tao 	}
286fd283ab1SHou Tao 	buf[got] = 0;
287fd283ab1SHou Tao 
288fd283ab1SHou Tao 	*value = strtoull(buf, NULL, 0);
289fd283ab1SHou Tao 
290fd283ab1SHou Tao 	close(fd);
291fd283ab1SHou Tao }
292fd283ab1SHou Tao 
htab_mem_measure(struct bench_res * res)293fd283ab1SHou Tao static void htab_mem_measure(struct bench_res *res)
294fd283ab1SHou Tao {
295fd283ab1SHou Tao 	res->hits = atomic_swap(&ctx.skel->bss->op_cnt, 0) / env.producer_cnt;
296fd283ab1SHou Tao 	htab_mem_read_mem_cgrp_file("memory.current", &res->gp_ct);
297fd283ab1SHou Tao }
298fd283ab1SHou Tao 
htab_mem_report_progress(int iter,struct bench_res * res,long delta_ns)299fd283ab1SHou Tao static void htab_mem_report_progress(int iter, struct bench_res *res, long delta_ns)
300fd283ab1SHou Tao {
301fd283ab1SHou Tao 	double loop, mem;
302fd283ab1SHou Tao 
303fd283ab1SHou Tao 	loop = res->hits / 1000.0 / (delta_ns / 1000000000.0);
304fd283ab1SHou Tao 	mem = res->gp_ct / 1048576.0;
305fd283ab1SHou Tao 	printf("Iter %3d (%7.3lfus): ", iter, (delta_ns - 1000000000) / 1000.0);
306fd283ab1SHou Tao 	printf("per-prod-op %7.2lfk/s, memory usage %7.2lfMiB\n", loop, mem);
307fd283ab1SHou Tao }
308fd283ab1SHou Tao 
htab_mem_report_final(struct bench_res res[],int res_cnt)309fd283ab1SHou Tao static void htab_mem_report_final(struct bench_res res[], int res_cnt)
310fd283ab1SHou Tao {
311fd283ab1SHou Tao 	double mem_mean = 0.0, mem_stddev = 0.0;
312fd283ab1SHou Tao 	double loop_mean = 0.0, loop_stddev = 0.0;
313fd283ab1SHou Tao 	unsigned long peak_mem;
314fd283ab1SHou Tao 	int i;
315fd283ab1SHou Tao 
316fd283ab1SHou Tao 	for (i = 0; i < res_cnt; i++) {
317fd283ab1SHou Tao 		loop_mean += res[i].hits / 1000.0 / (0.0 + res_cnt);
318fd283ab1SHou Tao 		mem_mean += res[i].gp_ct / 1048576.0 / (0.0 + res_cnt);
319fd283ab1SHou Tao 	}
320fd283ab1SHou Tao 	if (res_cnt > 1)  {
321fd283ab1SHou Tao 		for (i = 0; i < res_cnt; i++) {
322fd283ab1SHou Tao 			loop_stddev += (loop_mean - res[i].hits / 1000.0) *
323fd283ab1SHou Tao 				       (loop_mean - res[i].hits / 1000.0) /
324fd283ab1SHou Tao 				       (res_cnt - 1.0);
325fd283ab1SHou Tao 			mem_stddev += (mem_mean - res[i].gp_ct / 1048576.0) *
326fd283ab1SHou Tao 				      (mem_mean - res[i].gp_ct / 1048576.0) /
327fd283ab1SHou Tao 				      (res_cnt - 1.0);
328fd283ab1SHou Tao 		}
329fd283ab1SHou Tao 		loop_stddev = sqrt(loop_stddev);
330fd283ab1SHou Tao 		mem_stddev = sqrt(mem_stddev);
331fd283ab1SHou Tao 	}
332fd283ab1SHou Tao 
333fd283ab1SHou Tao 	htab_mem_read_mem_cgrp_file("memory.peak", &peak_mem);
334fd283ab1SHou Tao 	printf("Summary: per-prod-op %7.2lf \u00B1 %7.2lfk/s, memory usage %7.2lf \u00B1 %7.2lfMiB,"
335fd283ab1SHou Tao 	       " peak memory usage %7.2lfMiB\n",
336fd283ab1SHou Tao 	       loop_mean, loop_stddev, mem_mean, mem_stddev, peak_mem / 1048576.0);
337fd283ab1SHou Tao 
338*441c725eSHou Tao 	close(ctx.fd);
339fd283ab1SHou Tao 	cleanup_cgroup_environment();
340fd283ab1SHou Tao }
341fd283ab1SHou Tao 
342fd283ab1SHou Tao const struct bench bench_htab_mem = {
343fd283ab1SHou Tao 	.name = "htab-mem",
344fd283ab1SHou Tao 	.argp = &bench_htab_mem_argp,
345fd283ab1SHou Tao 	.validate = htab_mem_validate,
346fd283ab1SHou Tao 	.setup = htab_mem_setup,
347fd283ab1SHou Tao 	.producer_thread = htab_mem_producer,
348fd283ab1SHou Tao 	.measure = htab_mem_measure,
349fd283ab1SHou Tao 	.report_progress = htab_mem_report_progress,
350fd283ab1SHou Tao 	.report_final = htab_mem_report_final,
351fd283ab1SHou Tao };
352