builtin-bench.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. static const struct option bench_options[] = {
  90. OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
  91. OPT_END()
  92. };
  93. static const char * const bench_usage[] = {
  94. "perf bench [<common options>] <collection> <benchmark> [<options>]",
  95. NULL
  96. };
  97. static void print_usage(void)
  98. {
  99. struct collection *coll;
  100. int i;
  101. printf("Usage: \n");
  102. for (i = 0; bench_usage[i]; i++)
  103. printf("\t%s\n", bench_usage[i]);
  104. printf("\n");
  105. printf(" # List of all available benchmark collections:\n\n");
  106. for_each_collection(coll)
  107. printf("%14s: %s\n", coll->name, coll->summary);
  108. printf("\n");
  109. }
  110. static int bench_str2int(const char *str)
  111. {
  112. if (!str)
  113. return BENCH_FORMAT_DEFAULT;
  114. if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
  115. return BENCH_FORMAT_DEFAULT;
  116. else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
  117. return BENCH_FORMAT_SIMPLE;
  118. return BENCH_FORMAT_UNKNOWN;
  119. }
  120. /*
  121. * Run a specific benchmark but first rename the running task's ->comm[]
  122. * to something meaningful:
  123. */
  124. static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
  125. int argc, const char **argv, const char *prefix)
  126. {
  127. int size;
  128. char *name;
  129. int ret;
  130. size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
  131. name = zalloc(size);
  132. BUG_ON(!name);
  133. scnprintf(name, size, "%s-%s", coll_name, bench_name);
  134. prctl(PR_SET_NAME, name);
  135. argv[0] = name;
  136. ret = fn(argc, argv, prefix);
  137. free(name);
  138. return ret;
  139. }
  140. static void run_collection(struct collection *coll)
  141. {
  142. struct bench *bench;
  143. const char *argv[2];
  144. argv[1] = NULL;
  145. /*
  146. * TODO:
  147. *
  148. * Preparing preset parameters for
  149. * embedded, ordinary PC, HPC, etc...
  150. * would be helpful.
  151. */
  152. for_each_bench(coll, bench) {
  153. if (!bench->fn)
  154. break;
  155. printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
  156. fflush(stdout);
  157. argv[1] = bench->name;
  158. run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
  159. printf("\n");
  160. }
  161. }
  162. static void run_all_collections(void)
  163. {
  164. struct collection *coll;
  165. for_each_collection(coll)
  166. run_collection(coll);
  167. }
  168. int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
  169. {
  170. struct collection *coll;
  171. int ret = 0;
  172. if (argc < 2) {
  173. /* No collection specified. */
  174. print_usage();
  175. goto end;
  176. }
  177. argc = parse_options(argc, argv, bench_options, bench_usage,
  178. PARSE_OPT_STOP_AT_NON_OPTION);
  179. bench_format = bench_str2int(bench_format_str);
  180. if (bench_format == BENCH_FORMAT_UNKNOWN) {
  181. printf("Unknown format descriptor: '%s'\n", bench_format_str);
  182. goto end;
  183. }
  184. if (argc < 1) {
  185. print_usage();
  186. goto end;
  187. }
  188. if (!strcmp(argv[0], "all")) {
  189. run_all_collections();
  190. goto end;
  191. }
  192. for_each_collection(coll) {
  193. struct bench *bench;
  194. if (strcmp(coll->name, argv[0]))
  195. continue;
  196. if (argc < 2) {
  197. /* No bench specified. */
  198. dump_benchmarks(coll);
  199. goto end;
  200. }
  201. if (!strcmp(argv[1], "all")) {
  202. run_collection(coll);
  203. goto end;
  204. }
  205. for_each_bench(coll, bench) {
  206. if (strcmp(bench->name, argv[1]))
  207. continue;
  208. if (bench_format == BENCH_FORMAT_DEFAULT)
  209. printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
  210. fflush(stdout);
  211. ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
  212. goto end;
  213. }
  214. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
  215. dump_benchmarks(coll);
  216. goto end;
  217. }
  218. printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
  219. ret = 1;
  220. goto end;
  221. }
  222. printf("Unknown collection: '%s'\n", argv[0]);
  223. ret = 1;
  224. end:
  225. return ret;
  226. }