xref: /linux/tools/testing/selftests/bpf/bench.c (revision 208c4391204d25d9178fbc87f216daffad00cd15)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #define _GNU_SOURCE
4 #include <argp.h>
5 #include <linux/compiler.h>
6 #include <sys/time.h>
7 #include <sched.h>
8 #include <fcntl.h>
9 #include <pthread.h>
10 #include <sys/sysinfo.h>
11 #include <signal.h>
12 #include "bench.h"
13 #include "testing_helpers.h"
14 
15 struct env env = {
16 	.warmup_sec = 1,
17 	.duration_sec = 5,
18 	.affinity = false,
19 	.quiet = false,
20 	.consumer_cnt = 0,
21 	.producer_cnt = 1,
22 };
23 
24 static int libbpf_print_fn(enum libbpf_print_level level,
25 		    const char *format, va_list args)
26 {
27 	if (level == LIBBPF_DEBUG && !env.verbose)
28 		return 0;
29 	return vfprintf(stderr, format, args);
30 }
31 
32 void setup_libbpf(void)
33 {
34 	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
35 	libbpf_set_print(libbpf_print_fn);
36 }
37 
38 void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns)
39 {
40 	long total = res->false_hits  + res->hits + res->drops;
41 
42 	printf("Iter %3d (%7.3lfus): ",
43 	       iter, (delta_ns - 1000000000) / 1000.0);
44 
45 	printf("%ld false hits of %ld total operations. Percentage = %2.2f %%\n",
46 	       res->false_hits, total, ((float)res->false_hits / total) * 100);
47 }
48 
49 void false_hits_report_final(struct bench_res res[], int res_cnt)
50 {
51 	long total_hits = 0, total_drops = 0, total_false_hits = 0, total_ops = 0;
52 	int i;
53 
54 	for (i = 0; i < res_cnt; i++) {
55 		total_hits += res[i].hits;
56 		total_false_hits += res[i].false_hits;
57 		total_drops += res[i].drops;
58 	}
59 	total_ops = total_hits + total_false_hits + total_drops;
60 
61 	printf("Summary: %ld false hits of %ld total operations. ",
62 	       total_false_hits, total_ops);
63 	printf("Percentage =  %2.2f %%\n",
64 	       ((float)total_false_hits / total_ops) * 100);
65 }
66 
67 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns)
68 {
69 	double hits_per_sec, drops_per_sec;
70 	double hits_per_prod;
71 
72 	hits_per_sec = res->hits / 1000000.0 / (delta_ns / 1000000000.0);
73 	hits_per_prod = hits_per_sec / env.producer_cnt;
74 	drops_per_sec = res->drops / 1000000.0 / (delta_ns / 1000000000.0);
75 
76 	printf("Iter %3d (%7.3lfus): ",
77 	       iter, (delta_ns - 1000000000) / 1000.0);
78 
79 	printf("hits %8.3lfM/s (%7.3lfM/prod), drops %8.3lfM/s, total operations %8.3lfM/s\n",
80 	       hits_per_sec, hits_per_prod, drops_per_sec, hits_per_sec + drops_per_sec);
81 }
82 
83 void
84 grace_period_latency_basic_stats(struct bench_res res[], int res_cnt, struct basic_stats *gp_stat)
85 {
86 	int i;
87 
88 	memset(gp_stat, 0, sizeof(struct basic_stats));
89 
90 	for (i = 0; i < res_cnt; i++)
91 		gp_stat->mean += res[i].gp_ns / 1000.0 / (double)res[i].gp_ct / (0.0 + res_cnt);
92 
93 #define IT_MEAN_DIFF (res[i].gp_ns / 1000.0 / (double)res[i].gp_ct - gp_stat->mean)
94 	if (res_cnt > 1) {
95 		for (i = 0; i < res_cnt; i++)
96 			gp_stat->stddev += (IT_MEAN_DIFF * IT_MEAN_DIFF) / (res_cnt - 1.0);
97 	}
98 	gp_stat->stddev = sqrt(gp_stat->stddev);
99 #undef IT_MEAN_DIFF
100 }
101 
102 void
103 grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt, struct basic_stats *gp_stat)
104 {
105 	int i;
106 
107 	memset(gp_stat, 0, sizeof(struct basic_stats));
108 	for (i = 0; i < res_cnt; i++)
109 		gp_stat->mean += res[i].stime / (double)res[i].gp_ct / (0.0 + res_cnt);
110 
111 #define IT_MEAN_DIFF (res[i].stime / (double)res[i].gp_ct - gp_stat->mean)
112 	if (res_cnt > 1) {
113 		for (i = 0; i < res_cnt; i++)
114 			gp_stat->stddev += (IT_MEAN_DIFF * IT_MEAN_DIFF) / (res_cnt - 1.0);
115 	}
116 	gp_stat->stddev = sqrt(gp_stat->stddev);
117 #undef IT_MEAN_DIFF
118 }
119 
120 void hits_drops_report_final(struct bench_res res[], int res_cnt)
121 {
122 	int i;
123 	double hits_mean = 0.0, drops_mean = 0.0, total_ops_mean = 0.0;
124 	double hits_stddev = 0.0, drops_stddev = 0.0, total_ops_stddev = 0.0;
125 	double total_ops;
126 
127 	for (i = 0; i < res_cnt; i++) {
128 		hits_mean += res[i].hits / 1000000.0 / (0.0 + res_cnt);
129 		drops_mean += res[i].drops / 1000000.0 / (0.0 + res_cnt);
130 	}
131 	total_ops_mean = hits_mean + drops_mean;
132 
133 	if (res_cnt > 1)  {
134 		for (i = 0; i < res_cnt; i++) {
135 			hits_stddev += (hits_mean - res[i].hits / 1000000.0) *
136 				       (hits_mean - res[i].hits / 1000000.0) /
137 				       (res_cnt - 1.0);
138 			drops_stddev += (drops_mean - res[i].drops / 1000000.0) *
139 					(drops_mean - res[i].drops / 1000000.0) /
140 					(res_cnt - 1.0);
141 			total_ops = res[i].hits + res[i].drops;
142 			total_ops_stddev += (total_ops_mean - total_ops / 1000000.0) *
143 					(total_ops_mean - total_ops / 1000000.0) /
144 					(res_cnt - 1.0);
145 		}
146 		hits_stddev = sqrt(hits_stddev);
147 		drops_stddev = sqrt(drops_stddev);
148 		total_ops_stddev = sqrt(total_ops_stddev);
149 	}
150 	printf("Summary: hits %8.3lf \u00B1 %5.3lfM/s (%7.3lfM/prod), ",
151 	       hits_mean, hits_stddev, hits_mean / env.producer_cnt);
152 	printf("drops %8.3lf \u00B1 %5.3lfM/s, ",
153 	       drops_mean, drops_stddev);
154 	printf("total operations %8.3lf \u00B1 %5.3lfM/s\n",
155 	       total_ops_mean, total_ops_stddev);
156 }
157 
158 void ops_report_progress(int iter, struct bench_res *res, long delta_ns)
159 {
160 	double hits_per_sec, hits_per_prod;
161 
162 	hits_per_sec = res->hits / 1000000.0 / (delta_ns / 1000000000.0);
163 	hits_per_prod = hits_per_sec / env.producer_cnt;
164 
165 	printf("Iter %3d (%7.3lfus): ", iter, (delta_ns - 1000000000) / 1000.0);
166 
167 	printf("hits %8.3lfM/s (%7.3lfM/prod)\n", hits_per_sec, hits_per_prod);
168 }
169 
170 void ops_report_final(struct bench_res res[], int res_cnt)
171 {
172 	double hits_mean = 0.0, hits_stddev = 0.0;
173 	int i;
174 
175 	for (i = 0; i < res_cnt; i++)
176 		hits_mean += res[i].hits / 1000000.0 / (0.0 + res_cnt);
177 
178 	if (res_cnt > 1)  {
179 		for (i = 0; i < res_cnt; i++)
180 			hits_stddev += (hits_mean - res[i].hits / 1000000.0) *
181 				       (hits_mean - res[i].hits / 1000000.0) /
182 				       (res_cnt - 1.0);
183 
184 		hits_stddev = sqrt(hits_stddev);
185 	}
186 	printf("Summary: throughput %8.3lf \u00B1 %5.3lf M ops/s (%7.3lfM ops/prod), ",
187 	       hits_mean, hits_stddev, hits_mean / env.producer_cnt);
188 	printf("latency %8.3lf ns/op\n", 1000.0 / hits_mean * env.producer_cnt);
189 }
190 
191 void local_storage_report_progress(int iter, struct bench_res *res,
192 				   long delta_ns)
193 {
194 	double important_hits_per_sec, hits_per_sec;
195 	double delta_sec = delta_ns / 1000000000.0;
196 
197 	hits_per_sec = res->hits / 1000000.0 / delta_sec;
198 	important_hits_per_sec = res->important_hits / 1000000.0 / delta_sec;
199 
200 	printf("Iter %3d (%7.3lfus): ", iter, (delta_ns - 1000000000) / 1000.0);
201 
202 	printf("hits %8.3lfM/s ", hits_per_sec);
203 	printf("important_hits %8.3lfM/s\n", important_hits_per_sec);
204 }
205 
206 void local_storage_report_final(struct bench_res res[], int res_cnt)
207 {
208 	double important_hits_mean = 0.0, important_hits_stddev = 0.0;
209 	double hits_mean = 0.0, hits_stddev = 0.0;
210 	int i;
211 
212 	for (i = 0; i < res_cnt; i++) {
213 		hits_mean += res[i].hits / 1000000.0 / (0.0 + res_cnt);
214 		important_hits_mean += res[i].important_hits / 1000000.0 / (0.0 + res_cnt);
215 	}
216 
217 	if (res_cnt > 1)  {
218 		for (i = 0; i < res_cnt; i++) {
219 			hits_stddev += (hits_mean - res[i].hits / 1000000.0) *
220 				       (hits_mean - res[i].hits / 1000000.0) /
221 				       (res_cnt - 1.0);
222 			important_hits_stddev +=
223 				       (important_hits_mean - res[i].important_hits / 1000000.0) *
224 				       (important_hits_mean - res[i].important_hits / 1000000.0) /
225 				       (res_cnt - 1.0);
226 		}
227 
228 		hits_stddev = sqrt(hits_stddev);
229 		important_hits_stddev = sqrt(important_hits_stddev);
230 	}
231 	printf("Summary: hits throughput %8.3lf \u00B1 %5.3lf M ops/s, ",
232 	       hits_mean, hits_stddev);
233 	printf("hits latency %8.3lf ns/op, ", 1000.0 / hits_mean);
234 	printf("important_hits throughput %8.3lf \u00B1 %5.3lf M ops/s\n",
235 	       important_hits_mean, important_hits_stddev);
236 }
237 
238 const char *argp_program_version = "benchmark";
239 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
240 const char argp_program_doc[] =
241 "benchmark    Generic benchmarking framework.\n"
242 "\n"
243 "This tool runs benchmarks.\n"
244 "\n"
245 "USAGE: benchmark <bench-name>\n"
246 "\n"
247 "EXAMPLES:\n"
248 "    # run 'count-local' benchmark with 1 producer and 1 consumer\n"
249 "    benchmark count-local\n"
250 "    # run 'count-local' with 16 producer and 8 consumer thread, pinned to CPUs\n"
251 "    benchmark -p16 -c8 -a count-local\n";
252 
253 enum {
254 	ARG_PROD_AFFINITY_SET = 1000,
255 	ARG_CONS_AFFINITY_SET = 1001,
256 };
257 
258 static const struct argp_option opts[] = {
259 	{ "list", 'l', NULL, 0, "List available benchmarks"},
260 	{ "duration", 'd', "SEC", 0, "Duration of benchmark, seconds"},
261 	{ "warmup", 'w', "SEC", 0, "Warm-up period, seconds"},
262 	{ "producers", 'p', "NUM", 0, "Number of producer threads"},
263 	{ "consumers", 'c', "NUM", 0, "Number of consumer threads"},
264 	{ "verbose", 'v', NULL, 0, "Verbose debug output"},
265 	{ "affinity", 'a', NULL, 0, "Set consumer/producer thread affinity"},
266 	{ "quiet", 'q', NULL, 0, "Be more quiet"},
267 	{ "prod-affinity", ARG_PROD_AFFINITY_SET, "CPUSET", 0,
268 	  "Set of CPUs for producer threads; implies --affinity"},
269 	{ "cons-affinity", ARG_CONS_AFFINITY_SET, "CPUSET", 0,
270 	  "Set of CPUs for consumer threads; implies --affinity"},
271 	{},
272 };
273 
274 extern struct argp bench_ringbufs_argp;
275 extern struct argp bench_bloom_map_argp;
276 extern struct argp bench_bpf_loop_argp;
277 extern struct argp bench_local_storage_argp;
278 extern struct argp bench_local_storage_rcu_tasks_trace_argp;
279 extern struct argp bench_strncmp_argp;
280 extern struct argp bench_hashmap_lookup_argp;
281 extern struct argp bench_local_storage_create_argp;
282 extern struct argp bench_htab_mem_argp;
283 extern struct argp bench_trigger_batch_argp;
284 
285 static const struct argp_child bench_parsers[] = {
286 	{ &bench_ringbufs_argp, 0, "Ring buffers benchmark", 0 },
287 	{ &bench_bloom_map_argp, 0, "Bloom filter map benchmark", 0 },
288 	{ &bench_bpf_loop_argp, 0, "bpf_loop helper benchmark", 0 },
289 	{ &bench_local_storage_argp, 0, "local_storage benchmark", 0 },
290 	{ &bench_strncmp_argp, 0, "bpf_strncmp helper benchmark", 0 },
291 	{ &bench_local_storage_rcu_tasks_trace_argp, 0,
292 		"local_storage RCU Tasks Trace slowdown benchmark", 0 },
293 	{ &bench_hashmap_lookup_argp, 0, "Hashmap lookup benchmark", 0 },
294 	{ &bench_local_storage_create_argp, 0, "local-storage-create benchmark", 0 },
295 	{ &bench_htab_mem_argp, 0, "hash map memory benchmark", 0 },
296 	{ &bench_trigger_batch_argp, 0, "BPF triggering benchmark", 0 },
297 	{},
298 };
299 
300 /* Make pos_args global, so that we can run argp_parse twice, if necessary */
301 static int pos_args;
302 
303 static error_t parse_arg(int key, char *arg, struct argp_state *state)
304 {
305 	switch (key) {
306 	case 'v':
307 		env.verbose = true;
308 		break;
309 	case 'l':
310 		env.list = true;
311 		break;
312 	case 'd':
313 		env.duration_sec = strtol(arg, NULL, 10);
314 		if (env.duration_sec <= 0) {
315 			fprintf(stderr, "Invalid duration: %s\n", arg);
316 			argp_usage(state);
317 		}
318 		break;
319 	case 'w':
320 		env.warmup_sec = strtol(arg, NULL, 10);
321 		if (env.warmup_sec <= 0) {
322 			fprintf(stderr, "Invalid warm-up duration: %s\n", arg);
323 			argp_usage(state);
324 		}
325 		break;
326 	case 'p':
327 		env.producer_cnt = strtol(arg, NULL, 10);
328 		if (env.producer_cnt < 0) {
329 			fprintf(stderr, "Invalid producer count: %s\n", arg);
330 			argp_usage(state);
331 		}
332 		break;
333 	case 'c':
334 		env.consumer_cnt = strtol(arg, NULL, 10);
335 		if (env.consumer_cnt < 0) {
336 			fprintf(stderr, "Invalid consumer count: %s\n", arg);
337 			argp_usage(state);
338 		}
339 		break;
340 	case 'a':
341 		env.affinity = true;
342 		break;
343 	case 'q':
344 		env.quiet = true;
345 		break;
346 	case ARG_PROD_AFFINITY_SET:
347 		env.affinity = true;
348 		if (parse_num_list(arg, &env.prod_cpus.cpus,
349 				   &env.prod_cpus.cpus_len)) {
350 			fprintf(stderr, "Invalid format of CPU set for producers.");
351 			argp_usage(state);
352 		}
353 		break;
354 	case ARG_CONS_AFFINITY_SET:
355 		env.affinity = true;
356 		if (parse_num_list(arg, &env.cons_cpus.cpus,
357 				   &env.cons_cpus.cpus_len)) {
358 			fprintf(stderr, "Invalid format of CPU set for consumers.");
359 			argp_usage(state);
360 		}
361 		break;
362 	case ARGP_KEY_ARG:
363 		if (pos_args++) {
364 			fprintf(stderr,
365 				"Unrecognized positional argument: %s\n", arg);
366 			argp_usage(state);
367 		}
368 		env.bench_name = strdup(arg);
369 		break;
370 	default:
371 		return ARGP_ERR_UNKNOWN;
372 	}
373 	return 0;
374 }
375 
376 static void parse_cmdline_args_init(int argc, char **argv)
377 {
378 	static const struct argp argp = {
379 		.options = opts,
380 		.parser = parse_arg,
381 		.doc = argp_program_doc,
382 		.children = bench_parsers,
383 	};
384 	if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
385 		exit(1);
386 }
387 
388 static void parse_cmdline_args_final(int argc, char **argv)
389 {
390 	struct argp_child bench_parsers[2] = {};
391 	const struct argp argp = {
392 		.options = opts,
393 		.parser = parse_arg,
394 		.doc = argp_program_doc,
395 		.children = bench_parsers,
396 	};
397 
398 	/* Parse arguments the second time with the correct set of parsers */
399 	if (bench->argp) {
400 		bench_parsers[0].argp = bench->argp;
401 		bench_parsers[0].header = bench->name;
402 		pos_args = 0;
403 		if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
404 			exit(1);
405 	}
406 }
407 
408 static void collect_measurements(long delta_ns);
409 
410 static __u64 last_time_ns;
411 static void sigalarm_handler(int signo)
412 {
413 	long new_time_ns = get_time_ns();
414 	long delta_ns = new_time_ns - last_time_ns;
415 
416 	collect_measurements(delta_ns);
417 
418 	last_time_ns = new_time_ns;
419 }
420 
421 /* set up periodic 1-second timer */
422 static void setup_timer()
423 {
424 	static struct sigaction sigalarm_action = {
425 		.sa_handler = sigalarm_handler,
426 	};
427 	struct itimerval timer_settings = {};
428 	int err;
429 
430 	last_time_ns = get_time_ns();
431 	err = sigaction(SIGALRM, &sigalarm_action, NULL);
432 	if (err < 0) {
433 		fprintf(stderr, "failed to install SIGALRM handler: %d\n", -errno);
434 		exit(1);
435 	}
436 	timer_settings.it_interval.tv_sec = 1;
437 	timer_settings.it_value.tv_sec = 1;
438 	err = setitimer(ITIMER_REAL, &timer_settings, NULL);
439 	if (err < 0) {
440 		fprintf(stderr, "failed to arm interval timer: %d\n", -errno);
441 		exit(1);
442 	}
443 }
444 
445 static void set_thread_affinity(pthread_t thread, int cpu)
446 {
447 	cpu_set_t cpuset;
448 	int err;
449 
450 	CPU_ZERO(&cpuset);
451 	CPU_SET(cpu, &cpuset);
452 	err = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
453 	if (err) {
454 		fprintf(stderr, "setting affinity to CPU #%d failed: %d\n",
455 			cpu, -err);
456 		exit(1);
457 	}
458 }
459 
460 static int next_cpu(struct cpu_set *cpu_set)
461 {
462 	if (cpu_set->cpus) {
463 		int i;
464 
465 		/* find next available CPU */
466 		for (i = cpu_set->next_cpu; i < cpu_set->cpus_len; i++) {
467 			if (cpu_set->cpus[i]) {
468 				cpu_set->next_cpu = i + 1;
469 				return i;
470 			}
471 		}
472 		fprintf(stderr, "Not enough CPUs specified, need CPU #%d or higher.\n", i);
473 		exit(1);
474 	}
475 
476 	return cpu_set->next_cpu++ % env.nr_cpus;
477 }
478 
479 static struct bench_state {
480 	int res_cnt;
481 	struct bench_res *results;
482 	pthread_t *consumers;
483 	pthread_t *producers;
484 } state;
485 
486 const struct bench *bench = NULL;
487 
488 extern const struct bench bench_count_global;
489 extern const struct bench bench_count_local;
490 extern const struct bench bench_rename_base;
491 extern const struct bench bench_rename_kprobe;
492 extern const struct bench bench_rename_kretprobe;
493 extern const struct bench bench_rename_rawtp;
494 extern const struct bench bench_rename_fentry;
495 extern const struct bench bench_rename_fexit;
496 
497 /* pure counting benchmarks to establish theoretical lmits */
498 extern const struct bench bench_trig_usermode_count;
499 extern const struct bench bench_trig_syscall_count;
500 extern const struct bench bench_trig_kernel_count;
501 
502 /* batched, staying mostly in-kernel benchmarks */
503 extern const struct bench bench_trig_kprobe;
504 extern const struct bench bench_trig_kretprobe;
505 extern const struct bench bench_trig_kprobe_multi;
506 extern const struct bench bench_trig_kretprobe_multi;
507 extern const struct bench bench_trig_fentry;
508 extern const struct bench bench_trig_fexit;
509 
510 /* uprobe/uretprobe benchmarks */
511 extern const struct bench bench_trig_uprobe_nop;
512 extern const struct bench bench_trig_uretprobe_nop;
513 extern const struct bench bench_trig_uprobe_push;
514 extern const struct bench bench_trig_uretprobe_push;
515 extern const struct bench bench_trig_uprobe_ret;
516 extern const struct bench bench_trig_uretprobe_ret;
517 
518 extern const struct bench bench_rb_libbpf;
519 extern const struct bench bench_rb_custom;
520 extern const struct bench bench_pb_libbpf;
521 extern const struct bench bench_pb_custom;
522 extern const struct bench bench_bloom_lookup;
523 extern const struct bench bench_bloom_update;
524 extern const struct bench bench_bloom_false_positive;
525 extern const struct bench bench_hashmap_without_bloom;
526 extern const struct bench bench_hashmap_with_bloom;
527 extern const struct bench bench_bpf_loop;
528 extern const struct bench bench_strncmp_no_helper;
529 extern const struct bench bench_strncmp_helper;
530 extern const struct bench bench_bpf_hashmap_full_update;
531 extern const struct bench bench_local_storage_cache_seq_get;
532 extern const struct bench bench_local_storage_cache_interleaved_get;
533 extern const struct bench bench_local_storage_cache_hashmap_control;
534 extern const struct bench bench_local_storage_tasks_trace;
535 extern const struct bench bench_bpf_hashmap_lookup;
536 extern const struct bench bench_local_storage_create;
537 extern const struct bench bench_htab_mem;
538 
539 static const struct bench *benchs[] = {
540 	&bench_count_global,
541 	&bench_count_local,
542 	&bench_rename_base,
543 	&bench_rename_kprobe,
544 	&bench_rename_kretprobe,
545 	&bench_rename_rawtp,
546 	&bench_rename_fentry,
547 	&bench_rename_fexit,
548 	/* pure counting benchmarks for establishing theoretical limits */
549 	&bench_trig_usermode_count,
550 	&bench_trig_kernel_count,
551 	&bench_trig_syscall_count,
552 	/* batched, staying mostly in-kernel triggers */
553 	&bench_trig_kprobe,
554 	&bench_trig_kretprobe,
555 	&bench_trig_kprobe_multi,
556 	&bench_trig_kretprobe_multi,
557 	&bench_trig_fentry,
558 	&bench_trig_fexit,
559 	/* uprobes */
560 	&bench_trig_uprobe_nop,
561 	&bench_trig_uretprobe_nop,
562 	&bench_trig_uprobe_push,
563 	&bench_trig_uretprobe_push,
564 	&bench_trig_uprobe_ret,
565 	&bench_trig_uretprobe_ret,
566 	/* ringbuf/perfbuf benchmarks */
567 	&bench_rb_libbpf,
568 	&bench_rb_custom,
569 	&bench_pb_libbpf,
570 	&bench_pb_custom,
571 	&bench_bloom_lookup,
572 	&bench_bloom_update,
573 	&bench_bloom_false_positive,
574 	&bench_hashmap_without_bloom,
575 	&bench_hashmap_with_bloom,
576 	&bench_bpf_loop,
577 	&bench_strncmp_no_helper,
578 	&bench_strncmp_helper,
579 	&bench_bpf_hashmap_full_update,
580 	&bench_local_storage_cache_seq_get,
581 	&bench_local_storage_cache_interleaved_get,
582 	&bench_local_storage_cache_hashmap_control,
583 	&bench_local_storage_tasks_trace,
584 	&bench_bpf_hashmap_lookup,
585 	&bench_local_storage_create,
586 	&bench_htab_mem,
587 };
588 
589 static void find_benchmark(void)
590 {
591 	int i;
592 
593 	if (!env.bench_name) {
594 		fprintf(stderr, "benchmark name is not specified\n");
595 		exit(1);
596 	}
597 	for (i = 0; i < ARRAY_SIZE(benchs); i++) {
598 		if (strcmp(benchs[i]->name, env.bench_name) == 0) {
599 			bench = benchs[i];
600 			break;
601 		}
602 	}
603 	if (!bench) {
604 		fprintf(stderr, "benchmark '%s' not found\n", env.bench_name);
605 		exit(1);
606 	}
607 }
608 
609 static void setup_benchmark(void)
610 {
611 	int i, err;
612 
613 	if (!env.quiet)
614 		printf("Setting up benchmark '%s'...\n", bench->name);
615 
616 	state.producers = calloc(env.producer_cnt, sizeof(*state.producers));
617 	state.consumers = calloc(env.consumer_cnt, sizeof(*state.consumers));
618 	state.results = calloc(env.duration_sec + env.warmup_sec + 2,
619 			       sizeof(*state.results));
620 	if (!state.producers || !state.consumers || !state.results)
621 		exit(1);
622 
623 	if (bench->validate)
624 		bench->validate();
625 	if (bench->setup)
626 		bench->setup();
627 
628 	for (i = 0; i < env.consumer_cnt; i++) {
629 		if (!bench->consumer_thread) {
630 			fprintf(stderr, "benchmark doesn't support consumers!\n");
631 			exit(1);
632 		}
633 		err = pthread_create(&state.consumers[i], NULL,
634 				     bench->consumer_thread, (void *)(long)i);
635 		if (err) {
636 			fprintf(stderr, "failed to create consumer thread #%d: %d\n",
637 				i, -err);
638 			exit(1);
639 		}
640 		if (env.affinity)
641 			set_thread_affinity(state.consumers[i],
642 					    next_cpu(&env.cons_cpus));
643 	}
644 
645 	/* unless explicit producer CPU list is specified, continue after
646 	 * last consumer CPU
647 	 */
648 	if (!env.prod_cpus.cpus)
649 		env.prod_cpus.next_cpu = env.cons_cpus.next_cpu;
650 
651 	for (i = 0; i < env.producer_cnt; i++) {
652 		if (!bench->producer_thread) {
653 			fprintf(stderr, "benchmark doesn't support producers!\n");
654 			exit(1);
655 		}
656 		err = pthread_create(&state.producers[i], NULL,
657 				     bench->producer_thread, (void *)(long)i);
658 		if (err) {
659 			fprintf(stderr, "failed to create producer thread #%d: %d\n",
660 				i, -err);
661 			exit(1);
662 		}
663 		if (env.affinity)
664 			set_thread_affinity(state.producers[i],
665 					    next_cpu(&env.prod_cpus));
666 	}
667 
668 	if (!env.quiet)
669 		printf("Benchmark '%s' started.\n", bench->name);
670 }
671 
672 static pthread_mutex_t bench_done_mtx = PTHREAD_MUTEX_INITIALIZER;
673 static pthread_cond_t bench_done = PTHREAD_COND_INITIALIZER;
674 
675 static void collect_measurements(long delta_ns) {
676 	int iter = state.res_cnt++;
677 	struct bench_res *res = &state.results[iter];
678 
679 	bench->measure(res);
680 
681 	if (bench->report_progress)
682 		bench->report_progress(iter, res, delta_ns);
683 
684 	if (iter == env.duration_sec + env.warmup_sec) {
685 		pthread_mutex_lock(&bench_done_mtx);
686 		pthread_cond_signal(&bench_done);
687 		pthread_mutex_unlock(&bench_done_mtx);
688 	}
689 }
690 
691 int main(int argc, char **argv)
692 {
693 	env.nr_cpus = get_nprocs();
694 	parse_cmdline_args_init(argc, argv);
695 
696 	if (env.list) {
697 		int i;
698 
699 		printf("Available benchmarks:\n");
700 		for (i = 0; i < ARRAY_SIZE(benchs); i++) {
701 			printf("- %s\n", benchs[i]->name);
702 		}
703 		return 0;
704 	}
705 
706 	find_benchmark();
707 	parse_cmdline_args_final(argc, argv);
708 
709 	setup_benchmark();
710 
711 	setup_timer();
712 
713 	pthread_mutex_lock(&bench_done_mtx);
714 	pthread_cond_wait(&bench_done, &bench_done_mtx);
715 	pthread_mutex_unlock(&bench_done_mtx);
716 
717 	if (bench->report_final)
718 		/* skip first sample */
719 		bench->report_final(state.results + env.warmup_sec,
720 				    state.res_cnt - env.warmup_sec);
721 
722 	return 0;
723 }
724