blob: 4f176039fc8f46f1903189340b554b8b0f9fb93c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Hitoshi Mitake629cc352009-11-05 09:31:34 +09002/*
Hitoshi Mitake629cc352009-11-05 09:31:34 +09003 * builtin-bench.c
4 *
Ingo Molnar41579222013-10-23 14:37:56 +02005 * General benchmarking collections provided by perf
Hitoshi Mitake629cc352009-11-05 09:31:34 +09006 *
7 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
Hitoshi Mitake629cc352009-11-05 09:31:34 +09008 */
9
10/*
Ingo Molnar41579222013-10-23 14:37:56 +020011 * Available benchmark collection list:
Hitoshi Mitake629cc352009-11-05 09:31:34 +090012 *
Ingo Molnar41579222013-10-23 14:37:56 +020013 * sched ... scheduler and IPC performance
Davidlohr Buesoc2a08202019-03-08 10:17:47 -080014 * syscall ... System call performance
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090015 * mem ... memory access performance
Ingo Molnar41579222013-10-23 14:37:56 +020016 * numa ... NUMA scheduling and MM performance
Davidlohr Buesoa0439712013-12-14 20:31:55 -080017 * futex ... Futex performance
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -080018 * epoll ... Event poll performance
Hitoshi Mitake629cc352009-11-05 09:31:34 +090019 */
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060020#include <subcmd/parse-options.h>
Hitoshi Mitake629cc352009-11-05 09:31:34 +090021#include "builtin.h"
22#include "bench/bench.h"
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Ingo Molnar41579222013-10-23 14:37:56 +020027#include <sys/prctl.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030028#include <linux/zalloc.h>
Hitoshi Mitake629cc352009-11-05 09:31:34 +090029
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -030030typedef int (*bench_fn_t)(int argc, const char **argv);
Ingo Molnar41579222013-10-23 14:37:56 +020031
32struct bench {
33 const char *name;
34 const char *summary;
35 bench_fn_t fn;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090036};
37
Ingo Molnar89fe8082013-09-30 12:07:11 +020038#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020039static struct bench numa_benchmarks[] = {
40 { "mem", "Benchmark for NUMA workloads", bench_numa },
Ingo Molnaraa254af2015-10-19 10:04:30 +020041 { "all", "Run all NUMA benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020042 { NULL, NULL, NULL }
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010043};
Peter Hurley79d824e2013-01-27 20:51:22 -050044#endif
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010045
Ingo Molnar41579222013-10-23 14:37:56 +020046static 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 },
Ingo Molnaraa254af2015-10-19 10:04:30 +020049 { "all", "Run all scheduler benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020050 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090051};
52
Davidlohr Buesoc2a08202019-03-08 10:17:47 -080053static 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
Ingo Molnar41579222013-10-23 14:37:56 +020059static struct bench mem_benchmarks[] = {
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020060 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
61 { "memset", "Benchmark for memset() functions", bench_mem_memset },
Ian Rogers7c43b0c2020-07-29 15:00:34 -070062 { "find_bit", "Benchmark for find_bit() functions", bench_mem_find_bit },
Ingo Molnaraa254af2015-10-19 10:04:30 +020063 { "all", "Run all memory access benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020064 { NULL, NULL, NULL }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090065};
66
Davidlohr Buesoa0439712013-12-14 20:31:55 -080067static struct bench futex_benchmarks[] = {
68 { "hash", "Benchmark for futex hash table", bench_futex_hash },
Davidlohr Bueso27db7832013-12-14 20:31:56 -080069 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
Davidlohr Buesod65817b2015-05-08 11:37:59 -070070 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080071 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070072 /* pi-futexes */
73 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
Ingo Molnaraa254af2015-10-19 10:04:30 +020074 { "all", "Run all futex benchmarks", NULL },
Davidlohr Buesoa0439712013-12-14 20:31:55 -080075 { NULL, NULL, NULL }
76};
77
Arnaldo Carvalho de Meloba35fe92020-05-20 12:21:07 -030078#ifdef HAVE_EVENTFD_SUPPORT
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -080079static struct bench epoll_benchmarks[] = {
80 { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
Davidlohr Bueso231457e2018-11-06 07:22:26 -080081 { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -080082 { "all", "Run all futex benchmarks", NULL },
83 { NULL, NULL, NULL }
84};
Arnaldo Carvalho de Meloba35fe92020-05-20 12:21:07 -030085#endif // HAVE_EVENTFD_SUPPORT
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -080086
Ian Rogers2a4b5162020-04-02 08:43:53 -070087static struct bench internals_benchmarks[] = {
88 { "synthesize", "Benchmark perf event synthesis", bench_synthesize },
Ian Rogers51876bd2020-05-01 15:13:13 -070089 { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse },
Ian Rogers2a4b5162020-04-02 08:43:53 -070090 { NULL, NULL, NULL }
91};
92
Ingo Molnar41579222013-10-23 14:37:56 +020093struct collection {
94 const char *name;
95 const char *summary;
96 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090097};
98
Ingo Molnar41579222013-10-23 14:37:56 +020099static struct collection collections[] = {
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800100 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
Davidlohr Buesoc2a08202019-03-08 10:17:47 -0800101 { "syscall", "System call benchmarks", syscall_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +0200102 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +0200103#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +0200104 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -0500105#endif
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800106 {"futex", "Futex stressing benchmarks", futex_benchmarks },
Arnaldo Carvalho de Meloba35fe92020-05-20 12:21:07 -0300107#ifdef HAVE_EVENTFD_SUPPORT
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -0800108 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
109#endif
Ian Rogers2a4b5162020-04-02 08:43:53 -0700110 { "internals", "Perf-internals benchmarks", internals_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +0200111 { "all", "All benchmarks", NULL },
112 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900113};
114
Ingo Molnar41579222013-10-23 14:37:56 +0200115/* Iterate over all benchmark collections: */
116#define for_each_collection(coll) \
117 for (coll = collections; coll->name; coll++)
118
119/* Iterate over all benchmarks within a collection: */
120#define for_each_bench(coll, bench) \
Patrick Palka6eeefcc2014-03-12 18:40:51 -0400121 for (bench = coll->benchmarks; bench && bench->name; bench++)
Ingo Molnar41579222013-10-23 14:37:56 +0200122
123static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900124{
Ingo Molnar41579222013-10-23 14:37:56 +0200125 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900126
Ingo Molnar41579222013-10-23 14:37:56 +0200127 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900128
Ingo Molnar41579222013-10-23 14:37:56 +0200129 for_each_bench(coll, bench)
130 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900131
132 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900133}
134
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300135static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +0200136
137/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900138int bench_format = BENCH_FORMAT_DEFAULT;
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700139unsigned int bench_repeat = 10; /* default number of times to repeat the run */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900140
141static const struct option bench_options[] = {
Ingo Molnar7a46a8f2015-10-19 10:04:22 +0200142 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700143 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900144 OPT_END()
145};
146
147static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200148 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900149 NULL
150};
151
152static void print_usage(void)
153{
Ingo Molnar41579222013-10-23 14:37:56 +0200154 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900155 int i;
156
157 printf("Usage: \n");
158 for (i = 0; bench_usage[i]; i++)
159 printf("\t%s\n", bench_usage[i]);
160 printf("\n");
161
Ingo Molnar41579222013-10-23 14:37:56 +0200162 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900163
Ingo Molnar41579222013-10-23 14:37:56 +0200164 for_each_collection(coll)
165 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900166 printf("\n");
167}
168
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300169static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900170{
171 if (!str)
172 return BENCH_FORMAT_DEFAULT;
173
174 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
175 return BENCH_FORMAT_DEFAULT;
176 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
177 return BENCH_FORMAT_SIMPLE;
178
179 return BENCH_FORMAT_UNKNOWN;
180}
181
Ingo Molnar41579222013-10-23 14:37:56 +0200182/*
183 * Run a specific benchmark but first rename the running task's ->comm[]
184 * to something meaningful:
185 */
186static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300187 int argc, const char **argv)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900188{
Ingo Molnar41579222013-10-23 14:37:56 +0200189 int size;
190 char *name;
191 int ret;
192
193 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
194
195 name = zalloc(size);
196 BUG_ON(!name);
197
198 scnprintf(name, size, "%s-%s", coll_name, bench_name);
199
200 prctl(PR_SET_NAME, name);
201 argv[0] = name;
202
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300203 ret = fn(argc, argv);
Ingo Molnar41579222013-10-23 14:37:56 +0200204
205 free(name);
206
207 return ret;
208}
209
210static void run_collection(struct collection *coll)
211{
212 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900213 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900214
215 argv[1] = NULL;
216 /*
217 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200218 *
219 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900220 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200221 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900222 */
Ingo Molnar41579222013-10-23 14:37:56 +0200223 for_each_bench(coll, bench) {
224 if (!bench->fn)
225 break;
226 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900227 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900228
Ingo Molnar41579222013-10-23 14:37:56 +0200229 argv[1] = bench->name;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300230 run_bench(coll->name, bench->name, bench->fn, 1, argv);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900231 printf("\n");
232 }
233}
234
Ingo Molnar41579222013-10-23 14:37:56 +0200235static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900236{
Ingo Molnar41579222013-10-23 14:37:56 +0200237 struct collection *coll;
238
239 for_each_collection(coll)
240 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900241}
242
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300243int cmd_bench(int argc, const char **argv)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900244{
Ingo Molnar41579222013-10-23 14:37:56 +0200245 struct collection *coll;
246 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900247
248 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200249 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900250 print_usage();
251 goto end;
252 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900253
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900254 argc = parse_options(argc, argv, bench_options, bench_usage,
255 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900256
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900257 bench_format = bench_str2int(bench_format_str);
258 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200259 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900260 goto end;
261 }
262
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700263 if (bench_repeat == 0) {
264 printf("Invalid repeat option: Must specify a positive value\n");
265 goto end;
266 }
267
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900268 if (argc < 1) {
269 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900270 goto end;
271 }
272
Hitoshi Mitake20442792009-12-13 17:01:59 +0900273 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200274 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900275 goto end;
276 }
277
Ingo Molnar41579222013-10-23 14:37:56 +0200278 for_each_collection(coll) {
279 struct bench *bench;
280
281 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900282 continue;
283
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900284 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200285 /* No bench specified. */
286 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900287 goto end;
288 }
289
Hitoshi Mitake20442792009-12-13 17:01:59 +0900290 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200291 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900292 goto end;
293 }
294
Ingo Molnar41579222013-10-23 14:37:56 +0200295 for_each_bench(coll, bench) {
296 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900297 continue;
298
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900299 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200300 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900301 fflush(stdout);
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300302 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900303 goto end;
304 }
305
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900306 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200307 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900308 goto end;
309 }
310
Ingo Molnar41579222013-10-23 14:37:56 +0200311 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
312 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900313 goto end;
314 }
315
Ingo Molnar41579222013-10-23 14:37:56 +0200316 printf("Unknown collection: '%s'\n", argv[0]);
317 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900318
319end:
Ingo Molnar41579222013-10-23 14:37:56 +0200320 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900321}