bench.c 22 KB

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