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