xref: /openbmc/linux/tools/perf/builtin-bench.c (revision a0439711)
1 /*
2  * builtin-bench.c
3  *
4  * General benchmarking collections provided by perf
5  *
6  * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
7  */
8 
9 /*
10  * Available benchmark collection list:
11  *
12  *  sched ... scheduler and IPC performance
13  *  mem   ... memory access performance
14  *  numa  ... NUMA scheduling and MM performance
15  *  futex ... Futex performance
16  */
17 #include "perf.h"
18 #include "util/util.h"
19 #include "util/parse-options.h"
20 #include "builtin.h"
21 #include "bench/bench.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/prctl.h>
27 
28 typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix);
29 
30 struct bench {
31 	const char	*name;
32 	const char	*summary;
33 	bench_fn_t	fn;
34 };
35 
36 #ifdef HAVE_LIBNUMA_SUPPORT
37 static struct bench numa_benchmarks[] = {
38 	{ "mem",	"Benchmark for NUMA workloads",			bench_numa		},
39 	{ "all",	"Test all NUMA benchmarks",			NULL			},
40 	{ NULL,		NULL,						NULL			}
41 };
42 #endif
43 
44 static struct bench sched_benchmarks[] = {
45 	{ "messaging",	"Benchmark for scheduling and IPC",		bench_sched_messaging	},
46 	{ "pipe",	"Benchmark for pipe() between two processes",	bench_sched_pipe	},
47 	{ "all",	"Test all scheduler benchmarks",		NULL			},
48 	{ NULL,		NULL,						NULL			}
49 };
50 
51 static struct bench mem_benchmarks[] = {
52 	{ "memcpy",	"Benchmark for memcpy()",			bench_mem_memcpy	},
53 	{ "memset",	"Benchmark for memset() tests",			bench_mem_memset	},
54 	{ "all",	"Test all memory benchmarks",			NULL			},
55 	{ NULL,		NULL,						NULL			}
56 };
57 
58 static struct bench futex_benchmarks[] = {
59 	{ "hash",	"Benchmark for futex hash table",               bench_futex_hash	},
60 	{ "all",	"Test all futex benchmarks",			NULL			},
61 	{ NULL,		NULL,						NULL			}
62 };
63 
64 struct collection {
65 	const char	*name;
66 	const char	*summary;
67 	struct bench	*benchmarks;
68 };
69 
70 static struct collection collections[] = {
71 	{ "sched",	"Scheduler and IPC benchmarks",			sched_benchmarks	},
72 	{ "mem",	"Memory access benchmarks",			mem_benchmarks		},
73 #ifdef HAVE_LIBNUMA_SUPPORT
74 	{ "numa",	"NUMA scheduling and MM benchmarks",		numa_benchmarks		},
75 #endif
76 	{"futex",       "Futex stressing benchmarks",                   futex_benchmarks        },
77 	{ "all",	"All benchmarks",				NULL			},
78 	{ NULL,		NULL,						NULL			}
79 };
80 
81 /* Iterate over all benchmark collections: */
82 #define for_each_collection(coll) \
83 	for (coll = collections; coll->name; coll++)
84 
85 /* Iterate over all benchmarks within a collection: */
86 #define for_each_bench(coll, bench) \
87 	for (bench = coll->benchmarks; bench->name; bench++)
88 
89 static void dump_benchmarks(struct collection *coll)
90 {
91 	struct bench *bench;
92 
93 	printf("\n        # List of available benchmarks for collection '%s':\n\n", coll->name);
94 
95 	for_each_bench(coll, bench)
96 		printf("%14s: %s\n", bench->name, bench->summary);
97 
98 	printf("\n");
99 }
100 
101 static const char *bench_format_str;
102 
103 /* Output/formatting style, exported to benchmark modules: */
104 int bench_format = BENCH_FORMAT_DEFAULT;
105 
106 static const struct option bench_options[] = {
107 	OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
108 	OPT_END()
109 };
110 
111 static const char * const bench_usage[] = {
112 	"perf bench [<common options>] <collection> <benchmark> [<options>]",
113 	NULL
114 };
115 
116 static void print_usage(void)
117 {
118 	struct collection *coll;
119 	int i;
120 
121 	printf("Usage: \n");
122 	for (i = 0; bench_usage[i]; i++)
123 		printf("\t%s\n", bench_usage[i]);
124 	printf("\n");
125 
126 	printf("        # List of all available benchmark collections:\n\n");
127 
128 	for_each_collection(coll)
129 		printf("%14s: %s\n", coll->name, coll->summary);
130 	printf("\n");
131 }
132 
133 static int bench_str2int(const char *str)
134 {
135 	if (!str)
136 		return BENCH_FORMAT_DEFAULT;
137 
138 	if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
139 		return BENCH_FORMAT_DEFAULT;
140 	else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
141 		return BENCH_FORMAT_SIMPLE;
142 
143 	return BENCH_FORMAT_UNKNOWN;
144 }
145 
146 /*
147  * Run a specific benchmark but first rename the running task's ->comm[]
148  * to something meaningful:
149  */
150 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
151 		     int argc, const char **argv, const char *prefix)
152 {
153 	int size;
154 	char *name;
155 	int ret;
156 
157 	size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
158 
159 	name = zalloc(size);
160 	BUG_ON(!name);
161 
162 	scnprintf(name, size, "%s-%s", coll_name, bench_name);
163 
164 	prctl(PR_SET_NAME, name);
165 	argv[0] = name;
166 
167 	ret = fn(argc, argv, prefix);
168 
169 	free(name);
170 
171 	return ret;
172 }
173 
174 static void run_collection(struct collection *coll)
175 {
176 	struct bench *bench;
177 	const char *argv[2];
178 
179 	argv[1] = NULL;
180 	/*
181 	 * TODO:
182 	 *
183 	 * Preparing preset parameters for
184 	 * embedded, ordinary PC, HPC, etc...
185 	 * would be helpful.
186 	 */
187 	for_each_bench(coll, bench) {
188 		if (!bench->fn)
189 			break;
190 		printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
191 		fflush(stdout);
192 
193 		argv[1] = bench->name;
194 		run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
195 		printf("\n");
196 	}
197 }
198 
199 static void run_all_collections(void)
200 {
201 	struct collection *coll;
202 
203 	for_each_collection(coll)
204 		run_collection(coll);
205 }
206 
207 int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
208 {
209 	struct collection *coll;
210 	int ret = 0;
211 
212 	if (argc < 2) {
213 		/* No collection specified. */
214 		print_usage();
215 		goto end;
216 	}
217 
218 	argc = parse_options(argc, argv, bench_options, bench_usage,
219 			     PARSE_OPT_STOP_AT_NON_OPTION);
220 
221 	bench_format = bench_str2int(bench_format_str);
222 	if (bench_format == BENCH_FORMAT_UNKNOWN) {
223 		printf("Unknown format descriptor: '%s'\n", bench_format_str);
224 		goto end;
225 	}
226 
227 	if (argc < 1) {
228 		print_usage();
229 		goto end;
230 	}
231 
232 	if (!strcmp(argv[0], "all")) {
233 		run_all_collections();
234 		goto end;
235 	}
236 
237 	for_each_collection(coll) {
238 		struct bench *bench;
239 
240 		if (strcmp(coll->name, argv[0]))
241 			continue;
242 
243 		if (argc < 2) {
244 			/* No bench specified. */
245 			dump_benchmarks(coll);
246 			goto end;
247 		}
248 
249 		if (!strcmp(argv[1], "all")) {
250 			run_collection(coll);
251 			goto end;
252 		}
253 
254 		for_each_bench(coll, bench) {
255 			if (strcmp(bench->name, argv[1]))
256 				continue;
257 
258 			if (bench_format == BENCH_FORMAT_DEFAULT)
259 				printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
260 			fflush(stdout);
261 			ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
262 			goto end;
263 		}
264 
265 		if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
266 			dump_benchmarks(coll);
267 			goto end;
268 		}
269 
270 		printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
271 		ret = 1;
272 		goto end;
273 	}
274 
275 	printf("Unknown collection: '%s'\n", argv[0]);
276 	ret = 1;
277 
278 end:
279 	return ret;
280 }
281