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
28typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix);
29
30struct bench {
31	const char	*name;
32	const char	*summary;
33	bench_fn_t	fn;
34};
35
36#ifdef HAVE_LIBNUMA_SUPPORT
37static 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
44static 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
51static 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
58static struct bench futex_benchmarks[] = {
59	{ "hash",	"Benchmark for futex hash table",               bench_futex_hash	},
60	{ "wake",	"Benchmark for futex wake calls",               bench_futex_wake	},
61	{ "requeue",	"Benchmark for futex requeue calls",            bench_futex_requeue	},
62	{ "all",	"Test all futex benchmarks",			NULL			},
63	{ NULL,		NULL,						NULL			}
64};
65
66struct collection {
67	const char	*name;
68	const char	*summary;
69	struct bench	*benchmarks;
70};
71
72static struct collection collections[] = {
73	{ "sched",	"Scheduler and IPC benchmarks",			sched_benchmarks	},
74	{ "mem",	"Memory access benchmarks",			mem_benchmarks		},
75#ifdef HAVE_LIBNUMA_SUPPORT
76	{ "numa",	"NUMA scheduling and MM benchmarks",		numa_benchmarks		},
77#endif
78	{"futex",       "Futex stressing benchmarks",                   futex_benchmarks        },
79	{ "all",	"All benchmarks",				NULL			},
80	{ NULL,		NULL,						NULL			}
81};
82
83/* Iterate over all benchmark collections: */
84#define for_each_collection(coll) \
85	for (coll = collections; coll->name; coll++)
86
87/* Iterate over all benchmarks within a collection: */
88#define for_each_bench(coll, bench) \
89	for (bench = coll->benchmarks; bench && bench->name; bench++)
90
91static void dump_benchmarks(struct collection *coll)
92{
93	struct bench *bench;
94
95	printf("\n        # List of available benchmarks for collection '%s':\n\n", coll->name);
96
97	for_each_bench(coll, bench)
98		printf("%14s: %s\n", bench->name, bench->summary);
99
100	printf("\n");
101}
102
103static const char *bench_format_str;
104
105/* Output/formatting style, exported to benchmark modules: */
106int bench_format = BENCH_FORMAT_DEFAULT;
107unsigned int bench_repeat = 10; /* default number of times to repeat the run */
108
109static const struct option bench_options[] = {
110	OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
111	OPT_UINTEGER('r', "repeat",  &bench_repeat,   "Specify amount of times to repeat the run"),
112	OPT_END()
113};
114
115static const char * const bench_usage[] = {
116	"perf bench [<common options>] <collection> <benchmark> [<options>]",
117	NULL
118};
119
120static void print_usage(void)
121{
122	struct collection *coll;
123	int i;
124
125	printf("Usage: \n");
126	for (i = 0; bench_usage[i]; i++)
127		printf("\t%s\n", bench_usage[i]);
128	printf("\n");
129
130	printf("        # List of all available benchmark collections:\n\n");
131
132	for_each_collection(coll)
133		printf("%14s: %s\n", coll->name, coll->summary);
134	printf("\n");
135}
136
137static int bench_str2int(const char *str)
138{
139	if (!str)
140		return BENCH_FORMAT_DEFAULT;
141
142	if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
143		return BENCH_FORMAT_DEFAULT;
144	else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
145		return BENCH_FORMAT_SIMPLE;
146
147	return BENCH_FORMAT_UNKNOWN;
148}
149
150/*
151 * Run a specific benchmark but first rename the running task's ->comm[]
152 * to something meaningful:
153 */
154static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
155		     int argc, const char **argv, const char *prefix)
156{
157	int size;
158	char *name;
159	int ret;
160
161	size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
162
163	name = zalloc(size);
164	BUG_ON(!name);
165
166	scnprintf(name, size, "%s-%s", coll_name, bench_name);
167
168	prctl(PR_SET_NAME, name);
169	argv[0] = name;
170
171	ret = fn(argc, argv, prefix);
172
173	free(name);
174
175	return ret;
176}
177
178static void run_collection(struct collection *coll)
179{
180	struct bench *bench;
181	const char *argv[2];
182
183	argv[1] = NULL;
184	/*
185	 * TODO:
186	 *
187	 * Preparing preset parameters for
188	 * embedded, ordinary PC, HPC, etc...
189	 * would be helpful.
190	 */
191	for_each_bench(coll, bench) {
192		if (!bench->fn)
193			break;
194		printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
195		fflush(stdout);
196
197		argv[1] = bench->name;
198		run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
199		printf("\n");
200	}
201}
202
203static void run_all_collections(void)
204{
205	struct collection *coll;
206
207	for_each_collection(coll)
208		run_collection(coll);
209}
210
211int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
212{
213	struct collection *coll;
214	int ret = 0;
215
216	if (argc < 2) {
217		/* No collection specified. */
218		print_usage();
219		goto end;
220	}
221
222	argc = parse_options(argc, argv, bench_options, bench_usage,
223			     PARSE_OPT_STOP_AT_NON_OPTION);
224
225	bench_format = bench_str2int(bench_format_str);
226	if (bench_format == BENCH_FORMAT_UNKNOWN) {
227		printf("Unknown format descriptor: '%s'\n", bench_format_str);
228		goto end;
229	}
230
231	if (bench_repeat == 0) {
232		printf("Invalid repeat option: Must specify a positive value\n");
233		goto end;
234	}
235
236	if (argc < 1) {
237		print_usage();
238		goto end;
239	}
240
241	if (!strcmp(argv[0], "all")) {
242		run_all_collections();
243		goto end;
244	}
245
246	for_each_collection(coll) {
247		struct bench *bench;
248
249		if (strcmp(coll->name, argv[0]))
250			continue;
251
252		if (argc < 2) {
253			/* No bench specified. */
254			dump_benchmarks(coll);
255			goto end;
256		}
257
258		if (!strcmp(argv[1], "all")) {
259			run_collection(coll);
260			goto end;
261		}
262
263		for_each_bench(coll, bench) {
264			if (strcmp(bench->name, argv[1]))
265				continue;
266
267			if (bench_format == BENCH_FORMAT_DEFAULT)
268				printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
269			fflush(stdout);
270			ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
271			goto end;
272		}
273
274		if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
275			dump_benchmarks(coll);
276			goto end;
277		}
278
279		printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
280		ret = 1;
281		goto end;
282	}
283
284	printf("Unknown collection: '%s'\n", argv[0]);
285	ret = 1;
286
287end:
288	return ret;
289}
290