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