builtin-bench.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. */
  17. #include "perf.h"
  18. #include "util/util.h"
  19. #include <subcmd/parse-options.h>
  20. #include "builtin.h"
  21. #include "bench/bench.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/prctl.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. struct collection {
  62. const char *name;
  63. const char *summary;
  64. struct bench *benchmarks;
  65. };
  66. static struct collection collections[] = {
  67. { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
  68. { "mem", "Memory access benchmarks", mem_benchmarks },
  69. #ifdef HAVE_LIBNUMA_SUPPORT
  70. { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
  71. #endif
  72. {"futex", "Futex stressing benchmarks", futex_benchmarks },
  73. { "all", "All benchmarks", NULL },
  74. { NULL, NULL, NULL }
  75. };
  76. /* Iterate over all benchmark collections: */
  77. #define for_each_collection(coll) \
  78. for (coll = collections; coll->name; coll++)
  79. /* Iterate over all benchmarks within a collection: */
  80. #define for_each_bench(coll, bench) \
  81. for (bench = coll->benchmarks; bench && bench->name; bench++)
  82. static void dump_benchmarks(struct collection *coll)
  83. {
  84. struct bench *bench;
  85. printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
  86. for_each_bench(coll, bench)
  87. printf("%14s: %s\n", bench->name, bench->summary);
  88. printf("\n");
  89. }
  90. static const char *bench_format_str;
  91. /* Output/formatting style, exported to benchmark modules: */
  92. int bench_format = BENCH_FORMAT_DEFAULT;
  93. unsigned int bench_repeat = 10; /* default number of times to repeat the run */
  94. static const struct option bench_options[] = {
  95. OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
  96. OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
  97. OPT_END()
  98. };
  99. static const char * const bench_usage[] = {
  100. "perf bench [<common options>] <collection> <benchmark> [<options>]",
  101. NULL
  102. };
  103. static void print_usage(void)
  104. {
  105. struct collection *coll;
  106. int i;
  107. printf("Usage: \n");
  108. for (i = 0; bench_usage[i]; i++)
  109. printf("\t%s\n", bench_usage[i]);
  110. printf("\n");
  111. printf(" # List of all available benchmark collections:\n\n");
  112. for_each_collection(coll)
  113. printf("%14s: %s\n", coll->name, coll->summary);
  114. printf("\n");
  115. }
  116. static int bench_str2int(const char *str)
  117. {
  118. if (!str)
  119. return BENCH_FORMAT_DEFAULT;
  120. if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
  121. return BENCH_FORMAT_DEFAULT;
  122. else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
  123. return BENCH_FORMAT_SIMPLE;
  124. return BENCH_FORMAT_UNKNOWN;
  125. }
  126. /*
  127. * Run a specific benchmark but first rename the running task's ->comm[]
  128. * to something meaningful:
  129. */
  130. static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
  131. int argc, const char **argv)
  132. {
  133. int size;
  134. char *name;
  135. int ret;
  136. size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
  137. name = zalloc(size);
  138. BUG_ON(!name);
  139. scnprintf(name, size, "%s-%s", coll_name, bench_name);
  140. prctl(PR_SET_NAME, name);
  141. argv[0] = name;
  142. ret = fn(argc, argv);
  143. free(name);
  144. return ret;
  145. }
  146. static void run_collection(struct collection *coll)
  147. {
  148. struct bench *bench;
  149. const char *argv[2];
  150. argv[1] = NULL;
  151. /*
  152. * TODO:
  153. *
  154. * Preparing preset parameters for
  155. * embedded, ordinary PC, HPC, etc...
  156. * would be helpful.
  157. */
  158. for_each_bench(coll, bench) {
  159. if (!bench->fn)
  160. break;
  161. printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
  162. fflush(stdout);
  163. argv[1] = bench->name;
  164. run_bench(coll->name, bench->name, bench->fn, 1, argv);
  165. printf("\n");
  166. }
  167. }
  168. static void run_all_collections(void)
  169. {
  170. struct collection *coll;
  171. for_each_collection(coll)
  172. run_collection(coll);
  173. }
  174. int cmd_bench(int argc, const char **argv)
  175. {
  176. struct collection *coll;
  177. int ret = 0;
  178. if (argc < 2) {
  179. /* No collection specified. */
  180. print_usage();
  181. goto end;
  182. }
  183. argc = parse_options(argc, argv, bench_options, bench_usage,
  184. PARSE_OPT_STOP_AT_NON_OPTION);
  185. bench_format = bench_str2int(bench_format_str);
  186. if (bench_format == BENCH_FORMAT_UNKNOWN) {
  187. printf("Unknown format descriptor: '%s'\n", bench_format_str);
  188. goto end;
  189. }
  190. if (bench_repeat == 0) {
  191. printf("Invalid repeat option: Must specify a positive value\n");
  192. goto end;
  193. }
  194. if (argc < 1) {
  195. print_usage();
  196. goto end;
  197. }
  198. if (!strcmp(argv[0], "all")) {
  199. run_all_collections();
  200. goto end;
  201. }
  202. for_each_collection(coll) {
  203. struct bench *bench;
  204. if (strcmp(coll->name, argv[0]))
  205. continue;
  206. if (argc < 2) {
  207. /* No bench specified. */
  208. dump_benchmarks(coll);
  209. goto end;
  210. }
  211. if (!strcmp(argv[1], "all")) {
  212. run_collection(coll);
  213. goto end;
  214. }
  215. for_each_bench(coll, bench) {
  216. if (strcmp(bench->name, argv[1]))
  217. continue;
  218. if (bench_format == BENCH_FORMAT_DEFAULT)
  219. printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
  220. fflush(stdout);
  221. ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
  222. goto end;
  223. }
  224. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
  225. dump_benchmarks(coll);
  226. goto end;
  227. }
  228. printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
  229. ret = 1;
  230. goto end;
  231. }
  232. printf("Unknown collection: '%s'\n", argv[0]);
  233. ret = 1;
  234. end:
  235. return ret;
  236. }