blob: e5d514bf5365a41f6325bd8467dcdc067a1da171 [file] [log] [blame]
Hitoshi Mitake629cc352009-11-05 09:31:34 +09001/*
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 Mitake827f3b42009-11-18 00:20:09 +090015 * mem ... memory access performance
Hitoshi Mitake629cc352009-11-05 09:31:34 +090016 *
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
29struct bench_suite {
30 const char *name;
31 const char *summary;
32 int (*fn)(int, const char **, const char *);
33};
Hitoshi Mitake20442792009-12-13 17:01:59 +090034 \
35/* sentinel: easy for help */
Namhyung Kim08942f62012-06-20 15:08:06 +090036#define suite_all { "all", "Test all benchmark suites", NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090037
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010038static struct bench_suite numa_suites[] = {
39 { "mem",
40 "Benchmark for NUMA workloads",
41 bench_numa },
42 suite_all,
43 { NULL,
44 NULL,
45 NULL }
46};
47
Hitoshi Mitake629cc352009-11-05 09:31:34 +090048static struct bench_suite sched_suites[] = {
49 { "messaging",
50 "Benchmark for scheduler and IPC mechanisms",
51 bench_sched_messaging },
52 { "pipe",
53 "Flood of communication over pipe() between two processes",
54 bench_sched_pipe },
Hitoshi Mitake20442792009-12-13 17:01:59 +090055 suite_all,
Hitoshi Mitake629cc352009-11-05 09:31:34 +090056 { NULL,
57 NULL,
58 NULL }
59};
60
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090061static struct bench_suite mem_suites[] = {
62 { "memcpy",
63 "Simple memory copy in various ways",
64 bench_mem_memcpy },
Jan Beulichbe3de802012-01-24 10:03:22 -020065 { "memset",
66 "Simple memory set in various ways",
67 bench_mem_memset },
Hitoshi Mitake20442792009-12-13 17:01:59 +090068 suite_all,
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090069 { NULL,
70 NULL,
71 NULL }
72};
73
Hitoshi Mitake629cc352009-11-05 09:31:34 +090074struct bench_subsys {
75 const char *name;
76 const char *summary;
77 struct bench_suite *suites;
78};
79
80static struct bench_subsys subsystems[] = {
Ingo Molnar1c13f3c2012-12-06 13:51:59 +010081 { "numa",
82 "NUMA scheduling and MM behavior",
83 numa_suites },
Hitoshi Mitake629cc352009-11-05 09:31:34 +090084 { "sched",
85 "scheduler and IPC mechanism",
86 sched_suites },
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090087 { "mem",
88 "memory access performance",
89 mem_suites },
Hitoshi Mitake20442792009-12-13 17:01:59 +090090 { "all", /* sentinel: easy for help */
Namhyung Kim08942f62012-06-20 15:08:06 +090091 "all benchmark subsystem",
Hitoshi Mitake20442792009-12-13 17:01:59 +090092 NULL },
Hitoshi Mitake629cc352009-11-05 09:31:34 +090093 { NULL,
94 NULL,
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090095 NULL }
Hitoshi Mitake629cc352009-11-05 09:31:34 +090096};
97
98static void dump_suites(int subsys_index)
99{
100 int i;
101
Hitoshi Mitake20442792009-12-13 17:01:59 +0900102 printf("# List of available suites for %s...\n\n",
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900103 subsystems[subsys_index].name);
104
105 for (i = 0; subsystems[subsys_index].suites[i].name; i++)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900106 printf("%14s: %s\n",
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900107 subsystems[subsys_index].suites[i].name,
108 subsystems[subsys_index].suites[i].summary);
109
110 printf("\n");
111 return;
112}
113
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300114static const char *bench_format_str;
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900115int bench_format = BENCH_FORMAT_DEFAULT;
116
117static const struct option bench_options[] = {
118 OPT_STRING('f', "format", &bench_format_str, "default",
119 "Specify format style"),
120 OPT_END()
121};
122
123static const char * const bench_usage[] = {
124 "perf bench [<common options>] <subsystem> <suite> [<options>]",
125 NULL
126};
127
128static void print_usage(void)
129{
130 int i;
131
132 printf("Usage: \n");
133 for (i = 0; bench_usage[i]; i++)
134 printf("\t%s\n", bench_usage[i]);
135 printf("\n");
136
Hitoshi Mitake20442792009-12-13 17:01:59 +0900137 printf("# List of available subsystems...\n\n");
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900138
139 for (i = 0; subsystems[i].name; i++)
Hitoshi Mitake20442792009-12-13 17:01:59 +0900140 printf("%14s: %s\n",
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900141 subsystems[i].name, subsystems[i].summary);
142 printf("\n");
143}
144
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300145static int bench_str2int(const char *str)
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900146{
147 if (!str)
148 return BENCH_FORMAT_DEFAULT;
149
150 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR))
151 return BENCH_FORMAT_DEFAULT;
152 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR))
153 return BENCH_FORMAT_SIMPLE;
154
155 return BENCH_FORMAT_UNKNOWN;
156}
157
Hitoshi Mitake20442792009-12-13 17:01:59 +0900158static void all_suite(struct bench_subsys *subsys) /* FROM HERE */
159{
160 int i;
161 const char *argv[2];
162 struct bench_suite *suites = subsys->suites;
163
164 argv[1] = NULL;
165 /*
166 * TODO:
167 * preparing preset parameters for
168 * embedded, ordinary PC, HPC, etc...
169 * will be helpful
170 */
171 for (i = 0; suites[i].fn; i++) {
172 printf("# Running %s/%s benchmark...\n",
173 subsys->name,
174 suites[i].name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900175 fflush(stdout);
Hitoshi Mitake20442792009-12-13 17:01:59 +0900176
177 argv[1] = suites[i].name;
178 suites[i].fn(1, argv, NULL);
179 printf("\n");
180 }
181}
182
183static void all_subsystem(void)
184{
185 int i;
186 for (i = 0; subsystems[i].suites; i++)
187 all_suite(&subsystems[i]);
188}
189
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300190int cmd_bench(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900191{
192 int i, j, status = 0;
193
194 if (argc < 2) {
195 /* No subsystem specified. */
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900196 print_usage();
197 goto end;
198 }
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900199
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900200 argc = parse_options(argc, argv, bench_options, bench_usage,
201 PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900202
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900203 bench_format = bench_str2int(bench_format_str);
204 if (bench_format == BENCH_FORMAT_UNKNOWN) {
205 printf("Unknown format descriptor:%s\n", bench_format_str);
206 goto end;
207 }
208
209 if (argc < 1) {
210 print_usage();
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900211 goto end;
212 }
213
Hitoshi Mitake20442792009-12-13 17:01:59 +0900214 if (!strcmp(argv[0], "all")) {
215 all_subsystem();
216 goto end;
217 }
218
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900219 for (i = 0; subsystems[i].name; i++) {
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900220 if (strcmp(subsystems[i].name, argv[0]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900221 continue;
222
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900223 if (argc < 2) {
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900224 /* No suite specified. */
225 dump_suites(i);
226 goto end;
227 }
228
Hitoshi Mitake20442792009-12-13 17:01:59 +0900229 if (!strcmp(argv[1], "all")) {
230 all_suite(&subsystems[i]);
231 goto end;
232 }
233
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900234 for (j = 0; subsystems[i].suites[j].name; j++) {
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900235 if (strcmp(subsystems[i].suites[j].name, argv[1]))
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900236 continue;
237
Hitoshi Mitake79e295d2009-11-11 00:04:00 +0900238 if (bench_format == BENCH_FORMAT_DEFAULT)
239 printf("# Running %s/%s benchmark...\n",
240 subsystems[i].name,
241 subsystems[i].suites[j].name);
Namhyung Kim9b494ea2013-01-08 18:39:26 +0900242 fflush(stdout);
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900243 status = subsystems[i].suites[j].fn(argc - 1,
244 argv + 1, prefix);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900245 goto end;
246 }
247
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900248 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900249 dump_suites(i);
250 goto end;
251 }
252
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900253 printf("Unknown suite:%s for %s\n", argv[1], argv[0]);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900254 status = 1;
255 goto end;
256 }
257
Hitoshi Mitake386d7e92009-11-10 08:20:00 +0900258 printf("Unknown subsystem:%s\n", argv[0]);
Hitoshi Mitake629cc352009-11-05 09:31:34 +0900259 status = 1;
260
261end:
262 return status;
263}