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