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