1 // SPDX-License-Identifier: GPL-2.0
5 * General benchmarking collections provided by perf
7 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
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
20 #include "util/util.h"
21 #include <subcmd/parse-options.h>
23 #include "bench/bench.h"
28 #include <sys/prctl.h>
30 typedef int (*bench_fn_t)(int argc, const char **argv);
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 },
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 },
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 },
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 },
66 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi },
67 { "all", "Run all futex benchmarks", NULL },
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 },
78 #endif // HAVE_EVENTFD
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 },
92 {"futex", "Futex stressing benchmarks", futex_benchmarks },
94 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
96 { "all", "All benchmarks", 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)
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);
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"),
132 static const char * const bench_usage[] = {
133 "perf bench [<common options>] <collection> <benchmark> [<options>]",
137 static void print_usage(void)
139 struct collection *coll;
143 for (i = 0; bench_usage[i]; i++)
144 printf("\t%s\n", bench_usage[i]);
147 printf(" # List of all available benchmark collections:\n\n");
149 for_each_collection(coll)
150 printf("%14s: %s\n", coll->name, coll->summary);
154 static int bench_str2int(const char *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)
178 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
183 scnprintf(name, size, "%s-%s", coll_name, bench_name);
185 prctl(PR_SET_NAME, name);
188 ret = fn(argc, argv);
195 static void run_collection(struct collection *coll)
204 * Preparing preset parameters for
205 * embedded, ordinary PC, HPC, etc...
208 for_each_bench(coll, bench) {
211 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
214 argv[1] = bench->name;
215 run_bench(coll->name, bench->name, bench->fn, 1, argv);
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;
234 /* No collection specified. */
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);
248 if (bench_repeat == 0) {
249 printf("Invalid repeat option: Must specify a positive value\n");
258 if (!strcmp(argv[0], "all")) {
259 run_all_collections();
263 for_each_collection(coll) {
266 if (strcmp(coll->name, argv[0]))
270 /* No bench specified. */
271 dump_benchmarks(coll);
275 if (!strcmp(argv[1], "all")) {
276 run_collection(coll);
280 for_each_bench(coll, bench) {
281 if (strcmp(bench->name, argv[1]))
284 if (bench_format == BENCH_FORMAT_DEFAULT)
285 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
287 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
291 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
292 dump_benchmarks(coll);
296 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
301 printf("Unknown collection: '%s'\n", argv[0]);