builtin-bench.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. */
  15. #include "perf.h"
  16. #include "util/util.h"
  17. #include "util/parse-options.h"
  18. #include "builtin.h"
  19. #include "bench/bench.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/prctl.h>
  24. typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix);
  25. struct bench {
  26. const char *name;
  27. const char *summary;
  28. bench_fn_t fn;
  29. };
  30. #ifdef HAVE_LIBNUMA_SUPPORT
  31. static struct bench numa_benchmarks[] = {
  32. { "mem", "Benchmark for NUMA workloads", bench_numa },
  33. { "all", "Test all NUMA benchmarks", NULL },
  34. { NULL, NULL, NULL }
  35. };
  36. #endif
  37. static struct bench sched_benchmarks[] = {
  38. { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
  39. { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
  40. { "all", "Test all scheduler benchmarks", NULL },
  41. { NULL, NULL, NULL }
  42. };
  43. static struct bench mem_benchmarks[] = {
  44. { "memcpy", "Benchmark for memcpy()", bench_mem_memcpy },
  45. { "memset", "Benchmark for memset() tests", bench_mem_memset },
  46. { "all", "Test all memory benchmarks", NULL },
  47. { NULL, NULL, NULL }
  48. };
  49. struct collection {
  50. const char *name;
  51. const char *summary;
  52. struct bench *benchmarks;
  53. };
  54. static struct collection collections[] = {
  55. { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
  56. { "mem", "Memory access benchmarks", mem_benchmarks },
  57. #ifdef HAVE_LIBNUMA_SUPPORT
  58. { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
  59. #endif
  60. { "all", "All benchmarks", NULL },
  61. { NULL, NULL, NULL }
  62. };
  63. /* Iterate over all benchmark collections: */
  64. #define for_each_collection(coll) \
  65. for (coll = collections; coll->name; coll++)
  66. /* Iterate over all benchmarks within a collection: */
  67. #define for_each_bench(coll, bench) \
  68. for (bench = coll->benchmarks; bench->name; bench++)
  69. static void dump_benchmarks(struct collection *coll)
  70. {
  71. struct bench *bench;
  72. printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
  73. for_each_bench(coll, bench)
  74. printf("%14s: %s\n", bench->name, bench->summary);
  75. printf("\n");
  76. }
  77. static const char *bench_format_str;
  78. /* Output/formatting style, exported to benchmark modules: */
  79. int bench_format = BENCH_FORMAT_DEFAULT;
  80. static const struct option bench_options[] = {
  81. OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
  82. OPT_END()
  83. };
  84. static const char * const bench_usage[] = {
  85. "perf bench [<common options>] <collection> <benchmark> [<options>]",
  86. NULL
  87. };
  88. static void print_usage(void)
  89. {
  90. struct collection *coll;
  91. int i;
  92. printf("Usage: \n");
  93. for (i = 0; bench_usage[i]; i++)
  94. printf("\t%s\n", bench_usage[i]);
  95. printf("\n");
  96. printf(" # List of all available benchmark collections:\n\n");
  97. for_each_collection(coll)
  98. printf("%14s: %s\n", coll->name, coll->summary);
  99. printf("\n");
  100. }
  101. static int bench_str2int(const char *str)
  102. {
  103. if (!str)
  104. return BENCH_FORMAT_DEFAULT;
  105. if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
  106. return BENCH_FORMAT_DEFAULT;
  107. else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
  108. return BENCH_FORMAT_SIMPLE;
  109. return BENCH_FORMAT_UNKNOWN;
  110. }
  111. /*
  112. * Run a specific benchmark but first rename the running task's ->comm[]
  113. * to something meaningful:
  114. */
  115. static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
  116. int argc, const char **argv, const char *prefix)
  117. {
  118. int size;
  119. char *name;
  120. int ret;
  121. size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
  122. name = zalloc(size);
  123. BUG_ON(!name);
  124. scnprintf(name, size, "%s-%s", coll_name, bench_name);
  125. prctl(PR_SET_NAME, name);
  126. argv[0] = name;
  127. ret = fn(argc, argv, prefix);
  128. free(name);
  129. return ret;
  130. }
  131. static void run_collection(struct collection *coll)
  132. {
  133. struct bench *bench;
  134. const char *argv[2];
  135. argv[1] = NULL;
  136. /*
  137. * TODO:
  138. *
  139. * Preparing preset parameters for
  140. * embedded, ordinary PC, HPC, etc...
  141. * would be helpful.
  142. */
  143. for_each_bench(coll, bench) {
  144. if (!bench->fn)
  145. break;
  146. printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
  147. fflush(stdout);
  148. argv[1] = bench->name;
  149. run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
  150. printf("\n");
  151. }
  152. }
  153. static void run_all_collections(void)
  154. {
  155. struct collection *coll;
  156. for_each_collection(coll)
  157. run_collection(coll);
  158. }
  159. int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
  160. {
  161. struct collection *coll;
  162. int ret = 0;
  163. if (argc < 2) {
  164. /* No collection specified. */
  165. print_usage();
  166. goto end;
  167. }
  168. argc = parse_options(argc, argv, bench_options, bench_usage,
  169. PARSE_OPT_STOP_AT_NON_OPTION);
  170. bench_format = bench_str2int(bench_format_str);
  171. if (bench_format == BENCH_FORMAT_UNKNOWN) {
  172. printf("Unknown format descriptor: '%s'\n", bench_format_str);
  173. goto end;
  174. }
  175. if (argc < 1) {
  176. print_usage();
  177. goto end;
  178. }
  179. if (!strcmp(argv[0], "all")) {
  180. run_all_collections();
  181. goto end;
  182. }
  183. for_each_collection(coll) {
  184. struct bench *bench;
  185. if (strcmp(coll->name, argv[0]))
  186. continue;
  187. if (argc < 2) {
  188. /* No bench specified. */
  189. dump_benchmarks(coll);
  190. goto end;
  191. }
  192. if (!strcmp(argv[1], "all")) {
  193. run_collection(coll);
  194. goto end;
  195. }
  196. for_each_bench(coll, bench) {
  197. if (strcmp(bench->name, argv[1]))
  198. continue;
  199. if (bench_format == BENCH_FORMAT_DEFAULT)
  200. printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
  201. fflush(stdout);
  202. ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
  203. goto end;
  204. }
  205. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
  206. dump_benchmarks(coll);
  207. goto end;
  208. }
  209. printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
  210. ret = 1;
  211. goto end;
  212. }
  213. printf("Unknown collection: '%s'\n", argv[0]);
  214. ret = 1;
  215. end:
  216. return ret;
  217. }