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 { "all", "Run all syscall benchmarks", NULL },
60 static struct bench mem_benchmarks[] = {
61 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
62 { "memset", "Benchmark for memset() functions", bench_mem_memset },
63 { "find_bit", "Benchmark for find_bit() functions", bench_mem_find_bit },
64 { "all", "Run all memory access benchmarks", NULL },
68 static struct bench futex_benchmarks[] = {
69 { "hash", "Benchmark for futex hash table", bench_futex_hash },
70 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
71 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
72 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
74 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
75 { "all", "Run all futex benchmarks", NULL },
79 #ifdef HAVE_EVENTFD_SUPPORT
80 static struct bench epoll_benchmarks[] = {
81 { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
82 { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
83 { "all", "Run all futex benchmarks", NULL },
86 #endif // HAVE_EVENTFD_SUPPORT
88 static struct bench internals_benchmarks[] = {
89 { "synthesize", "Benchmark perf event synthesis", bench_synthesize },
90 { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse },
91 { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id },
98 struct bench *benchmarks;
101 static struct collection collections[] = {
102 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
103 { "syscall", "System call benchmarks", syscall_benchmarks },
104 { "mem", "Memory access benchmarks", mem_benchmarks },
105 #ifdef HAVE_LIBNUMA_SUPPORT
106 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
108 {"futex", "Futex stressing benchmarks", futex_benchmarks },
109 #ifdef HAVE_EVENTFD_SUPPORT
110 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
112 { "internals", "Perf-internals benchmarks", internals_benchmarks },
113 { "all", "All benchmarks", NULL },
117 /* Iterate over all benchmark collections: */
118 #define for_each_collection(coll) \
119 for (coll = collections; coll->name; coll++)
121 /* Iterate over all benchmarks within a collection: */
122 #define for_each_bench(coll, bench) \
123 for (bench = coll->benchmarks; bench && bench->name; bench++)
125 static void dump_benchmarks(struct collection *coll)
129 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
131 for_each_bench(coll, bench)
132 printf("%14s: %s\n", bench->name, bench->summary);
137 static const char *bench_format_str;
139 /* Output/formatting style, exported to benchmark modules: */
140 int bench_format = BENCH_FORMAT_DEFAULT;
141 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 amount of times to repeat the run"),
149 static const char * const bench_usage[] = {
150 "perf bench [<common options>] <collection> <benchmark> [<options>]",
154 static void print_usage(void)
156 struct collection *coll;
160 for (i = 0; bench_usage[i]; i++)
161 printf("\t%s\n", bench_usage[i]);
164 printf(" # List of all available benchmark collections:\n\n");
166 for_each_collection(coll)
167 printf("%14s: %s\n", coll->name, coll->summary);
171 static int bench_str2int(const char *str)
174 return BENCH_FORMAT_DEFAULT;
176 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
177 return BENCH_FORMAT_DEFAULT;
178 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
179 return BENCH_FORMAT_SIMPLE;
181 return BENCH_FORMAT_UNKNOWN;
185 * Run a specific benchmark but first rename the running task's ->comm[]
186 * to something meaningful:
188 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
189 int argc, const char **argv)
195 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
200 scnprintf(name, size, "%s-%s", coll_name, bench_name);
202 prctl(PR_SET_NAME, name);
205 ret = fn(argc, argv);
212 static void run_collection(struct collection *coll)
221 * Preparing preset parameters for
222 * embedded, ordinary PC, HPC, etc...
225 for_each_bench(coll, bench) {
228 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
230 argv[1] = bench->name;
231 run_bench(coll->name, bench->name, bench->fn, 1, argv);
236 static void run_all_collections(void)
238 struct collection *coll;
240 for_each_collection(coll)
241 run_collection(coll);
244 int cmd_bench(int argc, const char **argv)
246 struct collection *coll;
249 /* Unbuffered output */
250 setvbuf(stdout, NULL, _IONBF, 0);
251 setlocale(LC_ALL, "");
254 /* No collection specified. */
259 argc = parse_options(argc, argv, bench_options, bench_usage,
260 PARSE_OPT_STOP_AT_NON_OPTION);
262 bench_format = bench_str2int(bench_format_str);
263 if (bench_format == BENCH_FORMAT_UNKNOWN) {
264 printf("Unknown format descriptor: '%s'\n", bench_format_str);
268 if (bench_repeat == 0) {
269 printf("Invalid repeat option: Must specify a positive value\n");
278 if (!strcmp(argv[0], "all")) {
279 run_all_collections();
283 for_each_collection(coll) {
286 if (strcmp(coll->name, argv[0]))
290 /* No bench specified. */
291 dump_benchmarks(coll);
295 if (!strcmp(argv[1], "all")) {
296 run_collection(coll);
300 for_each_bench(coll, bench) {
301 if (strcmp(bench->name, argv[1]))
304 if (bench_format == BENCH_FORMAT_DEFAULT)
305 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
306 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
310 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
311 dump_benchmarks(coll);
315 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
320 printf("Unknown collection: '%s'\n", argv[0]);