builtin-bench.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. *
  3. * builtin-bench.c
  4. *
  5. * General benchmarking subsystem provided by perf
  6. *
  7. * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
  8. *
  9. */
  10. /*
  11. *
  12. * Available subsystem list:
  13. * sched ... scheduler and IPC mechanism
  14. * mem ... memory access performance
  15. *
  16. */
  17. #include "perf.h"
  18. #include "util/util.h"
  19. #include "util/parse-options.h"
  20. #include "builtin.h"
  21. #include "bench/bench.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. struct bench_suite {
  26. const char *name;
  27. const char *summary;
  28. int (*fn)(int, const char **, const char *);
  29. };
  30. \
  31. /* sentinel: easy for help */
  32. #define suite_all { "all", "test all suite (pseudo suite)", NULL }
  33. static struct bench_suite sched_suites[] = {
  34. { "messaging",
  35. "Benchmark for scheduler and IPC mechanisms",
  36. bench_sched_messaging },
  37. { "pipe",
  38. "Flood of communication over pipe() between two processes",
  39. bench_sched_pipe },
  40. suite_all,
  41. { NULL,
  42. NULL,
  43. NULL }
  44. };
  45. static struct bench_suite mem_suites[] = {
  46. { "memcpy",
  47. "Simple memory copy in various ways",
  48. bench_mem_memcpy },
  49. suite_all,
  50. { NULL,
  51. NULL,
  52. NULL }
  53. };
  54. struct bench_subsys {
  55. const char *name;
  56. const char *summary;
  57. struct bench_suite *suites;
  58. };
  59. static struct bench_subsys subsystems[] = {
  60. { "sched",
  61. "scheduler and IPC mechanism",
  62. sched_suites },
  63. { "mem",
  64. "memory access performance",
  65. mem_suites },
  66. { "all", /* sentinel: easy for help */
  67. "test all subsystem (pseudo subsystem)",
  68. NULL },
  69. { NULL,
  70. NULL,
  71. NULL }
  72. };
  73. static void dump_suites(int subsys_index)
  74. {
  75. int i;
  76. printf("# List of available suites for %s...\n\n",
  77. subsystems[subsys_index].name);
  78. for (i = 0; subsystems[subsys_index].suites[i].name; i++)
  79. printf("%14s: %s\n",
  80. subsystems[subsys_index].suites[i].name,
  81. subsystems[subsys_index].suites[i].summary);
  82. printf("\n");
  83. return;
  84. }
  85. static const char *bench_format_str;
  86. int bench_format = BENCH_FORMAT_DEFAULT;
  87. static const struct option bench_options[] = {
  88. OPT_STRING('f', "format", &bench_format_str, "default",
  89. "Specify format style"),
  90. OPT_END()
  91. };
  92. static const char * const bench_usage[] = {
  93. "perf bench [<common options>] <subsystem> <suite> [<options>]",
  94. NULL
  95. };
  96. static void print_usage(void)
  97. {
  98. int i;
  99. printf("Usage: \n");
  100. for (i = 0; bench_usage[i]; i++)
  101. printf("\t%s\n", bench_usage[i]);
  102. printf("\n");
  103. printf("# List of available subsystems...\n\n");
  104. for (i = 0; subsystems[i].name; i++)
  105. printf("%14s: %s\n",
  106. subsystems[i].name, subsystems[i].summary);
  107. printf("\n");
  108. }
  109. static int bench_str2int(const char *str)
  110. {
  111. if (!str)
  112. return BENCH_FORMAT_DEFAULT;
  113. if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
  114. return BENCH_FORMAT_DEFAULT;
  115. else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
  116. return BENCH_FORMAT_SIMPLE;
  117. return BENCH_FORMAT_UNKNOWN;
  118. }
  119. static void all_suite(struct bench_subsys *subsys) /* FROM HERE */
  120. {
  121. int i;
  122. const char *argv[2];
  123. struct bench_suite *suites = subsys->suites;
  124. argv[1] = NULL;
  125. /*
  126. * TODO:
  127. * preparing preset parameters for
  128. * embedded, ordinary PC, HPC, etc...
  129. * will be helpful
  130. */
  131. for (i = 0; suites[i].fn; i++) {
  132. printf("# Running %s/%s benchmark...\n",
  133. subsys->name,
  134. suites[i].name);
  135. argv[1] = suites[i].name;
  136. suites[i].fn(1, argv, NULL);
  137. printf("\n");
  138. }
  139. }
  140. static void all_subsystem(void)
  141. {
  142. int i;
  143. for (i = 0; subsystems[i].suites; i++)
  144. all_suite(&subsystems[i]);
  145. }
  146. int cmd_bench(int argc, const char **argv, const char *prefix __used)
  147. {
  148. int i, j, status = 0;
  149. if (argc < 2) {
  150. /* No subsystem specified. */
  151. print_usage();
  152. goto end;
  153. }
  154. argc = parse_options(argc, argv, bench_options, bench_usage,
  155. PARSE_OPT_STOP_AT_NON_OPTION);
  156. bench_format = bench_str2int(bench_format_str);
  157. if (bench_format == BENCH_FORMAT_UNKNOWN) {
  158. printf("Unknown format descriptor:%s\n", bench_format_str);
  159. goto end;
  160. }
  161. if (argc < 1) {
  162. print_usage();
  163. goto end;
  164. }
  165. if (!strcmp(argv[0], "all")) {
  166. all_subsystem();
  167. goto end;
  168. }
  169. for (i = 0; subsystems[i].name; i++) {
  170. if (strcmp(subsystems[i].name, argv[0]))
  171. continue;
  172. if (argc < 2) {
  173. /* No suite specified. */
  174. dump_suites(i);
  175. goto end;
  176. }
  177. if (!strcmp(argv[1], "all")) {
  178. all_suite(&subsystems[i]);
  179. goto end;
  180. }
  181. for (j = 0; subsystems[i].suites[j].name; j++) {
  182. if (strcmp(subsystems[i].suites[j].name, argv[1]))
  183. continue;
  184. if (bench_format == BENCH_FORMAT_DEFAULT)
  185. printf("# Running %s/%s benchmark...\n",
  186. subsystems[i].name,
  187. subsystems[i].suites[j].name);
  188. status = subsystems[i].suites[j].fn(argc - 1,
  189. argv + 1, prefix);
  190. goto end;
  191. }
  192. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
  193. dump_suites(i);
  194. goto end;
  195. }
  196. printf("Unknown suite:%s for %s\n", argv[1], argv[0]);
  197. status = 1;
  198. goto end;
  199. }
  200. printf("Unknown subsystem:%s\n", argv[0]);
  201. status = 1;
  202. end:
  203. return status;
  204. }