4 * General benchmarking collections provided by perf
6 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
10 * Available benchmark collection list:
12 * sched ... scheduler and IPC performance
13 * mem ... memory access performance
14 * numa ... NUMA scheduling and MM performance
15 * futex ... Futex performance
18 #include "util/util.h"
19 #include <subcmd/parse-options.h>
21 #include "bench/bench.h"
26 #include <sys/prctl.h>
28 typedef int (*bench_fn_t
)(int argc
, const char **argv
, const char *prefix
);
36 #ifdef HAVE_LIBNUMA_SUPPORT
37 static struct bench numa_benchmarks
[] = {
38 { "mem", "Benchmark for NUMA workloads", bench_numa
},
39 { "all", "Run all NUMA benchmarks", NULL
},
44 static 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", "Run all scheduler benchmarks", NULL
},
51 static struct bench mem_benchmarks
[] = {
52 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy
},
53 { "memset", "Benchmark for memset() functions", bench_mem_memset
},
54 { "all", "Run all memory access benchmarks", NULL
},
58 static 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 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel
},
62 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue
},
64 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi
},
65 { "all", "Run all futex benchmarks", NULL
},
72 struct bench
*benchmarks
;
75 static struct collection collections
[] = {
76 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks
},
77 { "mem", "Memory access benchmarks", mem_benchmarks
},
78 #ifdef HAVE_LIBNUMA_SUPPORT
79 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks
},
81 {"futex", "Futex stressing benchmarks", futex_benchmarks
},
82 { "all", "All benchmarks", NULL
},
86 /* Iterate over all benchmark collections: */
87 #define for_each_collection(coll) \
88 for (coll = collections; coll->name; coll++)
90 /* Iterate over all benchmarks within a collection: */
91 #define for_each_bench(coll, bench) \
92 for (bench = coll->benchmarks; bench && bench->name; bench++)
94 static void dump_benchmarks(struct collection
*coll
)
98 printf("\n # List of available benchmarks for collection '%s':\n\n", coll
->name
);
100 for_each_bench(coll
, bench
)
101 printf("%14s: %s\n", bench
->name
, bench
->summary
);
106 static const char *bench_format_str
;
108 /* Output/formatting style, exported to benchmark modules: */
109 int bench_format
= BENCH_FORMAT_DEFAULT
;
110 unsigned int bench_repeat
= 10; /* default number of times to repeat the run */
112 static const struct option bench_options
[] = {
113 OPT_STRING('f', "format", &bench_format_str
, "default|simple", "Specify the output formatting style"),
114 OPT_UINTEGER('r', "repeat", &bench_repeat
, "Specify amount of times to repeat the run"),
118 static const char * const bench_usage
[] = {
119 "perf bench [<common options>] <collection> <benchmark> [<options>]",
123 static void print_usage(void)
125 struct collection
*coll
;
129 for (i
= 0; bench_usage
[i
]; i
++)
130 printf("\t%s\n", bench_usage
[i
]);
133 printf(" # List of all available benchmark collections:\n\n");
135 for_each_collection(coll
)
136 printf("%14s: %s\n", coll
->name
, coll
->summary
);
140 static int bench_str2int(const char *str
)
143 return BENCH_FORMAT_DEFAULT
;
145 if (!strcmp(str
, BENCH_FORMAT_DEFAULT_STR
))
146 return BENCH_FORMAT_DEFAULT
;
147 else if (!strcmp(str
, BENCH_FORMAT_SIMPLE_STR
))
148 return BENCH_FORMAT_SIMPLE
;
150 return BENCH_FORMAT_UNKNOWN
;
154 * Run a specific benchmark but first rename the running task's ->comm[]
155 * to something meaningful:
157 static int run_bench(const char *coll_name
, const char *bench_name
, bench_fn_t fn
,
158 int argc
, const char **argv
, const char *prefix
)
164 size
= strlen(coll_name
) + 1 + strlen(bench_name
) + 1;
169 scnprintf(name
, size
, "%s-%s", coll_name
, bench_name
);
171 prctl(PR_SET_NAME
, name
);
174 ret
= fn(argc
, argv
, prefix
);
181 static void run_collection(struct collection
*coll
)
190 * Preparing preset parameters for
191 * embedded, ordinary PC, HPC, etc...
194 for_each_bench(coll
, bench
) {
197 printf("# Running %s/%s benchmark...\n", coll
->name
, bench
->name
);
200 argv
[1] = bench
->name
;
201 run_bench(coll
->name
, bench
->name
, bench
->fn
, 1, argv
, NULL
);
206 static void run_all_collections(void)
208 struct collection
*coll
;
210 for_each_collection(coll
)
211 run_collection(coll
);
214 int cmd_bench(int argc
, const char **argv
, const char *prefix __maybe_unused
)
216 struct collection
*coll
;
220 /* No collection specified. */
225 argc
= parse_options(argc
, argv
, bench_options
, bench_usage
,
226 PARSE_OPT_STOP_AT_NON_OPTION
);
228 bench_format
= bench_str2int(bench_format_str
);
229 if (bench_format
== BENCH_FORMAT_UNKNOWN
) {
230 printf("Unknown format descriptor: '%s'\n", bench_format_str
);
234 if (bench_repeat
== 0) {
235 printf("Invalid repeat option: Must specify a positive value\n");
244 if (!strcmp(argv
[0], "all")) {
245 run_all_collections();
249 for_each_collection(coll
) {
252 if (strcmp(coll
->name
, argv
[0]))
256 /* No bench specified. */
257 dump_benchmarks(coll
);
261 if (!strcmp(argv
[1], "all")) {
262 run_collection(coll
);
266 for_each_bench(coll
, bench
) {
267 if (strcmp(bench
->name
, argv
[1]))
270 if (bench_format
== BENCH_FORMAT_DEFAULT
)
271 printf("# Running '%s/%s' benchmark:\n", coll
->name
, bench
->name
);
273 ret
= run_bench(coll
->name
, bench
->name
, bench
->fn
, argc
-1, argv
+1, prefix
);
277 if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
278 dump_benchmarks(coll
);
282 printf("Unknown benchmark: '%s' for collection '%s'\n", argv
[1], argv
[0]);
287 printf("Unknown collection: '%s'\n", argv
[0]);