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