1 // SPDX-License-Identifier: GPL-2.0
5 * General benchmarking collections provided by perf
7 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
11 * Available benchmark collection list:
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
20 #include <subcmd/parse-options.h>
22 #include "bench/bench.h"
28 #include <sys/prctl.h>
29 #include <linux/zalloc.h>
31 typedef int (*bench_fn_t)(int argc, const char **argv);
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 },
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 },
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 },
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 },
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 },
77 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
78 { "all", "Run all futex benchmarks", NULL },
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 },
89 #endif // HAVE_EVENTFD_SUPPORT
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 },
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 },
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 },
117 struct bench *benchmarks;
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 },
127 {"futex", "Futex stressing benchmarks", futex_benchmarks },
128 #ifdef HAVE_EVENTFD_SUPPORT
129 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
131 { "internals", "Perf-internals benchmarks", internals_benchmarks },
132 { "breakpoint", "Breakpoint benchmarks", breakpoint_benchmarks },
133 { "uprobe", "uprobe benchmarks", uprobe_benchmarks },
134 { "all", "All benchmarks", NULL },
138 /* Iterate over all benchmark collections: */
139 #define for_each_collection(coll) \
140 for (coll = collections; coll->name; coll++)
142 /* Iterate over all benchmarks within a collection: */
143 #define for_each_bench(coll, bench) \
144 for (bench = coll->benchmarks; bench && bench->name; bench++)
146 static void dump_benchmarks(struct collection *coll)
150 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
152 for_each_bench(coll, bench)
153 printf("%14s: %s\n", bench->name, bench->summary);
158 static const char *bench_format_str;
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 */
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"),
170 static const char * const bench_usage[] = {
171 "perf bench [<common options>] <collection> <benchmark> [<options>]",
175 static void print_usage(void)
177 struct collection *coll;
181 for (i = 0; bench_usage[i]; i++)
182 printf("\t%s\n", bench_usage[i]);
185 printf(" # List of all available benchmark collections:\n\n");
187 for_each_collection(coll)
188 printf("%14s: %s\n", coll->name, coll->summary);
192 static int bench_str2int(const char *str)
195 return BENCH_FORMAT_DEFAULT;
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;
202 return BENCH_FORMAT_UNKNOWN;
206 * Run a specific benchmark but first rename the running task's ->comm[]
207 * to something meaningful:
209 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
210 int argc, const char **argv)
216 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
221 scnprintf(name, size, "%s-%s", coll_name, bench_name);
223 prctl(PR_SET_NAME, name);
226 ret = fn(argc, argv);
233 static void run_collection(struct collection *coll)
242 * Preparing preset parameters for
243 * embedded, ordinary PC, HPC, etc...
246 for_each_bench(coll, bench) {
249 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
251 argv[1] = bench->name;
252 run_bench(coll->name, bench->name, bench->fn, 1, argv);
257 static void run_all_collections(void)
259 struct collection *coll;
261 for_each_collection(coll)
262 run_collection(coll);
265 int cmd_bench(int argc, const char **argv)
267 struct collection *coll;
270 /* Unbuffered output */
271 setvbuf(stdout, NULL, _IONBF, 0);
272 setlocale(LC_ALL, "");
275 /* No collection specified. */
280 argc = parse_options(argc, argv, bench_options, bench_usage,
281 PARSE_OPT_STOP_AT_NON_OPTION);
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);
289 if (bench_repeat == 0) {
290 printf("Invalid repeat option: Must specify a positive value\n");
299 if (!strcmp(argv[0], "all")) {
300 run_all_collections();
304 for_each_collection(coll) {
307 if (strcmp(coll->name, argv[0]))
311 /* No bench specified. */
312 dump_benchmarks(coll);
316 if (!strcmp(argv[1], "all")) {
317 run_collection(coll);
321 for_each_bench(coll, bench) {
322 if (strcmp(bench->name, argv[1]))
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);
331 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
332 dump_benchmarks(coll);
336 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
341 printf("Unknown collection: '%s'\n", argv[0]);