builtin-bench.c 6.8 KB

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