Linux Audio

Check our new training course

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