builtin-bench.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-bench.c
  4. *
  5. * General benchmarking collections provided by perf
  6. *
  7. * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
  8. */
  9. /*
  10. * Available benchmark collection list:
  11. *
  12. * sched ... scheduler and IPC performance
  13. * syscall ... System call performance
  14. * mem ... memory access performance
  15. * numa ... NUMA scheduling and MM performance
  16. * futex ... Futex performance
  17. * epoll ... Event poll performance
  18. */
  19. #include <subcmd/parse-options.h>
  20. #include "builtin.h"
  21. #include "bench/bench.h"
  22. #include <locale.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/prctl.h>
  27. #include <linux/zalloc.h>
  28. typedef int (*bench_fn_t)(int argc, const char **argv);
  29. struct bench {
  30. const char *name;
  31. const char *summary;
  32. bench_fn_t fn;
  33. };
  34. #ifdef HAVE_LIBNUMA_SUPPORT
  35. static struct bench numa_benchmarks[] = {
  36. { "mem", "Benchmark for NUMA workloads", bench_numa },
  37. { "all", "Run all NUMA benchmarks", NULL },
  38. { NULL, NULL, NULL }
  39. };
  40. #endif
  41. static struct bench sched_benchmarks[] = {
  42. { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
  43. { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
  44. { "seccomp-notify", "Benchmark for seccomp user notify", bench_sched_seccomp_notify},
  45. { "all", "Run all scheduler benchmarks", NULL },
  46. { NULL, NULL, NULL }
  47. };
  48. static struct bench syscall_benchmarks[] = {
  49. { "basic", "Benchmark for basic getppid(2) calls", bench_syscall_basic },
  50. { "getpgid", "Benchmark for getpgid(2) calls", bench_syscall_getpgid },
  51. { "fork", "Benchmark for fork(2) calls", bench_syscall_fork },
  52. { "execve", "Benchmark for execve(2) calls", bench_syscall_execve },
  53. { "all", "Run all syscall benchmarks", NULL },
  54. { NULL, NULL, NULL },
  55. };
  56. static struct bench mem_benchmarks[] = {
  57. { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
  58. { "memset", "Benchmark for memset() functions", bench_mem_memset },
  59. { "find_bit", "Benchmark for find_bit() functions", bench_mem_find_bit },
  60. { "all", "Run all memory access benchmarks", NULL },
  61. { NULL, NULL, NULL }
  62. };
  63. static struct bench futex_benchmarks[] = {
  64. { "hash", "Benchmark for futex hash table", bench_futex_hash },
  65. { "wake", "Benchmark for futex wake calls", bench_futex_wake },
  66. { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
  67. { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
  68. /* pi-futexes */
  69. { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
  70. { "all", "Run all futex benchmarks", NULL },
  71. { NULL, NULL, NULL }
  72. };
  73. #ifdef HAVE_EVENTFD_SUPPORT
  74. static struct bench epoll_benchmarks[] = {
  75. { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
  76. { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
  77. { "all", "Run all futex benchmarks", NULL },
  78. { NULL, NULL, NULL }
  79. };
  80. #endif // HAVE_EVENTFD_SUPPORT
  81. static struct bench internals_benchmarks[] = {
  82. { "synthesize", "Benchmark perf event synthesis", bench_synthesize },
  83. { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse },
  84. { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id },
  85. { "evlist-open-close", "Benchmark evlist open and close", bench_evlist_open_close },
  86. { "pmu-scan", "Benchmark sysfs PMU info scanning", bench_pmu_scan },
  87. { NULL, NULL, NULL }
  88. };
  89. static struct bench breakpoint_benchmarks[] = {
  90. { "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
  91. { "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
  92. { "all", "Run all breakpoint benchmarks", NULL},
  93. { NULL, NULL, NULL },
  94. };
  95. static struct bench uprobe_benchmarks[] = {
  96. { "baseline", "Baseline libc usleep(1000) call", bench_uprobe_baseline, },
  97. { "empty", "Attach empty BPF prog to uprobe on usleep, system wide", bench_uprobe_empty, },
  98. { "trace_printk", "Attach trace_printk BPF prog to uprobe on usleep syswide", bench_uprobe_trace_printk, },
  99. { "empty_ret", "Attach empty BPF prog to uretprobe on usleep, system wide", bench_uprobe_empty_ret, },
  100. { "trace_printk_ret", "Attach trace_printk BPF prog to uretprobe on usleep syswide", bench_uprobe_trace_printk_ret,},
  101. { NULL, NULL, NULL },
  102. };
  103. struct collection {
  104. const char *name;
  105. const char *summary;
  106. struct bench *benchmarks;
  107. };
  108. static struct collection collections[] = {
  109. { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
  110. { "syscall", "System call benchmarks", syscall_benchmarks },
  111. { "mem", "Memory access benchmarks", mem_benchmarks },
  112. #ifdef HAVE_LIBNUMA_SUPPORT
  113. { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
  114. #endif
  115. {"futex", "Futex stressing benchmarks", futex_benchmarks },
  116. #ifdef HAVE_EVENTFD_SUPPORT
  117. {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
  118. #endif
  119. { "internals", "Perf-internals benchmarks", internals_benchmarks },
  120. { "breakpoint", "Breakpoint benchmarks", breakpoint_benchmarks },
  121. { "uprobe", "uprobe benchmarks", uprobe_benchmarks },
  122. { "all", "All benchmarks", NULL },
  123. { NULL, NULL, NULL }
  124. };
  125. /* Iterate over all benchmark collections: */
  126. #define for_each_collection(coll) \
  127. for (coll = collections; coll->name; coll++)
  128. /* Iterate over all benchmarks within a collection: */
  129. #define for_each_bench(coll, bench) \
  130. for (bench = coll->benchmarks; bench && bench->name; bench++)
  131. static void dump_benchmarks(struct collection *coll)
  132. {
  133. struct bench *bench;
  134. printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
  135. for_each_bench(coll, bench)
  136. printf("%14s: %s\n", bench->name, bench->summary);
  137. printf("\n");
  138. }
  139. static const char *bench_format_str;
  140. /* Output/formatting style, exported to benchmark modules: */
  141. int bench_format = BENCH_FORMAT_DEFAULT;
  142. unsigned int bench_repeat = 10; /* default number of times to repeat the run */
  143. static const struct option bench_options[] = {
  144. OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
  145. OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify number of times to repeat the run"),
  146. OPT_END()
  147. };
  148. static const char * const bench_usage[] = {
  149. "perf bench [<common options>] <collection> <benchmark> [<options>]",
  150. NULL
  151. };
  152. static void print_usage(void)
  153. {
  154. struct collection *coll;
  155. int i;
  156. printf("Usage: \n");
  157. for (i = 0; bench_usage[i]; i++)
  158. printf("\t%s\n", bench_usage[i]);
  159. printf("\n");
  160. printf(" # List of all available benchmark collections:\n\n");
  161. for_each_collection(coll)
  162. printf("%14s: %s\n", coll->name, coll->summary);
  163. printf("\n");
  164. }
  165. static int bench_str2int(const char *str)
  166. {
  167. if (!str)
  168. return BENCH_FORMAT_DEFAULT;
  169. if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
  170. return BENCH_FORMAT_DEFAULT;
  171. else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
  172. return BENCH_FORMAT_SIMPLE;
  173. return BENCH_FORMAT_UNKNOWN;
  174. }
  175. /*
  176. * Run a specific benchmark but first rename the running task's ->comm[]
  177. * to something meaningful:
  178. */
  179. static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
  180. int argc, const char **argv)
  181. {
  182. int size;
  183. char *name;
  184. int ret;
  185. size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
  186. name = zalloc(size);
  187. BUG_ON(!name);
  188. scnprintf(name, size, "%s-%s", coll_name, bench_name);
  189. prctl(PR_SET_NAME, name);
  190. argv[0] = name;
  191. ret = fn(argc, argv);
  192. free(name);
  193. return ret;
  194. }
  195. static void run_collection(struct collection *coll)
  196. {
  197. struct bench *bench;
  198. const char *argv[2];
  199. argv[1] = NULL;
  200. /*
  201. * TODO:
  202. *
  203. * Preparing preset parameters for
  204. * embedded, ordinary PC, HPC, etc...
  205. * would be helpful.
  206. */
  207. for_each_bench(coll, bench) {
  208. if (!bench->fn)
  209. break;
  210. printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
  211. argv[1] = bench->name;
  212. run_bench(coll->name, bench->name, bench->fn, 1, argv);
  213. printf("\n");
  214. }
  215. }
  216. static void run_all_collections(void)
  217. {
  218. struct collection *coll;
  219. for_each_collection(coll)
  220. run_collection(coll);
  221. }
  222. int cmd_bench(int argc, const char **argv)
  223. {
  224. struct collection *coll;
  225. int ret = 0;
  226. /* Unbuffered output */
  227. setvbuf(stdout, NULL, _IONBF, 0);
  228. setlocale(LC_ALL, "");
  229. if (argc < 2) {
  230. /* No collection specified. */
  231. print_usage();
  232. goto end;
  233. }
  234. argc = parse_options(argc, argv, bench_options, bench_usage,
  235. PARSE_OPT_STOP_AT_NON_OPTION);
  236. bench_format = bench_str2int(bench_format_str);
  237. if (bench_format == BENCH_FORMAT_UNKNOWN) {
  238. printf("Unknown format descriptor: '%s'\n", bench_format_str);
  239. goto end;
  240. }
  241. if (bench_repeat == 0) {
  242. printf("Invalid repeat option: Must specify a positive value\n");
  243. goto end;
  244. }
  245. if (argc < 1) {
  246. print_usage();
  247. goto end;
  248. }
  249. if (!strcmp(argv[0], "all")) {
  250. run_all_collections();
  251. goto end;
  252. }
  253. for_each_collection(coll) {
  254. struct bench *bench;
  255. if (strcmp(coll->name, argv[0]))
  256. continue;
  257. if (argc < 2) {
  258. /* No bench specified. */
  259. dump_benchmarks(coll);
  260. goto end;
  261. }
  262. if (!strcmp(argv[1], "all")) {
  263. run_collection(coll);
  264. goto end;
  265. }
  266. for_each_bench(coll, bench) {
  267. if (strcmp(bench->name, argv[1]))
  268. continue;
  269. if (bench_format == BENCH_FORMAT_DEFAULT)
  270. printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
  271. ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
  272. goto end;
  273. }
  274. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
  275. dump_benchmarks(coll);
  276. goto end;
  277. }
  278. printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
  279. ret = 1;
  280. goto end;
  281. }
  282. printf("Unknown collection: '%s'\n", argv[0]);
  283. ret = 1;
  284. end:
  285. return ret;
  286. }