Blame view

tools/perf/builtin-bench.c 6.9 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
629cc3566   Hitoshi Mitake   perf bench: Add b...
2
  /*
629cc3566   Hitoshi Mitake   perf bench: Add b...
3
4
   * builtin-bench.c
   *
4157922a9   Ingo Molnar   perf bench: Chang...
5
   * General benchmarking collections provided by perf
629cc3566   Hitoshi Mitake   perf bench: Add b...
6
7
   *
   * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
629cc3566   Hitoshi Mitake   perf bench: Add b...
8
9
10
   */
  
  /*
4157922a9   Ingo Molnar   perf bench: Chang...
11
   * Available benchmark collection list:
629cc3566   Hitoshi Mitake   perf bench: Add b...
12
   *
4157922a9   Ingo Molnar   perf bench: Chang...
13
   *  sched ... scheduler and IPC performance
827f3b497   Hitoshi Mitake   perf bench: Add m...
14
   *  mem   ... memory access performance
4157922a9   Ingo Molnar   perf bench: Chang...
15
   *  numa  ... NUMA scheduling and MM performance
a04397114   Davidlohr Bueso   perf bench: Add f...
16
   *  futex ... Futex performance
629cc3566   Hitoshi Mitake   perf bench: Add b...
17
   */
629cc3566   Hitoshi Mitake   perf bench: Add b...
18
19
  #include "perf.h"
  #include "util/util.h"
4b6ab94ea   Josh Poimboeuf   perf subcmd: Crea...
20
  #include <subcmd/parse-options.h>
629cc3566   Hitoshi Mitake   perf bench: Add b...
21
22
23
24
25
26
  #include "builtin.h"
  #include "bench/bench.h"
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
4157922a9   Ingo Molnar   perf bench: Chang...
27
  #include <sys/prctl.h>
629cc3566   Hitoshi Mitake   perf bench: Add b...
28

b0ad8ea66   Arnaldo Carvalho de Melo   perf tools: Remov...
29
  typedef int (*bench_fn_t)(int argc, const char **argv);
4157922a9   Ingo Molnar   perf bench: Chang...
30
31
32
33
34
  
  struct bench {
  	const char	*name;
  	const char	*summary;
  	bench_fn_t	fn;
629cc3566   Hitoshi Mitake   perf bench: Add b...
35
  };
89fe808ae   Ingo Molnar   tools/perf: Stand...
36
  #ifdef HAVE_LIBNUMA_SUPPORT
4157922a9   Ingo Molnar   perf bench: Chang...
37
38
  static struct bench numa_benchmarks[] = {
  	{ "mem",	"Benchmark for NUMA workloads",			bench_numa		},
aa254af25   Ingo Molnar   perf bench: Run b...
39
  	{ "all",	"Run all NUMA benchmarks",			NULL			},
4157922a9   Ingo Molnar   perf bench: Chang...
40
  	{ NULL,		NULL,						NULL			}
1c13f3c90   Ingo Molnar   perf: Add 'perf b...
41
  };
79d824e31   Peter Hurley   perf tools: Make ...
42
  #endif
1c13f3c90   Ingo Molnar   perf: Add 'perf b...
43

4157922a9   Ingo Molnar   perf bench: Chang...
44
45
46
  static struct bench sched_benchmarks[] = {
  	{ "messaging",	"Benchmark for scheduling and IPC",		bench_sched_messaging	},
  	{ "pipe",	"Benchmark for pipe() between two processes",	bench_sched_pipe	},
aa254af25   Ingo Molnar   perf bench: Run b...
47
  	{ "all",	"Run all scheduler benchmarks",		NULL			},
4157922a9   Ingo Molnar   perf bench: Chang...
48
  	{ NULL,		NULL,						NULL			}
629cc3566   Hitoshi Mitake   perf bench: Add b...
49
  };
4157922a9   Ingo Molnar   perf bench: Chang...
50
  static struct bench mem_benchmarks[] = {
13b1fdce8   Ingo Molnar   perf bench mem: I...
51
52
  	{ "memcpy",	"Benchmark for memcpy() functions",		bench_mem_memcpy	},
  	{ "memset",	"Benchmark for memset() functions",		bench_mem_memset	},
aa254af25   Ingo Molnar   perf bench: Run b...
53
  	{ "all",	"Run all memory access benchmarks",		NULL			},
4157922a9   Ingo Molnar   perf bench: Chang...
54
  	{ NULL,		NULL,						NULL			}
827f3b497   Hitoshi Mitake   perf bench: Add m...
55
  };
a04397114   Davidlohr Bueso   perf bench: Add f...
56
57
  static struct bench futex_benchmarks[] = {
  	{ "hash",	"Benchmark for futex hash table",               bench_futex_hash	},
27db78307   Davidlohr Bueso   perf bench: Add f...
58
  	{ "wake",	"Benchmark for futex wake calls",               bench_futex_wake	},
d65817b4e   Davidlohr Bueso   perf bench futex:...
59
  	{ "wake-parallel", "Benchmark for parallel futex wake calls",   bench_futex_wake_parallel },
0fb298cf9   Davidlohr Bueso   perf bench: Add f...
60
  	{ "requeue",	"Benchmark for futex requeue calls",            bench_futex_requeue	},
d2f3f5d2e   Davidlohr Bueso   perf bench futex:...
61
62
  	/* pi-futexes */
  	{ "lock-pi",	"Benchmark for futex lock_pi calls",            bench_futex_lock_pi	},
aa254af25   Ingo Molnar   perf bench: Run b...
63
  	{ "all",	"Run all futex benchmarks",			NULL			},
a04397114   Davidlohr Bueso   perf bench: Add f...
64
65
  	{ NULL,		NULL,						NULL			}
  };
4157922a9   Ingo Molnar   perf bench: Chang...
66
67
68
69
  struct collection {
  	const char	*name;
  	const char	*summary;
  	struct bench	*benchmarks;
629cc3566   Hitoshi Mitake   perf bench: Add b...
70
  };
4157922a9   Ingo Molnar   perf bench: Chang...
71
  static struct collection collections[] = {
a04397114   Davidlohr Bueso   perf bench: Add f...
72
  	{ "sched",	"Scheduler and IPC benchmarks",			sched_benchmarks	},
4157922a9   Ingo Molnar   perf bench: Chang...
73
  	{ "mem",	"Memory access benchmarks",			mem_benchmarks		},
89fe808ae   Ingo Molnar   tools/perf: Stand...
74
  #ifdef HAVE_LIBNUMA_SUPPORT
4157922a9   Ingo Molnar   perf bench: Chang...
75
  	{ "numa",	"NUMA scheduling and MM benchmarks",		numa_benchmarks		},
79d824e31   Peter Hurley   perf tools: Make ...
76
  #endif
a04397114   Davidlohr Bueso   perf bench: Add f...
77
  	{"futex",       "Futex stressing benchmarks",                   futex_benchmarks        },
4157922a9   Ingo Molnar   perf bench: Chang...
78
79
  	{ "all",	"All benchmarks",				NULL			},
  	{ NULL,		NULL,						NULL			}
629cc3566   Hitoshi Mitake   perf bench: Add b...
80
  };
4157922a9   Ingo Molnar   perf bench: Chang...
81
82
83
84
85
86
  /* 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) \
6eeefccdc   Patrick Palka   perf bench: Fix N...
87
  	for (bench = coll->benchmarks; bench && bench->name; bench++)
4157922a9   Ingo Molnar   perf bench: Chang...
88
89
  
  static void dump_benchmarks(struct collection *coll)
629cc3566   Hitoshi Mitake   perf bench: Add b...
90
  {
4157922a9   Ingo Molnar   perf bench: Chang...
91
  	struct bench *bench;
629cc3566   Hitoshi Mitake   perf bench: Add b...
92

4157922a9   Ingo Molnar   perf bench: Chang...
93
94
95
96
  	printf("
          # List of available benchmarks for collection '%s':
  
  ", coll->name);
629cc3566   Hitoshi Mitake   perf bench: Add b...
97

4157922a9   Ingo Molnar   perf bench: Chang...
98
99
100
  	for_each_bench(coll, bench)
  		printf("%14s: %s
  ", bench->name, bench->summary);
629cc3566   Hitoshi Mitake   perf bench: Add b...
101
102
103
  
  	printf("
  ");
629cc3566   Hitoshi Mitake   perf bench: Add b...
104
  }
edb7c60e2   Arnaldo Carvalho de Melo   perf options: Typ...
105
  static const char *bench_format_str;
4157922a9   Ingo Molnar   perf bench: Chang...
106
107
  
  /* Output/formatting style, exported to benchmark modules: */
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
108
  int bench_format = BENCH_FORMAT_DEFAULT;
b6f0629a9   Davidlohr Bueso   perf bench: Add -...
109
  unsigned int bench_repeat = 10; /* default number of times to repeat the run */
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
110
111
  
  static const struct option bench_options[] = {
7a46a8fd1   Ingo Molnar   perf bench: List ...
112
  	OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
b6f0629a9   Davidlohr Bueso   perf bench: Add -...
113
  	OPT_UINTEGER('r', "repeat",  &bench_repeat,   "Specify amount of times to repeat the run"),
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
114
115
116
117
  	OPT_END()
  };
  
  static const char * const bench_usage[] = {
4157922a9   Ingo Molnar   perf bench: Chang...
118
  	"perf bench [<common options>] <collection> <benchmark> [<options>]",
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
119
120
121
122
123
  	NULL
  };
  
  static void print_usage(void)
  {
4157922a9   Ingo Molnar   perf bench: Chang...
124
  	struct collection *coll;
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
125
126
127
128
129
130
131
132
133
  	int i;
  
  	printf("Usage: 
  ");
  	for (i = 0; bench_usage[i]; i++)
  		printf("\t%s
  ", bench_usage[i]);
  	printf("
  ");
4157922a9   Ingo Molnar   perf bench: Chang...
134
135
136
  	printf("        # List of all available benchmark collections:
  
  ");
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
137

4157922a9   Ingo Molnar   perf bench: Chang...
138
139
140
  	for_each_collection(coll)
  		printf("%14s: %s
  ", coll->name, coll->summary);
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
141
142
143
  	printf("
  ");
  }
edb7c60e2   Arnaldo Carvalho de Melo   perf options: Typ...
144
  static int bench_str2int(const char *str)
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
145
146
147
148
149
150
151
152
153
154
155
  {
  	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;
  }
4157922a9   Ingo Molnar   perf bench: Chang...
156
157
158
159
160
  /*
   * 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,
b0ad8ea66   Arnaldo Carvalho de Melo   perf tools: Remov...
161
  		     int argc, const char **argv)
2044279d1   Hitoshi Mitake   perf bench: Add "...
162
  {
4157922a9   Ingo Molnar   perf bench: Chang...
163
164
165
166
167
168
169
170
171
172
173
174
175
  	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;
b0ad8ea66   Arnaldo Carvalho de Melo   perf tools: Remov...
176
  	ret = fn(argc, argv);
4157922a9   Ingo Molnar   perf bench: Chang...
177
178
179
180
181
182
183
184
185
  
  	free(name);
  
  	return ret;
  }
  
  static void run_collection(struct collection *coll)
  {
  	struct bench *bench;
2044279d1   Hitoshi Mitake   perf bench: Add "...
186
  	const char *argv[2];
2044279d1   Hitoshi Mitake   perf bench: Add "...
187
188
189
190
  
  	argv[1] = NULL;
  	/*
  	 * TODO:
4157922a9   Ingo Molnar   perf bench: Chang...
191
192
  	 *
  	 * Preparing preset parameters for
2044279d1   Hitoshi Mitake   perf bench: Add "...
193
  	 * embedded, ordinary PC, HPC, etc...
4157922a9   Ingo Molnar   perf bench: Chang...
194
  	 * would be helpful.
2044279d1   Hitoshi Mitake   perf bench: Add "...
195
  	 */
4157922a9   Ingo Molnar   perf bench: Chang...
196
197
198
199
200
  	for_each_bench(coll, bench) {
  		if (!bench->fn)
  			break;
  		printf("# Running %s/%s benchmark...
  ", coll->name, bench->name);
9b494ea2f   Namhyung Kim   perf bench: Flush...
201
  		fflush(stdout);
2044279d1   Hitoshi Mitake   perf bench: Add "...
202

4157922a9   Ingo Molnar   perf bench: Chang...
203
  		argv[1] = bench->name;
b0ad8ea66   Arnaldo Carvalho de Melo   perf tools: Remov...
204
  		run_bench(coll->name, bench->name, bench->fn, 1, argv);
2044279d1   Hitoshi Mitake   perf bench: Add "...
205
206
207
208
  		printf("
  ");
  	}
  }
4157922a9   Ingo Molnar   perf bench: Chang...
209
  static void run_all_collections(void)
2044279d1   Hitoshi Mitake   perf bench: Add "...
210
  {
4157922a9   Ingo Molnar   perf bench: Chang...
211
212
213
214
  	struct collection *coll;
  
  	for_each_collection(coll)
  		run_collection(coll);
2044279d1   Hitoshi Mitake   perf bench: Add "...
215
  }
b0ad8ea66   Arnaldo Carvalho de Melo   perf tools: Remov...
216
  int cmd_bench(int argc, const char **argv)
629cc3566   Hitoshi Mitake   perf bench: Add b...
217
  {
4157922a9   Ingo Molnar   perf bench: Chang...
218
219
  	struct collection *coll;
  	int ret = 0;
629cc3566   Hitoshi Mitake   perf bench: Add b...
220
221
  
  	if (argc < 2) {
4157922a9   Ingo Molnar   perf bench: Chang...
222
  		/* No collection specified. */
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
223
224
225
  		print_usage();
  		goto end;
  	}
629cc3566   Hitoshi Mitake   perf bench: Add b...
226

386d7e9e5   Hitoshi Mitake   perf bench: Modif...
227
228
229
230
231
  	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) {
4157922a9   Ingo Molnar   perf bench: Chang...
232
233
  		printf("Unknown format descriptor: '%s'
  ", bench_format_str);
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
234
235
  		goto end;
  	}
629cc3566   Hitoshi Mitake   perf bench: Add b...
236

b6f0629a9   Davidlohr Bueso   perf bench: Add -...
237
238
239
240
241
  	if (bench_repeat == 0) {
  		printf("Invalid repeat option: Must specify a positive value
  ");
  		goto end;
  	}
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
242
243
  	if (argc < 1) {
  		print_usage();
629cc3566   Hitoshi Mitake   perf bench: Add b...
244
245
  		goto end;
  	}
2044279d1   Hitoshi Mitake   perf bench: Add "...
246
  	if (!strcmp(argv[0], "all")) {
4157922a9   Ingo Molnar   perf bench: Chang...
247
  		run_all_collections();
2044279d1   Hitoshi Mitake   perf bench: Add "...
248
249
  		goto end;
  	}
4157922a9   Ingo Molnar   perf bench: Chang...
250
251
252
253
  	for_each_collection(coll) {
  		struct bench *bench;
  
  		if (strcmp(coll->name, argv[0]))
629cc3566   Hitoshi Mitake   perf bench: Add b...
254
  			continue;
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
255
  		if (argc < 2) {
4157922a9   Ingo Molnar   perf bench: Chang...
256
257
  			/* No bench specified. */
  			dump_benchmarks(coll);
629cc3566   Hitoshi Mitake   perf bench: Add b...
258
259
  			goto end;
  		}
2044279d1   Hitoshi Mitake   perf bench: Add "...
260
  		if (!strcmp(argv[1], "all")) {
4157922a9   Ingo Molnar   perf bench: Chang...
261
  			run_collection(coll);
2044279d1   Hitoshi Mitake   perf bench: Add "...
262
263
  			goto end;
  		}
4157922a9   Ingo Molnar   perf bench: Chang...
264
265
  		for_each_bench(coll, bench) {
  			if (strcmp(bench->name, argv[1]))
629cc3566   Hitoshi Mitake   perf bench: Add b...
266
  				continue;
79e295d4b   Hitoshi Mitake   perf bench: Impro...
267
  			if (bench_format == BENCH_FORMAT_DEFAULT)
4157922a9   Ingo Molnar   perf bench: Chang...
268
269
  				printf("# Running '%s/%s' benchmark:
  ", coll->name, bench->name);
9b494ea2f   Namhyung Kim   perf bench: Flush...
270
  			fflush(stdout);
b0ad8ea66   Arnaldo Carvalho de Melo   perf tools: Remov...
271
  			ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
629cc3566   Hitoshi Mitake   perf bench: Add b...
272
273
  			goto end;
  		}
386d7e9e5   Hitoshi Mitake   perf bench: Modif...
274
  		if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
4157922a9   Ingo Molnar   perf bench: Chang...
275
  			dump_benchmarks(coll);
629cc3566   Hitoshi Mitake   perf bench: Add b...
276
277
  			goto end;
  		}
4157922a9   Ingo Molnar   perf bench: Chang...
278
279
280
  		printf("Unknown benchmark: '%s' for collection '%s'
  ", argv[1], argv[0]);
  		ret = 1;
629cc3566   Hitoshi Mitake   perf bench: Add b...
281
282
  		goto end;
  	}
4157922a9   Ingo Molnar   perf bench: Chang...
283
284
285
  	printf("Unknown collection: '%s'
  ", argv[0]);
  	ret = 1;
629cc3566   Hitoshi Mitake   perf bench: Add b...
286
287
  
  end:
4157922a9   Ingo Molnar   perf bench: Chang...
288
  	return ret;
629cc3566   Hitoshi Mitake   perf bench: Add b...
289
  }