blob: b8e7c38ef221898a103549d82078dbd755145ec4 [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
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090014 * mem ... memory access performance
Ingo Molnar41579222013-10-23 14:37:56 +020015 * numa ... NUMA scheduling and MM performance
Davidlohr Buesoa0439712013-12-14 20:31:55 -080016 * futex ... Futex performance
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -080017 * epoll ... Event poll performance
Hitoshi Mitake629cc352009-11-05 09:31:34 +090018 */
Hitoshi Mitake629cc352009-11-05 09:31:34 +090019#include "perf.h"
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
Ingo Molnar41579222013-10-23 14:37:56 +020053static struct bench mem_benchmarks[] = {
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020054 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
55 { "memset", "Benchmark for memset() functions", bench_mem_memset },
Ingo Molnaraa254af2015-10-19 10:04:30 +020056 { "all", "Run all memory access benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020057 { NULL, NULL, NULL }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090058};
59
Davidlohr Buesoa0439712013-12-14 20:31:55 -080060static struct bench futex_benchmarks[] = {
61 { "hash", "Benchmark for futex hash table", bench_futex_hash },
Davidlohr Bueso27db7832013-12-14 20:31:56 -080062 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
Davidlohr Buesod65817b2015-05-08 11:37:59 -070063 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080064 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070065 /* pi-futexes */
66 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
Ingo Molnaraa254af2015-10-19 10:04:30 +020067 { "all", "Run all futex benchmarks", NULL },
Davidlohr Buesoa0439712013-12-14 20:31:55 -080068 { NULL, NULL, NULL }
69};
70
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -080071#ifdef HAVE_EVENTFD
72static struct bench epoll_benchmarks[] = {
73 { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
Davidlohr Bueso231457e2018-11-06 07:22:26 -080074 { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -080075 { "all", "Run all futex benchmarks", NULL },
76 { NULL, NULL, NULL }
77};
78#endif // HAVE_EVENTFD
79
Ingo Molnar41579222013-10-23 14:37:56 +020080struct collection {
81 const char *name;
82 const char *summary;
83 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090084};
85
Ingo Molnar41579222013-10-23 14:37:56 +020086static struct collection collections[] = {
Davidlohr Buesoa0439712013-12-14 20:31:55 -080087 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020088 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +020089#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020090 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -050091#endif
Davidlohr Buesoa0439712013-12-14 20:31:55 -080092 {"futex", "Futex stressing benchmarks", futex_benchmarks },
Davidlohr Bueso121dd9e2018-11-06 07:22:25 -080093#ifdef HAVE_EVENTFD
94 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
95#endif
Ingo Molnar41579222013-10-23 14:37:56 +020096 { "all", "All benchmarks", NULL },
97 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090098};
99
Ingo Molnar41579222013-10-23 14:37:56 +0200100/* Iterate over all benchmark collections: */
101#define for_each_collection(coll) \
102 for (coll = collections; coll->name; coll++)
103
104/* Iterate over all benchmarks within a collection: */
105#define for_each_bench(coll, bench) \
Patrick Palka6eeefcc2014-03-12 18:40:51 -0400106 for (bench = coll->benchmarks; bench && bench->name; bench++)
Ingo Molnar41579222013-10-23 14:37:56 +0200107
108static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900109{
Ingo Molnar41579222013-10-23 14:37:56 +0200110 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900111
Ingo Molnar41579222013-10-23 14:37:56 +0200112 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900113
Ingo Molnar41579222013-10-23 14:37:56 +0200114 for_each_bench(coll, bench)
115 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900116
117 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900118}
119
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300120static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +0200121
122/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900123int bench_format = BENCH_FORMAT_DEFAULT;
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700124unsigned int bench_repeat = 10; /* default number of times to repeat the run */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900125
126static const struct option bench_options[] = {
Ingo Molnar7a46a8f2015-10-19 10:04:22 +0200127 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700128 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900129 OPT_END()
130};
131
132static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200133 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900134 NULL
135};
136
137static void print_usage(void)
138{
Ingo Molnar41579222013-10-23 14:37:56 +0200139 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900140 int i;
141
142 printf("Usage: \n");
143 for (i = 0; bench_usage[i]; i++)
144 printf("\t%s\n", bench_usage[i]);
145 printf("\n");
146
Ingo Molnar41579222013-10-23 14:37:56 +0200147 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900148
Ingo Molnar41579222013-10-23 14:37:56 +0200149 for_each_collection(coll)
150 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900151 printf("\n");
152}
153
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300154static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900155{
156 if (!str)
157 return BENCH_FORMAT_DEFAULT;
158
159 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
160 return BENCH_FORMAT_DEFAULT;
161 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
162 return BENCH_FORMAT_SIMPLE;
163
164 return BENCH_FORMAT_UNKNOWN;
165}
166
Ingo Molnar41579222013-10-23 14:37:56 +0200167/*
168 * Run a specific benchmark but first rename the running task's ->comm[]
169 * to something meaningful:
170 */
171static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300172 int argc, const char **argv)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900173{
Ingo Molnar41579222013-10-23 14:37:56 +0200174 int size;
175 char *name;
176 int ret;
177
178 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
179
180 name = zalloc(size);
181 BUG_ON(!name);
182
183 scnprintf(name, size, "%s-%s", coll_name, bench_name);
184
185 prctl(PR_SET_NAME, name);
186 argv[0] = name;
187
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300188 ret = fn(argc, argv);
Ingo Molnar41579222013-10-23 14:37:56 +0200189
190 free(name);
191
192 return ret;
193}
194
195static void run_collection(struct collection *coll)
196{
197 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900198 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900199
200 argv[1] = NULL;
201 /*
202 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200203 *
204 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900205 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200206 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900207 */
Ingo Molnar41579222013-10-23 14:37:56 +0200208 for_each_bench(coll, bench) {
209 if (!bench->fn)
210 break;
211 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900212 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900213
Ingo Molnar41579222013-10-23 14:37:56 +0200214 argv[1] = bench->name;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300215 run_bench(coll->name, bench->name, bench->fn, 1, argv);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900216 printf("\n");
217 }
218}
219
Ingo Molnar41579222013-10-23 14:37:56 +0200220static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900221{
Ingo Molnar41579222013-10-23 14:37:56 +0200222 struct collection *coll;
223
224 for_each_collection(coll)
225 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900226}
227
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300228int cmd_bench(int argc, const char **argv)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900229{
Ingo Molnar41579222013-10-23 14:37:56 +0200230 struct collection *coll;
231 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900232
233 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200234 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900235 print_usage();
236 goto end;
237 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900238
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900239 argc = parse_options(argc, argv, bench_options, bench_usage,
240 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900241
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900242 bench_format = bench_str2int(bench_format_str);
243 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200244 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900245 goto end;
246 }
247
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700248 if (bench_repeat == 0) {
249 printf("Invalid repeat option: Must specify a positive value\n");
250 goto end;
251 }
252
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900253 if (argc < 1) {
254 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900255 goto end;
256 }
257
Hitoshi Mitake20442792009-12-13 17:01:59 +0900258 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200259 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900260 goto end;
261 }
262
Ingo Molnar41579222013-10-23 14:37:56 +0200263 for_each_collection(coll) {
264 struct bench *bench;
265
266 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900267 continue;
268
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900269 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200270 /* No bench specified. */
271 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900272 goto end;
273 }
274
Hitoshi Mitake20442792009-12-13 17:01:59 +0900275 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200276 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900277 goto end;
278 }
279
Ingo Molnar41579222013-10-23 14:37:56 +0200280 for_each_bench(coll, bench) {
281 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900282 continue;
283
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900284 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200285 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900286 fflush(stdout);
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300287 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900288 goto end;
289 }
290
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900291 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200292 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900293 goto end;
294 }
295
Ingo Molnar41579222013-10-23 14:37:56 +0200296 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
297 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900298 goto end;
299 }
300
Ingo Molnar41579222013-10-23 14:37:56 +0200301 printf("Unknown collection: '%s'\n", argv[0]);
302 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900303
304end:
Ingo Molnar41579222013-10-23 14:37:56 +0200305 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900306}