blob: 6a4145e5ad2cdfbe7d08c2955184df8c4ab8afd9 [file] [log] [blame]
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001/*
2 * builtin-test.c
3 *
4 * Builtin regression testing command: ever growing number of sanity tests
5 */
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +02006#include <unistd.h>
7#include <string.h>
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03008#include "builtin.h"
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -03009#include "intlist.h"
Jiri Olsa0a4e1ae2012-11-10 01:46:41 +010010#include "tests.h"
Jiri Olsac81251e2012-11-10 01:46:51 +010011#include "debug.h"
12#include "color.h"
13#include "parse-options.h"
14#include "symbol.h"
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -020015
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030016static struct test {
17 const char *desc;
18 int (*func)(void);
19} tests[] = {
20 {
21 .desc = "vmlinux symtab matches kallsyms",
22 .func = test__vmlinux_matches_kallsyms,
23 },
24 {
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -020025 .desc = "detect open syscall event",
26 .func = test__open_syscall_event,
27 },
28 {
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -020029 .desc = "detect open syscall event on all cpus",
30 .func = test__open_syscall_event_on_all_cpus,
31 },
32 {
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -020033 .desc = "read samples using the mmap interface",
34 .func = test__basic_mmap,
35 },
36 {
Jiri Olsa13b62562011-07-14 11:25:33 +020037 .desc = "parse events tests",
Jiri Olsac81251e2012-11-10 01:46:51 +010038 .func = test__parse_events,
Jiri Olsa13b62562011-07-14 11:25:33 +020039 },
Peter Zijlstra08aa0d12011-11-21 14:42:47 +010040#if defined(__x86_64__) || defined(__i386__)
41 {
42 .desc = "x86 rdpmc test",
43 .func = test__rdpmc,
44 },
45#endif
Jiri Olsa13b62562011-07-14 11:25:33 +020046 {
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -020047 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
48 .func = test__PERF_RECORD,
49 },
50 {
Jiri Olsacd82a322012-03-15 20:09:17 +010051 .desc = "Test perf pmu format parsing",
Jiri Olsacff7f952012-11-10 01:46:50 +010052 .func = test__pmu,
Jiri Olsacd82a322012-03-15 20:09:17 +010053 },
54 {
Jiri Olsa4ebbcb82014-05-12 14:43:53 +020055 .desc = "Test dso data read",
Jiri Olsac81251e2012-11-10 01:46:51 +010056 .func = test__dso_data,
Jiri Olsaf7add552012-07-22 14:14:40 +020057 },
58 {
Jiri Olsa4ebbcb82014-05-12 14:43:53 +020059 .desc = "Test dso data cache",
60 .func = test__dso_data_cache,
61 },
62 {
Jiri Olsa45dc1bb52014-05-12 14:50:03 +020063 .desc = "Test dso data reopen",
64 .func = test__dso_data_reopen,
65 },
66 {
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -030067 .desc = "roundtrip evsel->name check",
Jiri Olsacfffae22012-11-10 01:46:47 +010068 .func = test__perf_evsel__roundtrip_name_test,
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -030069 },
70 {
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -030071 .desc = "Check parsing of sched tracepoints fields",
Jiri Olsa5e24a092012-11-10 01:46:48 +010072 .func = test__perf_evsel__tp_sched_test,
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -030073 },
74 {
Arnaldo Carvalho de Meloeb2f2702012-09-26 13:23:10 -030075 .desc = "Generate and check syscalls:sys_enter_open event fields",
76 .func = test__syscall_open_tp_fields,
77 },
78 {
Jiri Olsad898b242012-10-30 23:02:05 +010079 .desc = "struct perf_event_attr setup",
Jiri Olsac81251e2012-11-10 01:46:51 +010080 .func = test__attr,
Jiri Olsad898b242012-10-30 23:02:05 +010081 },
82 {
Arnaldo Carvalho de Meloffcbaa12013-05-23 12:08:38 +020083 .desc = "Test matching and linking multiple hists",
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090084 .func = test__hists_link,
85 },
86 {
Arnaldo Carvalho de Melo54359d32012-12-14 13:06:13 -030087 .desc = "Try 'use perf' in python, checking link problems",
88 .func = test__python_use,
89 },
90 {
Jiri Olsa5a6bef42013-03-10 19:41:10 +010091 .desc = "Test breakpoint overflow signal handler",
92 .func = test__bp_signal,
93 },
94 {
Jiri Olsa06933e32013-03-10 19:41:11 +010095 .desc = "Test breakpoint overflow sampling",
96 .func = test__bp_signal_overflow,
97 },
98 {
Namhyung Kimd723a552013-03-15 14:58:11 +090099 .desc = "Test number of exit event of a simple workload",
100 .func = test__task_exit,
101 },
102 {
Namhyung Kimbc96b362013-03-18 11:41:47 +0900103 .desc = "Test software clock events have valid period values",
104 .func = test__sw_clock_freq,
105 },
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300106#if defined(__x86_64__) || defined(__i386__)
107 {
108 .desc = "Test converting perf time to TSC",
109 .func = test__perf_time_to_tsc,
110 },
111#endif
Namhyung Kimbc96b362013-03-18 11:41:47 +0900112 {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300113 .desc = "Test object code reading",
114 .func = test__code_reading,
115 },
116 {
Adrian Hunter045f8cd82013-08-27 11:23:13 +0300117 .desc = "Test sample parsing",
118 .func = test__sample_parsing,
119 },
120 {
Adrian Hunter395c3072013-08-31 21:50:53 +0300121 .desc = "Test using a dummy software event to keep tracking",
122 .func = test__keep_tracking,
123 },
124 {
Adrian Hunter53a277e2013-09-04 23:18:16 +0300125 .desc = "Test parsing with no sample_id_all bit set",
126 .func = test__parse_no_sample_id_all,
127 },
Jean Pihet90fa9de2014-05-16 10:41:11 +0200128#if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
Jiri Olsa9ff125d2014-01-07 13:47:28 +0100129#ifdef HAVE_DWARF_UNWIND_SUPPORT
Jiri Olsaaa16b812014-01-07 13:47:22 +0100130 {
131 .desc = "Test dwarf unwind",
132 .func = test__dwarf_unwind,
133 },
134#endif
135#endif
Adrian Hunter53a277e2013-09-04 23:18:16 +0300136 {
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900137 .desc = "Test filtering hist entries",
138 .func = test__hists_filter,
139 },
140 {
Jiri Olsa4e85edf2014-03-05 17:20:31 +0100141 .desc = "Test mmap thread lookup",
142 .func = test__mmap_thread_lookup,
143 },
144 {
Jiri Olsafabf012382014-03-17 14:39:00 +0100145 .desc = "Test thread mg sharing",
146 .func = test__thread_mg_share,
147 },
148 {
Namhyung Kimf21d1812014-05-12 14:43:18 +0900149 .desc = "Test output sorting of hist entries",
150 .func = test__hists_output,
151 },
152 {
Namhyung Kim0506aec2014-05-23 18:04:42 +0900153 .desc = "Test cumulation of child hist entries",
154 .func = test__hists_cumulate,
155 },
156 {
Adrian Hunterd44bc552014-08-15 22:08:36 +0300157 .desc = "Test tracking with sched_switch",
158 .func = test__switch_tracking,
159 },
160 {
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300161 .func = NULL,
162 },
163};
164
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200165static bool perf_test__matches(int curr, int argc, const char *argv[])
166{
167 int i;
168
169 if (argc == 0)
170 return true;
171
172 for (i = 0; i < argc; ++i) {
173 char *end;
174 long nr = strtoul(argv[i], &end, 10);
175
176 if (*end == '\0') {
177 if (nr == curr + 1)
178 return true;
179 continue;
180 }
181
182 if (strstr(tests[curr].desc, argv[i]))
183 return true;
184 }
185
186 return false;
187}
188
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200189static int run_test(struct test *test)
190{
191 int status, err = -1, child = fork();
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000192 char sbuf[STRERR_BUFSIZE];
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200193
194 if (child < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000195 pr_err("failed to fork test: %s\n",
196 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200197 return -1;
198 }
199
200 if (!child) {
201 pr_debug("test child forked, pid %d\n", getpid());
202 err = test->func();
203 exit(err);
204 }
205
206 wait(&status);
207
208 if (WIFEXITED(status)) {
209 err = WEXITSTATUS(status);
210 pr_debug("test child finished with %d\n", err);
211 } else if (WIFSIGNALED(status)) {
212 err = -1;
213 pr_debug("test child interrupted\n");
214 }
215
216 return err;
217}
218
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300219static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300220{
221 int i = 0;
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200222 int width = 0;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300223
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300224 while (tests[i].func) {
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200225 int len = strlen(tests[i].desc);
226
227 if (width < len)
228 width = len;
229 ++i;
230 }
Jiri Olsa945aea22012-10-30 23:01:43 +0100231
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200232 i = 0;
233 while (tests[i].func) {
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200234 int curr = i++, err;
235
236 if (!perf_test__matches(curr, argc, argv))
237 continue;
238
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200239 pr_info("%2d: %-*s:", i, width, tests[curr].desc);
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300240
241 if (intlist__find(skiplist, i)) {
242 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
243 continue;
244 }
245
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300246 pr_debug("\n--- start ---\n");
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200247 err = run_test(&tests[curr]);
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200248 pr_debug("---- end ----\n%s:", tests[curr].desc);
Jiri Olsaf4c1ea52012-12-19 11:33:39 -0300249
250 switch (err) {
251 case TEST_OK:
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200252 pr_info(" Ok\n");
Jiri Olsaf4c1ea52012-12-19 11:33:39 -0300253 break;
254 case TEST_SKIP:
255 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
256 break;
257 case TEST_FAIL:
258 default:
259 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
260 break;
261 }
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300262 }
263
264 return 0;
265}
266
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200267static int perf_test__list(int argc, const char **argv)
268{
269 int i = 0;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300270
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200271 while (tests[i].func) {
272 int curr = i++;
273
274 if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
275 continue;
276
277 pr_info("%2d: %s\n", i, tests[curr].desc);
278 }
279
280 return 0;
281}
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300282
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300283int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300284{
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200285 const char * const test_usage[] = {
286 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
287 NULL,
288 };
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300289 const char *skip = NULL;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200290 const struct option test_options[] = {
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300291 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
Namhyung Kimc30ab8a2012-01-08 02:25:26 +0900292 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200293 "be more verbose (show symbol address, etc)"),
294 OPT_END()
295 };
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300296 struct intlist *skiplist = NULL;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200297
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300298 argc = parse_options(argc, argv, test_options, test_usage, 0);
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200299 if (argc >= 1 && !strcmp(argv[0], "list"))
300 return perf_test__list(argc, argv);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300301
302 symbol_conf.priv_size = sizeof(int);
303 symbol_conf.sort_by_name = true;
304 symbol_conf.try_vmlinux_path = true;
305
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900306 if (symbol__init(NULL) < 0)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300307 return -1;
308
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300309 if (skip != NULL)
310 skiplist = intlist__new(skip);
311
312 return __cmd_test(argc, argv, skiplist);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300313}