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 |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include "perf.h" |
| 19 | #include "util/util.h" |
| 20 | #include "util/parse-options.h" |
| 21 | #include "builtin.h" |
| 22 | #include "bench/bench.h" |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | struct bench_suite { |
| 29 | const char *name; |
| 30 | const char *summary; |
| 31 | int (*fn)(int, const char **, const char *); |
| 32 | }; |
| 33 | |
| 34 | static struct bench_suite sched_suites[] = { |
| 35 | { "messaging", |
| 36 | "Benchmark for scheduler and IPC mechanisms", |
| 37 | bench_sched_messaging }, |
| 38 | { "pipe", |
| 39 | "Flood of communication over pipe() between two processes", |
| 40 | bench_sched_pipe }, |
| 41 | { NULL, |
| 42 | NULL, |
| 43 | NULL } |
| 44 | }; |
| 45 | |
| 46 | struct bench_subsys { |
| 47 | const char *name; |
| 48 | const char *summary; |
| 49 | struct bench_suite *suites; |
| 50 | }; |
| 51 | |
| 52 | static struct bench_subsys subsystems[] = { |
| 53 | { "sched", |
| 54 | "scheduler and IPC mechanism", |
| 55 | sched_suites }, |
| 56 | { NULL, |
| 57 | NULL, |
| 58 | NULL } |
| 59 | }; |
| 60 | |
| 61 | static void dump_suites(int subsys_index) |
| 62 | { |
| 63 | int i; |
| 64 | |
| 65 | printf("List of available suites for %s...\n\n", |
| 66 | subsystems[subsys_index].name); |
| 67 | |
| 68 | for (i = 0; subsystems[subsys_index].suites[i].name; i++) |
| 69 | printf("\t%s: %s\n", |
| 70 | subsystems[subsys_index].suites[i].name, |
| 71 | subsystems[subsys_index].suites[i].summary); |
| 72 | |
| 73 | printf("\n"); |
| 74 | return; |
| 75 | } |
| 76 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 77 | static char *bench_format_str; |
| 78 | int bench_format = BENCH_FORMAT_DEFAULT; |
| 79 | |
| 80 | static const struct option bench_options[] = { |
| 81 | OPT_STRING('f', "format", &bench_format_str, "default", |
| 82 | "Specify format style"), |
| 83 | OPT_END() |
| 84 | }; |
| 85 | |
| 86 | static const char * const bench_usage[] = { |
| 87 | "perf bench [<common options>] <subsystem> <suite> [<options>]", |
| 88 | NULL |
| 89 | }; |
| 90 | |
| 91 | static void print_usage(void) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | printf("Usage: \n"); |
| 96 | for (i = 0; bench_usage[i]; i++) |
| 97 | printf("\t%s\n", bench_usage[i]); |
| 98 | printf("\n"); |
| 99 | |
| 100 | printf("List of available subsystems...\n\n"); |
| 101 | |
| 102 | for (i = 0; subsystems[i].name; i++) |
| 103 | printf("\t%s: %s\n", |
| 104 | subsystems[i].name, subsystems[i].summary); |
| 105 | printf("\n"); |
| 106 | } |
| 107 | |
| 108 | static int bench_str2int(char *str) |
| 109 | { |
| 110 | if (!str) |
| 111 | return BENCH_FORMAT_DEFAULT; |
| 112 | |
| 113 | if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR)) |
| 114 | return BENCH_FORMAT_DEFAULT; |
| 115 | else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR)) |
| 116 | return BENCH_FORMAT_SIMPLE; |
| 117 | |
| 118 | return BENCH_FORMAT_UNKNOWN; |
| 119 | } |
| 120 | |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 121 | int cmd_bench(int argc, const char **argv, const char *prefix __used) |
| 122 | { |
| 123 | int i, j, status = 0; |
| 124 | |
| 125 | if (argc < 2) { |
| 126 | /* No subsystem specified. */ |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 127 | print_usage(); |
| 128 | goto end; |
| 129 | } |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 130 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 131 | argc = parse_options(argc, argv, bench_options, bench_usage, |
| 132 | PARSE_OPT_STOP_AT_NON_OPTION); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 133 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 134 | bench_format = bench_str2int(bench_format_str); |
| 135 | if (bench_format == BENCH_FORMAT_UNKNOWN) { |
| 136 | printf("Unknown format descriptor:%s\n", bench_format_str); |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | if (argc < 1) { |
| 141 | print_usage(); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | for (i = 0; subsystems[i].name; i++) { |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 146 | if (strcmp(subsystems[i].name, argv[0])) |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 147 | continue; |
| 148 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 149 | if (argc < 2) { |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 150 | /* No suite specified. */ |
| 151 | dump_suites(i); |
| 152 | goto end; |
| 153 | } |
| 154 | |
| 155 | for (j = 0; subsystems[i].suites[j].name; j++) { |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 156 | if (strcmp(subsystems[i].suites[j].name, argv[1])) |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 157 | continue; |
| 158 | |
Hitoshi Mitake | 79e295d | 2009-11-11 00:04:00 +0900 | [diff] [blame^] | 159 | if (bench_format == BENCH_FORMAT_DEFAULT) |
| 160 | printf("# Running %s/%s benchmark...\n", |
| 161 | subsystems[i].name, |
| 162 | subsystems[i].suites[j].name); |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 163 | status = subsystems[i].suites[j].fn(argc - 1, |
| 164 | argv + 1, prefix); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 165 | goto end; |
| 166 | } |
| 167 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 168 | if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 169 | dump_suites(i); |
| 170 | goto end; |
| 171 | } |
| 172 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 173 | printf("Unknown suite:%s for %s\n", argv[1], argv[0]); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 174 | status = 1; |
| 175 | goto end; |
| 176 | } |
| 177 | |
Hitoshi Mitake | 386d7e9 | 2009-11-10 08:20:00 +0900 | [diff] [blame] | 178 | printf("Unknown subsystem:%s\n", argv[0]); |
Hitoshi Mitake | 629cc35 | 2009-11-05 09:31:34 +0900 | [diff] [blame] | 179 | status = 1; |
| 180 | |
| 181 | end: |
| 182 | return status; |
| 183 | } |