regulator: s2mps11: Adjust supported buck voltages to real values
[linux/fpc-iii.git] / tools / perf / builtin-bench.c
blob334c77ffc1d9627fedd5be4edf510139d291bf0d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * builtin-bench.c
5 * General benchmarking collections provided by perf
7 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
8 */
11 * Available benchmark collection list:
13 * sched ... scheduler and IPC performance
14 * mem ... memory access performance
15 * numa ... NUMA scheduling and MM performance
16 * futex ... Futex performance
17 * epoll ... Event poll performance
19 #include "perf.h"
20 #include "util/util.h"
21 #include <subcmd/parse-options.h>
22 #include "builtin.h"
23 #include "bench/bench.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/prctl.h>
30 typedef int (*bench_fn_t)(int argc, const char **argv);
32 struct bench {
33 const char *name;
34 const char *summary;
35 bench_fn_t fn;
38 #ifdef HAVE_LIBNUMA_SUPPORT
39 static struct bench numa_benchmarks[] = {
40 { "mem", "Benchmark for NUMA workloads", bench_numa },
41 { "all", "Run all NUMA benchmarks", NULL },
42 { NULL, NULL, NULL }
44 #endif
46 static struct bench sched_benchmarks[] = {
47 { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
48 { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
49 { "all", "Run all scheduler benchmarks", NULL },
50 { NULL, NULL, NULL }
53 static struct bench mem_benchmarks[] = {
54 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy },
55 { "memset", "Benchmark for memset() functions", bench_mem_memset },
56 { "all", "Run all memory access benchmarks", NULL },
57 { NULL, NULL, NULL }
60 static struct bench futex_benchmarks[] = {
61 { "hash", "Benchmark for futex hash table", bench_futex_hash },
62 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
63 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel },
64 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
65 /* pi-futexes */
66 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
67 { "all", "Run all futex benchmarks", NULL },
68 { NULL, NULL, NULL }
71 #ifdef HAVE_EVENTFD
72 static struct bench epoll_benchmarks[] = {
73 { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait },
74 { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl },
75 { "all", "Run all futex benchmarks", NULL },
76 { NULL, NULL, NULL }
78 #endif // HAVE_EVENTFD
80 struct collection {
81 const char *name;
82 const char *summary;
83 struct bench *benchmarks;
86 static struct collection collections[] = {
87 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
88 { "mem", "Memory access benchmarks", mem_benchmarks },
89 #ifdef HAVE_LIBNUMA_SUPPORT
90 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
91 #endif
92 {"futex", "Futex stressing benchmarks", futex_benchmarks },
93 #ifdef HAVE_EVENTFD
94 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
95 #endif
96 { "all", "All benchmarks", NULL },
97 { NULL, NULL, NULL }
100 /* Iterate over all benchmark collections: */
101 #define for_each_collection(coll) \
102 for (coll = collections; coll->name; coll++)
104 /* Iterate over all benchmarks within a collection: */
105 #define for_each_bench(coll, bench) \
106 for (bench = coll->benchmarks; bench && bench->name; bench++)
108 static void dump_benchmarks(struct collection *coll)
110 struct bench *bench;
112 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
114 for_each_bench(coll, bench)
115 printf("%14s: %s\n", bench->name, bench->summary);
117 printf("\n");
120 static const char *bench_format_str;
122 /* Output/formatting style, exported to benchmark modules: */
123 int bench_format = BENCH_FORMAT_DEFAULT;
124 unsigned int bench_repeat = 10; /* default number of times to repeat the run */
126 static const struct option bench_options[] = {
127 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"),
128 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
129 OPT_END()
132 static const char * const bench_usage[] = {
133 "perf bench [<common options>] <collection> <benchmark> [<options>]",
134 NULL
137 static void print_usage(void)
139 struct collection *coll;
140 int i;
142 printf("Usage: \n");
143 for (i = 0; bench_usage[i]; i++)
144 printf("\t%s\n", bench_usage[i]);
145 printf("\n");
147 printf(" # List of all available benchmark collections:\n\n");
149 for_each_collection(coll)
150 printf("%14s: %s\n", coll->name, coll->summary);
151 printf("\n");
154 static int bench_str2int(const char *str)
156 if (!str)
157 return BENCH_FORMAT_DEFAULT;
159 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
160 return BENCH_FORMAT_DEFAULT;
161 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
162 return BENCH_FORMAT_SIMPLE;
164 return BENCH_FORMAT_UNKNOWN;
168 * Run a specific benchmark but first rename the running task's ->comm[]
169 * to something meaningful:
171 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
172 int argc, const char **argv)
174 int size;
175 char *name;
176 int ret;
178 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
180 name = zalloc(size);
181 BUG_ON(!name);
183 scnprintf(name, size, "%s-%s", coll_name, bench_name);
185 prctl(PR_SET_NAME, name);
186 argv[0] = name;
188 ret = fn(argc, argv);
190 free(name);
192 return ret;
195 static void run_collection(struct collection *coll)
197 struct bench *bench;
198 const char *argv[2];
200 argv[1] = NULL;
202 * TODO:
204 * Preparing preset parameters for
205 * embedded, ordinary PC, HPC, etc...
206 * would be helpful.
208 for_each_bench(coll, bench) {
209 if (!bench->fn)
210 break;
211 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
212 fflush(stdout);
214 argv[1] = bench->name;
215 run_bench(coll->name, bench->name, bench->fn, 1, argv);
216 printf("\n");
220 static void run_all_collections(void)
222 struct collection *coll;
224 for_each_collection(coll)
225 run_collection(coll);
228 int cmd_bench(int argc, const char **argv)
230 struct collection *coll;
231 int ret = 0;
233 if (argc < 2) {
234 /* No collection specified. */
235 print_usage();
236 goto end;
239 argc = parse_options(argc, argv, bench_options, bench_usage,
240 PARSE_OPT_STOP_AT_NON_OPTION);
242 bench_format = bench_str2int(bench_format_str);
243 if (bench_format == BENCH_FORMAT_UNKNOWN) {
244 printf("Unknown format descriptor: '%s'\n", bench_format_str);
245 goto end;
248 if (bench_repeat == 0) {
249 printf("Invalid repeat option: Must specify a positive value\n");
250 goto end;
253 if (argc < 1) {
254 print_usage();
255 goto end;
258 if (!strcmp(argv[0], "all")) {
259 run_all_collections();
260 goto end;
263 for_each_collection(coll) {
264 struct bench *bench;
266 if (strcmp(coll->name, argv[0]))
267 continue;
269 if (argc < 2) {
270 /* No bench specified. */
271 dump_benchmarks(coll);
272 goto end;
275 if (!strcmp(argv[1], "all")) {
276 run_collection(coll);
277 goto end;
280 for_each_bench(coll, bench) {
281 if (strcmp(bench->name, argv[1]))
282 continue;
284 if (bench_format == BENCH_FORMAT_DEFAULT)
285 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
286 fflush(stdout);
287 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
288 goto end;
291 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
292 dump_benchmarks(coll);
293 goto end;
296 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
297 ret = 1;
298 goto end;
301 printf("Unknown collection: '%s'\n", argv[0]);
302 ret = 1;
304 end:
305 return ret;