builtin-bench.c 6.7 KB

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