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 "util/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", "Test 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", "Test all scheduler benchmarks", NULL },
51 static struct bench mem_benchmarks[] = {
52 { "memcpy", "Benchmark for memcpy()", bench_mem_memcpy },
53 { "memset", "Benchmark for memset() tests", bench_mem_memset },
54 { "all", "Test all memory 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 },
63 { "all", "Test all futex benchmarks", NULL },
70 struct bench *benchmarks;
73 static struct collection collections[] = {
74 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
75 { "mem", "Memory access benchmarks", mem_benchmarks },
76 #ifdef HAVE_LIBNUMA_SUPPORT
77 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
79 {"futex", "Futex stressing benchmarks", futex_benchmarks },
80 { "all", "All benchmarks", NULL },
84 /* Iterate over all benchmark collections: */
85 #define for_each_collection(coll) \
86 for (coll = collections; coll->name; coll++)
88 /* Iterate over all benchmarks within a collection: */
89 #define for_each_bench(coll, bench) \
90 for (bench = coll->benchmarks; bench && bench->name; bench++)
92 static void dump_benchmarks(struct collection *coll)
96 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
98 for_each_bench(coll, bench)
99 printf("%14s: %s\n", bench->name, bench->summary);
104 static const char *bench_format_str;
106 /* Output/formatting style, exported to benchmark modules: */
107 int bench_format = BENCH_FORMAT_DEFAULT;
108 unsigned int bench_repeat = 10; /* default number of times to repeat the run */
110 static const struct option bench_options[] = {
111 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
112 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify amount of times to repeat the run"),
116 static const char * const bench_usage[] = {
117 "perf bench [<common options>] <collection> <benchmark> [<options>]",
121 static void print_usage(void)
123 struct collection *coll;
127 for (i = 0; bench_usage[i]; i++)
128 printf("\t%s\n", bench_usage[i]);
131 printf(" # List of all available benchmark collections:\n\n");
133 for_each_collection(coll)
134 printf("%14s: %s\n", coll->name, coll->summary);
138 static int bench_str2int(const char *str)
141 return BENCH_FORMAT_DEFAULT;
143 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
144 return BENCH_FORMAT_DEFAULT;
145 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
146 return BENCH_FORMAT_SIMPLE;
148 return BENCH_FORMAT_UNKNOWN;
152 * Run a specific benchmark but first rename the running task's ->comm[]
153 * to something meaningful:
155 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
156 int argc, const char **argv, const char *prefix)
162 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
167 scnprintf(name, size, "%s-%s", coll_name, bench_name);
169 prctl(PR_SET_NAME, name);
172 ret = fn(argc, argv, prefix);
179 static void run_collection(struct collection *coll)
188 * Preparing preset parameters for
189 * embedded, ordinary PC, HPC, etc...
192 for_each_bench(coll, bench) {
195 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
198 argv[1] = bench->name;
199 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
204 static void run_all_collections(void)
206 struct collection *coll;
208 for_each_collection(coll)
209 run_collection(coll);
212 int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
214 struct collection *coll;
218 /* No collection specified. */
223 argc = parse_options(argc, argv, bench_options, bench_usage,
224 PARSE_OPT_STOP_AT_NON_OPTION);
226 bench_format = bench_str2int(bench_format_str);
227 if (bench_format == BENCH_FORMAT_UNKNOWN) {
228 printf("Unknown format descriptor: '%s'\n", bench_format_str);
232 if (bench_repeat == 0) {
233 printf("Invalid repeat option: Must specify a positive value\n");
242 if (!strcmp(argv[0], "all")) {
243 run_all_collections();
247 for_each_collection(coll) {
250 if (strcmp(coll->name, argv[0]))
254 /* No bench specified. */
255 dump_benchmarks(coll);
259 if (!strcmp(argv[1], "all")) {
260 run_collection(coll);
264 for_each_bench(coll, bench) {
265 if (strcmp(bench->name, argv[1]))
268 if (bench_format == BENCH_FORMAT_DEFAULT)
269 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
271 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
275 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
276 dump_benchmarks(coll);
280 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
285 printf("Unknown collection: '%s'\n", argv[0]);