Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / tools / perf / builtin-bench.c
CommitLineData
629cc356 1/*
629cc356
HM
2 * builtin-bench.c
3 *
4157922a 4 * General benchmarking collections provided by perf
629cc356
HM
5 *
6 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
629cc356
HM
7 */
8
9/*
4157922a 10 * Available benchmark collection list:
629cc356 11 *
4157922a 12 * sched ... scheduler and IPC performance
827f3b49 13 * mem ... memory access performance
4157922a 14 * numa ... NUMA scheduling and MM performance
629cc356 15 */
629cc356
HM
16#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>
4157922a 25#include <sys/prctl.h>
629cc356 26
4157922a
IM
27typedef 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;
629cc356
HM
33};
34
89fe808a 35#ifdef HAVE_LIBNUMA_SUPPORT
4157922a
IM
36static struct bench numa_benchmarks[] = {
37 { "mem", "Benchmark for NUMA workloads", bench_numa },
38 { "all", "Test all NUMA benchmarks", NULL },
39 { NULL, NULL, NULL }
1c13f3c9 40};
79d824e3 41#endif
1c13f3c9 42
4157922a
IM
43static 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 }
629cc356
HM
48};
49
4157922a
IM
50static 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 }
827f3b49
HM
55};
56
4157922a
IM
57struct collection {
58 const char *name;
59 const char *summary;
60 struct bench *benchmarks;
629cc356
HM
61};
62
4157922a
IM
63static struct collection collections[] = {
64 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
65 { "mem", "Memory access benchmarks", mem_benchmarks },
89fe808a 66#ifdef HAVE_LIBNUMA_SUPPORT
4157922a 67 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
79d824e3 68#endif
4157922a
IM
69 { "all", "All benchmarks", NULL },
70 { NULL, NULL, NULL }
629cc356
HM
71};
72
4157922a
IM
73/* 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)
629cc356 82{
4157922a 83 struct bench *bench;
629cc356 84
4157922a 85 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
629cc356 86
4157922a
IM
87 for_each_bench(coll, bench)
88 printf("%14s: %s\n", bench->name, bench->summary);
629cc356
HM
89
90 printf("\n");
629cc356
HM
91}
92
edb7c60e 93static const char *bench_format_str;
4157922a
IM
94
95/* Output/formatting style, exported to benchmark modules: */
386d7e9e
HM
96int bench_format = BENCH_FORMAT_DEFAULT;
97
98static const struct option bench_options[] = {
4157922a 99 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
386d7e9e
HM
100 OPT_END()
101};
102
103static const char * const bench_usage[] = {
4157922a 104 "perf bench [<common options>] <collection> <benchmark> [<options>]",
386d7e9e
HM
105 NULL
106};
107
108static void print_usage(void)
109{
4157922a 110 struct collection *coll;
386d7e9e
HM
111 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
4157922a 118 printf(" # List of all available benchmark collections:\n\n");
386d7e9e 119
4157922a
IM
120 for_each_collection(coll)
121 printf("%14s: %s\n", coll->name, coll->summary);
386d7e9e
HM
122 printf("\n");
123}
124
edb7c60e 125static int bench_str2int(const char *str)
386d7e9e
HM
126{
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
4157922a
IM
138/*
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)
2044279d 144{
4157922a
IM
145 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;
2044279d 169 const char *argv[2];
2044279d
HM
170
171 argv[1] = NULL;
172 /*
173 * TODO:
4157922a
IM
174 *
175 * Preparing preset parameters for
2044279d 176 * embedded, ordinary PC, HPC, etc...
4157922a 177 * would be helpful.
2044279d 178 */
4157922a
IM
179 for_each_bench(coll, bench) {
180 if (!bench->fn)
181 break;
182 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
9b494ea2 183 fflush(stdout);
2044279d 184
4157922a
IM
185 argv[1] = bench->name;
186 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
2044279d
HM
187 printf("\n");
188 }
189}
190
4157922a 191static void run_all_collections(void)
2044279d 192{
4157922a
IM
193 struct collection *coll;
194
195 for_each_collection(coll)
196 run_collection(coll);
2044279d
HM
197}
198
1d037ca1 199int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
629cc356 200{
4157922a
IM
201 struct collection *coll;
202 int ret = 0;
629cc356
HM
203
204 if (argc < 2) {
4157922a 205 /* No collection specified. */
386d7e9e
HM
206 print_usage();
207 goto end;
208 }
629cc356 209
386d7e9e
HM
210 argc = parse_options(argc, argv, bench_options, bench_usage,
211 PARSE_OPT_STOP_AT_NON_OPTION);
212
213 bench_format = bench_str2int(bench_format_str);
214 if (bench_format == BENCH_FORMAT_UNKNOWN) {
4157922a 215 printf("Unknown format descriptor: '%s'\n", bench_format_str);
386d7e9e
HM
216 goto end;
217 }
629cc356 218
386d7e9e
HM
219 if (argc < 1) {
220 print_usage();
629cc356
HM
221 goto end;
222 }
223
2044279d 224 if (!strcmp(argv[0], "all")) {
4157922a 225 run_all_collections();
2044279d
HM
226 goto end;
227 }
228
4157922a
IM
229 for_each_collection(coll) {
230 struct bench *bench;
231
232 if (strcmp(coll->name, argv[0]))
629cc356
HM
233 continue;
234
386d7e9e 235 if (argc < 2) {
4157922a
IM
236 /* No bench specified. */
237 dump_benchmarks(coll);
629cc356
HM
238 goto end;
239 }
240
2044279d 241 if (!strcmp(argv[1], "all")) {
4157922a 242 run_collection(coll);
2044279d
HM
243 goto end;
244 }
245
4157922a
IM
246 for_each_bench(coll, bench) {
247 if (strcmp(bench->name, argv[1]))
629cc356
HM
248 continue;
249
79e295d4 250 if (bench_format == BENCH_FORMAT_DEFAULT)
4157922a 251 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
9b494ea2 252 fflush(stdout);
4157922a 253 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
629cc356
HM
254 goto end;
255 }
256
386d7e9e 257 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
4157922a 258 dump_benchmarks(coll);
629cc356
HM
259 goto end;
260 }
261
4157922a
IM
262 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
263 ret = 1;
629cc356
HM
264 goto end;
265 }
266
4157922a
IM
267 printf("Unknown collection: '%s'\n", argv[0]);
268 ret = 1;
629cc356
HM
269
270end:
4157922a 271 return ret;
629cc356 272}
This page took 0.201428 seconds and 5 git commands to generate.