builtin-bench.c 7.0 KB

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