xref: /openbmc/linux/tools/perf/builtin-bench.c (revision 4daa669e)
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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/prctl.h>
28 #include <linux/zalloc.h>
29 
30 typedef int (*bench_fn_t)(int argc, const char **argv);
31 
32 struct bench {
33 	const char	*name;
34 	const char	*summary;
35 	bench_fn_t	fn;
36 };
37 
38 #ifdef HAVE_LIBNUMA_SUPPORT
39 static struct bench numa_benchmarks[] = {
40 	{ "mem",	"Benchmark for NUMA workloads",			bench_numa		},
41 	{ "all",	"Run all NUMA benchmarks",			NULL			},
42 	{ NULL,		NULL,						NULL			}
43 };
44 #endif
45 
46 static struct bench sched_benchmarks[] = {
47 	{ "messaging",	"Benchmark for scheduling and IPC",		bench_sched_messaging	},
48 	{ "pipe",	"Benchmark for pipe() between two processes",	bench_sched_pipe	},
49 	{ "all",	"Run all scheduler benchmarks",		NULL			},
50 	{ NULL,		NULL,						NULL			}
51 };
52 
53 static struct bench syscall_benchmarks[] = {
54 	{ "basic",	"Benchmark for basic getppid(2) calls",		bench_syscall_basic	},
55 	{ "getpgid",	"Benchmark for getpgid(2) calls",		bench_syscall_getpgid	},
56 	{ "execve",	"Benchmark for execve(2) calls",		bench_syscall_execve	},
57 	{ "all",	"Run all syscall benchmarks",			NULL			},
58 	{ NULL,		NULL,						NULL			},
59 };
60 
61 static struct bench mem_benchmarks[] = {
62 	{ "memcpy",	"Benchmark for memcpy() functions",		bench_mem_memcpy	},
63 	{ "memset",	"Benchmark for memset() functions",		bench_mem_memset	},
64 	{ "find_bit",	"Benchmark for find_bit() functions",		bench_mem_find_bit	},
65 	{ "all",	"Run all memory access benchmarks",		NULL			},
66 	{ NULL,		NULL,						NULL			}
67 };
68 
69 static struct bench futex_benchmarks[] = {
70 	{ "hash",	"Benchmark for futex hash table",               bench_futex_hash	},
71 	{ "wake",	"Benchmark for futex wake calls",               bench_futex_wake	},
72 	{ "wake-parallel", "Benchmark for parallel futex wake calls",   bench_futex_wake_parallel },
73 	{ "requeue",	"Benchmark for futex requeue calls",            bench_futex_requeue	},
74 	/* pi-futexes */
75 	{ "lock-pi",	"Benchmark for futex lock_pi calls",            bench_futex_lock_pi	},
76 	{ "all",	"Run all futex benchmarks",			NULL			},
77 	{ NULL,		NULL,						NULL			}
78 };
79 
80 #ifdef HAVE_EVENTFD_SUPPORT
81 static struct bench epoll_benchmarks[] = {
82 	{ "wait",	"Benchmark epoll concurrent epoll_waits",       bench_epoll_wait	},
83 	{ "ctl",	"Benchmark epoll concurrent epoll_ctls",        bench_epoll_ctl		},
84 	{ "all",	"Run all futex benchmarks",			NULL			},
85 	{ NULL,		NULL,						NULL			}
86 };
87 #endif // HAVE_EVENTFD_SUPPORT
88 
89 static struct bench internals_benchmarks[] = {
90 	{ "synthesize", "Benchmark perf event synthesis",	bench_synthesize	},
91 	{ "kallsyms-parse", "Benchmark kallsyms parsing",	bench_kallsyms_parse	},
92 	{ "inject-build-id", "Benchmark build-id injection",	bench_inject_build_id	},
93 	{ "evlist-open-close", "Benchmark evlist open and close",	bench_evlist_open_close	},
94 	{ NULL,		NULL,					NULL			}
95 };
96 
97 static struct bench breakpoint_benchmarks[] = {
98 	{ "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
99 	{ "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
100 	{ "all", "Run all breakpoint benchmarks", NULL},
101 	{ NULL,	NULL, NULL },
102 };
103 
104 struct collection {
105 	const char	*name;
106 	const char	*summary;
107 	struct bench	*benchmarks;
108 };
109 
110 static struct collection collections[] = {
111 	{ "sched",	"Scheduler and IPC benchmarks",			sched_benchmarks	},
112 	{ "syscall",	"System call benchmarks",			syscall_benchmarks	},
113 	{ "mem",	"Memory access benchmarks",			mem_benchmarks		},
114 #ifdef HAVE_LIBNUMA_SUPPORT
115 	{ "numa",	"NUMA scheduling and MM benchmarks",		numa_benchmarks		},
116 #endif
117 	{"futex",       "Futex stressing benchmarks",                   futex_benchmarks        },
118 #ifdef HAVE_EVENTFD_SUPPORT
119 	{"epoll",       "Epoll stressing benchmarks",                   epoll_benchmarks        },
120 #endif
121 	{ "internals",	"Perf-internals benchmarks",			internals_benchmarks	},
122 	{ "breakpoint",	"Breakpoint benchmarks",			breakpoint_benchmarks	},
123 	{ "all",	"All benchmarks",				NULL			},
124 	{ NULL,		NULL,						NULL			}
125 };
126 
127 /* Iterate over all benchmark collections: */
128 #define for_each_collection(coll) \
129 	for (coll = collections; coll->name; coll++)
130 
131 /* Iterate over all benchmarks within a collection: */
132 #define for_each_bench(coll, bench) \
133 	for (bench = coll->benchmarks; bench && bench->name; bench++)
134 
135 static void dump_benchmarks(struct collection *coll)
136 {
137 	struct bench *bench;
138 
139 	printf("\n        # List of available benchmarks for collection '%s':\n\n", coll->name);
140 
141 	for_each_bench(coll, bench)
142 		printf("%14s: %s\n", bench->name, bench->summary);
143 
144 	printf("\n");
145 }
146 
147 static const char *bench_format_str;
148 
149 /* Output/formatting style, exported to benchmark modules: */
150 int bench_format = BENCH_FORMAT_DEFAULT;
151 unsigned int bench_repeat = 10; /* default number of times to repeat the run */
152 
153 static const struct option bench_options[] = {
154 	OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
155 	OPT_UINTEGER('r', "repeat",  &bench_repeat,   "Specify number of times to repeat the run"),
156 	OPT_END()
157 };
158 
159 static const char * const bench_usage[] = {
160 	"perf bench [<common options>] <collection> <benchmark> [<options>]",
161 	NULL
162 };
163 
164 static void print_usage(void)
165 {
166 	struct collection *coll;
167 	int i;
168 
169 	printf("Usage: \n");
170 	for (i = 0; bench_usage[i]; i++)
171 		printf("\t%s\n", bench_usage[i]);
172 	printf("\n");
173 
174 	printf("        # List of all available benchmark collections:\n\n");
175 
176 	for_each_collection(coll)
177 		printf("%14s: %s\n", coll->name, coll->summary);
178 	printf("\n");
179 }
180 
181 static int bench_str2int(const char *str)
182 {
183 	if (!str)
184 		return BENCH_FORMAT_DEFAULT;
185 
186 	if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
187 		return BENCH_FORMAT_DEFAULT;
188 	else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
189 		return BENCH_FORMAT_SIMPLE;
190 
191 	return BENCH_FORMAT_UNKNOWN;
192 }
193 
194 /*
195  * Run a specific benchmark but first rename the running task's ->comm[]
196  * to something meaningful:
197  */
198 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
199 		     int argc, const char **argv)
200 {
201 	int size;
202 	char *name;
203 	int ret;
204 
205 	size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
206 
207 	name = zalloc(size);
208 	BUG_ON(!name);
209 
210 	scnprintf(name, size, "%s-%s", coll_name, bench_name);
211 
212 	prctl(PR_SET_NAME, name);
213 	argv[0] = name;
214 
215 	ret = fn(argc, argv);
216 
217 	free(name);
218 
219 	return ret;
220 }
221 
222 static void run_collection(struct collection *coll)
223 {
224 	struct bench *bench;
225 	const char *argv[2];
226 
227 	argv[1] = NULL;
228 	/*
229 	 * TODO:
230 	 *
231 	 * Preparing preset parameters for
232 	 * embedded, ordinary PC, HPC, etc...
233 	 * would be helpful.
234 	 */
235 	for_each_bench(coll, bench) {
236 		if (!bench->fn)
237 			break;
238 		printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
239 
240 		argv[1] = bench->name;
241 		run_bench(coll->name, bench->name, bench->fn, 1, argv);
242 		printf("\n");
243 	}
244 }
245 
246 static void run_all_collections(void)
247 {
248 	struct collection *coll;
249 
250 	for_each_collection(coll)
251 		run_collection(coll);
252 }
253 
254 int cmd_bench(int argc, const char **argv)
255 {
256 	struct collection *coll;
257 	int ret = 0;
258 
259 	/* Unbuffered output */
260 	setvbuf(stdout, NULL, _IONBF, 0);
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