builtin-bench.c 8.6 KB

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