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 * mem ... memory access performance
15 * numa ... NUMA scheduling and MM performance
16 * futex ... Futex performance
17 * epoll ... Event poll performance
19 #include <subcmd/parse-options.h>
21 #include "bench/bench.h"
26 #include <sys/prctl.h>
27 #include <linux/zalloc.h>
29 typedef int (*bench_fn_t)(int argc, const char **argv);
37 #ifdef HAVE_LIBNUMA_SUPPORT
38 static struct bench numa_benchmarks[] = {
39 { "mem", "Benchmark for NUMA workloads", bench_numa },
40 { "all", "Run all NUMA benchmarks", NULL },
45 static struct bench sched_benchmarks[] = {
46 { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
47 { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
48 { "all", "Run all scheduler benchmarks", NULL },
52 static struct bench mem_benchmarks[] = {
53 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
54 { "memset", "Benchmark for memset() functions", bench_mem_memset },
55 { "all", "Run all memory access benchmarks", NULL },
59 static struct bench futex_benchmarks[] = {
60 { "hash", "Benchmark for futex hash table", bench_futex_hash },
61 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
62 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
63 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
65 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
66 { "all", "Run all futex benchmarks", NULL },
70 #ifdef HAVE_EVENTFD_SUPPORT
71 static struct bench epoll_benchmarks[] = {
72 { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
73 { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
74 { "all", "Run all futex benchmarks", NULL },
77 #endif // HAVE_EVENTFD_SUPPORT
79 static struct bench internals_benchmarks[] = {
80 { "synthesize", "Benchmark perf event synthesis", bench_synthesize },
81 { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse },
88 struct bench *benchmarks;
91 static struct collection collections[] = {
92 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
93 { "mem", "Memory access benchmarks", mem_benchmarks },
94 #ifdef HAVE_LIBNUMA_SUPPORT
95 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
97 {"futex", "Futex stressing benchmarks", futex_benchmarks },
98 #ifdef HAVE_EVENTFD_SUPPORT
99 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
101 { "internals", "Perf-internals benchmarks", internals_benchmarks },
102 { "all", "All benchmarks", NULL },
106 /* Iterate over all benchmark collections: */
107 #define for_each_collection(coll) \
108 for (coll = collections; coll->name; coll++)
110 /* Iterate over all benchmarks within a collection: */
111 #define for_each_bench(coll, bench) \
112 for (bench = coll->benchmarks; bench && bench->name; bench++)
114 static void dump_benchmarks(struct collection *coll)
118 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
120 for_each_bench(coll, bench)
121 printf("%14s: %s\n", bench->name, bench->summary);
126 static const char *bench_format_str;
128 /* Output/formatting style, exported to benchmark modules: */
129 int bench_format = BENCH_FORMAT_DEFAULT;
130 unsigned int bench_repeat = 10; /* default number of times to repeat the run */
132 static const struct option bench_options[] = {
133 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
134 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
138 static const char * const bench_usage[] = {
139 "perf bench [<common options>] <collection> <benchmark> [<options>]",
143 static void print_usage(void)
145 struct collection *coll;
149 for (i = 0; bench_usage[i]; i++)
150 printf("\t%s\n", bench_usage[i]);
153 printf(" # List of all available benchmark collections:\n\n");
155 for_each_collection(coll)
156 printf("%14s: %s\n", coll->name, coll->summary);
160 static int bench_str2int(const char *str)
163 return BENCH_FORMAT_DEFAULT;
165 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
166 return BENCH_FORMAT_DEFAULT;
167 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
168 return BENCH_FORMAT_SIMPLE;
170 return BENCH_FORMAT_UNKNOWN;
174 * Run a specific benchmark but first rename the running task's ->comm[]
175 * to something meaningful:
177 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
178 int argc, const char **argv)
184 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
189 scnprintf(name, size, "%s-%s", coll_name, bench_name);
191 prctl(PR_SET_NAME, name);
194 ret = fn(argc, argv);
201 static void run_collection(struct collection *coll)
210 * Preparing preset parameters for
211 * embedded, ordinary PC, HPC, etc...
214 for_each_bench(coll, bench) {
217 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
220 argv[1] = bench->name;
221 run_bench(coll->name, bench->name, bench->fn, 1, argv);
226 static void run_all_collections(void)
228 struct collection *coll;
230 for_each_collection(coll)
231 run_collection(coll);
234 int cmd_bench(int argc, const char **argv)
236 struct collection *coll;
240 /* No collection specified. */
245 argc = parse_options(argc, argv, bench_options, bench_usage,
246 PARSE_OPT_STOP_AT_NON_OPTION);
248 bench_format = bench_str2int(bench_format_str);
249 if (bench_format == BENCH_FORMAT_UNKNOWN) {
250 printf("Unknown format descriptor: '%s'\n", bench_format_str);
254 if (bench_repeat == 0) {
255 printf("Invalid repeat option: Must specify a positive value\n");
264 if (!strcmp(argv[0], "all")) {
265 run_all_collections();
269 for_each_collection(coll) {
272 if (strcmp(coll->name, argv[0]))
276 /* No bench specified. */
277 dump_benchmarks(coll);
281 if (!strcmp(argv[1], "all")) {
282 run_collection(coll);
286 for_each_bench(coll, bench) {
287 if (strcmp(bench->name, argv[1]))
290 if (bench_format == BENCH_FORMAT_DEFAULT)
291 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
293 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
297 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
298 dump_benchmarks(coll);
302 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
307 printf("Unknown collection: '%s'\n", argv[0]);