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 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 33 void setup_libbpf(void) 34 { 35 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 36 libbpf_set_print(libbpf_print_fn); 37 } 38 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 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 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 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 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 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 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 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 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 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 extern struct argp bench_trigger_batch_argp; 285 extern struct argp bench_crypto_argp; 286 extern struct argp bench_sockmap_argp; 287 extern struct argp bench_lpm_trie_map_argp; 288 289 static const struct argp_child bench_parsers[] = { 290 { &bench_ringbufs_argp, 0, "Ring buffers benchmark", 0 }, 291 { &bench_bloom_map_argp, 0, "Bloom filter map benchmark", 0 }, 292 { &bench_bpf_loop_argp, 0, "bpf_loop helper benchmark", 0 }, 293 { &bench_local_storage_argp, 0, "local_storage benchmark", 0 }, 294 { &bench_strncmp_argp, 0, "bpf_strncmp helper benchmark", 0 }, 295 { &bench_local_storage_rcu_tasks_trace_argp, 0, 296 "local_storage RCU Tasks Trace slowdown benchmark", 0 }, 297 { &bench_hashmap_lookup_argp, 0, "Hashmap lookup benchmark", 0 }, 298 { &bench_local_storage_create_argp, 0, "local-storage-create benchmark", 0 }, 299 { &bench_htab_mem_argp, 0, "hash map memory benchmark", 0 }, 300 { &bench_trigger_batch_argp, 0, "BPF triggering benchmark", 0 }, 301 { &bench_crypto_argp, 0, "bpf crypto benchmark", 0 }, 302 { &bench_sockmap_argp, 0, "bpf sockmap benchmark", 0 }, 303 { &bench_lpm_trie_map_argp, 0, "LPM trie map benchmark", 0 }, 304 {}, 305 }; 306 307 /* Make pos_args global, so that we can run argp_parse twice, if necessary */ 308 static int pos_args; 309 310 static error_t parse_arg(int key, char *arg, struct argp_state *state) 311 { 312 switch (key) { 313 case 'v': 314 env.verbose = true; 315 break; 316 case 'l': 317 env.list = true; 318 break; 319 case 'd': 320 env.duration_sec = strtol(arg, NULL, 10); 321 if (env.duration_sec <= 0) { 322 fprintf(stderr, "Invalid duration: %s\n", arg); 323 argp_usage(state); 324 } 325 break; 326 case 'w': 327 env.warmup_sec = strtol(arg, NULL, 10); 328 if (env.warmup_sec <= 0) { 329 fprintf(stderr, "Invalid warm-up duration: %s\n", arg); 330 argp_usage(state); 331 } 332 break; 333 case 'p': 334 env.producer_cnt = strtol(arg, NULL, 10); 335 if (env.producer_cnt < 0) { 336 fprintf(stderr, "Invalid producer count: %s\n", arg); 337 argp_usage(state); 338 } 339 break; 340 case 'c': 341 env.consumer_cnt = strtol(arg, NULL, 10); 342 if (env.consumer_cnt < 0) { 343 fprintf(stderr, "Invalid consumer count: %s\n", arg); 344 argp_usage(state); 345 } 346 break; 347 case 'a': 348 env.affinity = true; 349 break; 350 case 'q': 351 env.quiet = true; 352 break; 353 case ARG_PROD_AFFINITY_SET: 354 env.affinity = true; 355 if (parse_num_list(arg, &env.prod_cpus.cpus, 356 &env.prod_cpus.cpus_len)) { 357 fprintf(stderr, "Invalid format of CPU set for producers."); 358 argp_usage(state); 359 } 360 break; 361 case ARG_CONS_AFFINITY_SET: 362 env.affinity = true; 363 if (parse_num_list(arg, &env.cons_cpus.cpus, 364 &env.cons_cpus.cpus_len)) { 365 fprintf(stderr, "Invalid format of CPU set for consumers."); 366 argp_usage(state); 367 } 368 break; 369 case ARGP_KEY_ARG: 370 if (pos_args++) { 371 fprintf(stderr, 372 "Unrecognized positional argument: %s\n", arg); 373 argp_usage(state); 374 } 375 env.bench_name = strdup(arg); 376 break; 377 default: 378 return ARGP_ERR_UNKNOWN; 379 } 380 return 0; 381 } 382 383 static void parse_cmdline_args_init(int argc, char **argv) 384 { 385 static const struct argp argp = { 386 .options = opts, 387 .parser = parse_arg, 388 .doc = argp_program_doc, 389 .children = bench_parsers, 390 }; 391 if (argp_parse(&argp, argc, argv, 0, NULL, NULL)) 392 exit(1); 393 } 394 395 static void parse_cmdline_args_final(int argc, char **argv) 396 { 397 struct argp_child bench_parsers[2] = {}; 398 const struct argp argp = { 399 .options = opts, 400 .parser = parse_arg, 401 .doc = argp_program_doc, 402 .children = bench_parsers, 403 }; 404 405 /* Parse arguments the second time with the correct set of parsers */ 406 if (bench->argp) { 407 bench_parsers[0].argp = bench->argp; 408 bench_parsers[0].header = bench->name; 409 pos_args = 0; 410 if (argp_parse(&argp, argc, argv, 0, NULL, NULL)) 411 exit(1); 412 } 413 } 414 415 static void collect_measurements(long delta_ns); 416 417 static __u64 last_time_ns; 418 static void sigalarm_handler(int signo) 419 { 420 long new_time_ns = get_time_ns(); 421 long delta_ns = new_time_ns - last_time_ns; 422 423 collect_measurements(delta_ns); 424 425 last_time_ns = new_time_ns; 426 } 427 428 /* set up periodic 1-second timer */ 429 static void setup_timer() 430 { 431 static struct sigaction sigalarm_action = { 432 .sa_handler = sigalarm_handler, 433 }; 434 struct itimerval timer_settings = {}; 435 int err; 436 437 last_time_ns = get_time_ns(); 438 err = sigaction(SIGALRM, &sigalarm_action, NULL); 439 if (err < 0) { 440 fprintf(stderr, "failed to install SIGALRM handler: %d\n", -errno); 441 exit(1); 442 } 443 timer_settings.it_interval.tv_sec = 1; 444 timer_settings.it_value.tv_sec = 1; 445 err = setitimer(ITIMER_REAL, &timer_settings, NULL); 446 if (err < 0) { 447 fprintf(stderr, "failed to arm interval timer: %d\n", -errno); 448 exit(1); 449 } 450 } 451 452 static void set_thread_affinity(pthread_t thread, int cpu) 453 { 454 cpu_set_t cpuset; 455 int err; 456 457 CPU_ZERO(&cpuset); 458 CPU_SET(cpu, &cpuset); 459 err = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset); 460 if (err) { 461 fprintf(stderr, "setting affinity to CPU #%d failed: %d\n", 462 cpu, -err); 463 exit(1); 464 } 465 } 466 467 static int next_cpu(struct cpu_set *cpu_set) 468 { 469 if (cpu_set->cpus) { 470 int i; 471 472 /* find next available CPU */ 473 for (i = cpu_set->next_cpu; i < cpu_set->cpus_len; i++) { 474 if (cpu_set->cpus[i]) { 475 cpu_set->next_cpu = i + 1; 476 return i; 477 } 478 } 479 fprintf(stderr, "Not enough CPUs specified, need CPU #%d or higher.\n", i); 480 exit(1); 481 } 482 483 return cpu_set->next_cpu++ % env.nr_cpus; 484 } 485 486 static struct bench_state { 487 int res_cnt; 488 struct bench_res *results; 489 pthread_t *consumers; 490 pthread_t *producers; 491 } state; 492 493 const struct bench *bench = NULL; 494 495 extern const struct bench bench_count_global; 496 extern const struct bench bench_count_local; 497 extern const struct bench bench_rename_base; 498 extern const struct bench bench_rename_kprobe; 499 extern const struct bench bench_rename_kretprobe; 500 extern const struct bench bench_rename_rawtp; 501 extern const struct bench bench_rename_fentry; 502 extern const struct bench bench_rename_fexit; 503 504 /* pure counting benchmarks to establish theoretical limits */ 505 extern const struct bench bench_trig_usermode_count; 506 extern const struct bench bench_trig_syscall_count; 507 extern const struct bench bench_trig_kernel_count; 508 509 /* batched, staying mostly in-kernel benchmarks */ 510 extern const struct bench bench_trig_kprobe; 511 extern const struct bench bench_trig_kretprobe; 512 extern const struct bench bench_trig_kprobe_multi; 513 extern const struct bench bench_trig_kretprobe_multi; 514 extern const struct bench bench_trig_fentry; 515 extern const struct bench bench_trig_kprobe_multi_all; 516 extern const struct bench bench_trig_kretprobe_multi_all; 517 extern const struct bench bench_trig_fexit; 518 extern const struct bench bench_trig_fmodret; 519 extern const struct bench bench_trig_tp; 520 extern const struct bench bench_trig_rawtp; 521 522 /* uprobe/uretprobe benchmarks */ 523 extern const struct bench bench_trig_uprobe_nop; 524 extern const struct bench bench_trig_uretprobe_nop; 525 extern const struct bench bench_trig_uprobe_push; 526 extern const struct bench bench_trig_uretprobe_push; 527 extern const struct bench bench_trig_uprobe_ret; 528 extern const struct bench bench_trig_uretprobe_ret; 529 extern const struct bench bench_trig_uprobe_multi_nop; 530 extern const struct bench bench_trig_uretprobe_multi_nop; 531 extern const struct bench bench_trig_uprobe_multi_push; 532 extern const struct bench bench_trig_uretprobe_multi_push; 533 extern const struct bench bench_trig_uprobe_multi_ret; 534 extern const struct bench bench_trig_uretprobe_multi_ret; 535 #ifdef __x86_64__ 536 extern const struct bench bench_trig_uprobe_nop5; 537 extern const struct bench bench_trig_uretprobe_nop5; 538 extern const struct bench bench_trig_uprobe_multi_nop5; 539 extern const struct bench bench_trig_uretprobe_multi_nop5; 540 #endif 541 542 extern const struct bench bench_rb_libbpf; 543 extern const struct bench bench_rb_custom; 544 extern const struct bench bench_pb_libbpf; 545 extern const struct bench bench_pb_custom; 546 extern const struct bench bench_bloom_lookup; 547 extern const struct bench bench_bloom_update; 548 extern const struct bench bench_bloom_false_positive; 549 extern const struct bench bench_hashmap_without_bloom; 550 extern const struct bench bench_hashmap_with_bloom; 551 extern const struct bench bench_bpf_loop; 552 extern const struct bench bench_strncmp_no_helper; 553 extern const struct bench bench_strncmp_helper; 554 extern const struct bench bench_bpf_hashmap_full_update; 555 extern const struct bench bench_local_storage_cache_seq_get; 556 extern const struct bench bench_local_storage_cache_interleaved_get; 557 extern const struct bench bench_local_storage_cache_hashmap_control; 558 extern const struct bench bench_local_storage_tasks_trace; 559 extern const struct bench bench_bpf_hashmap_lookup; 560 extern const struct bench bench_local_storage_create; 561 extern const struct bench bench_htab_mem; 562 extern const struct bench bench_crypto_encrypt; 563 extern const struct bench bench_crypto_decrypt; 564 extern const struct bench bench_sockmap; 565 extern const struct bench bench_lpm_trie_noop; 566 extern const struct bench bench_lpm_trie_baseline; 567 extern const struct bench bench_lpm_trie_lookup; 568 extern const struct bench bench_lpm_trie_insert; 569 extern const struct bench bench_lpm_trie_update; 570 extern const struct bench bench_lpm_trie_delete; 571 extern const struct bench bench_lpm_trie_free; 572 573 static const struct bench *benchs[] = { 574 &bench_count_global, 575 &bench_count_local, 576 &bench_rename_base, 577 &bench_rename_kprobe, 578 &bench_rename_kretprobe, 579 &bench_rename_rawtp, 580 &bench_rename_fentry, 581 &bench_rename_fexit, 582 /* pure counting benchmarks for establishing theoretical limits */ 583 &bench_trig_usermode_count, 584 &bench_trig_kernel_count, 585 &bench_trig_syscall_count, 586 /* batched, staying mostly in-kernel triggers */ 587 &bench_trig_kprobe, 588 &bench_trig_kretprobe, 589 &bench_trig_kprobe_multi, 590 &bench_trig_kretprobe_multi, 591 &bench_trig_fentry, 592 &bench_trig_kprobe_multi_all, 593 &bench_trig_kretprobe_multi_all, 594 &bench_trig_fexit, 595 &bench_trig_fmodret, 596 &bench_trig_tp, 597 &bench_trig_rawtp, 598 /* uprobes */ 599 &bench_trig_uprobe_nop, 600 &bench_trig_uretprobe_nop, 601 &bench_trig_uprobe_push, 602 &bench_trig_uretprobe_push, 603 &bench_trig_uprobe_ret, 604 &bench_trig_uretprobe_ret, 605 &bench_trig_uprobe_multi_nop, 606 &bench_trig_uretprobe_multi_nop, 607 &bench_trig_uprobe_multi_push, 608 &bench_trig_uretprobe_multi_push, 609 &bench_trig_uprobe_multi_ret, 610 &bench_trig_uretprobe_multi_ret, 611 #ifdef __x86_64__ 612 &bench_trig_uprobe_nop5, 613 &bench_trig_uretprobe_nop5, 614 &bench_trig_uprobe_multi_nop5, 615 &bench_trig_uretprobe_multi_nop5, 616 #endif 617 /* ringbuf/perfbuf benchmarks */ 618 &bench_rb_libbpf, 619 &bench_rb_custom, 620 &bench_pb_libbpf, 621 &bench_pb_custom, 622 &bench_bloom_lookup, 623 &bench_bloom_update, 624 &bench_bloom_false_positive, 625 &bench_hashmap_without_bloom, 626 &bench_hashmap_with_bloom, 627 &bench_bpf_loop, 628 &bench_strncmp_no_helper, 629 &bench_strncmp_helper, 630 &bench_bpf_hashmap_full_update, 631 &bench_local_storage_cache_seq_get, 632 &bench_local_storage_cache_interleaved_get, 633 &bench_local_storage_cache_hashmap_control, 634 &bench_local_storage_tasks_trace, 635 &bench_bpf_hashmap_lookup, 636 &bench_local_storage_create, 637 &bench_htab_mem, 638 &bench_crypto_encrypt, 639 &bench_crypto_decrypt, 640 &bench_sockmap, 641 &bench_lpm_trie_noop, 642 &bench_lpm_trie_baseline, 643 &bench_lpm_trie_lookup, 644 &bench_lpm_trie_insert, 645 &bench_lpm_trie_update, 646 &bench_lpm_trie_delete, 647 &bench_lpm_trie_free, 648 }; 649 650 static void find_benchmark(void) 651 { 652 int i; 653 654 if (!env.bench_name) { 655 fprintf(stderr, "benchmark name is not specified\n"); 656 exit(1); 657 } 658 for (i = 0; i < ARRAY_SIZE(benchs); i++) { 659 if (strcmp(benchs[i]->name, env.bench_name) == 0) { 660 bench = benchs[i]; 661 break; 662 } 663 } 664 if (!bench) { 665 fprintf(stderr, "benchmark '%s' not found\n", env.bench_name); 666 exit(1); 667 } 668 } 669 670 static void setup_benchmark(void) 671 { 672 int i, err; 673 674 if (!env.quiet) 675 printf("Setting up benchmark '%s'...\n", bench->name); 676 677 state.producers = calloc(env.producer_cnt, sizeof(*state.producers)); 678 state.consumers = calloc(env.consumer_cnt, sizeof(*state.consumers)); 679 state.results = calloc(env.duration_sec + env.warmup_sec + 2, 680 sizeof(*state.results)); 681 if (!state.producers || !state.consumers || !state.results) 682 exit(1); 683 684 if (bench->validate) 685 bench->validate(); 686 if (bench->setup) 687 bench->setup(); 688 689 for (i = 0; i < env.consumer_cnt; i++) { 690 if (!bench->consumer_thread) { 691 fprintf(stderr, "benchmark doesn't support consumers!\n"); 692 exit(1); 693 } 694 err = pthread_create(&state.consumers[i], NULL, 695 bench->consumer_thread, (void *)(long)i); 696 if (err) { 697 fprintf(stderr, "failed to create consumer thread #%d: %d\n", 698 i, -err); 699 exit(1); 700 } 701 if (env.affinity) 702 set_thread_affinity(state.consumers[i], 703 next_cpu(&env.cons_cpus)); 704 } 705 706 /* unless explicit producer CPU list is specified, continue after 707 * last consumer CPU 708 */ 709 if (!env.prod_cpus.cpus) 710 env.prod_cpus.next_cpu = env.cons_cpus.next_cpu; 711 712 for (i = 0; i < env.producer_cnt; i++) { 713 if (!bench->producer_thread) { 714 fprintf(stderr, "benchmark doesn't support producers!\n"); 715 exit(1); 716 } 717 err = pthread_create(&state.producers[i], NULL, 718 bench->producer_thread, (void *)(long)i); 719 if (err) { 720 fprintf(stderr, "failed to create producer thread #%d: %d\n", 721 i, -err); 722 exit(1); 723 } 724 if (env.affinity) 725 set_thread_affinity(state.producers[i], 726 next_cpu(&env.prod_cpus)); 727 } 728 729 if (!env.quiet) 730 printf("Benchmark '%s' started.\n", bench->name); 731 } 732 733 static pthread_mutex_t bench_done_mtx = PTHREAD_MUTEX_INITIALIZER; 734 static pthread_cond_t bench_done = PTHREAD_COND_INITIALIZER; 735 736 static void collect_measurements(long delta_ns) { 737 int iter = state.res_cnt++; 738 struct bench_res *res = &state.results[iter]; 739 740 bench->measure(res); 741 742 if (bench->report_progress) 743 bench->report_progress(iter, res, delta_ns); 744 745 if (iter == env.duration_sec + env.warmup_sec) { 746 pthread_mutex_lock(&bench_done_mtx); 747 pthread_cond_signal(&bench_done); 748 pthread_mutex_unlock(&bench_done_mtx); 749 } 750 } 751 752 int main(int argc, char **argv) 753 { 754 env.nr_cpus = get_nprocs(); 755 parse_cmdline_args_init(argc, argv); 756 757 if (env.list) { 758 int i; 759 760 printf("Available benchmarks:\n"); 761 for (i = 0; i < ARRAY_SIZE(benchs); i++) { 762 printf("- %s\n", benchs[i]->name); 763 } 764 return 0; 765 } 766 767 find_benchmark(); 768 parse_cmdline_args_final(argc, argv); 769 770 setup_benchmark(); 771 772 setup_timer(); 773 774 pthread_mutex_lock(&bench_done_mtx); 775 pthread_cond_wait(&bench_done, &bench_done_mtx); 776 pthread_mutex_unlock(&bench_done_mtx); 777 778 if (bench->report_final) 779 /* skip first sample */ 780 bench->report_final(state.results + env.warmup_sec, 781 state.res_cnt - env.warmup_sec); 782 783 return 0; 784 } 785