blob: a1cddc6bbf0f178f44ee03b2a3729b85a385dbd4 [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"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060019#include <subcmd/parse-options.h>
Hitoshi Mitake629cc352009-11-05 09:31:34 +090020#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 },
Ingo Molnaraa254af2015-10-19 10:04:30 +020039 { "all", "Run all NUMA benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020040 { 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 },
Ingo Molnaraa254af2015-10-19 10:04:30 +020047 { "all", "Run all scheduler benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020048 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090049};
50
Ingo Molnar41579222013-10-23 14:37:56 +020051static struct bench mem_benchmarks[] = {
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020052 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
53 { "memset", "Benchmark for memset() functions", bench_mem_memset },
Ingo Molnaraa254af2015-10-19 10:04:30 +020054 { "all", "Run all memory access benchmarks", NULL },
Ingo Molnar41579222013-10-23 14:37:56 +020055 { 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 Buesod2f3f5d2015-07-07 01:55:53 -070063 /* pi-futexes */
64 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
Ingo Molnaraa254af2015-10-19 10:04:30 +020065 { "all", "Run all futex benchmarks", NULL },
Davidlohr Buesoa0439712013-12-14 20:31:55 -080066 { NULL, NULL, NULL }
67};
68
Ingo Molnar41579222013-10-23 14:37:56 +020069struct collection {
70 const char *name;
71 const char *summary;
72 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090073};
74
Ingo Molnar41579222013-10-23 14:37:56 +020075static struct collection collections[] = {
Davidlohr Buesoa0439712013-12-14 20:31:55 -080076 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020077 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +020078#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020079 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -050080#endif
Davidlohr Buesoa0439712013-12-14 20:31:55 -080081 {"futex", "Futex stressing benchmarks", futex_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020082 { "all", "All benchmarks", NULL },
83 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090084};
85
Ingo Molnar41579222013-10-23 14:37:56 +020086/* Iterate over all benchmark collections: */
87#define for_each_collection(coll) \
88 for (coll = collections; coll->name; coll++)
89
90/* Iterate over all benchmarks within a collection: */
91#define for_each_bench(coll, bench) \
Patrick Palka6eeefcc2014-03-12 18:40:51 -040092 for (bench = coll->benchmarks; bench && bench->name; bench++)
Ingo Molnar41579222013-10-23 14:37:56 +020093
94static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +090095{
Ingo Molnar41579222013-10-23 14:37:56 +020096 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090097
Ingo Molnar41579222013-10-23 14:37:56 +020098 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +090099
Ingo Molnar41579222013-10-23 14:37:56 +0200100 for_each_bench(coll, bench)
101 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900102
103 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900104}
105
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300106static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +0200107
108/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900109int bench_format = BENCH_FORMAT_DEFAULT;
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700110unsigned int bench_repeat = 10; /* default number of times to repeat the run */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900111
112static const struct option bench_options[] = {
Ingo Molnar7a46a8f2015-10-19 10:04:22 +0200113 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700114 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900115 OPT_END()
116};
117
118static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200119 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900120 NULL
121};
122
123static void print_usage(void)
124{
Ingo Molnar41579222013-10-23 14:37:56 +0200125 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900126 int i;
127
128 printf("Usage: \n");
129 for (i = 0; bench_usage[i]; i++)
130 printf("\t%s\n", bench_usage[i]);
131 printf("\n");
132
Ingo Molnar41579222013-10-23 14:37:56 +0200133 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900134
Ingo Molnar41579222013-10-23 14:37:56 +0200135 for_each_collection(coll)
136 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900137 printf("\n");
138}
139
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300140static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900141{
142 if (!str)
143 return BENCH_FORMAT_DEFAULT;
144
145 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
146 return BENCH_FORMAT_DEFAULT;
147 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
148 return BENCH_FORMAT_SIMPLE;
149
150 return BENCH_FORMAT_UNKNOWN;
151}
152
Ingo Molnar41579222013-10-23 14:37:56 +0200153/*
154 * Run a specific benchmark but first rename the running task's ->comm[]
155 * to something meaningful:
156 */
157static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
158 int argc, const char **argv, const char *prefix)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900159{
Ingo Molnar41579222013-10-23 14:37:56 +0200160 int size;
161 char *name;
162 int ret;
163
164 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
165
166 name = zalloc(size);
167 BUG_ON(!name);
168
169 scnprintf(name, size, "%s-%s", coll_name, bench_name);
170
171 prctl(PR_SET_NAME, name);
172 argv[0] = name;
173
174 ret = fn(argc, argv, prefix);
175
176 free(name);
177
178 return ret;
179}
180
181static void run_collection(struct collection *coll)
182{
183 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900184 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900185
186 argv[1] = NULL;
187 /*
188 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200189 *
190 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900191 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200192 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900193 */
Ingo Molnar41579222013-10-23 14:37:56 +0200194 for_each_bench(coll, bench) {
195 if (!bench->fn)
196 break;
197 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900198 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900199
Ingo Molnar41579222013-10-23 14:37:56 +0200200 argv[1] = bench->name;
201 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900202 printf("\n");
203 }
204}
205
Ingo Molnar41579222013-10-23 14:37:56 +0200206static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900207{
Ingo Molnar41579222013-10-23 14:37:56 +0200208 struct collection *coll;
209
210 for_each_collection(coll)
211 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900212}
213
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300214int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900215{
Ingo Molnar41579222013-10-23 14:37:56 +0200216 struct collection *coll;
217 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900218
219 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200220 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900221 print_usage();
222 goto end;
223 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900224
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900225 argc = parse_options(argc, argv, bench_options, bench_usage,
226 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900227
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900228 bench_format = bench_str2int(bench_format_str);
229 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200230 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900231 goto end;
232 }
233
Davidlohr Buesob6f06292014-06-16 11:14:19 -0700234 if (bench_repeat == 0) {
235 printf("Invalid repeat option: Must specify a positive value\n");
236 goto end;
237 }
238
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900239 if (argc < 1) {
240 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900241 goto end;
242 }
243
Hitoshi Mitake20442792009-12-13 17:01:59 +0900244 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200245 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900246 goto end;
247 }
248
Ingo Molnar41579222013-10-23 14:37:56 +0200249 for_each_collection(coll) {
250 struct bench *bench;
251
252 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900253 continue;
254
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900255 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200256 /* No bench specified. */
257 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900258 goto end;
259 }
260
Hitoshi Mitake20442792009-12-13 17:01:59 +0900261 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200262 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900263 goto end;
264 }
265
Ingo Molnar41579222013-10-23 14:37:56 +0200266 for_each_bench(coll, bench) {
267 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900268 continue;
269
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900270 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200271 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900272 fflush(stdout);
Ingo Molnar41579222013-10-23 14:37:56 +0200273 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900274 goto end;
275 }
276
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900277 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200278 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900279 goto end;
280 }
281
Ingo Molnar41579222013-10-23 14:37:56 +0200282 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
283 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900284 goto end;
285 }
286
Ingo Molnar41579222013-10-23 14:37:56 +0200287 printf("Unknown collection: '%s'\n", argv[0]);
288 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900289
290end:
Ingo Molnar41579222013-10-23 14:37:56 +0200291 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900292}