blob: a8b0138fc6a0db4e939f18a00ec558b663a29523 [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 },
60 { "all", "Test all futex benchmarks", NULL },
61 { NULL, NULL, NULL }
62};
63
Ingo Molnar41579222013-10-23 14:37:56 +020064struct collection {
65 const char *name;
66 const char *summary;
67 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090068};
69
Ingo Molnar41579222013-10-23 14:37:56 +020070static struct collection collections[] = {
Davidlohr Buesoa0439712013-12-14 20:31:55 -080071 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020072 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +020073#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020074 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -050075#endif
Davidlohr Buesoa0439712013-12-14 20:31:55 -080076 {"futex", "Futex stressing benchmarks", futex_benchmarks },
Ingo Molnar41579222013-10-23 14:37:56 +020077 { "all", "All benchmarks", NULL },
78 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090079};
80
Ingo Molnar41579222013-10-23 14:37:56 +020081/* Iterate over all benchmark collections: */
82#define for_each_collection(coll) \
83 for (coll = collections; coll->name; coll++)
84
85/* Iterate over all benchmarks within a collection: */
86#define for_each_bench(coll, bench) \
87 for (bench = coll->benchmarks; bench->name; bench++)
88
89static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +090090{
Ingo Molnar41579222013-10-23 14:37:56 +020091 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090092
Ingo Molnar41579222013-10-23 14:37:56 +020093 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +090094
Ingo Molnar41579222013-10-23 14:37:56 +020095 for_each_bench(coll, bench)
96 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +090097
98 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +090099}
100
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300101static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +0200102
103/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900104int bench_format = BENCH_FORMAT_DEFAULT;
105
106static const struct option bench_options[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200107 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900108 OPT_END()
109};
110
111static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200112 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900113 NULL
114};
115
116static void print_usage(void)
117{
Ingo Molnar41579222013-10-23 14:37:56 +0200118 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900119 int i;
120
121 printf("Usage: \n");
122 for (i = 0; bench_usage[i]; i++)
123 printf("\t%s\n", bench_usage[i]);
124 printf("\n");
125
Ingo Molnar41579222013-10-23 14:37:56 +0200126 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900127
Ingo Molnar41579222013-10-23 14:37:56 +0200128 for_each_collection(coll)
129 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900130 printf("\n");
131}
132
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300133static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900134{
135 if (!str)
136 return BENCH_FORMAT_DEFAULT;
137
138 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
139 return BENCH_FORMAT_DEFAULT;
140 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
141 return BENCH_FORMAT_SIMPLE;
142
143 return BENCH_FORMAT_UNKNOWN;
144}
145
Ingo Molnar41579222013-10-23 14:37:56 +0200146/*
147 * Run a specific benchmark but first rename the running task's ->comm[]
148 * to something meaningful:
149 */
150static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
151 int argc, const char **argv, const char *prefix)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900152{
Ingo Molnar41579222013-10-23 14:37:56 +0200153 int size;
154 char *name;
155 int ret;
156
157 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
158
159 name = zalloc(size);
160 BUG_ON(!name);
161
162 scnprintf(name, size, "%s-%s", coll_name, bench_name);
163
164 prctl(PR_SET_NAME, name);
165 argv[0] = name;
166
167 ret = fn(argc, argv, prefix);
168
169 free(name);
170
171 return ret;
172}
173
174static void run_collection(struct collection *coll)
175{
176 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900177 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900178
179 argv[1] = NULL;
180 /*
181 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200182 *
183 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900184 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200185 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900186 */
Ingo Molnar41579222013-10-23 14:37:56 +0200187 for_each_bench(coll, bench) {
188 if (!bench->fn)
189 break;
190 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900191 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900192
Ingo Molnar41579222013-10-23 14:37:56 +0200193 argv[1] = bench->name;
194 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900195 printf("\n");
196 }
197}
198
Ingo Molnar41579222013-10-23 14:37:56 +0200199static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900200{
Ingo Molnar41579222013-10-23 14:37:56 +0200201 struct collection *coll;
202
203 for_each_collection(coll)
204 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900205}
206
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300207int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900208{
Ingo Molnar41579222013-10-23 14:37:56 +0200209 struct collection *coll;
210 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900211
212 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200213 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900214 print_usage();
215 goto end;
216 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900217
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900218 argc = parse_options(argc, argv, bench_options, bench_usage,
219 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900220
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900221 bench_format = bench_str2int(bench_format_str);
222 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200223 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900224 goto end;
225 }
226
227 if (argc < 1) {
228 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900229 goto end;
230 }
231
Hitoshi Mitake20442792009-12-13 17:01:59 +0900232 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200233 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900234 goto end;
235 }
236
Ingo Molnar41579222013-10-23 14:37:56 +0200237 for_each_collection(coll) {
238 struct bench *bench;
239
240 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900241 continue;
242
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900243 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200244 /* No bench specified. */
245 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900246 goto end;
247 }
248
Hitoshi Mitake20442792009-12-13 17:01:59 +0900249 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200250 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900251 goto end;
252 }
253
Ingo Molnar41579222013-10-23 14:37:56 +0200254 for_each_bench(coll, bench) {
255 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900256 continue;
257
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900258 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200259 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900260 fflush(stdout);
Ingo Molnar41579222013-10-23 14:37:56 +0200261 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900262 goto end;
263 }
264
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900265 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200266 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900267 goto end;
268 }
269
Ingo Molnar41579222013-10-23 14:37:56 +0200270 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
271 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900272 goto end;
273 }
274
Ingo Molnar41579222013-10-23 14:37:56 +0200275 printf("Unknown collection: '%s'\n", argv[0]);
276 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900277
278end:
Ingo Molnar41579222013-10-23 14:37:56 +0200279 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900280}