Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / tools / perf / builtin-bench.c
blobe47f90cc7b98cdde57079975bbd5637f3249ce44
1 /*
2 * builtin-bench.c
4 * General benchmarking collections provided by perf
6 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
7 */
9 /*
10 * Available benchmark collection list:
12 * sched ... scheduler and IPC performance
13 * mem ... memory access performance
14 * numa ... NUMA scheduling and MM performance
16 #include "perf.h"
17 #include "util/util.h"
18 #include "util/parse-options.h"
19 #include "builtin.h"
20 #include "bench/bench.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/prctl.h>
27 typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix);
29 struct bench {
30 const char *name;
31 const char *summary;
32 bench_fn_t fn;
35 #ifdef HAVE_LIBNUMA_SUPPORT
36 static struct bench numa_benchmarks[] = {
37 { "mem", "Benchmark for NUMA workloads", bench_numa },
38 { "all", "Test all NUMA benchmarks", NULL },
39 { NULL, NULL, NULL }
41 #endif
43 static 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 }
50 static 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 }
57 struct collection {
58 const char *name;
59 const char *summary;
60 struct bench *benchmarks;
63 static struct collection collections[] = {
64 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
65 { "mem", "Memory access benchmarks", mem_benchmarks },
66 #ifdef HAVE_LIBNUMA_SUPPORT
67 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
68 #endif
69 { "all", "All benchmarks", NULL },
70 { NULL, NULL, NULL }
73 /* Iterate over all benchmark collections: */
74 #define for_each_collection(coll) \
75 for (coll = collections; coll->name; coll++)
77 /* Iterate over all benchmarks within a collection: */
78 #define for_each_bench(coll, bench) \
79 for (bench = coll->benchmarks; bench->name; bench++)
81 static void dump_benchmarks(struct collection *coll)
83 struct bench *bench;
85 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
87 for_each_bench(coll, bench)
88 printf("%14s: %s\n", bench->name, bench->summary);
90 printf("\n");
93 static const char *bench_format_str;
95 /* Output/formatting style, exported to benchmark modules: */
96 int bench_format = BENCH_FORMAT_DEFAULT;
98 static const struct option bench_options[] = {
99 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
100 OPT_END()
103 static const char * const bench_usage[] = {
104 "perf bench [<common options>] <collection> <benchmark> [<options>]",
105 NULL
108 static void print_usage(void)
110 struct collection *coll;
111 int i;
113 printf("Usage: \n");
114 for (i = 0; bench_usage[i]; i++)
115 printf("\t%s\n", bench_usage[i]);
116 printf("\n");
118 printf(" # List of all available benchmark collections:\n\n");
120 for_each_collection(coll)
121 printf("%14s: %s\n", coll->name, coll->summary);
122 printf("\n");
125 static int bench_str2int(const char *str)
127 if (!str)
128 return BENCH_FORMAT_DEFAULT;
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;
135 return BENCH_FORMAT_UNKNOWN;
139 * Run a specific benchmark but first rename the running task's ->comm[]
140 * to something meaningful:
142 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
143 int argc, const char **argv, const char *prefix)
145 int size;
146 char *name;
147 int ret;
149 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
151 name = zalloc(size);
152 BUG_ON(!name);
154 scnprintf(name, size, "%s-%s", coll_name, bench_name);
156 prctl(PR_SET_NAME, name);
157 argv[0] = name;
159 ret = fn(argc, argv, prefix);
161 free(name);
163 return ret;
166 static void run_collection(struct collection *coll)
168 struct bench *bench;
169 const char *argv[2];
171 argv[1] = NULL;
173 * TODO:
175 * Preparing preset parameters for
176 * embedded, ordinary PC, HPC, etc...
177 * would be helpful.
179 for_each_bench(coll, bench) {
180 if (!bench->fn)
181 break;
182 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
183 fflush(stdout);
185 argv[1] = bench->name;
186 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
187 printf("\n");
191 static void run_all_collections(void)
193 struct collection *coll;
195 for_each_collection(coll)
196 run_collection(coll);
199 int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
201 struct collection *coll;
202 int ret = 0;
204 if (argc < 2) {
205 /* No collection specified. */
206 print_usage();
207 goto end;
210 argc = parse_options(argc, argv, bench_options, bench_usage,
211 PARSE_OPT_STOP_AT_NON_OPTION);
213 bench_format = bench_str2int(bench_format_str);
214 if (bench_format == BENCH_FORMAT_UNKNOWN) {
215 printf("Unknown format descriptor: '%s'\n", bench_format_str);
216 goto end;
219 if (argc < 1) {
220 print_usage();
221 goto end;
224 if (!strcmp(argv[0], "all")) {
225 run_all_collections();
226 goto end;
229 for_each_collection(coll) {
230 struct bench *bench;
232 if (strcmp(coll->name, argv[0]))
233 continue;
235 if (argc < 2) {
236 /* No bench specified. */
237 dump_benchmarks(coll);
238 goto end;
241 if (!strcmp(argv[1], "all")) {
242 run_collection(coll);
243 goto end;
246 for_each_bench(coll, bench) {
247 if (strcmp(bench->name, argv[1]))
248 continue;
250 if (bench_format == BENCH_FORMAT_DEFAULT)
251 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
252 fflush(stdout);
253 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
254 goto end;
257 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
258 dump_benchmarks(coll);
259 goto end;
262 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
263 ret = 1;
264 goto end;
267 printf("Unknown collection: '%s'\n", argv[0]);
268 ret = 1;
270 end:
271 return ret;