1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
5 #include <linux/compiler.h>
10 #include <sys/sysinfo.h>
13 #include "testing_helpers.h"
24 static int libbpf_print_fn(enum libbpf_print_level level,
25 const char *format, va_list args)
27 if (level == LIBBPF_DEBUG && !env.verbose)
29 return vfprintf(stderr, format, args);
32 void setup_libbpf(void)
34 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
35 libbpf_set_print(libbpf_print_fn);
38 void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns)
40 long total = res->false_hits + res->hits + res->drops;
42 printf("Iter %3d (%7.3lfus): ",
43 iter, (delta_ns - 1000000000) / 1000.0);
45 printf("%ld false hits of %ld total operations. Percentage = %2.2f %%\n",
46 res->false_hits, total, ((float)res->false_hits / total) * 100);
49 void false_hits_report_final(struct bench_res res[], int res_cnt)
51 long total_hits = 0, total_drops = 0, total_false_hits = 0, total_ops = 0;
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;
59 total_ops = total_hits + total_false_hits + total_drops;
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);
67 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns)
69 double hits_per_sec, drops_per_sec;
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);
76 printf("Iter %3d (%7.3lfus): ",
77 iter, (delta_ns - 1000000000) / 1000.0);
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);
84 grace_period_latency_basic_stats(struct bench_res res[], int res_cnt, struct basic_stats *gp_stat)
88 memset(gp_stat, 0, sizeof(struct basic_stats));
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);
93 #define IT_MEAN_DIFF (res[i].gp_ns / 1000.0 / (double)res[i].gp_ct - gp_stat->mean)
95 for (i = 0; i < res_cnt; i++)
96 gp_stat->stddev += (IT_MEAN_DIFF * IT_MEAN_DIFF) / (res_cnt - 1.0);
98 gp_stat->stddev = sqrt(gp_stat->stddev);
103 grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt, struct basic_stats *gp_stat)
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);
111 #define IT_MEAN_DIFF (res[i].stime / (double)res[i].gp_ct - gp_stat->mean)
113 for (i = 0; i < res_cnt; i++)
114 gp_stat->stddev += (IT_MEAN_DIFF * IT_MEAN_DIFF) / (res_cnt - 1.0);
116 gp_stat->stddev = sqrt(gp_stat->stddev);
120 void hits_drops_report_final(struct bench_res res[], int res_cnt)
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;
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);
131 total_ops_mean = hits_mean + drops_mean;
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) /
138 drops_stddev += (drops_mean - res[i].drops / 1000000.0) *
139 (drops_mean - res[i].drops / 1000000.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) /
146 hits_stddev = sqrt(hits_stddev);
147 drops_stddev = sqrt(drops_stddev);
148 total_ops_stddev = sqrt(total_ops_stddev);
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);
158 void ops_report_progress(int iter, struct bench_res *res, long delta_ns)
160 double hits_per_sec, hits_per_prod;
162 hits_per_sec = res->hits / 1000000.0 / (delta_ns / 1000000000.0);
163 hits_per_prod = hits_per_sec / env.producer_cnt;
165 printf("Iter %3d (%7.3lfus): ", iter, (delta_ns - 1000000000) / 1000.0);
167 printf("hits %8.3lfM/s (%7.3lfM/prod)\n", hits_per_sec, hits_per_prod);
170 void ops_report_final(struct bench_res res[], int res_cnt)
172 double hits_mean = 0.0, hits_stddev = 0.0;
175 for (i = 0; i < res_cnt; i++)
176 hits_mean += res[i].hits / 1000000.0 / (0.0 + res_cnt);
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) /
184 hits_stddev = sqrt(hits_stddev);
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);
191 void local_storage_report_progress(int iter, struct bench_res *res,
194 double important_hits_per_sec, hits_per_sec;
195 double delta_sec = delta_ns / 1000000000.0;
197 hits_per_sec = res->hits / 1000000.0 / delta_sec;
198 important_hits_per_sec = res->important_hits / 1000000.0 / delta_sec;
200 printf("Iter %3d (%7.3lfus): ", iter, (delta_ns - 1000000000) / 1000.0);
202 printf("hits %8.3lfM/s ", hits_per_sec);
203 printf("important_hits %8.3lfM/s\n", important_hits_per_sec);
206 void local_storage_report_final(struct bench_res res[], int res_cnt)
208 double important_hits_mean = 0.0, important_hits_stddev = 0.0;
209 double hits_mean = 0.0, hits_stddev = 0.0;
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);
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) /
222 important_hits_stddev +=
223 (important_hits_mean - res[i].important_hits / 1000000.0) *
224 (important_hits_mean - res[i].important_hits / 1000000.0) /
228 hits_stddev = sqrt(hits_stddev);
229 important_hits_stddev = sqrt(important_hits_stddev);
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);
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"
243 "This tool runs benchmarks.\n"
245 "USAGE: benchmark <bench-name>\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";
254 ARG_PROD_AFFINITY_SET = 1000,
255 ARG_CONS_AFFINITY_SET = 1001,
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"},
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 extern struct argp bench_crypto_argp;
286 static const struct argp_child bench_parsers[] = {
287 { &bench_ringbufs_argp, 0, "Ring buffers benchmark", 0 },
288 { &bench_bloom_map_argp, 0, "Bloom filter map benchmark", 0 },
289 { &bench_bpf_loop_argp, 0, "bpf_loop helper benchmark", 0 },
290 { &bench_local_storage_argp, 0, "local_storage benchmark", 0 },
291 { &bench_strncmp_argp, 0, "bpf_strncmp helper benchmark", 0 },
292 { &bench_local_storage_rcu_tasks_trace_argp, 0,
293 "local_storage RCU Tasks Trace slowdown benchmark", 0 },
294 { &bench_hashmap_lookup_argp, 0, "Hashmap lookup benchmark", 0 },
295 { &bench_local_storage_create_argp, 0, "local-storage-create benchmark", 0 },
296 { &bench_htab_mem_argp, 0, "hash map memory benchmark", 0 },
297 { &bench_trigger_batch_argp, 0, "BPF triggering benchmark", 0 },
298 { &bench_crypto_argp, 0, "bpf crypto benchmark", 0 },
302 /* Make pos_args global, so that we can run argp_parse twice, if necessary */
305 static error_t parse_arg(int key, char *arg, struct argp_state *state)
315 env.duration_sec = strtol(arg, NULL, 10);
316 if (env.duration_sec <= 0) {
317 fprintf(stderr, "Invalid duration: %s\n", arg);
322 env.warmup_sec = strtol(arg, NULL, 10);
323 if (env.warmup_sec <= 0) {
324 fprintf(stderr, "Invalid warm-up duration: %s\n", arg);
329 env.producer_cnt = strtol(arg, NULL, 10);
330 if (env.producer_cnt < 0) {
331 fprintf(stderr, "Invalid producer count: %s\n", arg);
336 env.consumer_cnt = strtol(arg, NULL, 10);
337 if (env.consumer_cnt < 0) {
338 fprintf(stderr, "Invalid consumer count: %s\n", arg);
348 case ARG_PROD_AFFINITY_SET:
350 if (parse_num_list(arg, &env.prod_cpus.cpus,
351 &env.prod_cpus.cpus_len)) {
352 fprintf(stderr, "Invalid format of CPU set for producers.");
356 case ARG_CONS_AFFINITY_SET:
358 if (parse_num_list(arg, &env.cons_cpus.cpus,
359 &env.cons_cpus.cpus_len)) {
360 fprintf(stderr, "Invalid format of CPU set for consumers.");
367 "Unrecognized positional argument: %s\n", arg);
370 env.bench_name = strdup(arg);
373 return ARGP_ERR_UNKNOWN;
378 static void parse_cmdline_args_init(int argc, char **argv)
380 static const struct argp argp = {
383 .doc = argp_program_doc,
384 .children = bench_parsers,
386 if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
390 static void parse_cmdline_args_final(int argc, char **argv)
392 struct argp_child bench_parsers[2] = {};
393 const struct argp argp = {
396 .doc = argp_program_doc,
397 .children = bench_parsers,
400 /* Parse arguments the second time with the correct set of parsers */
402 bench_parsers[0].argp = bench->argp;
403 bench_parsers[0].header = bench->name;
405 if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
410 static void collect_measurements(long delta_ns);
412 static __u64 last_time_ns;
413 static void sigalarm_handler(int signo)
415 long new_time_ns = get_time_ns();
416 long delta_ns = new_time_ns - last_time_ns;
418 collect_measurements(delta_ns);
420 last_time_ns = new_time_ns;
423 /* set up periodic 1-second timer */
424 static void setup_timer()
426 static struct sigaction sigalarm_action = {
427 .sa_handler = sigalarm_handler,
429 struct itimerval timer_settings = {};
432 last_time_ns = get_time_ns();
433 err = sigaction(SIGALRM, &sigalarm_action, NULL);
435 fprintf(stderr, "failed to install SIGALRM handler: %d\n", -errno);
438 timer_settings.it_interval.tv_sec = 1;
439 timer_settings.it_value.tv_sec = 1;
440 err = setitimer(ITIMER_REAL, &timer_settings, NULL);
442 fprintf(stderr, "failed to arm interval timer: %d\n", -errno);
447 static void set_thread_affinity(pthread_t thread, int cpu)
453 CPU_SET(cpu, &cpuset);
454 err = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
456 fprintf(stderr, "setting affinity to CPU #%d failed: %d\n",
462 static int next_cpu(struct cpu_set *cpu_set)
467 /* find next available CPU */
468 for (i = cpu_set->next_cpu; i < cpu_set->cpus_len; i++) {
469 if (cpu_set->cpus[i]) {
470 cpu_set->next_cpu = i + 1;
474 fprintf(stderr, "Not enough CPUs specified, need CPU #%d or higher.\n", i);
478 return cpu_set->next_cpu++ % env.nr_cpus;
481 static struct bench_state {
483 struct bench_res *results;
484 pthread_t *consumers;
485 pthread_t *producers;
488 const struct bench *bench = NULL;
490 extern const struct bench bench_count_global;
491 extern const struct bench bench_count_local;
492 extern const struct bench bench_rename_base;
493 extern const struct bench bench_rename_kprobe;
494 extern const struct bench bench_rename_kretprobe;
495 extern const struct bench bench_rename_rawtp;
496 extern const struct bench bench_rename_fentry;
497 extern const struct bench bench_rename_fexit;
499 /* pure counting benchmarks to establish theoretical lmits */
500 extern const struct bench bench_trig_usermode_count;
501 extern const struct bench bench_trig_syscall_count;
502 extern const struct bench bench_trig_kernel_count;
504 /* batched, staying mostly in-kernel benchmarks */
505 extern const struct bench bench_trig_kprobe;
506 extern const struct bench bench_trig_kretprobe;
507 extern const struct bench bench_trig_kprobe_multi;
508 extern const struct bench bench_trig_kretprobe_multi;
509 extern const struct bench bench_trig_fentry;
510 extern const struct bench bench_trig_fexit;
511 extern const struct bench bench_trig_fmodret;
512 extern const struct bench bench_trig_tp;
513 extern const struct bench bench_trig_rawtp;
515 /* uprobe/uretprobe benchmarks */
516 extern const struct bench bench_trig_uprobe_nop;
517 extern const struct bench bench_trig_uretprobe_nop;
518 extern const struct bench bench_trig_uprobe_push;
519 extern const struct bench bench_trig_uretprobe_push;
520 extern const struct bench bench_trig_uprobe_ret;
521 extern const struct bench bench_trig_uretprobe_ret;
523 extern const struct bench bench_rb_libbpf;
524 extern const struct bench bench_rb_custom;
525 extern const struct bench bench_pb_libbpf;
526 extern const struct bench bench_pb_custom;
527 extern const struct bench bench_bloom_lookup;
528 extern const struct bench bench_bloom_update;
529 extern const struct bench bench_bloom_false_positive;
530 extern const struct bench bench_hashmap_without_bloom;
531 extern const struct bench bench_hashmap_with_bloom;
532 extern const struct bench bench_bpf_loop;
533 extern const struct bench bench_strncmp_no_helper;
534 extern const struct bench bench_strncmp_helper;
535 extern const struct bench bench_bpf_hashmap_full_update;
536 extern const struct bench bench_local_storage_cache_seq_get;
537 extern const struct bench bench_local_storage_cache_interleaved_get;
538 extern const struct bench bench_local_storage_cache_hashmap_control;
539 extern const struct bench bench_local_storage_tasks_trace;
540 extern const struct bench bench_bpf_hashmap_lookup;
541 extern const struct bench bench_local_storage_create;
542 extern const struct bench bench_htab_mem;
543 extern const struct bench bench_crypto_encrypt;
544 extern const struct bench bench_crypto_decrypt;
546 static const struct bench *benchs[] = {
550 &bench_rename_kprobe,
551 &bench_rename_kretprobe,
553 &bench_rename_fentry,
555 /* pure counting benchmarks for establishing theoretical limits */
556 &bench_trig_usermode_count,
557 &bench_trig_kernel_count,
558 &bench_trig_syscall_count,
559 /* batched, staying mostly in-kernel triggers */
561 &bench_trig_kretprobe,
562 &bench_trig_kprobe_multi,
563 &bench_trig_kretprobe_multi,
570 &bench_trig_uprobe_nop,
571 &bench_trig_uretprobe_nop,
572 &bench_trig_uprobe_push,
573 &bench_trig_uretprobe_push,
574 &bench_trig_uprobe_ret,
575 &bench_trig_uretprobe_ret,
576 /* ringbuf/perfbuf benchmarks */
583 &bench_bloom_false_positive,
584 &bench_hashmap_without_bloom,
585 &bench_hashmap_with_bloom,
587 &bench_strncmp_no_helper,
588 &bench_strncmp_helper,
589 &bench_bpf_hashmap_full_update,
590 &bench_local_storage_cache_seq_get,
591 &bench_local_storage_cache_interleaved_get,
592 &bench_local_storage_cache_hashmap_control,
593 &bench_local_storage_tasks_trace,
594 &bench_bpf_hashmap_lookup,
595 &bench_local_storage_create,
597 &bench_crypto_encrypt,
598 &bench_crypto_decrypt,
601 static void find_benchmark(void)
605 if (!env.bench_name) {
606 fprintf(stderr, "benchmark name is not specified\n");
609 for (i = 0; i < ARRAY_SIZE(benchs); i++) {
610 if (strcmp(benchs[i]->name, env.bench_name) == 0) {
616 fprintf(stderr, "benchmark '%s' not found\n", env.bench_name);
621 static void setup_benchmark(void)
626 printf("Setting up benchmark '%s'...\n", bench->name);
628 state.producers = calloc(env.producer_cnt, sizeof(*state.producers));
629 state.consumers = calloc(env.consumer_cnt, sizeof(*state.consumers));
630 state.results = calloc(env.duration_sec + env.warmup_sec + 2,
631 sizeof(*state.results));
632 if (!state.producers || !state.consumers || !state.results)
640 for (i = 0; i < env.consumer_cnt; i++) {
641 if (!bench->consumer_thread) {
642 fprintf(stderr, "benchmark doesn't support consumers!\n");
645 err = pthread_create(&state.consumers[i], NULL,
646 bench->consumer_thread, (void *)(long)i);
648 fprintf(stderr, "failed to create consumer thread #%d: %d\n",
653 set_thread_affinity(state.consumers[i],
654 next_cpu(&env.cons_cpus));
657 /* unless explicit producer CPU list is specified, continue after
660 if (!env.prod_cpus.cpus)
661 env.prod_cpus.next_cpu = env.cons_cpus.next_cpu;
663 for (i = 0; i < env.producer_cnt; i++) {
664 if (!bench->producer_thread) {
665 fprintf(stderr, "benchmark doesn't support producers!\n");
668 err = pthread_create(&state.producers[i], NULL,
669 bench->producer_thread, (void *)(long)i);
671 fprintf(stderr, "failed to create producer thread #%d: %d\n",
676 set_thread_affinity(state.producers[i],
677 next_cpu(&env.prod_cpus));
681 printf("Benchmark '%s' started.\n", bench->name);
684 static pthread_mutex_t bench_done_mtx = PTHREAD_MUTEX_INITIALIZER;
685 static pthread_cond_t bench_done = PTHREAD_COND_INITIALIZER;
687 static void collect_measurements(long delta_ns) {
688 int iter = state.res_cnt++;
689 struct bench_res *res = &state.results[iter];
693 if (bench->report_progress)
694 bench->report_progress(iter, res, delta_ns);
696 if (iter == env.duration_sec + env.warmup_sec) {
697 pthread_mutex_lock(&bench_done_mtx);
698 pthread_cond_signal(&bench_done);
699 pthread_mutex_unlock(&bench_done_mtx);
703 int main(int argc, char **argv)
705 env.nr_cpus = get_nprocs();
706 parse_cmdline_args_init(argc, argv);
711 printf("Available benchmarks:\n");
712 for (i = 0; i < ARRAY_SIZE(benchs); i++) {
713 printf("- %s\n", benchs[i]->name);
719 parse_cmdline_args_final(argc, argv);
725 pthread_mutex_lock(&bench_done_mtx);
726 pthread_cond_wait(&bench_done, &bench_done_mtx);
727 pthread_mutex_unlock(&bench_done_mtx);
729 if (bench->report_final)
730 /* skip first sample */
731 bench->report_final(state.results + env.warmup_sec,
732 state.res_cnt - env.warmup_sec);