blob: 62a7b7420a448517a2474c816ec3f5bb5a88800e [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 },
Namhyung Kim0bf02a02020-10-12 16:02:09 +090090 { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id },
Ian Rogers2a4b5162020-04-02 08:43:53 -070091 { NULL, NULL, NULL }
92};
93
Ingo Molnar41579222013-10-23 14:37:56 +020094struct collection {
95 const char *name;
96 const char *summary;
97 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090098};
99
Ingo Molnar41579222013-10-23 14:37:56 +0200100static struct collection collections[] = {
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800101 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
Davidlohr Buesoc2a08202019-03-08 10:17:47 -0800102 { "syscall", "System call benchmarks", syscall_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +0200103 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +0200104#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +0200105 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -0500106#endif
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800107 {"futex", "Futex stressing benchmarks", futex_benchmarks },
Arnaldo Carvalho de Meloba35fe92020-05-20 12:21:07 -0300108#ifdef HAVE_EVENTFD_SUPPORT
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -0800109 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
110#endif
Ian Rogers2a4b5162020-04-02 08:43:53 -0700111 { "internals", "Perf-internals benchmarks", internals_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +0200112 { "all", "All benchmarks", NULL },
113 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900114};
115
Ingo Molnar41579222013-10-23 14:37:56 +0200116/* 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) \
Patrick Palka6eeefcc2014-03-12 18:40:51 -0400122 for (bench = coll->benchmarks; bench && bench->name; bench++)
Ingo Molnar41579222013-10-23 14:37:56 +0200123
124static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900125{
Ingo Molnar41579222013-10-23 14:37:56 +0200126 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900127
Ingo Molnar41579222013-10-23 14:37:56 +0200128 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900129
Ingo Molnar41579222013-10-23 14:37:56 +0200130 for_each_bench(coll, bench)
131 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900132
133 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900134}
135
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300136static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +0200137
138/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900139int bench_format = BENCH_FORMAT_DEFAULT;
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700140unsigned int bench_repeat = 10; /* default number of times to repeat the run */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900141
142static const struct option bench_options[] = {
Ingo Molnar7a46a8f2015-10-19 10:04:22 +0200143 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700144 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900145 OPT_END()
146};
147
148static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200149 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900150 NULL
151};
152
153static void print_usage(void)
154{
Ingo Molnar41579222013-10-23 14:37:56 +0200155 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900156 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
Ingo Molnar41579222013-10-23 14:37:56 +0200163 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900164
Ingo Molnar41579222013-10-23 14:37:56 +0200165 for_each_collection(coll)
166 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900167 printf("\n");
168}
169
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300170static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900171{
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
Ingo Molnar41579222013-10-23 14:37:56 +0200183/*
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,
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300188 int argc, const char **argv)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900189{
Ingo Molnar41579222013-10-23 14:37:56 +0200190 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
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300204 ret = fn(argc, argv);
Ingo Molnar41579222013-10-23 14:37:56 +0200205
206 free(name);
207
208 return ret;
209}
210
211static void run_collection(struct collection *coll)
212{
213 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900214 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900215
216 argv[1] = NULL;
217 /*
218 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200219 *
220 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900221 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200222 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900223 */
Ingo Molnar41579222013-10-23 14:37:56 +0200224 for_each_bench(coll, bench) {
225 if (!bench->fn)
226 break;
227 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900228 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900229
Ingo Molnar41579222013-10-23 14:37:56 +0200230 argv[1] = bench->name;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300231 run_bench(coll->name, bench->name, bench->fn, 1, argv);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900232 printf("\n");
233 }
234}
235
Ingo Molnar41579222013-10-23 14:37:56 +0200236static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900237{
Ingo Molnar41579222013-10-23 14:37:56 +0200238 struct collection *coll;
239
240 for_each_collection(coll)
241 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900242}
243
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300244int cmd_bench(int argc, const char **argv)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900245{
Ingo Molnar41579222013-10-23 14:37:56 +0200246 struct collection *coll;
247 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900248
249 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200250 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900251 print_usage();
252 goto end;
253 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900254
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900255 argc = parse_options(argc, argv, bench_options, bench_usage,
256 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900257
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900258 bench_format = bench_str2int(bench_format_str);
259 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200260 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900261 goto end;
262 }
263
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700264 if (bench_repeat == 0) {
265 printf("Invalid repeat option: Must specify a positive value\n");
266 goto end;
267 }
268
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900269 if (argc < 1) {
270 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900271 goto end;
272 }
273
Hitoshi Mitake20442792009-12-13 17:01:59 +0900274 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200275 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900276 goto end;
277 }
278
Ingo Molnar41579222013-10-23 14:37:56 +0200279 for_each_collection(coll) {
280 struct bench *bench;
281
282 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900283 continue;
284
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900285 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200286 /* No bench specified. */
287 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900288 goto end;
289 }
290
Hitoshi Mitake20442792009-12-13 17:01:59 +0900291 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200292 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900293 goto end;
294 }
295
Ingo Molnar41579222013-10-23 14:37:56 +0200296 for_each_bench(coll, bench) {
297 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900298 continue;
299
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900300 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200301 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900302 fflush(stdout);
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300303 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900304 goto end;
305 }
306
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900307 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200308 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900309 goto end;
310 }
311
Ingo Molnar41579222013-10-23 14:37:56 +0200312 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
313 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900314 goto end;
315 }
316
Ingo Molnar41579222013-10-23 14:37:56 +0200317 printf("Unknown collection: '%s'\n", argv[0]);
318 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900319
320end:
Ingo Molnar41579222013-10-23 14:37:56 +0200321 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900322}