builtin-bench.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. static struct bench_suite sched_suites[] = {
  31. { "messaging",
  32. "Benchmark for scheduler and IPC mechanisms",
  33. bench_sched_messaging },
  34. { "pipe",
  35. "Flood of communication over pipe() between two processes",
  36. bench_sched_pipe },
  37. { NULL,
  38. NULL,
  39. NULL }
  40. };
  41. static struct bench_suite mem_suites[] = {
  42. { "memcpy",
  43. "Simple memory copy in various ways",
  44. bench_mem_memcpy },
  45. { NULL,
  46. NULL,
  47. NULL }
  48. };
  49. struct bench_subsys {
  50. const char *name;
  51. const char *summary;
  52. struct bench_suite *suites;
  53. };
  54. static struct bench_subsys subsystems[] = {
  55. { "sched",
  56. "scheduler and IPC mechanism",
  57. sched_suites },
  58. { "mem",
  59. "memory access performance",
  60. mem_suites },
  61. { NULL,
  62. NULL,
  63. NULL }
  64. };
  65. static void dump_suites(int subsys_index)
  66. {
  67. int i;
  68. printf("List of available suites for %s...\n\n",
  69. subsystems[subsys_index].name);
  70. for (i = 0; subsystems[subsys_index].suites[i].name; i++)
  71. printf("\t%s: %s\n",
  72. subsystems[subsys_index].suites[i].name,
  73. subsystems[subsys_index].suites[i].summary);
  74. printf("\n");
  75. return;
  76. }
  77. static char *bench_format_str;
  78. int bench_format = BENCH_FORMAT_DEFAULT;
  79. static const struct option bench_options[] = {
  80. OPT_STRING('f', "format", &bench_format_str, "default",
  81. "Specify format style"),
  82. OPT_END()
  83. };
  84. static const char * const bench_usage[] = {
  85. "perf bench [<common options>] <subsystem> <suite> [<options>]",
  86. NULL
  87. };
  88. static void print_usage(void)
  89. {
  90. int i;
  91. printf("Usage: \n");
  92. for (i = 0; bench_usage[i]; i++)
  93. printf("\t%s\n", bench_usage[i]);
  94. printf("\n");
  95. printf("List of available subsystems...\n\n");
  96. for (i = 0; subsystems[i].name; i++)
  97. printf("\t%s: %s\n",
  98. subsystems[i].name, subsystems[i].summary);
  99. printf("\n");
  100. }
  101. static int bench_str2int(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. int cmd_bench(int argc, const char **argv, const char *prefix __used)
  112. {
  113. int i, j, status = 0;
  114. if (argc < 2) {
  115. /* No subsystem specified. */
  116. print_usage();
  117. goto end;
  118. }
  119. argc = parse_options(argc, argv, bench_options, bench_usage,
  120. PARSE_OPT_STOP_AT_NON_OPTION);
  121. bench_format = bench_str2int(bench_format_str);
  122. if (bench_format == BENCH_FORMAT_UNKNOWN) {
  123. printf("Unknown format descriptor:%s\n", bench_format_str);
  124. goto end;
  125. }
  126. if (argc < 1) {
  127. print_usage();
  128. goto end;
  129. }
  130. for (i = 0; subsystems[i].name; i++) {
  131. if (strcmp(subsystems[i].name, argv[0]))
  132. continue;
  133. if (argc < 2) {
  134. /* No suite specified. */
  135. dump_suites(i);
  136. goto end;
  137. }
  138. for (j = 0; subsystems[i].suites[j].name; j++) {
  139. if (strcmp(subsystems[i].suites[j].name, argv[1]))
  140. continue;
  141. if (bench_format == BENCH_FORMAT_DEFAULT)
  142. printf("# Running %s/%s benchmark...\n",
  143. subsystems[i].name,
  144. subsystems[i].suites[j].name);
  145. status = subsystems[i].suites[j].fn(argc - 1,
  146. argv + 1, prefix);
  147. goto end;
  148. }
  149. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
  150. dump_suites(i);
  151. goto end;
  152. }
  153. printf("Unknown suite:%s for %s\n", argv[1], argv[0]);
  154. status = 1;
  155. goto end;
  156. }
  157. printf("Unknown subsystem:%s\n", argv[0]);
  158. status = 1;
  159. end:
  160. return status;
  161. }