bench.c 23 KB

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