blob: e47f90cc7b98cdde57079975bbd5637f3249ce44 [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
Hitoshi Mitake629cc352009-11-05 09:31:34 +090015 */
Hitoshi Mitake629cc352009-11-05 09:31:34 +090016#include "perf.h"
17#include "util/util.h"
18#include "util/parse-options.h"
19#include "builtin.h"
20#include "bench/bench.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Ingo Molnar41579222013-10-23 14:37:56 +020025#include <sys/prctl.h>
Hitoshi Mitake629cc352009-11-05 09:31:34 +090026
Ingo Molnar41579222013-10-23 14:37:56 +020027typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix);
28
29struct bench {
30 const char *name;
31 const char *summary;
32 bench_fn_t fn;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090033};
34
Ingo Molnar89fe8082013-09-30 12:07:11 +020035#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020036static struct bench numa_benchmarks[] = {
37 { "mem", "Benchmark for NUMA workloads", bench_numa },
38 { "all", "Test all NUMA benchmarks", NULL },
39 { NULL, NULL, NULL }
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010040};
Peter Hurley79d824e2013-01-27 20:51:22 -050041#endif
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010042
Ingo Molnar41579222013-10-23 14:37:56 +020043static struct bench sched_benchmarks[] = {
44 { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
45 { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
46 { "all", "Test all scheduler benchmarks", NULL },
47 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090048};
49
Ingo Molnar41579222013-10-23 14:37:56 +020050static struct bench mem_benchmarks[] = {
51 { "memcpy", "Benchmark for memcpy()", bench_mem_memcpy },
52 { "memset", "Benchmark for memset() tests", bench_mem_memset },
53 { "all", "Test all memory benchmarks", NULL },
54 { NULL, NULL, NULL }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090055};
56
Ingo Molnar41579222013-10-23 14:37:56 +020057struct collection {
58 const char *name;
59 const char *summary;
60 struct bench *benchmarks;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090061};
62
Ingo Molnar41579222013-10-23 14:37:56 +020063static struct collection collections[] = {
64 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
65 { "mem", "Memory access benchmarks", mem_benchmarks },
Ingo Molnar89fe8082013-09-30 12:07:11 +020066#ifdef HAVE_LIBNUMA_SUPPORT
Ingo Molnar41579222013-10-23 14:37:56 +020067 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
Peter Hurley79d824e2013-01-27 20:51:22 -050068#endif
Ingo Molnar41579222013-10-23 14:37:56 +020069 { "all", "All benchmarks", NULL },
70 { NULL, NULL, NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090071};
72
Ingo Molnar41579222013-10-23 14:37:56 +020073/* Iterate over all benchmark collections: */
74#define for_each_collection(coll) \
75 for (coll = collections; coll->name; coll++)
76
77/* Iterate over all benchmarks within a collection: */
78#define for_each_bench(coll, bench) \
79 for (bench = coll->benchmarks; bench->name; bench++)
80
81static void dump_benchmarks(struct collection *coll)
Hitoshi Mitake629cc352009-11-05 09:31:34 +090082{
Ingo Molnar41579222013-10-23 14:37:56 +020083 struct bench *bench;
Hitoshi Mitake629cc352009-11-05 09:31:34 +090084
Ingo Molnar41579222013-10-23 14:37:56 +020085 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
Hitoshi Mitake629cc352009-11-05 09:31:34 +090086
Ingo Molnar41579222013-10-23 14:37:56 +020087 for_each_bench(coll, bench)
88 printf("%14s: %s\n", bench->name, bench->summary);
Hitoshi Mitake629cc352009-11-05 09:31:34 +090089
90 printf("\n");
Hitoshi Mitake629cc352009-11-05 09:31:34 +090091}
92
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030093static const char *bench_format_str;
Ingo Molnar41579222013-10-23 14:37:56 +020094
95/* Output/formatting style, exported to benchmark modules: */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +090096int bench_format = BENCH_FORMAT_DEFAULT;
97
98static const struct option bench_options[] = {
Ingo Molnar41579222013-10-23 14:37:56 +020099 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900100 OPT_END()
101};
102
103static const char * const bench_usage[] = {
Ingo Molnar41579222013-10-23 14:37:56 +0200104 "perf bench [<common options>] <collection> <benchmark> [<options>]",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900105 NULL
106};
107
108static void print_usage(void)
109{
Ingo Molnar41579222013-10-23 14:37:56 +0200110 struct collection *coll;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900111 int i;
112
113 printf("Usage: \n");
114 for (i = 0; bench_usage[i]; i++)
115 printf("\t%s\n", bench_usage[i]);
116 printf("\n");
117
Ingo Molnar41579222013-10-23 14:37:56 +0200118 printf(" # List of all available benchmark collections:\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900119
Ingo Molnar41579222013-10-23 14:37:56 +0200120 for_each_collection(coll)
121 printf("%14s: %s\n", coll->name, coll->summary);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900122 printf("\n");
123}
124
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300125static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900126{
127 if (!str)
128 return BENCH_FORMAT_DEFAULT;
129
130 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
131 return BENCH_FORMAT_DEFAULT;
132 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
133 return BENCH_FORMAT_SIMPLE;
134
135 return BENCH_FORMAT_UNKNOWN;
136}
137
Ingo Molnar41579222013-10-23 14:37:56 +0200138/*
139 * Run a specific benchmark but first rename the running task's ->comm[]
140 * to something meaningful:
141 */
142static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
143 int argc, const char **argv, const char *prefix)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900144{
Ingo Molnar41579222013-10-23 14:37:56 +0200145 int size;
146 char *name;
147 int ret;
148
149 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
150
151 name = zalloc(size);
152 BUG_ON(!name);
153
154 scnprintf(name, size, "%s-%s", coll_name, bench_name);
155
156 prctl(PR_SET_NAME, name);
157 argv[0] = name;
158
159 ret = fn(argc, argv, prefix);
160
161 free(name);
162
163 return ret;
164}
165
166static void run_collection(struct collection *coll)
167{
168 struct bench *bench;
Hitoshi Mitake20442792009-12-13 17:01:59 +0900169 const char *argv[2];
Hitoshi Mitake20442792009-12-13 17:01:59 +0900170
171 argv[1] = NULL;
172 /*
173 * TODO:
Ingo Molnar41579222013-10-23 14:37:56 +0200174 *
175 * Preparing preset parameters for
Hitoshi Mitake20442792009-12-13 17:01:59 +0900176 * embedded, ordinary PC, HPC, etc...
Ingo Molnar41579222013-10-23 14:37:56 +0200177 * would be helpful.
Hitoshi Mitake20442792009-12-13 17:01:59 +0900178 */
Ingo Molnar41579222013-10-23 14:37:56 +0200179 for_each_bench(coll, bench) {
180 if (!bench->fn)
181 break;
182 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900183 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900184
Ingo Molnar41579222013-10-23 14:37:56 +0200185 argv[1] = bench->name;
186 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900187 printf("\n");
188 }
189}
190
Ingo Molnar41579222013-10-23 14:37:56 +0200191static void run_all_collections(void)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900192{
Ingo Molnar41579222013-10-23 14:37:56 +0200193 struct collection *coll;
194
195 for_each_collection(coll)
196 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900197}
198
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300199int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900200{
Ingo Molnar41579222013-10-23 14:37:56 +0200201 struct collection *coll;
202 int ret = 0;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900203
204 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200205 /* No collection specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900206 print_usage();
207 goto end;
208 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900209
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900210 argc = parse_options(argc, argv, bench_options, bench_usage,
211 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900212
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900213 bench_format = bench_str2int(bench_format_str);
214 if (bench_format == BENCH_FORMAT_UNKNOWN) {
Ingo Molnar41579222013-10-23 14:37:56 +0200215 printf("Unknown format descriptor: '%s'\n", bench_format_str);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900216 goto end;
217 }
218
219 if (argc < 1) {
220 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900221 goto end;
222 }
223
Hitoshi Mitake20442792009-12-13 17:01:59 +0900224 if (!strcmp(argv[0], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200225 run_all_collections();
Hitoshi Mitake20442792009-12-13 17:01:59 +0900226 goto end;
227 }
228
Ingo Molnar41579222013-10-23 14:37:56 +0200229 for_each_collection(coll) {
230 struct bench *bench;
231
232 if (strcmp(coll->name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900233 continue;
234
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900235 if (argc < 2) {
Ingo Molnar41579222013-10-23 14:37:56 +0200236 /* No bench specified. */
237 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900238 goto end;
239 }
240
Hitoshi Mitake20442792009-12-13 17:01:59 +0900241 if (!strcmp(argv[1], "all")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200242 run_collection(coll);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900243 goto end;
244 }
245
Ingo Molnar41579222013-10-23 14:37:56 +0200246 for_each_bench(coll, bench) {
247 if (strcmp(bench->name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900248 continue;
249
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900250 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar41579222013-10-23 14:37:56 +0200251 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900252 fflush(stdout);
Ingo Molnar41579222013-10-23 14:37:56 +0200253 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900254 goto end;
255 }
256
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900257 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Ingo Molnar41579222013-10-23 14:37:56 +0200258 dump_benchmarks(coll);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900259 goto end;
260 }
261
Ingo Molnar41579222013-10-23 14:37:56 +0200262 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
263 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900264 goto end;
265 }
266
Ingo Molnar41579222013-10-23 14:37:56 +0200267 printf("Unknown collection: '%s'\n", argv[0]);
268 ret = 1;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900269
270end:
Ingo Molnar41579222013-10-23 14:37:56 +0200271 return ret;
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900272}