Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 1 | /* |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 2 | * builtin-bench.c |
| 3 | * |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 4 | * General benchmarking collections provided by perf |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 10 | * Available benchmark collection list: |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 11 | * |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 12 | * sched ... scheduler and IPC performance |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 13 | * mem ... memory access performance |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 14 | * numa ... NUMA scheduling and MM performance |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 15 | * futex ... Futex performance |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 16 | */ |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 17 | #include "perf.h" |
| 18 | #include "util/util.h" |
| 19 | #include "util/parse-options.h" |
| 20 | #include "builtin.h" |
| 21 | #include "bench/bench.h" |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 26 | #include <sys/prctl.h> |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 27 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 28 | typedef int (*bench_fn_t)(int argc, const char **argv, const char *prefix); |
| 29 | |
| 30 | struct bench { |
| 31 | const char *name; |
| 32 | const char *summary; |
| 33 | bench_fn_t fn; |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 34 | }; |
| 35 | |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 36 | #ifdef HAVE_LIBNUMA_SUPPORT |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 37 | static struct bench numa_benchmarks[] = { |
| 38 | { "mem", "Benchmark for NUMA workloads", bench_numa }, |
| 39 | { "all", "Test all NUMA benchmarks", NULL }, |
| 40 | { NULL, NULL, NULL } |
Ingo Molnar | 1c13f3c | 2012-12-06 13:51:59 +0100 | [diff] [blame] | 41 | }; |
Peter Hurley | 79d824e | 2013-01-27 20:51:22 -0500 | [diff] [blame] | 42 | #endif |
Ingo Molnar | 1c13f3c | 2012-12-06 13:51:59 +0100 | [diff] [blame] | 43 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 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 }, |
| 48 | { NULL, NULL, NULL } |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 49 | }; |
| 50 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 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 }, |
| 55 | { NULL, NULL, NULL } |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 56 | }; |
| 57 | |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 58 | static struct bench futex_benchmarks[] = { |
| 59 | { "hash", "Benchmark for futex hash table", bench_futex_hash }, |
Davidlohr Bueso | 27db783 | 2013-12-14 20:31:56 -0800 | [diff] [blame^] | 60 | { "wake", "Benchmark for futex wake calls", bench_futex_wake }, |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 61 | { "all", "Test all futex benchmarks", NULL }, |
| 62 | { NULL, NULL, NULL } |
| 63 | }; |
| 64 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 65 | struct collection { |
| 66 | const char *name; |
| 67 | const char *summary; |
| 68 | struct bench *benchmarks; |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 69 | }; |
| 70 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 71 | static struct collection collections[] = { |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 72 | { "sched", "Scheduler and IPC benchmarks", sched_benchmarks }, |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 73 | { "mem", "Memory access benchmarks", mem_benchmarks }, |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 74 | #ifdef HAVE_LIBNUMA_SUPPORT |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 75 | { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks }, |
Peter Hurley | 79d824e | 2013-01-27 20:51:22 -0500 | [diff] [blame] | 76 | #endif |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 77 | {"futex", "Futex stressing benchmarks", futex_benchmarks }, |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 78 | { "all", "All benchmarks", NULL }, |
| 79 | { NULL, NULL, NULL } |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 80 | }; |
| 81 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 82 | /* Iterate over all benchmark collections: */ |
| 83 | #define for_each_collection(coll) \ |
| 84 | for (coll = collections; coll->name; coll++) |
| 85 | |
| 86 | /* Iterate over all benchmarks within a collection: */ |
| 87 | #define for_each_bench(coll, bench) \ |
| 88 | for (bench = coll->benchmarks; bench->name; bench++) |
| 89 | |
| 90 | static void dump_benchmarks(struct collection *coll) |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 91 | { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 92 | struct bench *bench; |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 93 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 94 | printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 95 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 96 | for_each_bench(coll, bench) |
| 97 | printf("%14s: %s\n", bench->name, bench->summary); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 98 | |
| 99 | printf("\n"); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 100 | } |
| 101 | |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 102 | static const char *bench_format_str; |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 103 | |
| 104 | /* Output/formatting style, exported to benchmark modules: */ |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 105 | int bench_format = BENCH_FORMAT_DEFAULT; |
| 106 | |
| 107 | static const struct option bench_options[] = { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 108 | OPT_STRING('f', "format", &bench_format_str, "default", "Specify format style"), |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 109 | OPT_END() |
| 110 | }; |
| 111 | |
| 112 | static const char * const bench_usage[] = { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 113 | "perf bench [<common options>] <collection> <benchmark> [<options>]", |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 114 | NULL |
| 115 | }; |
| 116 | |
| 117 | static void print_usage(void) |
| 118 | { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 119 | struct collection *coll; |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 120 | int i; |
| 121 | |
| 122 | printf("Usage: \n"); |
| 123 | for (i = 0; bench_usage[i]; i++) |
| 124 | printf("\t%s\n", bench_usage[i]); |
| 125 | printf("\n"); |
| 126 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 127 | printf(" # List of all available benchmark collections:\n\n"); |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 128 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 129 | for_each_collection(coll) |
| 130 | printf("%14s: %s\n", coll->name, coll->summary); |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 131 | printf("\n"); |
| 132 | } |
| 133 | |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 134 | static int bench_str2int(const char *str) |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 135 | { |
| 136 | if (!str) |
| 137 | return BENCH_FORMAT_DEFAULT; |
| 138 | |
| 139 | if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR)) |
| 140 | return BENCH_FORMAT_DEFAULT; |
| 141 | else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR)) |
| 142 | return BENCH_FORMAT_SIMPLE; |
| 143 | |
| 144 | return BENCH_FORMAT_UNKNOWN; |
| 145 | } |
| 146 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 147 | /* |
| 148 | * Run a specific benchmark but first rename the running task's ->comm[] |
| 149 | * to something meaningful: |
| 150 | */ |
| 151 | static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn, |
| 152 | int argc, const char **argv, const char *prefix) |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 153 | { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 154 | int size; |
| 155 | char *name; |
| 156 | int ret; |
| 157 | |
| 158 | size = strlen(coll_name) + 1 + strlen(bench_name) + 1; |
| 159 | |
| 160 | name = zalloc(size); |
| 161 | BUG_ON(!name); |
| 162 | |
| 163 | scnprintf(name, size, "%s-%s", coll_name, bench_name); |
| 164 | |
| 165 | prctl(PR_SET_NAME, name); |
| 166 | argv[0] = name; |
| 167 | |
| 168 | ret = fn(argc, argv, prefix); |
| 169 | |
| 170 | free(name); |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static void run_collection(struct collection *coll) |
| 176 | { |
| 177 | struct bench *bench; |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 178 | const char *argv[2]; |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 179 | |
| 180 | argv[1] = NULL; |
| 181 | /* |
| 182 | * TODO: |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 183 | * |
| 184 | * Preparing preset parameters for |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 185 | * embedded, ordinary PC, HPC, etc... |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 186 | * would be helpful. |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 187 | */ |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 188 | for_each_bench(coll, bench) { |
| 189 | if (!bench->fn) |
| 190 | break; |
| 191 | printf("# Running %s/%s benchmark...\n", coll->name, bench->name); |
Namhyung Kim | 9b494ea | 2013-01-08 18:39:26 +0900 | [diff] [blame] | 192 | fflush(stdout); |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 193 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 194 | argv[1] = bench->name; |
| 195 | run_bench(coll->name, bench->name, bench->fn, 1, argv, NULL); |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 196 | printf("\n"); |
| 197 | } |
| 198 | } |
| 199 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 200 | static void run_all_collections(void) |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 201 | { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 202 | struct collection *coll; |
| 203 | |
| 204 | for_each_collection(coll) |
| 205 | run_collection(coll); |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 206 | } |
| 207 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 208 | int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused) |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 209 | { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 210 | struct collection *coll; |
| 211 | int ret = 0; |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 212 | |
| 213 | if (argc < 2) { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 214 | /* No collection specified. */ |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 215 | print_usage(); |
| 216 | goto end; |
| 217 | } |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 218 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 219 | argc = parse_options(argc, argv, bench_options, bench_usage, |
| 220 | PARSE_OPT_STOP_AT_NON_OPTION); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 221 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 222 | bench_format = bench_str2int(bench_format_str); |
| 223 | if (bench_format == BENCH_FORMAT_UNKNOWN) { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 224 | printf("Unknown format descriptor: '%s'\n", bench_format_str); |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 225 | goto end; |
| 226 | } |
| 227 | |
| 228 | if (argc < 1) { |
| 229 | print_usage(); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 230 | goto end; |
| 231 | } |
| 232 | |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 233 | if (!strcmp(argv[0], "all")) { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 234 | run_all_collections(); |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 235 | goto end; |
| 236 | } |
| 237 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 238 | for_each_collection(coll) { |
| 239 | struct bench *bench; |
| 240 | |
| 241 | if (strcmp(coll->name, argv[0])) |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 242 | continue; |
| 243 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 244 | if (argc < 2) { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 245 | /* No bench specified. */ |
| 246 | dump_benchmarks(coll); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 247 | goto end; |
| 248 | } |
| 249 | |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 250 | if (!strcmp(argv[1], "all")) { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 251 | run_collection(coll); |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 252 | goto end; |
| 253 | } |
| 254 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 255 | for_each_bench(coll, bench) { |
| 256 | if (strcmp(bench->name, argv[1])) |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 257 | continue; |
| 258 | |
Hitoshi Mitake | 79e295d | 2009-11-11 00:04:00 +0900 | [diff] [blame] | 259 | if (bench_format == BENCH_FORMAT_DEFAULT) |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 260 | printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name); |
Namhyung Kim | 9b494ea | 2013-01-08 18:39:26 +0900 | [diff] [blame] | 261 | fflush(stdout); |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 262 | ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1, prefix); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 263 | goto end; |
| 264 | } |
| 265 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 266 | if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 267 | dump_benchmarks(coll); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 268 | goto end; |
| 269 | } |
| 270 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 271 | printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]); |
| 272 | ret = 1; |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 273 | goto end; |
| 274 | } |
| 275 | |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 276 | printf("Unknown collection: '%s'\n", argv[0]); |
| 277 | ret = 1; |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 278 | |
| 279 | end: |
Ingo Molnar | 4157922 | 2013-10-23 14:37:56 +0200 | [diff] [blame] | 280 | return ret; |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 281 | } |