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