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