blob: 17a6bcd01aa678b70deb807de80cfa3db2f69875 [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
Hitoshi Mitake629cc352009-11-05 09:31:34 +090017 */
Hitoshi Mitake629cc352009-11-05 09:31:34 +090018#include "perf.h"
19#include "util/util.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>
Hitoshi Mitake629cc352009-11-05 09:31:34 +090028
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -030029typedef int (*bench_fn_t)(int argc, const char **argv);
Ingo Molnar41579222013-10-23 14:37:56 +020030
31struct bench {
32 const char *name;
33 const char *summary;
34 bench_fn_t fn;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090035};
36
Ingo Molnar89fe8082013-09-30 12:07:11 +020037#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020038static struct bench numa_benchmarks[] = {
39 { "mem", "Benchmark for NUMA workloads", bench_numa },
Ingo Molnaraa254af2015-10-19 10:04:30 +020040 { "all", "Run all NUMA benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020041 { NULL, NULL, NULL }
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010042};
Peter Hurley79d824e2013-01-27 20:51:22 -050043#endif
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010044
Ingo Molnar41579222013-10-23 14:37:56 +020045static 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 },
Ingo Molnaraa254af2015-10-19 10:04:30 +020048 { "all", "Run all scheduler benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020049 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090050};
51
Ingo Molnar41579222013-10-23 14:37:56 +020052static struct bench mem_benchmarks[] = {
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020053 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
54 { "memset", "Benchmark for memset() functions", bench_mem_memset },
Ingo Molnaraa254af2015-10-19 10:04:30 +020055 { "all", "Run all memory access benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020056 { NULL, NULL, NULL }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090057};
58
Davidlohr Buesoa0439712013-12-14 20:31:55 -080059static struct bench futex_benchmarks[] = {
60 { "hash", "Benchmark for futex hash table", bench_futex_hash },
Davidlohr Bueso27db7832013-12-14 20:31:56 -080061 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
Davidlohr Buesod65817b2015-05-08 11:37:59 -070062 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080063 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070064 /* pi-futexes */
65 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
Ingo Molnaraa254af2015-10-19 10:04:30 +020066 { "all", "Run all futex benchmarks", NULL },
Davidlohr Buesoa0439712013-12-14 20:31:55 -080067 { NULL, NULL, NULL }
68};
69
Ingo Molnar41579222013-10-23 14:37:56 +020070struct collection {
71 const char *name;
72 const char *summary;
73 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090074};
75
Ingo Molnar41579222013-10-23 14:37:56 +020076static struct collection collections[] = {
Davidlohr Buesoa0439712013-12-14 20:31:55 -080077 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020078 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +020079#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020080 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -050081#endif
Davidlohr Buesoa0439712013-12-14 20:31:55 -080082 {"futex", "Futex stressing benchmarks", futex_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020083 { "all", "All benchmarks", NULL },
84 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090085};
86
Ingo Molnar41579222013-10-23 14:37:56 +020087/* Iterate over all benchmark collections: */
88#define for_each_collection(coll) \
89 for (coll = collections; coll->name; coll++)
90
91/* Iterate over all benchmarks within a collection: */
92#define for_each_bench(coll, bench) \
Patrick Palka6eeefcc2014-03-12 18:40:51 -040093 for (bench = coll->benchmarks; bench && bench->name; bench++)
Ingo Molnar41579222013-10-23 14:37:56 +020094
95static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +090096{
Ingo Molnar41579222013-10-23 14:37:56 +020097 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090098
Ingo Molnar41579222013-10-23 14:37:56 +020099 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900100
Ingo Molnar41579222013-10-23 14:37:56 +0200101 for_each_bench(coll, bench)
102 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900103
104 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900105}
106
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300107static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +0200108
109/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900110int bench_format = BENCH_FORMAT_DEFAULT;
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700111unsigned int bench_repeat = 10; /* default number of times to repeat the run */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900112
113static const struct option bench_options[] = {
Ingo Molnar7a46a8f2015-10-19 10:04:22 +0200114 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700115 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900116 OPT_END()
117};
118
119static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200120 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900121 NULL
122};
123
124static void print_usage(void)
125{
Ingo Molnar41579222013-10-23 14:37:56 +0200126 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900127 int i;
128
129 printf("Usage: \n");
130 for (i = 0; bench_usage[i]; i++)
131 printf("\t%s\n", bench_usage[i]);
132 printf("\n");
133
Ingo Molnar41579222013-10-23 14:37:56 +0200134 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900135
Ingo Molnar41579222013-10-23 14:37:56 +0200136 for_each_collection(coll)
137 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900138 printf("\n");
139}
140
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300141static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900142{
143 if (!str)
144 return BENCH_FORMAT_DEFAULT;
145
146 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
147 return BENCH_FORMAT_DEFAULT;
148 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
149 return BENCH_FORMAT_SIMPLE;
150
151 return BENCH_FORMAT_UNKNOWN;
152}
153
Ingo Molnar41579222013-10-23 14:37:56 +0200154/*
155 * Run a specific benchmark but first rename the running task's ->comm[]
156 * to something meaningful:
157 */
158static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300159 int argc, const char **argv)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900160{
Ingo Molnar41579222013-10-23 14:37:56 +0200161 int size;
162 char *name;
163 int ret;
164
165 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
166
167 name = zalloc(size);
168 BUG_ON(!name);
169
170 scnprintf(name, size, "%s-%s", coll_name, bench_name);
171
172 prctl(PR_SET_NAME, name);
173 argv[0] = name;
174
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300175 ret = fn(argc, argv);
Ingo Molnar41579222013-10-23 14:37:56 +0200176
177 free(name);
178
179 return ret;
180}
181
182static void run_collection(struct collection *coll)
183{
184 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900185 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900186
187 argv[1] = NULL;
188 /*
189 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200190 *
191 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900192 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200193 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900194 */
Ingo Molnar41579222013-10-23 14:37:56 +0200195 for_each_bench(coll, bench) {
196 if (!bench->fn)
197 break;
198 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900199 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900200
Ingo Molnar41579222013-10-23 14:37:56 +0200201 argv[1] = bench->name;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300202 run_bench(coll->name, bench->name, bench->fn, 1, argv);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900203 printf("\n");
204 }
205}
206
Ingo Molnar41579222013-10-23 14:37:56 +0200207static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900208{
Ingo Molnar41579222013-10-23 14:37:56 +0200209 struct collection *coll;
210
211 for_each_collection(coll)
212 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900213}
214
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300215int cmd_bench(int argc, const char **argv)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900216{
Ingo Molnar41579222013-10-23 14:37:56 +0200217 struct collection *coll;
218 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900219
220 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200221 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900222 print_usage();
223 goto end;
224 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900225
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900226 argc = parse_options(argc, argv, bench_options, bench_usage,
227 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900228
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900229 bench_format = bench_str2int(bench_format_str);
230 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200231 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900232 goto end;
233 }
234
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700235 if (bench_repeat == 0) {
236 printf("Invalid repeat option: Must specify a positive value\n");
237 goto end;
238 }
239
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900240 if (argc < 1) {
241 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900242 goto end;
243 }
244
Hitoshi Mitake20442792009-12-13 17:01:59 +0900245 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200246 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900247 goto end;
248 }
249
Ingo Molnar41579222013-10-23 14:37:56 +0200250 for_each_collection(coll) {
251 struct bench *bench;
252
253 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900254 continue;
255
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900256 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200257 /* No bench specified. */
258 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900259 goto end;
260 }
261
Hitoshi Mitake20442792009-12-13 17:01:59 +0900262 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200263 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900264 goto end;
265 }
266
Ingo Molnar41579222013-10-23 14:37:56 +0200267 for_each_bench(coll, bench) {
268 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900269 continue;
270
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900271 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200272 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900273 fflush(stdout);
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300274 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900275 goto end;
276 }
277
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900278 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200279 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900280 goto end;
281 }
282
Ingo Molnar41579222013-10-23 14:37:56 +0200283 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
284 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900285 goto end;
286 }
287
Ingo Molnar41579222013-10-23 14:37:56 +0200288 printf("Unknown collection: '%s'\n", argv[0]);
289 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900290
291end:
Ingo Molnar41579222013-10-23 14:37:56 +0200292 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900293}