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