blob: b9a56fa8333065271a9242e98cf052d9c8100b87 [file] [log] [blame]
Hitoshi Mitake629cc352009-11-05 09:31:34 +09001/*
Hitoshi Mitake629cc352009-11-05 09:31:34 +09002 * builtin-bench.c
3 *
Ingo Molnar41579222013-10-23 14:37:56 +02004 * General benchmarking collections provided by perf
Hitoshi Mitake629cc352009-11-05 09:31:34 +09005 *
6 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
Hitoshi Mitake629cc352009-11-05 09:31:34 +09007 */
8
9/*
Ingo Molnar41579222013-10-23 14:37:56 +020010 * Available benchmark collection list:
Hitoshi Mitake629cc352009-11-05 09:31:34 +090011 *
Ingo Molnar41579222013-10-23 14:37:56 +020012 * sched ... scheduler and IPC performance
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090013 * mem ... memory access performance
Ingo Molnar41579222013-10-23 14:37:56 +020014 * numa ... NUMA scheduling and MM performance
Davidlohr Buesoa0439712013-12-14 20:31:55 -080015 * futex ... Futex performance
Hitoshi Mitake629cc352009-11-05 09:31:34 +090016 */
Hitoshi Mitake629cc352009-11-05 09:31:34 +090017#include "perf.h"
18#include "util/util.h"
19#include "util/parse-options.h"
20#include "builtin.h"
21#include "bench/bench.h"
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Ingo Molnar41579222013-10-23 14:37:56 +020026#include <sys/prctl.h>
Hitoshi Mitake629cc352009-11-05 09:31:34 +090027
Ingo Molnar41579222013-10-23 14:37:56 +020028typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix);
29
30struct bench {
31 const char *name;
32 const char *summary;
33 bench_fn_t fn;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090034};
35
Ingo Molnar89fe8082013-09-30 12:07:11 +020036#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020037static struct bench numa_benchmarks[] = {
38 { "mem", "Benchmark for NUMA workloads", bench_numa },
39 { "all", "Test all NUMA benchmarks", NULL },
40 { NULL, NULL, NULL }
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010041};
Peter Hurley79d824e2013-01-27 20:51:22 -050042#endif
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010043
Ingo Molnar41579222013-10-23 14:37:56 +020044static struct bench sched_benchmarks[] = {
45 { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
46 { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
47 { "all", "Test all scheduler benchmarks", NULL },
48 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090049};
50
Ingo Molnar41579222013-10-23 14:37:56 +020051static struct bench mem_benchmarks[] = {
52 { "memcpy", "Benchmark for memcpy()", bench_mem_memcpy },
53 { "memset", "Benchmark for memset() tests", bench_mem_memset },
54 { "all", "Test all memory benchmarks", NULL },
55 { NULL, NULL, NULL }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090056};
57
Davidlohr Buesoa0439712013-12-14 20:31:55 -080058static struct bench futex_benchmarks[] = {
59 { "hash", "Benchmark for futex hash table", bench_futex_hash },
Davidlohr Bueso27db7832013-12-14 20:31:56 -080060 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080061 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
Davidlohr Buesoa0439712013-12-14 20:31:55 -080062 { "all", "Test all futex benchmarks", NULL },
63 { NULL, NULL, NULL }
64};
65
Ingo Molnar41579222013-10-23 14:37:56 +020066struct collection {
67 const char *name;
68 const char *summary;
69 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090070};
71
Ingo Molnar41579222013-10-23 14:37:56 +020072static struct collection collections[] = {
Davidlohr Buesoa0439712013-12-14 20:31:55 -080073 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020074 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +020075#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020076 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -050077#endif
Davidlohr Buesoa0439712013-12-14 20:31:55 -080078 {"futex", "Futex stressing benchmarks", futex_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020079 { "all", "All benchmarks", NULL },
80 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090081};
82
Ingo Molnar41579222013-10-23 14:37:56 +020083/* Iterate over all benchmark collections: */
84#define for_each_collection(coll) \
85 for (coll = collections; coll->name; coll++)
86
87/* Iterate over all benchmarks within a collection: */
88#define for_each_bench(coll, bench) \
Patrick Palka6eeefcc2014-03-12 18:40:51 -040089 for (bench = coll->benchmarks; bench && bench->name; bench++)
Ingo Molnar41579222013-10-23 14:37:56 +020090
91static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +090092{
Ingo Molnar41579222013-10-23 14:37:56 +020093 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090094
Ingo Molnar41579222013-10-23 14:37:56 +020095 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +090096
Ingo Molnar41579222013-10-23 14:37:56 +020097 for_each_bench(coll, bench)
98 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +090099
100 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900101}
102
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300103static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +0200104
105/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900106int bench_format = BENCH_FORMAT_DEFAULT;
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700107unsigned int bench_repeat = 10; /* default number of times to repeat the run */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900108
109static const struct option bench_options[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200110 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700111 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900112 OPT_END()
113};
114
115static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200116 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900117 NULL
118};
119
120static void print_usage(void)
121{
Ingo Molnar41579222013-10-23 14:37:56 +0200122 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900123 int i;
124
125 printf("Usage: \n");
126 for (i = 0; bench_usage[i]; i++)
127 printf("\t%s\n", bench_usage[i]);
128 printf("\n");
129
Ingo Molnar41579222013-10-23 14:37:56 +0200130 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900131
Ingo Molnar41579222013-10-23 14:37:56 +0200132 for_each_collection(coll)
133 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900134 printf("\n");
135}
136
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300137static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900138{
139 if (!str)
140 return BENCH_FORMAT_DEFAULT;
141
142 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
143 return BENCH_FORMAT_DEFAULT;
144 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
145 return BENCH_FORMAT_SIMPLE;
146
147 return BENCH_FORMAT_UNKNOWN;
148}
149
Ingo Molnar41579222013-10-23 14:37:56 +0200150/*
151 * Run a specific benchmark but first rename the running task's ->comm[]
152 * to something meaningful:
153 */
154static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
155 int argc, const char **argv, const char *prefix)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900156{
Ingo Molnar41579222013-10-23 14:37:56 +0200157 int size;
158 char *name;
159 int ret;
160
161 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
162
163 name = zalloc(size);
164 BUG_ON(!name);
165
166 scnprintf(name, size, "%s-%s", coll_name, bench_name);
167
168 prctl(PR_SET_NAME, name);
169 argv[0] = name;
170
171 ret = fn(argc, argv, prefix);
172
173 free(name);
174
175 return ret;
176}
177
178static void run_collection(struct collection *coll)
179{
180 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900181 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900182
183 argv[1] = NULL;
184 /*
185 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200186 *
187 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900188 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200189 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900190 */
Ingo Molnar41579222013-10-23 14:37:56 +0200191 for_each_bench(coll, bench) {
192 if (!bench->fn)
193 break;
194 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900195 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900196
Ingo Molnar41579222013-10-23 14:37:56 +0200197 argv[1] = bench->name;
198 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900199 printf("\n");
200 }
201}
202
Ingo Molnar41579222013-10-23 14:37:56 +0200203static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900204{
Ingo Molnar41579222013-10-23 14:37:56 +0200205 struct collection *coll;
206
207 for_each_collection(coll)
208 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900209}
210
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300211int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900212{
Ingo Molnar41579222013-10-23 14:37:56 +0200213 struct collection *coll;
214 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900215
216 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200217 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900218 print_usage();
219 goto end;
220 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900221
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900222 argc = parse_options(argc, argv, bench_options, bench_usage,
223 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900224
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900225 bench_format = bench_str2int(bench_format_str);
226 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200227 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900228 goto end;
229 }
230
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700231 if (bench_repeat == 0) {
232 printf("Invalid repeat option: Must specify a positive value\n");
233 goto end;
234 }
235
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900236 if (argc < 1) {
237 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900238 goto end;
239 }
240
Hitoshi Mitake20442792009-12-13 17:01:59 +0900241 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200242 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900243 goto end;
244 }
245
Ingo Molnar41579222013-10-23 14:37:56 +0200246 for_each_collection(coll) {
247 struct bench *bench;
248
249 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900250 continue;
251
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900252 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200253 /* No bench specified. */
254 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900255 goto end;
256 }
257
Hitoshi Mitake20442792009-12-13 17:01:59 +0900258 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200259 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900260 goto end;
261 }
262
Ingo Molnar41579222013-10-23 14:37:56 +0200263 for_each_bench(coll, bench) {
264 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900265 continue;
266
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900267 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200268 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900269 fflush(stdout);
Ingo Molnar41579222013-10-23 14:37:56 +0200270 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900271 goto end;
272 }
273
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900274 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200275 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900276 goto end;
277 }
278
Ingo Molnar41579222013-10-23 14:37:56 +0200279 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
280 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900281 goto end;
282 }
283
Ingo Molnar41579222013-10-23 14:37:56 +0200284 printf("Unknown collection: '%s'\n", argv[0]);
285 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900286
287end:
Ingo Molnar41579222013-10-23 14:37:56 +0200288 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900289}