1 // SPDX-License-Identifier: GPL-2.0
5 * General benchmarking collections provided by perf
7 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
11 * Available benchmark collection list:
13 * sched ... scheduler and IPC performance
14 * syscall ... System call performance
15 * mem ... memory access performance
16 * numa ... NUMA scheduling and MM performance
17 * futex ... Futex performance
18 * epoll ... Event poll performance
20 #include <subcmd/parse-options.h>
22 #include "bench/bench.h"
28 #include <sys/prctl.h>
29 #include <linux/zalloc.h>
31 typedef int (*bench_fn_t)(int argc, const char **argv);
39 #ifdef HAVE_LIBNUMA_SUPPORT
40 static struct bench numa_benchmarks[] = {
41 { "mem", "Benchmark for NUMA workloads", bench_numa },
42 { "all", "Run all NUMA benchmarks", NULL },
47 static struct bench sched_benchmarks[] = {
48 { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
49 { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
50 { "all", "Run all scheduler benchmarks", NULL },
54 static struct bench syscall_benchmarks[] = {
55 { "basic", "Benchmark for basic getppid(2) calls", bench_syscall_basic },
56 { "getpgid", "Benchmark for getpgid(2) calls", bench_syscall_getpgid },
57 { "fork", "Benchmark for fork(2) calls", bench_syscall_fork },
58 { "execve", "Benchmark for execve(2) calls", bench_syscall_execve },
59 { "all", "Run all syscall benchmarks", NULL },
63 static struct bench mem_benchmarks[] = {
64 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
65 { "memset", "Benchmark for memset() functions", bench_mem_memset },
66 { "find_bit", "Benchmark for find_bit() functions", bench_mem_find_bit },
67 { "all", "Run all memory access benchmarks", NULL },
71 static struct bench futex_benchmarks[] = {
72 { "hash", "Benchmark for futex hash table", bench_futex_hash },
73 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
74 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
75 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
77 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
78 { "all", "Run all futex benchmarks", NULL },
82 #ifdef HAVE_EVENTFD_SUPPORT
83 static struct bench epoll_benchmarks[] = {
84 { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
85 { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
86 { "all", "Run all futex benchmarks", NULL },
89 #endif // HAVE_EVENTFD_SUPPORT
91 static struct bench internals_benchmarks[] = {
92 { "synthesize", "Benchmark perf event synthesis", bench_synthesize },
93 { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse },
94 { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id },
95 { "evlist-open-close", "Benchmark evlist open and close", bench_evlist_open_close },
96 { "pmu-scan", "Benchmark sysfs PMU info scanning", bench_pmu_scan },
100 static struct bench breakpoint_benchmarks[] = {
101 { "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
102 { "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
103 { "all", "Run all breakpoint benchmarks", NULL},
104 { NULL, NULL, NULL },
110 struct bench *benchmarks;
113 static struct collection collections[] = {
114 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
115 { "syscall", "System call benchmarks", syscall_benchmarks },
116 { "mem", "Memory access benchmarks", mem_benchmarks },
117 #ifdef HAVE_LIBNUMA_SUPPORT
118 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
120 {"futex", "Futex stressing benchmarks", futex_benchmarks },
121 #ifdef HAVE_EVENTFD_SUPPORT
122 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
124 { "internals", "Perf-internals benchmarks", internals_benchmarks },
125 { "breakpoint", "Breakpoint benchmarks", breakpoint_benchmarks },
126 { "all", "All benchmarks", NULL },
130 /* Iterate over all benchmark collections: */
131 #define for_each_collection(coll) \
132 for (coll = collections; coll->name; coll++)
134 /* Iterate over all benchmarks within a collection: */
135 #define for_each_bench(coll, bench) \
136 for (bench = coll->benchmarks; bench && bench->name; bench++)
138 static void dump_benchmarks(struct collection *coll)
142 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
144 for_each_bench(coll, bench)
145 printf("%14s: %s\n", bench->name, bench->summary);
150 static const char *bench_format_str;
152 /* Output/formatting style, exported to benchmark modules: */
153 int bench_format = BENCH_FORMAT_DEFAULT;
154 unsigned int bench_repeat = 10; /* default number of times to repeat the run */
156 static const struct option bench_options[] = {
157 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
158 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify number of times to repeat the run"),
162 static const char * const bench_usage[] = {
163 "perf bench [<common options>] <collection> <benchmark> [<options>]",
167 static void print_usage(void)
169 struct collection *coll;
173 for (i = 0; bench_usage[i]; i++)
174 printf("\t%s\n", bench_usage[i]);
177 printf(" # List of all available benchmark collections:\n\n");
179 for_each_collection(coll)
180 printf("%14s: %s\n", coll->name, coll->summary);
184 static int bench_str2int(const char *str)
187 return BENCH_FORMAT_DEFAULT;
189 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
190 return BENCH_FORMAT_DEFAULT;
191 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
192 return BENCH_FORMAT_SIMPLE;
194 return BENCH_FORMAT_UNKNOWN;
198 * Run a specific benchmark but first rename the running task's ->comm[]
199 * to something meaningful:
201 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
202 int argc, const char **argv)
208 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
213 scnprintf(name, size, "%s-%s", coll_name, bench_name);
215 prctl(PR_SET_NAME, name);
218 ret = fn(argc, argv);
225 static void run_collection(struct collection *coll)
234 * Preparing preset parameters for
235 * embedded, ordinary PC, HPC, etc...
238 for_each_bench(coll, bench) {
241 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
243 argv[1] = bench->name;
244 run_bench(coll->name, bench->name, bench->fn, 1, argv);
249 static void run_all_collections(void)
251 struct collection *coll;
253 for_each_collection(coll)
254 run_collection(coll);
257 int cmd_bench(int argc, const char **argv)
259 struct collection *coll;
262 /* Unbuffered output */
263 setvbuf(stdout, NULL, _IONBF, 0);
264 setlocale(LC_ALL, "");
267 /* No collection specified. */
272 argc = parse_options(argc, argv, bench_options, bench_usage,
273 PARSE_OPT_STOP_AT_NON_OPTION);
275 bench_format = bench_str2int(bench_format_str);
276 if (bench_format == BENCH_FORMAT_UNKNOWN) {
277 printf("Unknown format descriptor: '%s'\n", bench_format_str);
281 if (bench_repeat == 0) {
282 printf("Invalid repeat option: Must specify a positive value\n");
291 if (!strcmp(argv[0], "all")) {
292 run_all_collections();
296 for_each_collection(coll) {
299 if (strcmp(coll->name, argv[0]))
303 /* No bench specified. */
304 dump_benchmarks(coll);
308 if (!strcmp(argv[1], "all")) {
309 run_collection(coll);
313 for_each_bench(coll, bench) {
314 if (strcmp(bench->name, argv[1]))
317 if (bench_format == BENCH_FORMAT_DEFAULT)
318 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
319 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
323 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
324 dump_benchmarks(coll);
328 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
333 printf("Unknown collection: '%s'\n", argv[0]);