builtin-bench.c 6.5 KB
Newer Older
1 2 3
/*
 * builtin-bench.c
 *
4
 * General benchmarking collections provided by perf
5 6 7 8 9
 *
 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
 */

/*
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 18 19 20 21 22 23 24 25
 */
#include "perf.h"
#include "util/util.h"
#include "util/parse-options.h"
#include "builtin.h"
#include "bench/bench.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
26
#include <sys/prctl.h>
27

28 29 30 31 32 33
typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix);

struct bench {
	const char	*name;
	const char	*summary;
	bench_fn_t	fn;
34 35
};

36
#ifdef HAVE_LIBNUMA_SUPPORT
37 38 39 40
static struct bench numa_benchmarks[] = {
	{ "mem",	"Benchmark for NUMA workloads",			bench_numa		},
	{ "all",	"Test all NUMA benchmarks",			NULL			},
	{ NULL,		NULL,						NULL			}
41
};
42
#endif
43

44 45 46 47 48
static struct bench sched_benchmarks[] = {
	{ "messaging",	"Benchmark for scheduling and IPC",		bench_sched_messaging	},
	{ "pipe",	"Benchmark for pipe() between two processes",	bench_sched_pipe	},
	{ "all",	"Test all scheduler benchmarks",		NULL			},
	{ NULL,		NULL,						NULL			}
49 50
};

51 52 53 54 55
static struct bench mem_benchmarks[] = {
	{ "memcpy",	"Benchmark for memcpy()",			bench_mem_memcpy	},
	{ "memset",	"Benchmark for memset() tests",			bench_mem_memset	},
	{ "all",	"Test all memory benchmarks",			NULL			},
	{ NULL,		NULL,						NULL			}
56 57
};

58 59
static struct bench futex_benchmarks[] = {
	{ "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 63 64 65
	{ "all",	"Test all futex benchmarks",			NULL			},
	{ NULL,		NULL,						NULL			}
};

66 67 68 69
struct collection {
	const char	*name;
	const char	*summary;
	struct bench	*benchmarks;
70 71
};

72
static 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 80
	{ "all",	"All benchmarks",				NULL			},
	{ NULL,		NULL,						NULL			}
81 82
};

83 84 85 86 87 88
/* Iterate over all benchmark collections: */
#define for_each_collection(coll) \
	for (coll = collections; coll->name; coll++)

/* Iterate over all benchmarks within a collection: */
#define for_each_bench(coll, bench) \
89
	for (bench = coll->benchmarks; bench && bench->name; bench++)
90 91

static 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 98
	for_each_bench(coll, bench)
		printf("%14s: %s\n", bench->name, bench->summary);
99 100 101 102

	printf("\n");
}

103
static const char *bench_format_str;
104 105

/* Output/formatting style, exported to benchmark modules: */
106 107 108
int bench_format = BENCH_FORMAT_DEFAULT;

static const struct option bench_options[] = {
109
	OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
110 111 112 113
	OPT_END()
};

static const char * const bench_usage[] = {
114
	"perf bench [<common options>] <collection> <benchmark> [<options>]",
115 116 117 118 119
	NULL
};

static void print_usage(void)
{
120
	struct collection *coll;
121 122 123 124 125 126 127
	int i;

	printf("Usage: \n");
	for (i = 0; bench_usage[i]; i++)
		printf("\t%s\n", bench_usage[i]);
	printf("\n");

128
	printf("        # List of all available benchmark collections:\n\n");
129

130 131
	for_each_collection(coll)
		printf("%14s: %s\n", coll->name, coll->summary);
132 133 134
	printf("\n");
}

135
static int bench_str2int(const char *str)
136 137 138 139 140 141 142 143 144 145 146 147
{
	if (!str)
		return BENCH_FORMAT_DEFAULT;

	if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
		return BENCH_FORMAT_DEFAULT;
	else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
		return BENCH_FORMAT_SIMPLE;

	return BENCH_FORMAT_UNKNOWN;
}

148 149 150 151 152 153
/*
 * Run a specific benchmark but first rename the running task's ->comm[]
 * to something meaningful:
 */
static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
		     int argc, const char **argv, const char *prefix)
154
{
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	int size;
	char *name;
	int ret;

	size = strlen(coll_name) + 1 + strlen(bench_name) + 1;

	name = zalloc(size);
	BUG_ON(!name);

	scnprintf(name, size, "%s-%s", coll_name, bench_name);

	prctl(PR_SET_NAME, name);
	argv[0] = name;

	ret = fn(argc, argv, prefix);

	free(name);

	return ret;
}

static void run_collection(struct collection *coll)
{
	struct bench *bench;
179 180 181 182 183
	const char *argv[2];

	argv[1] = NULL;
	/*
	 * TODO:
184 185
	 *
	 * Preparing preset parameters for
186
	 * embedded, ordinary PC, HPC, etc...
187
	 * would be helpful.
188
	 */
189 190 191 192
	for_each_bench(coll, bench) {
		if (!bench->fn)
			break;
		printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
193
		fflush(stdout);
194

195 196
		argv[1] = bench->name;
		run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
197 198 199 200
		printf("\n");
	}
}

201
static void run_all_collections(void)
202
{
203 204 205 206
	struct collection *coll;

	for_each_collection(coll)
		run_collection(coll);
207 208
}

209
int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
210
{
211 212
	struct collection *coll;
	int ret = 0;
213 214

	if (argc < 2) {
215
		/* No collection specified. */
216 217 218
		print_usage();
		goto end;
	}
219

220 221 222 223 224
	argc = parse_options(argc, argv, bench_options, bench_usage,
			     PARSE_OPT_STOP_AT_NON_OPTION);

	bench_format = bench_str2int(bench_format_str);
	if (bench_format == BENCH_FORMAT_UNKNOWN) {
225
		printf("Unknown format descriptor: '%s'\n", bench_format_str);
226 227
		goto end;
	}
228

229 230
	if (argc < 1) {
		print_usage();
231 232 233
		goto end;
	}

234
	if (!strcmp(argv[0], "all")) {
235
		run_all_collections();
236 237 238
		goto end;
	}

239 240 241 242
	for_each_collection(coll) {
		struct bench *bench;

		if (strcmp(coll->name, argv[0]))
243 244
			continue;

245
		if (argc < 2) {
246 247
			/* No bench specified. */
			dump_benchmarks(coll);
248 249 250
			goto end;
		}

251
		if (!strcmp(argv[1], "all")) {
252
			run_collection(coll);
253 254 255
			goto end;
		}

256 257
		for_each_bench(coll, bench) {
			if (strcmp(bench->name, argv[1]))
258 259
				continue;

260
			if (bench_format == BENCH_FORMAT_DEFAULT)
261
				printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
262
			fflush(stdout);
263
			ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
264 265 266
			goto end;
		}

267
		if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
268
			dump_benchmarks(coll);
269 270 271
			goto end;
		}

272 273
		printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
		ret = 1;
274 275 276
		goto end;
	}

277 278
	printf("Unknown collection: '%s'\n", argv[0]);
	ret = 1;
279 280

end:
281
	return ret;
282
}