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