]>
Commit | Line | Data |
---|---|---|
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 |
a0439711 | 15 | * futex ... Futex performance |
629cc356 | 16 | */ |
629cc356 HM |
17 | #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> | |
4157922a | 26 | #include <sys/prctl.h> |
629cc356 | 27 | |
4157922a IM |
28 | typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix); |
29 | ||
30 | struct bench { | |
31 | const char *name; | |
32 | const char *summary; | |
33 | bench_fn_t fn; | |
629cc356 HM |
34 | }; |
35 | ||
89fe808a | 36 | #ifdef HAVE_LIBNUMA_SUPPORT |
4157922a IM |
37 | static struct bench numa_benchmarks[] = { |
38 | { "mem", "Benchmark for NUMA workloads", bench_numa }, | |
39 | { "all", "Test all NUMA benchmarks", NULL }, | |
40 | { NULL, NULL, NULL } | |
1c13f3c9 | 41 | }; |
79d824e3 | 42 | #endif |
1c13f3c9 | 43 | |
4157922a IM |
44 | static 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 } | |
629cc356 HM |
49 | }; |
50 | ||
4157922a IM |
51 | static 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 } | |
827f3b49 HM |
56 | }; |
57 | ||
a0439711 DB |
58 | static struct bench futex_benchmarks[] = { |
59 | { "hash", "Benchmark for futex hash table", bench_futex_hash }, | |
27db7830 | 60 | { "wake", "Benchmark for futex wake calls", bench_futex_wake }, |
d65817b4 | 61 | { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel }, |
0fb298cf | 62 | { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue }, |
a0439711 DB |
63 | { "all", "Test all futex benchmarks", NULL }, |
64 | { NULL, NULL, NULL } | |
65 | }; | |
66 | ||
4157922a IM |
67 | struct collection { |
68 | const char *name; | |
69 | const char *summary; | |
70 | struct bench *benchmarks; | |
629cc356 HM |
71 | }; |
72 | ||
4157922a | 73 | static struct collection collections[] = { |
a0439711 | 74 | { "sched", "Scheduler and IPC benchmarks", sched_benchmarks }, |
4157922a | 75 | { "mem", "Memory access benchmarks", mem_benchmarks }, |
89fe808a | 76 | #ifdef HAVE_LIBNUMA_SUPPORT |
4157922a | 77 | { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks }, |
79d824e3 | 78 | #endif |
a0439711 | 79 | {"futex", "Futex stressing benchmarks", futex_benchmarks }, |
4157922a IM |
80 | { "all", "All benchmarks", NULL }, |
81 | { NULL, NULL, NULL } | |
629cc356 HM |
82 | }; |
83 | ||
4157922a IM |
84 | /* Iterate over all benchmark collections: */ |
85 | #define for_each_collection(coll) \ | |
86 | for (coll = collections; coll->name; coll++) | |
87 | ||
88 | /* Iterate over all benchmarks within a collection: */ | |
89 | #define for_each_bench(coll, bench) \ | |
6eeefccd | 90 | for (bench = coll->benchmarks; bench && bench->name; bench++) |
4157922a IM |
91 | |
92 | static void dump_benchmarks(struct collection *coll) | |
629cc356 | 93 | { |
4157922a | 94 | struct bench *bench; |
629cc356 | 95 | |
4157922a | 96 | printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name); |
629cc356 | 97 | |
4157922a IM |
98 | for_each_bench(coll, bench) |
99 | printf("%14s: %s\n", bench->name, bench->summary); | |
629cc356 HM |
100 | |
101 | printf("\n"); | |
629cc356 HM |
102 | } |
103 | ||
edb7c60e | 104 | static const char *bench_format_str; |
4157922a IM |
105 | |
106 | /* Output/formatting style, exported to benchmark modules: */ | |
386d7e9e | 107 | int bench_format = BENCH_FORMAT_DEFAULT; |
b6f0629a | 108 | unsigned int bench_repeat = 10; /* default number of times to repeat the run */ |
386d7e9e HM |
109 | |
110 | static const struct option bench_options[] = { | |
4157922a | 111 | OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"), |
b6f0629a | 112 | OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"), |
386d7e9e HM |
113 | OPT_END() |
114 | }; | |
115 | ||
116 | static const char * const bench_usage[] = { | |
4157922a | 117 | "perf bench [<common options>] <collection> <benchmark> [<options>]", |
386d7e9e HM |
118 | NULL |
119 | }; | |
120 | ||
121 | static void print_usage(void) | |
122 | { | |
4157922a | 123 | struct collection *coll; |
386d7e9e HM |
124 | int i; |
125 | ||
126 | printf("Usage: \n"); | |
127 | for (i = 0; bench_usage[i]; i++) | |
128 | printf("\t%s\n", bench_usage[i]); | |
129 | printf("\n"); | |
130 | ||
4157922a | 131 | printf(" # List of all available benchmark collections:\n\n"); |
386d7e9e | 132 | |
4157922a IM |
133 | for_each_collection(coll) |
134 | printf("%14s: %s\n", coll->name, coll->summary); | |
386d7e9e HM |
135 | printf("\n"); |
136 | } | |
137 | ||
edb7c60e | 138 | static int bench_str2int(const char *str) |
386d7e9e HM |
139 | { |
140 | if (!str) | |
141 | return BENCH_FORMAT_DEFAULT; | |
142 | ||
143 | if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR)) | |
144 | return BENCH_FORMAT_DEFAULT; | |
145 | else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR)) | |
146 | return BENCH_FORMAT_SIMPLE; | |
147 | ||
148 | return BENCH_FORMAT_UNKNOWN; | |
149 | } | |
150 | ||
4157922a IM |
151 | /* |
152 | * Run a specific benchmark but first rename the running task's ->comm[] | |
153 | * to something meaningful: | |
154 | */ | |
155 | static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn, | |
156 | int argc, const char **argv, const char *prefix) | |
2044279d | 157 | { |
4157922a IM |
158 | int size; |
159 | char *name; | |
160 | int ret; | |
161 | ||
162 | size = strlen(coll_name) + 1 + strlen(bench_name) + 1; | |
163 | ||
164 | name = zalloc(size); | |
165 | BUG_ON(!name); | |
166 | ||
167 | scnprintf(name, size, "%s-%s", coll_name, bench_name); | |
168 | ||
169 | prctl(PR_SET_NAME, name); | |
170 | argv[0] = name; | |
171 | ||
172 | ret = fn(argc, argv, prefix); | |
173 | ||
174 | free(name); | |
175 | ||
176 | return ret; | |
177 | } | |
178 | ||
179 | static void run_collection(struct collection *coll) | |
180 | { | |
181 | struct bench *bench; | |
2044279d | 182 | const char *argv[2]; |
2044279d HM |
183 | |
184 | argv[1] = NULL; | |
185 | /* | |
186 | * TODO: | |
4157922a IM |
187 | * |
188 | * Preparing preset parameters for | |
2044279d | 189 | * embedded, ordinary PC, HPC, etc... |
4157922a | 190 | * would be helpful. |
2044279d | 191 | */ |
4157922a IM |
192 | for_each_bench(coll, bench) { |
193 | if (!bench->fn) | |
194 | break; | |
195 | printf("# Running %s/%s benchmark...\n", coll->name, bench->name); | |
9b494ea2 | 196 | fflush(stdout); |
2044279d | 197 | |
4157922a IM |
198 | argv[1] = bench->name; |
199 | run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL); | |
2044279d HM |
200 | printf("\n"); |
201 | } | |
202 | } | |
203 | ||
4157922a | 204 | static void run_all_collections(void) |
2044279d | 205 | { |
4157922a IM |
206 | struct collection *coll; |
207 | ||
208 | for_each_collection(coll) | |
209 | run_collection(coll); | |
2044279d HM |
210 | } |
211 | ||
1d037ca1 | 212 | int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused) |
629cc356 | 213 | { |
4157922a IM |
214 | struct collection *coll; |
215 | int ret = 0; | |
629cc356 HM |
216 | |
217 | if (argc < 2) { | |
4157922a | 218 | /* No collection specified. */ |
386d7e9e HM |
219 | print_usage(); |
220 | goto end; | |
221 | } | |
629cc356 | 222 | |
386d7e9e HM |
223 | argc = parse_options(argc, argv, bench_options, bench_usage, |
224 | PARSE_OPT_STOP_AT_NON_OPTION); | |
225 | ||
226 | bench_format = bench_str2int(bench_format_str); | |
227 | if (bench_format == BENCH_FORMAT_UNKNOWN) { | |
4157922a | 228 | printf("Unknown format descriptor: '%s'\n", bench_format_str); |
386d7e9e HM |
229 | goto end; |
230 | } | |
629cc356 | 231 | |
b6f0629a DB |
232 | if (bench_repeat == 0) { |
233 | printf("Invalid repeat option: Must specify a positive value\n"); | |
234 | goto end; | |
235 | } | |
236 | ||
386d7e9e HM |
237 | if (argc < 1) { |
238 | print_usage(); | |
629cc356 HM |
239 | goto end; |
240 | } | |
241 | ||
2044279d | 242 | if (!strcmp(argv[0], "all")) { |
4157922a | 243 | run_all_collections(); |
2044279d HM |
244 | goto end; |
245 | } | |
246 | ||
4157922a IM |
247 | for_each_collection(coll) { |
248 | struct bench *bench; | |
249 | ||
250 | if (strcmp(coll->name, argv[0])) | |
629cc356 HM |
251 | continue; |
252 | ||
386d7e9e | 253 | if (argc < 2) { |
4157922a IM |
254 | /* No bench specified. */ |
255 | dump_benchmarks(coll); | |
629cc356 HM |
256 | goto end; |
257 | } | |
258 | ||
2044279d | 259 | if (!strcmp(argv[1], "all")) { |
4157922a | 260 | run_collection(coll); |
2044279d HM |
261 | goto end; |
262 | } | |
263 | ||
4157922a IM |
264 | for_each_bench(coll, bench) { |
265 | if (strcmp(bench->name, argv[1])) | |
629cc356 HM |
266 | continue; |
267 | ||
79e295d4 | 268 | if (bench_format == BENCH_FORMAT_DEFAULT) |
4157922a | 269 | printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name); |
9b494ea2 | 270 | fflush(stdout); |
4157922a | 271 | ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix); |
629cc356 HM |
272 | goto end; |
273 | } | |
274 | ||
386d7e9e | 275 | if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { |
4157922a | 276 | dump_benchmarks(coll); |
629cc356 HM |
277 | goto end; |
278 | } | |
279 | ||
4157922a IM |
280 | printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]); |
281 | ret = 1; | |
629cc356 HM |
282 | goto end; |
283 | } | |
284 | ||
4157922a IM |
285 | printf("Unknown collection: '%s'\n", argv[0]); |
286 | ret = 1; | |
629cc356 HM |
287 | |
288 | end: | |
4157922a | 289 | return ret; |
629cc356 | 290 | } |