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