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 { "evlist-open-close", "Benchmark evlist open and close", bench_evlist_open_close },
93 { NULL, NULL, NULL }
94 };
95
96 static struct bench breakpoint_benchmarks[] = {
97 { "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
98 { "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
99 { "all", "Run all breakpoint benchmarks", NULL},
100 { NULL, NULL, NULL },
101 };
102
103 struct collection {
104 const char *name;
105 const char *summary;
106 struct bench *benchmarks;
107 };
108
109 static struct collection collections[] = {
110 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
111 { "syscall", "System call benchmarks", syscall_benchmarks },
112 { "mem", "Memory access benchmarks", mem_benchmarks },
113 #ifdef HAVE_LIBNUMA_SUPPORT
114 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
115 #endif
116 {"futex", "Futex stressing benchmarks", futex_benchmarks },
117 #ifdef HAVE_EVENTFD_SUPPORT
118 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
119 #endif
120 { "internals", "Perf-internals benchmarks", internals_benchmarks },
121 { "breakpoint", "Breakpoint benchmarks", breakpoint_benchmarks },
122 { "all", "All benchmarks", NULL },
123 { NULL, NULL, NULL }
124 };
125
126 /* Iterate over all benchmark collections: */
127 #define for_each_collection(coll) \
128 for (coll = collections; coll->name; coll++)
129
130 /* Iterate over all benchmarks within a collection: */
131 #define for_each_bench(coll, bench) \
132 for (bench = coll->benchmarks; bench && bench->name; bench++)
133
dump_benchmarks(struct collection * coll)134 static void dump_benchmarks(struct collection *coll)
135 {
136 struct bench *bench;
137
138 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
139
140 for_each_bench(coll, bench)
141 printf("%14s: %s\n", bench->name, bench->summary);
142
143 printf("\n");
144 }
145
146 static const char *bench_format_str;
147
148 /* Output/formatting style, exported to benchmark modules: */
149 int bench_format = BENCH_FORMAT_DEFAULT;
150 unsigned int bench_repeat = 10; /* default number of times to repeat the run */
151
152 static const struct option bench_options[] = {
153 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
154 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
155 OPT_END()
156 };
157
158 static const char * const bench_usage[] = {
159 "perf bench [<common options>] <collection> <benchmark> [<options>]",
160 NULL
161 };
162
print_usage(void)163 static void print_usage(void)
164 {
165 struct collection *coll;
166 int i;
167
168 printf("Usage: \n");
169 for (i = 0; bench_usage[i]; i++)
170 printf("\t%s\n", bench_usage[i]);
171 printf("\n");
172
173 printf(" # List of all available benchmark collections:\n\n");
174
175 for_each_collection(coll)
176 printf("%14s: %s\n", coll->name, coll->summary);
177 printf("\n");
178 }
179
bench_str2int(const char * str)180 static int bench_str2int(const char *str)
181 {
182 if (!str)
183 return BENCH_FORMAT_DEFAULT;
184
185 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
186 return BENCH_FORMAT_DEFAULT;
187 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
188 return BENCH_FORMAT_SIMPLE;
189
190 return BENCH_FORMAT_UNKNOWN;
191 }
192
193 /*
194 * Run a specific benchmark but first rename the running task's ->comm[]
195 * to something meaningful:
196 */
run_bench(const char * coll_name,const char * bench_name,bench_fn_t fn,int argc,const char ** argv)197 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
198 int argc, const char **argv)
199 {
200 int size;
201 char *name;
202 int ret;
203
204 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
205
206 name = zalloc(size);
207 BUG_ON(!name);
208
209 scnprintf(name, size, "%s-%s", coll_name, bench_name);
210
211 prctl(PR_SET_NAME, name);
212 argv[0] = name;
213
214 ret = fn(argc, argv);
215
216 free(name);
217
218 return ret;
219 }
220
run_collection(struct collection * coll)221 static void run_collection(struct collection *coll)
222 {
223 struct bench *bench;
224 const char *argv[2];
225
226 argv[1] = NULL;
227 /*
228 * TODO:
229 *
230 * Preparing preset parameters for
231 * embedded, ordinary PC, HPC, etc...
232 * would be helpful.
233 */
234 for_each_bench(coll, bench) {
235 if (!bench->fn)
236 break;
237 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
238
239 argv[1] = bench->name;
240 run_bench(coll->name, bench->name, bench->fn, 1, argv);
241 printf("\n");
242 }
243 }
244
run_all_collections(void)245 static void run_all_collections(void)
246 {
247 struct collection *coll;
248
249 for_each_collection(coll)
250 run_collection(coll);
251 }
252
cmd_bench(int argc,const char ** argv)253 int cmd_bench(int argc, const char **argv)
254 {
255 struct collection *coll;
256 int ret = 0;
257
258 /* Unbuffered output */
259 setvbuf(stdout, NULL, _IONBF, 0);
260 setlocale(LC_ALL, "");
261
262 if (argc < 2) {
263 /* No collection specified. */
264 print_usage();
265 goto end;
266 }
267
268 argc = parse_options(argc, argv, bench_options, bench_usage,
269 PARSE_OPT_STOP_AT_NON_OPTION);
270
271 bench_format = bench_str2int(bench_format_str);
272 if (bench_format == BENCH_FORMAT_UNKNOWN) {
273 printf("Unknown format descriptor: '%s'\n", bench_format_str);
274 goto end;
275 }
276
277 if (bench_repeat == 0) {
278 printf("Invalid repeat option: Must specify a positive value\n");
279 goto end;
280 }
281
282 if (argc < 1) {
283 print_usage();
284 goto end;
285 }
286
287 if (!strcmp(argv[0], "all")) {
288 run_all_collections();
289 goto end;
290 }
291
292 for_each_collection(coll) {
293 struct bench *bench;
294
295 if (strcmp(coll->name, argv[0]))
296 continue;
297
298 if (argc < 2) {
299 /* No bench specified. */
300 dump_benchmarks(coll);
301 goto end;
302 }
303
304 if (!strcmp(argv[1], "all")) {
305 run_collection(coll);
306 goto end;
307 }
308
309 for_each_bench(coll, bench) {
310 if (strcmp(bench->name, argv[1]))
311 continue;
312
313 if (bench_format == BENCH_FORMAT_DEFAULT)
314 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
315 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
316 goto end;
317 }
318
319 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
320 dump_benchmarks(coll);
321 goto end;
322 }
323
324 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
325 ret = 1;
326 goto end;
327 }
328
329 printf("Unknown collection: '%s'\n", argv[0]);
330 ret = 1;
331
332 end:
333 return ret;
334 }
335