bench.c 18 KB

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