4 * General benchmarking collections provided by perf
10 * Available benchmark collection list:
12 * sched ... scheduler and IPC performance
13 * mem ... memory access performance
14 * numa ... NUMA scheduling and MM performance
17 #include "util/util.h"
18 #include "util/parse-options.h"
20 #include "bench/bench.h"
25 #include <sys/prctl.h>
27 typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix);
35 #ifdef HAVE_LIBNUMA_SUPPORT
36 static struct bench numa_benchmarks[] = {
37 { "mem", "Benchmark for NUMA workloads", bench_numa },
38 { "all", "Test all NUMA benchmarks", NULL },
43 static struct bench sched_benchmarks[] = {
44 { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
45 { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
46 { "all", "Test all scheduler benchmarks", NULL },
50 static struct bench mem_benchmarks[] = {
51 { "memcpy", "Benchmark for memcpy()", bench_mem_memcpy },
52 { "memset", "Benchmark for memset() tests", bench_mem_memset },
53 { "all", "Test all memory benchmarks", NULL },
60 struct bench *benchmarks;
63 static struct collection collections[] = {
64 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
65 { "mem", "Memory access benchmarks", mem_benchmarks },
66 #ifdef HAVE_LIBNUMA_SUPPORT
67 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
69 { "all", "All benchmarks", NULL },
73 /* Iterate over all benchmark collections: */
74 #define for_each_collection(coll) \
75 for (coll = collections; coll->name; coll++)
77 /* Iterate over all benchmarks within a collection: */
78 #define for_each_bench(coll, bench) \
79 for (bench = coll->benchmarks; bench->name; bench++)
81 static void dump_benchmarks(struct collection *coll)
85 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name);
87 for_each_bench(coll, bench)
88 printf("%14s: %s\n", bench->name, bench->summary);
93 static const char *bench_format_str;
95 /* Output/formatting style, exported to benchmark modules: */
96 int bench_format = BENCH_FORMAT_DEFAULT;
98 static const struct option bench_options[] = {
99 OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"),
103 static const char * const bench_usage[] = {
104 "perf bench [<common options>] <collection> <benchmark> [<options>]",
108 static void print_usage(void)
110 struct collection *coll;
114 for (i = 0; bench_usage[i]; i++)
115 printf("\t%s\n", bench_usage[i]);
118 printf(" # List of all available benchmark collections:\n\n");
120 for_each_collection(coll)
121 printf("%14s: %s\n", coll->name, coll->summary);
125 static int bench_str2int(const char *str)
128 return BENCH_FORMAT_DEFAULT;
130 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
131 return BENCH_FORMAT_DEFAULT;
132 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
133 return BENCH_FORMAT_SIMPLE;
135 return BENCH_FORMAT_UNKNOWN;
139 * Run a specific benchmark but first rename the running task's ->comm[]
140 * to something meaningful:
142 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn,
143 int argc, const char **argv, const char *prefix)
149 size = strlen(coll_name) + 1 + strlen(bench_name) + 1;
154 scnprintf(name, size, "%s-%s", coll_name, bench_name);
156 prctl(PR_SET_NAME, name);
159 ret = fn(argc, argv, prefix);
166 static void run_collection(struct collection *coll)
175 * Preparing preset parameters for
176 * embedded, ordinary PC, HPC, etc...
179 for_each_bench(coll, bench) {
182 printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
185 argv[1] = bench->name;
186 run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL);
191 static void run_all_collections(void)
193 struct collection *coll;
195 for_each_collection(coll)
196 run_collection(coll);
199 int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
201 struct collection *coll;
205 /* No collection specified. */
210 argc = parse_options(argc, argv, bench_options, bench_usage,
211 PARSE_OPT_STOP_AT_NON_OPTION);
213 bench_format = bench_str2int(bench_format_str);
214 if (bench_format == BENCH_FORMAT_UNKNOWN) {
215 printf("Unknown format descriptor: '%s'\n", bench_format_str);
224 if (!strcmp(argv[0], "all")) {
225 run_all_collections();
229 for_each_collection(coll) {
232 if (strcmp(coll->name, argv[0]))
236 /* No bench specified. */
237 dump_benchmarks(coll);
241 if (!strcmp(argv[1], "all")) {
242 run_collection(coll);
246 for_each_bench(coll, bench) {
247 if (strcmp(bench->name, argv[1]))
250 if (bench_format == BENCH_FORMAT_DEFAULT)
251 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
253 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix);
257 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
258 dump_benchmarks(coll);
262 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]);
267 printf("Unknown collection: '%s'\n", argv[0]);