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