]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-bench.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
[mirror_ubuntu-hirsute-kernel.git] / tools / perf / builtin-bench.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
629cc356 2/*
629cc356
HM
3 * builtin-bench.c
4 *
4157922a 5 * General benchmarking collections provided by perf
629cc356
HM
6 *
7 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
629cc356
HM
8 */
9
10/*
4157922a 11 * Available benchmark collection list:
629cc356 12 *
4157922a 13 * sched ... scheduler and IPC performance
c2a08203 14 * syscall ... System call performance
827f3b49 15 * mem ... memory access performance
4157922a 16 * numa ... NUMA scheduling and MM performance
a0439711 17 * futex ... Futex performance
121dd9ea 18 * epoll ... Event poll performance
629cc356 19 */
4b6ab94e 20#include <subcmd/parse-options.h>
629cc356
HM
21#include "builtin.h"
22#include "bench/bench.h"
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
4157922a 27#include <sys/prctl.h>
7f7c536f 28#include <linux/zalloc.h>
629cc356 29
b0ad8ea6 30typedef int (*bench_fn_t)(int argc, const char **argv);
4157922a
IM
31
32struct bench {
33 const char *name;
34 const char *summary;
35 bench_fn_t fn;
629cc356
HM
36};
37
89fe808a 38#ifdef HAVE_LIBNUMA_SUPPORT
4157922a
IM
39static struct bench numa_benchmarks[] = {
40 { "mem", "Benchmark for NUMA workloads", bench_numa },
aa254af2 41 { "all", "Run all NUMA benchmarks", NULL },
4157922a 42 { NULL, NULL, NULL }
1c13f3c9 43};
79d824e3 44#endif
1c13f3c9 45
4157922a
IM
46static struct bench sched_benchmarks[] = {
47 { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
48 { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
aa254af2 49 { "all", "Run all scheduler benchmarks", NULL },
4157922a 50 { NULL, NULL, NULL }
629cc356
HM
51};
52
c2a08203
DB
53static struct bench syscall_benchmarks[] = {
54 { "basic", "Benchmark for basic getppid(2) calls", bench_syscall_basic },
55 { "all", "Run all syscall benchmarks", NULL },
56 { NULL, NULL, NULL },
57};
58
4157922a 59static struct bench mem_benchmarks[] = {
13b1fdce
IM
60 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
61 { "memset", "Benchmark for memset() functions", bench_mem_memset },
7c43b0c1 62 { "find_bit", "Benchmark for find_bit() functions", bench_mem_find_bit },
aa254af2 63 { "all", "Run all memory access benchmarks", NULL },
4157922a 64 { NULL, NULL, NULL }
827f3b49
HM
65};
66
a0439711
DB
67static struct bench futex_benchmarks[] = {
68 { "hash", "Benchmark for futex hash table", bench_futex_hash },
27db7830 69 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
d65817b4 70 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
0fb298cf 71 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
d2f3f5d2
DB
72 /* pi-futexes */
73 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
aa254af2 74 { "all", "Run all futex benchmarks", NULL },
a0439711
DB
75 { NULL, NULL, NULL }
76};
77
ba35fe93 78#ifdef HAVE_EVENTFD_SUPPORT
121dd9ea
DB
79static struct bench epoll_benchmarks[] = {
80 { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
231457ec 81 { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
121dd9ea
DB
82 { "all", "Run all futex benchmarks", NULL },
83 { NULL, NULL, NULL }
84};
ba35fe93 85#endif // HAVE_EVENTFD_SUPPORT
121dd9ea 86
2a4b5166
IR
87static struct bench internals_benchmarks[] = {
88 { "synthesize", "Benchmark perf event synthesis", bench_synthesize },
51876bd4 89 { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse },
0bf02a0d 90 { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id },
2a4b5166
IR
91 { NULL, NULL, NULL }
92};
93
4157922a
IM
94struct collection {
95 const char *name;
96 const char *summary;
97 struct bench *benchmarks;
629cc356
HM
98};
99
4157922a 100static struct collection collections[] = {
a0439711 101 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
c2a08203 102 { "syscall", "System call benchmarks", syscall_benchmarks },
4157922a 103 { "mem", "Memory access benchmarks", mem_benchmarks },
89fe808a 104#ifdef HAVE_LIBNUMA_SUPPORT
4157922a 105 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
79d824e3 106#endif
a0439711 107 {"futex", "Futex stressing benchmarks", futex_benchmarks },
ba35fe93 108#ifdef HAVE_EVENTFD_SUPPORT
121dd9ea
DB
109 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
110#endif
2a4b5166 111 { "internals", "Perf-internals benchmarks", internals_benchmarks },
4157922a
IM
112 { "all", "All benchmarks", NULL },
113 { NULL, NULL, NULL }
629cc356
HM
114};
115
4157922a
IM
116/* Iterate over all benchmark collections: */
117#define for_each_collection(coll) \
118 for (coll = collections; coll->name; coll++)
119
120/* Iterate over all benchmarks within a collection: */
121#define for_each_bench(coll, bench) \
6eeefccd 122 for (bench = coll->benchmarks; bench && bench->name; bench++)
4157922a
IM
123
124static void dump_benchmarks(struct collection *coll)
629cc356 125{
4157922a 126 struct bench *bench;
629cc356 127
4157922a 128 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
629cc356 129
4157922a
IM
130 for_each_bench(coll, bench)
131 printf("%14s: %s\n", bench->name, bench->summary);
629cc356
HM
132
133 printf("\n");
629cc356
HM
134}
135
edb7c60e 136static const char *bench_format_str;
4157922a
IM
137
138/* Output/formatting style, exported to benchmark modules: */
386d7e9e 139int bench_format = BENCH_FORMAT_DEFAULT;
b6f0629a 140unsigned int bench_repeat = 10; /* default number of times to repeat the run */
386d7e9e
HM
141
142static const struct option bench_options[] = {
7a46a8fd 143 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
b6f0629a 144 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
386d7e9e
HM
145 OPT_END()
146};
147
148static const char * const bench_usage[] = {
4157922a 149 "perf bench [<common options>] <collection> <benchmark> [<options>]",
386d7e9e
HM
150 NULL
151};
152
153static void print_usage(void)
154{
4157922a 155 struct collection *coll;
386d7e9e
HM
156 int i;
157
158 printf("Usage: \n");
159 for (i = 0; bench_usage[i]; i++)
160 printf("\t%s\n", bench_usage[i]);
161 printf("\n");
162
4157922a 163 printf(" # List of all available benchmark collections:\n\n");
386d7e9e 164
4157922a
IM
165 for_each_collection(coll)
166 printf("%14s: %s\n", coll->name, coll->summary);
386d7e9e
HM
167 printf("\n");
168}
169
edb7c60e 170static int bench_str2int(const char *str)
386d7e9e
HM
171{
172 if (!str)
173 return BENCH_FORMAT_DEFAULT;
174
175 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
176 return BENCH_FORMAT_DEFAULT;
177 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
178 return BENCH_FORMAT_SIMPLE;
179
180 return BENCH_FORMAT_UNKNOWN;
181}
182
4157922a
IM
183/*
184 * Run a specific benchmark but first rename the running task's ->comm[]
185 * to something meaningful:
186 */
187static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
b0ad8ea6 188 int argc, const char **argv)
2044279d 189{
4157922a
IM
190 int size;
191 char *name;
192 int ret;
193
194 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
195
196 name = zalloc(size);
197 BUG_ON(!name);
198
199 scnprintf(name, size, "%s-%s", coll_name, bench_name);
200
201 prctl(PR_SET_NAME, name);
202 argv[0] = name;
203
b0ad8ea6 204 ret = fn(argc, argv);
4157922a
IM
205
206 free(name);
207
208 return ret;
209}
210
211static void run_collection(struct collection *coll)
212{
213 struct bench *bench;
2044279d 214 const char *argv[2];
2044279d
HM
215
216 argv[1] = NULL;
217 /*
218 * TODO:
4157922a
IM
219 *
220 * Preparing preset parameters for
2044279d 221 * embedded, ordinary PC, HPC, etc...
4157922a 222 * would be helpful.
2044279d 223 */
4157922a
IM
224 for_each_bench(coll, bench) {
225 if (!bench->fn)
226 break;
227 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
9b494ea2 228 fflush(stdout);
2044279d 229
4157922a 230 argv[1] = bench->name;
b0ad8ea6 231 run_bench(coll->name, bench->name, bench->fn, 1, argv);
2044279d
HM
232 printf("\n");
233 }
234}
235
4157922a 236static void run_all_collections(void)
2044279d 237{
4157922a
IM
238 struct collection *coll;
239
240 for_each_collection(coll)
241 run_collection(coll);
2044279d
HM
242}
243
b0ad8ea6 244int cmd_bench(int argc, const char **argv)
629cc356 245{
4157922a
IM
246 struct collection *coll;
247 int ret = 0;
629cc356
HM
248
249 if (argc < 2) {
4157922a 250 /* No collection specified. */
386d7e9e
HM
251 print_usage();
252 goto end;
253 }
629cc356 254
386d7e9e
HM
255 argc = parse_options(argc, argv, bench_options, bench_usage,
256 PARSE_OPT_STOP_AT_NON_OPTION);
257
258 bench_format = bench_str2int(bench_format_str);
259 if (bench_format == BENCH_FORMAT_UNKNOWN) {
4157922a 260 printf("Unknown format descriptor: '%s'\n", bench_format_str);
386d7e9e
HM
261 goto end;
262 }
629cc356 263
b6f0629a
DB
264 if (bench_repeat == 0) {
265 printf("Invalid repeat option: Must specify a positive value\n");
266 goto end;
267 }
268
386d7e9e
HM
269 if (argc < 1) {
270 print_usage();
629cc356
HM
271 goto end;
272 }
273
2044279d 274 if (!strcmp(argv[0], "all")) {
4157922a 275 run_all_collections();
2044279d
HM
276 goto end;
277 }
278
4157922a
IM
279 for_each_collection(coll) {
280 struct bench *bench;
281
282 if (strcmp(coll->name, argv[0]))
629cc356
HM
283 continue;
284
386d7e9e 285 if (argc < 2) {
4157922a
IM
286 /* No bench specified. */
287 dump_benchmarks(coll);
629cc356
HM
288 goto end;
289 }
290
2044279d 291 if (!strcmp(argv[1], "all")) {
4157922a 292 run_collection(coll);
2044279d
HM
293 goto end;
294 }
295
4157922a
IM
296 for_each_bench(coll, bench) {
297 if (strcmp(bench->name, argv[1]))
629cc356
HM
298 continue;
299
79e295d4 300 if (bench_format == BENCH_FORMAT_DEFAULT)
4157922a 301 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
9b494ea2 302 fflush(stdout);
b0ad8ea6 303 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
629cc356
HM
304 goto end;
305 }
306
386d7e9e 307 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
4157922a 308 dump_benchmarks(coll);
629cc356
HM
309 goto end;
310 }
311
4157922a
IM
312 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
313 ret = 1;
629cc356
HM
314 goto end;
315 }
316
4157922a
IM
317 printf("Unknown collection: '%s'\n", argv[0]);
318 ret = 1;
629cc356
HM
319
320end:
4157922a 321 return ret;
629cc356 322}