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 { "all", "Test all futex benchmarks", NULL },
67 struct bench *benchmarks;
70 static struct collection collections[] = {
71 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
72 { "mem", "Memory access benchmarks", mem_benchmarks },
73 #ifdef HAVE_LIBNUMA_SUPPORT
74 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
76 {"futex", "Futex stressing benchmarks", futex_benchmarks },
77 { "all", "All benchmarks", NULL },
81 /* Iterate over all benchmark collections: */
82 #define for_each_collection(coll) \
83 for (coll = collections; coll->name; coll++)
85 /* Iterate over all benchmarks within a collection: */
86 #define for_each_bench(coll, bench) \
87 for (bench = coll->benchmarks; bench->name; bench++)
89 static void dump_benchmarks(struct collection *coll)
93 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
95 for_each_bench(coll, bench)
96 printf("%14s: %s\n", bench->name, bench->summary);
101 static const char *bench_format_str;
103 /* Output/formatting style, exported to benchmark modules: */
104 int bench_format = BENCH_FORMAT_DEFAULT;
106 static const struct option bench_options[] = {
107 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
111 static const char * const bench_usage[] = {
112 "perf bench [<common options>] <collection> <benchmark> [<options>]",
116 static void print_usage(void)
118 struct collection *coll;
122 for (i = 0; bench_usage[i]; i++)
123 printf("\t%s\n", bench_usage[i]);
126 printf(" # List of all available benchmark collections:\n\n");
128 for_each_collection(coll)
129 printf("%14s: %s\n", coll->name, coll->summary);
133 static int bench_str2int(const char *str)
136 return BENCH_FORMAT_DEFAULT;
138 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
139 return BENCH_FORMAT_DEFAULT;
140 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
141 return BENCH_FORMAT_SIMPLE;
143 return BENCH_FORMAT_UNKNOWN;
147 * Run a specific benchmark but first rename the running task's ->comm[]
148 * to something meaningful:
150 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
151 int argc, const char **argv, const char *prefix)
157 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
162 scnprintf(name, size, "%s-%s", coll_name, bench_name);
164 prctl(PR_SET_NAME, name);
167 ret = fn(argc, argv, prefix);
174 static void run_collection(struct collection *coll)
183 * Preparing preset parameters for
184 * embedded, ordinary PC, HPC, etc...
187 for_each_bench(coll, bench) {
190 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
193 argv[1] = bench->name;
194 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
199 static void run_all_collections(void)
201 struct collection *coll;
203 for_each_collection(coll)
204 run_collection(coll);
207 int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
209 struct collection *coll;
213 /* No collection specified. */
218 argc = parse_options(argc, argv, bench_options, bench_usage,
219 PARSE_OPT_STOP_AT_NON_OPTION);
221 bench_format = bench_str2int(bench_format_str);
222 if (bench_format == BENCH_FORMAT_UNKNOWN) {
223 printf("Unknown format descriptor: '%s'\n", bench_format_str);
232 if (!strcmp(argv[0], "all")) {
233 run_all_collections();
237 for_each_collection(coll) {
240 if (strcmp(coll->name, argv[0]))
244 /* No bench specified. */
245 dump_benchmarks(coll);
249 if (!strcmp(argv[1], "all")) {
250 run_collection(coll);
254 for_each_bench(coll, bench) {
255 if (strcmp(bench->name, argv[1]))
258 if (bench_format == BENCH_FORMAT_DEFAULT)
259 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
261 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
265 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
266 dump_benchmarks(coll);
270 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
275 printf("Unknown collection: '%s'\n", argv[0]);