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