blob: b5314e452ec7f24a2e9e4e6b8473d39b8ed7a4a7 [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 Buesod65817b2015-05-08 11:37:59 -070061 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080062 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
Davidlohr Buesoa0439712013-12-14 20:31:55 -080063 { "all", "Test all futex benchmarks", NULL },
64 { NULL, NULL, NULL }
65};
66
Ingo Molnar41579222013-10-23 14:37:56 +020067struct collection {
68 const char *name;
69 const char *summary;
70 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090071};
72
Ingo Molnar41579222013-10-23 14:37:56 +020073static struct collection collections[] = {
Davidlohr Buesoa0439712013-12-14 20:31:55 -080074 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020075 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +020076#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020077 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -050078#endif
Davidlohr Buesoa0439712013-12-14 20:31:55 -080079 {"futex", "Futex stressing benchmarks", futex_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020080 { "all", "All benchmarks", NULL },
81 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090082};
83
Ingo Molnar41579222013-10-23 14:37:56 +020084/* Iterate over all benchmark collections: */
85#define for_each_collection(coll) \
86 for (coll = collections; coll->name; coll++)
87
88/* Iterate over all benchmarks within a collection: */
89#define for_each_bench(coll, bench) \
Patrick Palka6eeefcc2014-03-12 18:40:51 -040090 for (bench = coll->benchmarks; bench && bench->name; bench++)
Ingo Molnar41579222013-10-23 14:37:56 +020091
92static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +090093{
Ingo Molnar41579222013-10-23 14:37:56 +020094 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090095
Ingo Molnar41579222013-10-23 14:37:56 +020096 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +090097
Ingo Molnar41579222013-10-23 14:37:56 +020098 for_each_bench(coll, bench)
99 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900100
101 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900102}
103
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300104static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +0200105
106/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900107int bench_format = BENCH_FORMAT_DEFAULT;
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700108unsigned int bench_repeat = 10; /* default number of times to repeat the run */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900109
110static const struct option bench_options[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200111 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700112 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900113 OPT_END()
114};
115
116static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200117 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900118 NULL
119};
120
121static void print_usage(void)
122{
Ingo Molnar41579222013-10-23 14:37:56 +0200123 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900124 int i;
125
126 printf("Usage: \n");
127 for (i = 0; bench_usage[i]; i++)
128 printf("\t%s\n", bench_usage[i]);
129 printf("\n");
130
Ingo Molnar41579222013-10-23 14:37:56 +0200131 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900132
Ingo Molnar41579222013-10-23 14:37:56 +0200133 for_each_collection(coll)
134 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900135 printf("\n");
136}
137
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300138static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900139{
140 if (!str)
141 return BENCH_FORMAT_DEFAULT;
142
143 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
144 return BENCH_FORMAT_DEFAULT;
145 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
146 return BENCH_FORMAT_SIMPLE;
147
148 return BENCH_FORMAT_UNKNOWN;
149}
150
Ingo Molnar41579222013-10-23 14:37:56 +0200151/*
152 * Run a specific benchmark but first rename the running task's ->comm[]
153 * to something meaningful:
154 */
155static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
156 int argc, const char **argv, const char *prefix)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900157{
Ingo Molnar41579222013-10-23 14:37:56 +0200158 int size;
159 char *name;
160 int ret;
161
162 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
163
164 name = zalloc(size);
165 BUG_ON(!name);
166
167 scnprintf(name, size, "%s-%s", coll_name, bench_name);
168
169 prctl(PR_SET_NAME, name);
170 argv[0] = name;
171
172 ret = fn(argc, argv, prefix);
173
174 free(name);
175
176 return ret;
177}
178
179static void run_collection(struct collection *coll)
180{
181 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900182 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900183
184 argv[1] = NULL;
185 /*
186 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200187 *
188 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900189 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200190 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900191 */
Ingo Molnar41579222013-10-23 14:37:56 +0200192 for_each_bench(coll, bench) {
193 if (!bench->fn)
194 break;
195 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900196 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900197
Ingo Molnar41579222013-10-23 14:37:56 +0200198 argv[1] = bench->name;
199 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900200 printf("\n");
201 }
202}
203
Ingo Molnar41579222013-10-23 14:37:56 +0200204static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900205{
Ingo Molnar41579222013-10-23 14:37:56 +0200206 struct collection *coll;
207
208 for_each_collection(coll)
209 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900210}
211
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300212int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900213{
Ingo Molnar41579222013-10-23 14:37:56 +0200214 struct collection *coll;
215 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900216
217 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200218 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900219 print_usage();
220 goto end;
221 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900222
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900223 argc = parse_options(argc, argv, bench_options, bench_usage,
224 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900225
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900226 bench_format = bench_str2int(bench_format_str);
227 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200228 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900229 goto end;
230 }
231
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700232 if (bench_repeat == 0) {
233 printf("Invalid repeat option: Must specify a positive value\n");
234 goto end;
235 }
236
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900237 if (argc < 1) {
238 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900239 goto end;
240 }
241
Hitoshi Mitake20442792009-12-13 17:01:59 +0900242 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200243 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900244 goto end;
245 }
246
Ingo Molnar41579222013-10-23 14:37:56 +0200247 for_each_collection(coll) {
248 struct bench *bench;
249
250 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900251 continue;
252
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900253 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200254 /* No bench specified. */
255 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900256 goto end;
257 }
258
Hitoshi Mitake20442792009-12-13 17:01:59 +0900259 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200260 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900261 goto end;
262 }
263
Ingo Molnar41579222013-10-23 14:37:56 +0200264 for_each_bench(coll, bench) {
265 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900266 continue;
267
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900268 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200269 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900270 fflush(stdout);
Ingo Molnar41579222013-10-23 14:37:56 +0200271 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900272 goto end;
273 }
274
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900275 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200276 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900277 goto end;
278 }
279
Ingo Molnar41579222013-10-23 14:37:56 +0200280 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
281 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900282 goto end;
283 }
284
Ingo Molnar41579222013-10-23 14:37:56 +0200285 printf("Unknown collection: '%s'\n", argv[0]);
286 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900287
288end:
Ingo Molnar41579222013-10-23 14:37:56 +0200289 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900290}