builtin-bench.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-bench.c
  4. *
  5. * General benchmarking collections provided by perf
  6. *
  7. * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
  8. */
  9. /*
  10. * Available benchmark collection list:
  11. *
  12. * sched ... scheduler and IPC performance
  13. * mem ... memory access performance
  14. * numa ... NUMA scheduling and MM performance
  15. * futex ... Futex performance
  16. * epoll ... Event poll performance
  17. */
  18. #include <subcmd/parse-options.h>
  19. #include "builtin.h"
  20. #include "bench/bench.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/prctl.h>
  25. #include <linux/zalloc.h>
  26. typedef int (*bench_fn_t)(int argc, const char **argv);
  27. struct bench {
  28. const char *name;
  29. const char *summary;
  30. bench_fn_t fn;
  31. };
  32. #ifdef HAVE_LIBNUMA_SUPPORT
  33. static struct bench numa_benchmarks[] = {
  34. { "mem", "Benchmark for NUMA workloads", bench_numa },
  35. { "all", "Run all NUMA benchmarks", NULL },
  36. { NULL, NULL, NULL }
  37. };
  38. #endif
  39. static struct bench sched_benchmarks[] = {
  40. { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
  41. { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
  42. { "all", "Run all scheduler benchmarks", NULL },
  43. { NULL, NULL, NULL }
  44. };
  45. static struct bench mem_benchmarks[] = {
  46. { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
  47. { "memset", "Benchmark for memset() functions", bench_mem_memset },
  48. { "all", "Run all memory access benchmarks", NULL },
  49. { NULL, NULL, NULL }
  50. };
  51. static struct bench futex_benchmarks[] = {
  52. { "hash", "Benchmark for futex hash table", bench_futex_hash },
  53. { "wake", "Benchmark for futex wake calls", bench_futex_wake },
  54. { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
  55. { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
  56. /* pi-futexes */
  57. { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
  58. { "all", "Run all futex benchmarks", NULL },
  59. { NULL, NULL, NULL }
  60. };
  61. #ifdef HAVE_EVENTFD
  62. static struct bench epoll_benchmarks[] = {
  63. { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
  64. { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
  65. { "all", "Run all futex benchmarks", NULL },
  66. { NULL, NULL, NULL }
  67. };
  68. #endif // HAVE_EVENTFD
  69. struct collection {
  70. const char *name;
  71. const char *summary;
  72. struct bench *benchmarks;
  73. };
  74. static struct collection collections[] = {
  75. { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
  76. { "mem", "Memory access benchmarks", mem_benchmarks },
  77. #ifdef HAVE_LIBNUMA_SUPPORT
  78. { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
  79. #endif
  80. {"futex", "Futex stressing benchmarks", futex_benchmarks },
  81. #ifdef HAVE_EVENTFD
  82. {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
  83. #endif
  84. { "all", "All benchmarks", NULL },
  85. { NULL, NULL, NULL }
  86. };
  87. /* Iterate over all benchmark collections: */
  88. #define for_each_collection(coll) \
  89. for (coll = collections; coll->name; coll++)
  90. /* Iterate over all benchmarks within a collection: */
  91. #define for_each_bench(coll, bench) \
  92. for (bench = coll->benchmarks; bench && bench->name; bench++)
  93. static void dump_benchmarks(struct collection *coll)
  94. {
  95. struct bench *bench;
  96. printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
  97. for_each_bench(coll, bench)
  98. printf("%14s: %s\n", bench->name, bench->summary);
  99. printf("\n");
  100. }
  101. static const char *bench_format_str;
  102. /* Output/formatting style, exported to benchmark modules: */
  103. int bench_format = BENCH_FORMAT_DEFAULT;
  104. unsigned int bench_repeat = 10; /* default number of times to repeat the run */
  105. static const struct option bench_options[] = {
  106. OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
  107. OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
  108. OPT_END()
  109. };
  110. static const char * const bench_usage[] = {
  111. "perf bench [<common options>] <collection> <benchmark> [<options>]",
  112. NULL
  113. };
  114. static void print_usage(void)
  115. {
  116. struct collection *coll;
  117. int i;
  118. printf("Usage: \n");
  119. for (i = 0; bench_usage[i]; i++)
  120. printf("\t%s\n", bench_usage[i]);
  121. printf("\n");
  122. printf(" # List of all available benchmark collections:\n\n");
  123. for_each_collection(coll)
  124. printf("%14s: %s\n", coll->name, coll->summary);
  125. printf("\n");
  126. }
  127. static int bench_str2int(const char *str)
  128. {
  129. if (!str)
  130. return BENCH_FORMAT_DEFAULT;
  131. if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
  132. return BENCH_FORMAT_DEFAULT;
  133. else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
  134. return BENCH_FORMAT_SIMPLE;
  135. return BENCH_FORMAT_UNKNOWN;
  136. }
  137. /*
  138. * Run a specific benchmark but first rename the running task's ->comm[]
  139. * to something meaningful:
  140. */
  141. static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
  142. int argc, const char **argv)
  143. {
  144. int size;
  145. char *name;
  146. int ret;
  147. size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
  148. name = zalloc(size);
  149. BUG_ON(!name);
  150. scnprintf(name, size, "%s-%s", coll_name, bench_name);
  151. prctl(PR_SET_NAME, name);
  152. argv[0] = name;
  153. ret = fn(argc, argv);
  154. free(name);
  155. return ret;
  156. }
  157. static void run_collection(struct collection *coll)
  158. {
  159. struct bench *bench;
  160. const char *argv[2];
  161. argv[1] = NULL;
  162. /*
  163. * TODO:
  164. *
  165. * Preparing preset parameters for
  166. * embedded, ordinary PC, HPC, etc...
  167. * would be helpful.
  168. */
  169. for_each_bench(coll, bench) {
  170. if (!bench->fn)
  171. break;
  172. printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
  173. fflush(stdout);
  174. argv[1] = bench->name;
  175. run_bench(coll->name, bench->name, bench->fn, 1, argv);
  176. printf("\n");
  177. }
  178. }
  179. static void run_all_collections(void)
  180. {
  181. struct collection *coll;
  182. for_each_collection(coll)
  183. run_collection(coll);
  184. }
  185. int cmd_bench(int argc, const char **argv)
  186. {
  187. struct collection *coll;
  188. int ret = 0;
  189. if (argc < 2) {
  190. /* No collection specified. */
  191. print_usage();
  192. goto end;
  193. }
  194. argc = parse_options(argc, argv, bench_options, bench_usage,
  195. PARSE_OPT_STOP_AT_NON_OPTION);
  196. bench_format = bench_str2int(bench_format_str);
  197. if (bench_format == BENCH_FORMAT_UNKNOWN) {
  198. printf("Unknown format descriptor: '%s'\n", bench_format_str);
  199. goto end;
  200. }
  201. if (bench_repeat == 0) {
  202. printf("Invalid repeat option: Must specify a positive value\n");
  203. goto end;
  204. }
  205. if (argc < 1) {
  206. print_usage();
  207. goto end;
  208. }
  209. if (!strcmp(argv[0], "all")) {
  210. run_all_collections();
  211. goto end;
  212. }
  213. for_each_collection(coll) {
  214. struct bench *bench;
  215. if (strcmp(coll->name, argv[0]))
  216. continue;
  217. if (argc < 2) {
  218. /* No bench specified. */
  219. dump_benchmarks(coll);
  220. goto end;
  221. }
  222. if (!strcmp(argv[1], "all")) {
  223. run_collection(coll);
  224. goto end;
  225. }
  226. for_each_bench(coll, bench) {
  227. if (strcmp(bench->name, argv[1]))
  228. continue;
  229. if (bench_format == BENCH_FORMAT_DEFAULT)
  230. printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
  231. fflush(stdout);
  232. ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
  233. goto end;
  234. }
  235. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
  236. dump_benchmarks(coll);
  237. goto end;
  238. }
  239. printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
  240. ret = 1;
  241. goto end;
  242. }
  243. printf("Unknown collection: '%s'\n", argv[0]);
  244. ret = 1;
  245. end:
  246. return ret;
  247. }