builtin-bench.c 3.6 KB

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