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 |
c2a08203 | 14 | * syscall ... System call performance |
827f3b49 | 15 | * mem ... memory access performance |
4157922a | 16 | * numa ... NUMA scheduling and MM performance |
a0439711 | 17 | * futex ... Futex performance |
121dd9ea | 18 | * epoll ... Event poll performance |
629cc356 | 19 | */ |
4b6ab94e | 20 | #include <subcmd/parse-options.h> |
629cc356 HM |
21 | #include "builtin.h" |
22 | #include "bench/bench.h" | |
23 | ||
16203e9c | 24 | #include <locale.h> |
629cc356 HM |
25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
4157922a | 28 | #include <sys/prctl.h> |
7f7c536f | 29 | #include <linux/zalloc.h> |
629cc356 | 30 | |
b0ad8ea6 | 31 | typedef int (*bench_fn_t)(int argc, const char **argv); |
4157922a IM |
32 | |
33 | struct bench { | |
34 | const char *name; | |
35 | const char *summary; | |
36 | bench_fn_t fn; | |
629cc356 HM |
37 | }; |
38 | ||
89fe808a | 39 | #ifdef HAVE_LIBNUMA_SUPPORT |
4157922a IM |
40 | static struct bench numa_benchmarks[] = { |
41 | { "mem", "Benchmark for NUMA workloads", bench_numa }, | |
aa254af2 | 42 | { "all", "Run all NUMA benchmarks", NULL }, |
4157922a | 43 | { NULL, NULL, NULL } |
1c13f3c9 | 44 | }; |
79d824e3 | 45 | #endif |
1c13f3c9 | 46 | |
4157922a IM |
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 }, | |
7d5cb68a | 50 | { "seccomp-notify", "Benchmark for seccomp user notify", bench_sched_seccomp_notify}, |
aa254af2 | 51 | { "all", "Run all scheduler benchmarks", NULL }, |
4157922a | 52 | { NULL, NULL, NULL } |
629cc356 HM |
53 | }; |
54 | ||
c2a08203 DB |
55 | static struct bench syscall_benchmarks[] = { |
56 | { "basic", "Benchmark for basic getppid(2) calls", bench_syscall_basic }, | |
391f84e5 | 57 | { "getpgid", "Benchmark for getpgid(2) calls", bench_syscall_getpgid }, |
ece7f7c0 | 58 | { "fork", "Benchmark for fork(2) calls", bench_syscall_fork }, |
540f8b56 | 59 | { "execve", "Benchmark for execve(2) calls", bench_syscall_execve }, |
c2a08203 DB |
60 | { "all", "Run all syscall benchmarks", NULL }, |
61 | { NULL, NULL, NULL }, | |
62 | }; | |
63 | ||
4157922a | 64 | static struct bench mem_benchmarks[] = { |
13b1fdce IM |
65 | { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy }, |
66 | { "memset", "Benchmark for memset() functions", bench_mem_memset }, | |
7c43b0c1 | 67 | { "find_bit", "Benchmark for find_bit() functions", bench_mem_find_bit }, |
aa254af2 | 68 | { "all", "Run all memory access benchmarks", NULL }, |
4157922a | 69 | { NULL, NULL, NULL } |
827f3b49 HM |
70 | }; |
71 | ||
a0439711 DB |
72 | static struct bench futex_benchmarks[] = { |
73 | { "hash", "Benchmark for futex hash table", bench_futex_hash }, | |
27db7830 | 74 | { "wake", "Benchmark for futex wake calls", bench_futex_wake }, |
d65817b4 | 75 | { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel }, |
0fb298cf | 76 | { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue }, |
d2f3f5d2 DB |
77 | /* pi-futexes */ |
78 | { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi }, | |
aa254af2 | 79 | { "all", "Run all futex benchmarks", NULL }, |
a0439711 DB |
80 | { NULL, NULL, NULL } |
81 | }; | |
82 | ||
ba35fe93 | 83 | #ifdef HAVE_EVENTFD_SUPPORT |
121dd9ea DB |
84 | static struct bench epoll_benchmarks[] = { |
85 | { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait }, | |
231457ec | 86 | { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl }, |
121dd9ea DB |
87 | { "all", "Run all futex benchmarks", NULL }, |
88 | { NULL, NULL, NULL } | |
89 | }; | |
ba35fe93 | 90 | #endif // HAVE_EVENTFD_SUPPORT |
121dd9ea | 91 | |
2a4b5166 IR |
92 | static struct bench internals_benchmarks[] = { |
93 | { "synthesize", "Benchmark perf event synthesis", bench_synthesize }, | |
51876bd4 | 94 | { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse }, |
0bf02a0d | 95 | { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id }, |
4241eabf | 96 | { "evlist-open-close", "Benchmark evlist open and close", bench_evlist_open_close }, |
f6a7bbbf | 97 | { "pmu-scan", "Benchmark sysfs PMU info scanning", bench_pmu_scan }, |
2a4b5166 IR |
98 | { NULL, NULL, NULL } |
99 | }; | |
100 | ||
68a6772f DV |
101 | static struct bench breakpoint_benchmarks[] = { |
102 | { "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread}, | |
103 | { "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable}, | |
104 | { "all", "Run all breakpoint benchmarks", NULL}, | |
105 | { NULL, NULL, NULL }, | |
106 | }; | |
107 | ||
2df27071 | 108 | static struct bench uprobe_benchmarks[] = { |
6af5e4cf ACM |
109 | { "baseline", "Baseline libc usleep(1000) call", bench_uprobe_baseline, }, |
110 | { "empty", "Attach empty BPF prog to uprobe on usleep, system wide", bench_uprobe_empty, }, | |
7b47623b | 111 | { "trace_printk", "Attach trace_printk BPF prog to uprobe on usleep syswide", bench_uprobe_trace_printk, }, |
988052f4 IR |
112 | { "empty_ret", "Attach empty BPF prog to uretprobe on usleep, system wide", bench_uprobe_empty_ret, }, |
113 | { "trace_printk_ret", "Attach trace_printk BPF prog to uretprobe on usleep syswide", bench_uprobe_trace_printk_ret,}, | |
2df27071 ACM |
114 | { NULL, NULL, NULL }, |
115 | }; | |
116 | ||
4157922a IM |
117 | struct collection { |
118 | const char *name; | |
119 | const char *summary; | |
120 | struct bench *benchmarks; | |
629cc356 HM |
121 | }; |
122 | ||
4157922a | 123 | static struct collection collections[] = { |
a0439711 | 124 | { "sched", "Scheduler and IPC benchmarks", sched_benchmarks }, |
c2a08203 | 125 | { "syscall", "System call benchmarks", syscall_benchmarks }, |
4157922a | 126 | { "mem", "Memory access benchmarks", mem_benchmarks }, |
89fe808a | 127 | #ifdef HAVE_LIBNUMA_SUPPORT |
4157922a | 128 | { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks }, |
79d824e3 | 129 | #endif |
a0439711 | 130 | {"futex", "Futex stressing benchmarks", futex_benchmarks }, |
ba35fe93 | 131 | #ifdef HAVE_EVENTFD_SUPPORT |
121dd9ea DB |
132 | {"epoll", "Epoll stressing benchmarks", epoll_benchmarks }, |
133 | #endif | |
2a4b5166 | 134 | { "internals", "Perf-internals benchmarks", internals_benchmarks }, |
68a6772f | 135 | { "breakpoint", "Breakpoint benchmarks", breakpoint_benchmarks }, |
2df27071 | 136 | { "uprobe", "uprobe benchmarks", uprobe_benchmarks }, |
4157922a IM |
137 | { "all", "All benchmarks", NULL }, |
138 | { NULL, NULL, NULL } | |
629cc356 HM |
139 | }; |
140 | ||
4157922a IM |
141 | /* Iterate over all benchmark collections: */ |
142 | #define for_each_collection(coll) \ | |
143 | for (coll = collections; coll->name; coll++) | |
144 | ||
145 | /* Iterate over all benchmarks within a collection: */ | |
146 | #define for_each_bench(coll, bench) \ | |
6eeefccd | 147 | for (bench = coll->benchmarks; bench && bench->name; bench++) |
4157922a IM |
148 | |
149 | static void dump_benchmarks(struct collection *coll) | |
629cc356 | 150 | { |
4157922a | 151 | struct bench *bench; |
629cc356 | 152 | |
4157922a | 153 | printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name); |
629cc356 | 154 | |
4157922a IM |
155 | for_each_bench(coll, bench) |
156 | printf("%14s: %s\n", bench->name, bench->summary); | |
629cc356 HM |
157 | |
158 | printf("\n"); | |
629cc356 HM |
159 | } |
160 | ||
edb7c60e | 161 | static const char *bench_format_str; |
4157922a IM |
162 | |
163 | /* Output/formatting style, exported to benchmark modules: */ | |
386d7e9e | 164 | int bench_format = BENCH_FORMAT_DEFAULT; |
b6f0629a | 165 | unsigned int bench_repeat = 10; /* default number of times to repeat the run */ |
386d7e9e HM |
166 | |
167 | static const struct option bench_options[] = { | |
7a46a8fd | 168 | OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"), |
fc5d836c | 169 | OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify number of times to repeat the run"), |
386d7e9e HM |
170 | OPT_END() |
171 | }; | |
172 | ||
173 | static const char * const bench_usage[] = { | |
4157922a | 174 | "perf bench [<common options>] <collection> <benchmark> [<options>]", |
386d7e9e HM |
175 | NULL |
176 | }; | |
177 | ||
178 | static void print_usage(void) | |
179 | { | |
4157922a | 180 | struct collection *coll; |
386d7e9e HM |
181 | int i; |
182 | ||
183 | printf("Usage: \n"); | |
184 | for (i = 0; bench_usage[i]; i++) | |
185 | printf("\t%s\n", bench_usage[i]); | |
186 | printf("\n"); | |
187 | ||
4157922a | 188 | printf(" # List of all available benchmark collections:\n\n"); |
386d7e9e | 189 | |
4157922a IM |
190 | for_each_collection(coll) |
191 | printf("%14s: %s\n", coll->name, coll->summary); | |
386d7e9e HM |
192 | printf("\n"); |
193 | } | |
194 | ||
edb7c60e | 195 | static int bench_str2int(const char *str) |
386d7e9e HM |
196 | { |
197 | if (!str) | |
198 | return BENCH_FORMAT_DEFAULT; | |
199 | ||
200 | if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR)) | |
201 | return BENCH_FORMAT_DEFAULT; | |
202 | else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR)) | |
203 | return BENCH_FORMAT_SIMPLE; | |
204 | ||
205 | return BENCH_FORMAT_UNKNOWN; | |
206 | } | |
207 | ||
4157922a IM |
208 | /* |
209 | * Run a specific benchmark but first rename the running task's ->comm[] | |
210 | * to something meaningful: | |
211 | */ | |
212 | static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn, | |
b0ad8ea6 | 213 | int argc, const char **argv) |
2044279d | 214 | { |
4157922a IM |
215 | int size; |
216 | char *name; | |
217 | int ret; | |
218 | ||
219 | size = strlen(coll_name) + 1 + strlen(bench_name) + 1; | |
220 | ||
221 | name = zalloc(size); | |
222 | BUG_ON(!name); | |
223 | ||
224 | scnprintf(name, size, "%s-%s", coll_name, bench_name); | |
225 | ||
226 | prctl(PR_SET_NAME, name); | |
227 | argv[0] = name; | |
228 | ||
b0ad8ea6 | 229 | ret = fn(argc, argv); |
4157922a IM |
230 | |
231 | free(name); | |
232 | ||
233 | return ret; | |
234 | } | |
235 | ||
236 | static void run_collection(struct collection *coll) | |
237 | { | |
238 | struct bench *bench; | |
2044279d | 239 | const char *argv[2]; |
2044279d HM |
240 | |
241 | argv[1] = NULL; | |
242 | /* | |
243 | * TODO: | |
4157922a IM |
244 | * |
245 | * Preparing preset parameters for | |
2044279d | 246 | * embedded, ordinary PC, HPC, etc... |
4157922a | 247 | * would be helpful. |
2044279d | 248 | */ |
4157922a IM |
249 | for_each_bench(coll, bench) { |
250 | if (!bench->fn) | |
251 | break; | |
252 | printf("# Running %s/%s benchmark...\n", coll->name, bench->name); | |
2044279d | 253 | |
4157922a | 254 | argv[1] = bench->name; |
b0ad8ea6 | 255 | run_bench(coll->name, bench->name, bench->fn, 1, argv); |
2044279d HM |
256 | printf("\n"); |
257 | } | |
258 | } | |
259 | ||
4157922a | 260 | static void run_all_collections(void) |
2044279d | 261 | { |
4157922a IM |
262 | struct collection *coll; |
263 | ||
264 | for_each_collection(coll) | |
265 | run_collection(coll); | |
2044279d HM |
266 | } |
267 | ||
b0ad8ea6 | 268 | int cmd_bench(int argc, const char **argv) |
629cc356 | 269 | { |
4157922a IM |
270 | struct collection *coll; |
271 | int ret = 0; | |
629cc356 | 272 | |
f0a29c96 SM |
273 | /* Unbuffered output */ |
274 | setvbuf(stdout, NULL, _IONBF, 0); | |
16203e9c | 275 | setlocale(LC_ALL, ""); |
f0a29c96 | 276 | |
629cc356 | 277 | if (argc < 2) { |
4157922a | 278 | /* No collection specified. */ |
386d7e9e HM |
279 | print_usage(); |
280 | goto end; | |
281 | } | |
629cc356 | 282 | |
386d7e9e HM |
283 | argc = parse_options(argc, argv, bench_options, bench_usage, |
284 | PARSE_OPT_STOP_AT_NON_OPTION); | |
285 | ||
286 | bench_format = bench_str2int(bench_format_str); | |
287 | if (bench_format == BENCH_FORMAT_UNKNOWN) { | |
4157922a | 288 | printf("Unknown format descriptor: '%s'\n", bench_format_str); |
386d7e9e HM |
289 | goto end; |
290 | } | |
629cc356 | 291 | |
b6f0629a DB |
292 | if (bench_repeat == 0) { |
293 | printf("Invalid repeat option: Must specify a positive value\n"); | |
294 | goto end; | |
295 | } | |
296 | ||
386d7e9e HM |
297 | if (argc < 1) { |
298 | print_usage(); | |
629cc356 HM |
299 | goto end; |
300 | } | |
301 | ||
2044279d | 302 | if (!strcmp(argv[0], "all")) { |
4157922a | 303 | run_all_collections(); |
2044279d HM |
304 | goto end; |
305 | } | |
306 | ||
4157922a IM |
307 | for_each_collection(coll) { |
308 | struct bench *bench; | |
309 | ||
310 | if (strcmp(coll->name, argv[0])) | |
629cc356 HM |
311 | continue; |
312 | ||
386d7e9e | 313 | if (argc < 2) { |
4157922a IM |
314 | /* No bench specified. */ |
315 | dump_benchmarks(coll); | |
629cc356 HM |
316 | goto end; |
317 | } | |
318 | ||
2044279d | 319 | if (!strcmp(argv[1], "all")) { |
4157922a | 320 | run_collection(coll); |
2044279d HM |
321 | goto end; |
322 | } | |
323 | ||
4157922a IM |
324 | for_each_bench(coll, bench) { |
325 | if (strcmp(bench->name, argv[1])) | |
629cc356 HM |
326 | continue; |
327 | ||
79e295d4 | 328 | if (bench_format == BENCH_FORMAT_DEFAULT) |
4157922a | 329 | printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name); |
b0ad8ea6 | 330 | ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1); |
629cc356 HM |
331 | goto end; |
332 | } | |
333 | ||
386d7e9e | 334 | if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { |
4157922a | 335 | dump_benchmarks(coll); |
629cc356 HM |
336 | goto end; |
337 | } | |
338 | ||
4157922a IM |
339 | printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]); |
340 | ret = 1; | |
629cc356 HM |
341 | goto end; |
342 | } | |
343 | ||
4157922a IM |
344 | printf("Unknown collection: '%s'\n", argv[0]); |
345 | ret = 1; | |
629cc356 HM |
346 | |
347 | end: | |
4157922a | 348 | return ret; |
629cc356 | 349 | } |