Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * builtin-bench.c |
| 4 | * |
| 5 | * General benchmarking subsystem provided by perf |
| 6 | * |
| 7 | * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * |
| 13 | * Available subsystem list: |
| 14 | * sched ... scheduler and IPC mechanism |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 15 | * mem ... memory access performance |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "perf.h" |
| 20 | #include "util/util.h" |
| 21 | #include "util/parse-options.h" |
| 22 | #include "builtin.h" |
| 23 | #include "bench/bench.h" |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | struct bench_suite { |
| 30 | const char *name; |
| 31 | const char *summary; |
| 32 | int (*fn)(int, const char **, const char *); |
| 33 | }; |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 34 | \ |
| 35 | /* sentinel: easy for help */ |
Namhyung Kim | 08942f6 | 2012-06-20 15:08:06 +0900 | [diff] [blame] | 36 | #define suite_all { "all", "Test all benchmark suites", NULL } |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 37 | |
Peter Hurley | 79d824e | 2013-01-27 20:51:22 -0500 | [diff] [blame] | 38 | #ifdef LIBNUMA_SUPPORT |
Ingo Molnar | 1c13f3c | 2012-12-06 13:51:59 +0100 | [diff] [blame] | 39 | static struct bench_suite numa_suites[] = { |
| 40 | { "mem", |
| 41 | "Benchmark for NUMA workloads", |
| 42 | bench_numa }, |
| 43 | suite_all, |
| 44 | { NULL, |
| 45 | NULL, |
| 46 | NULL } |
| 47 | }; |
Peter Hurley | 79d824e | 2013-01-27 20:51:22 -0500 | [diff] [blame] | 48 | #endif |
Ingo Molnar | 1c13f3c | 2012-12-06 13:51:59 +0100 | [diff] [blame] | 49 | |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 50 | static struct bench_suite sched_suites[] = { |
| 51 | { "messaging", |
| 52 | "Benchmark for scheduler and IPC mechanisms", |
| 53 | bench_sched_messaging }, |
| 54 | { "pipe", |
| 55 | "Flood of communication over pipe() between two processes", |
| 56 | bench_sched_pipe }, |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 57 | suite_all, |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 58 | { NULL, |
| 59 | NULL, |
| 60 | NULL } |
| 61 | }; |
| 62 | |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 63 | static struct bench_suite mem_suites[] = { |
| 64 | { "memcpy", |
| 65 | "Simple memory copy in various ways", |
| 66 | bench_mem_memcpy }, |
Jan Beulich | be3de80 | 2012-01-24 10:03:22 -0200 | [diff] [blame] | 67 | { "memset", |
| 68 | "Simple memory set in various ways", |
| 69 | bench_mem_memset }, |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 70 | suite_all, |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 71 | { NULL, |
| 72 | NULL, |
| 73 | NULL } |
| 74 | }; |
| 75 | |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 76 | struct bench_subsys { |
| 77 | const char *name; |
| 78 | const char *summary; |
| 79 | struct bench_suite *suites; |
| 80 | }; |
| 81 | |
| 82 | static struct bench_subsys subsystems[] = { |
Peter Hurley | 79d824e | 2013-01-27 20:51:22 -0500 | [diff] [blame] | 83 | #ifdef LIBNUMA_SUPPORT |
Ingo Molnar | 1c13f3c | 2012-12-06 13:51:59 +0100 | [diff] [blame] | 84 | { "numa", |
| 85 | "NUMA scheduling and MM behavior", |
| 86 | numa_suites }, |
Peter Hurley | 79d824e | 2013-01-27 20:51:22 -0500 | [diff] [blame] | 87 | #endif |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 88 | { "sched", |
| 89 | "scheduler and IPC mechanism", |
| 90 | sched_suites }, |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 91 | { "mem", |
| 92 | "memory access performance", |
| 93 | mem_suites }, |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 94 | { "all", /* sentinel: easy for help */ |
Namhyung Kim | 08942f6 | 2012-06-20 15:08:06 +0900 | [diff] [blame] | 95 | "all benchmark subsystem", |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 96 | NULL }, |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 97 | { NULL, |
| 98 | NULL, |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 99 | NULL } |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | static void dump_suites(int subsys_index) |
| 103 | { |
| 104 | int i; |
| 105 | |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 106 | printf("# List of available suites for %s...\n\n", |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 107 | subsystems[subsys_index].name); |
| 108 | |
| 109 | for (i = 0; subsystems[subsys_index].suites[i].name; i++) |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 110 | printf("%14s: %s\n", |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 111 | subsystems[subsys_index].suites[i].name, |
| 112 | subsystems[subsys_index].suites[i].summary); |
| 113 | |
| 114 | printf("\n"); |
| 115 | return; |
| 116 | } |
| 117 | |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 118 | static const char *bench_format_str; |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 119 | int bench_format = BENCH_FORMAT_DEFAULT; |
| 120 | |
| 121 | static const struct option bench_options[] = { |
| 122 | OPT_STRING('f', "format", &bench_format_str, "default", |
| 123 | "Specify format style"), |
| 124 | OPT_END() |
| 125 | }; |
| 126 | |
| 127 | static const char * const bench_usage[] = { |
| 128 | "perf bench [<common options>] <subsystem> <suite> [<options>]", |
| 129 | NULL |
| 130 | }; |
| 131 | |
| 132 | static void print_usage(void) |
| 133 | { |
| 134 | int i; |
| 135 | |
| 136 | printf("Usage: \n"); |
| 137 | for (i = 0; bench_usage[i]; i++) |
| 138 | printf("\t%s\n", bench_usage[i]); |
| 139 | printf("\n"); |
| 140 | |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 141 | printf("# List of available subsystems...\n\n"); |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 142 | |
| 143 | for (i = 0; subsystems[i].name; i++) |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 144 | printf("%14s: %s\n", |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 145 | subsystems[i].name, subsystems[i].summary); |
| 146 | printf("\n"); |
| 147 | } |
| 148 | |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 149 | static int bench_str2int(const char *str) |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 150 | { |
| 151 | if (!str) |
| 152 | return BENCH_FORMAT_DEFAULT; |
| 153 | |
| 154 | if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR)) |
| 155 | return BENCH_FORMAT_DEFAULT; |
| 156 | else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR)) |
| 157 | return BENCH_FORMAT_SIMPLE; |
| 158 | |
| 159 | return BENCH_FORMAT_UNKNOWN; |
| 160 | } |
| 161 | |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 162 | static void all_suite(struct bench_subsys *subsys) /* FROM HERE */ |
| 163 | { |
| 164 | int i; |
| 165 | const char *argv[2]; |
| 166 | struct bench_suite *suites = subsys->suites; |
| 167 | |
| 168 | argv[1] = NULL; |
| 169 | /* |
| 170 | * TODO: |
| 171 | * preparing preset parameters for |
| 172 | * embedded, ordinary PC, HPC, etc... |
| 173 | * will be helpful |
| 174 | */ |
| 175 | for (i = 0; suites[i].fn; i++) { |
| 176 | printf("# Running %s/%s benchmark...\n", |
| 177 | subsys->name, |
| 178 | suites[i].name); |
Namhyung Kim | 9b494ea | 2013-01-08 18:39:26 +0900 | [diff] [blame] | 179 | fflush(stdout); |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 180 | |
| 181 | argv[1] = suites[i].name; |
| 182 | suites[i].fn(1, argv, NULL); |
| 183 | printf("\n"); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void all_subsystem(void) |
| 188 | { |
| 189 | int i; |
| 190 | for (i = 0; subsystems[i].suites; i++) |
| 191 | all_suite(&subsystems[i]); |
| 192 | } |
| 193 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 194 | 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] | 195 | { |
| 196 | int i, j, status = 0; |
| 197 | |
| 198 | if (argc < 2) { |
| 199 | /* No subsystem specified. */ |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 200 | print_usage(); |
| 201 | goto end; |
| 202 | } |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 203 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 204 | argc = parse_options(argc, argv, bench_options, bench_usage, |
| 205 | PARSE_OPT_STOP_AT_NON_OPTION); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 206 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 207 | bench_format = bench_str2int(bench_format_str); |
| 208 | if (bench_format == BENCH_FORMAT_UNKNOWN) { |
| 209 | printf("Unknown format descriptor:%s\n", bench_format_str); |
| 210 | goto end; |
| 211 | } |
| 212 | |
| 213 | if (argc < 1) { |
| 214 | print_usage(); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 215 | goto end; |
| 216 | } |
| 217 | |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 218 | if (!strcmp(argv[0], "all")) { |
| 219 | all_subsystem(); |
| 220 | goto end; |
| 221 | } |
| 222 | |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 223 | for (i = 0; subsystems[i].name; i++) { |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 224 | if (strcmp(subsystems[i].name, argv[0])) |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 225 | continue; |
| 226 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 227 | if (argc < 2) { |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 228 | /* No suite specified. */ |
| 229 | dump_suites(i); |
| 230 | goto end; |
| 231 | } |
| 232 | |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 233 | if (!strcmp(argv[1], "all")) { |
| 234 | all_suite(&subsystems[i]); |
| 235 | goto end; |
| 236 | } |
| 237 | |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 238 | for (j = 0; subsystems[i].suites[j].name; j++) { |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 239 | if (strcmp(subsystems[i].suites[j].name, argv[1])) |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 240 | continue; |
| 241 | |
Hitoshi Mitake | 79e295d | 2009-11-11 00:04:00 +0900 | [diff] [blame] | 242 | if (bench_format == BENCH_FORMAT_DEFAULT) |
| 243 | printf("# Running %s/%s benchmark...\n", |
| 244 | subsystems[i].name, |
| 245 | subsystems[i].suites[j].name); |
Namhyung Kim | 9b494ea | 2013-01-08 18:39:26 +0900 | [diff] [blame] | 246 | fflush(stdout); |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 247 | status = subsystems[i].suites[j].fn(argc - 1, |
| 248 | argv + 1, prefix); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 249 | goto end; |
| 250 | } |
| 251 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 252 | if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 253 | dump_suites(i); |
| 254 | goto end; |
| 255 | } |
| 256 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 257 | printf("Unknown suite:%s for %s\n", argv[1], argv[0]); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 258 | status = 1; |
| 259 | goto end; |
| 260 | } |
| 261 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 262 | printf("Unknown subsystem:%s\n", argv[0]); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 263 | status = 1; |
| 264 | |
| 265 | end: |
| 266 | return status; |
| 267 | } |