blob: 136cd934be66a862baa5300397d6b7254155d069 [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 Meloa635fc52014-10-09 16:16:00 -03009#include "hist.h"
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -030010#include "intlist.h"
Jiri Olsa0a4e1ae2012-11-10 01:46:41 +010011#include "tests.h"
Jiri Olsac81251e2012-11-10 01:46:51 +010012#include "debug.h"
13#include "color.h"
14#include "parse-options.h"
15#include "symbol.h"
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -020016
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030017static struct test {
18 const char *desc;
19 int (*func)(void);
20} tests[] = {
21 {
22 .desc = "vmlinux symtab matches kallsyms",
23 .func = test__vmlinux_matches_kallsyms,
24 },
25 {
Riku Voipio43f322b2015-04-16 16:52:53 +030026 .desc = "detect openat syscall event",
27 .func = test__openat_syscall_event,
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -020028 },
29 {
Riku Voipio43f322b2015-04-16 16:52:53 +030030 .desc = "detect openat syscall event on all cpus",
31 .func = test__openat_syscall_event_on_all_cpus,
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -020032 },
33 {
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -020034 .desc = "read samples using the mmap interface",
35 .func = test__basic_mmap,
36 },
37 {
Jiri Olsa13b62562011-07-14 11:25:33 +020038 .desc = "parse events tests",
Jiri Olsac81251e2012-11-10 01:46:51 +010039 .func = test__parse_events,
Jiri Olsa13b62562011-07-14 11:25:33 +020040 },
Peter Zijlstra08aa0d12011-11-21 14:42:47 +010041#if defined(__x86_64__) || defined(__i386__)
42 {
43 .desc = "x86 rdpmc test",
44 .func = test__rdpmc,
45 },
46#endif
Jiri Olsa13b62562011-07-14 11:25:33 +020047 {
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -020048 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
49 .func = test__PERF_RECORD,
50 },
51 {
Jiri Olsacd82a322012-03-15 20:09:17 +010052 .desc = "Test perf pmu format parsing",
Jiri Olsacff7f952012-11-10 01:46:50 +010053 .func = test__pmu,
Jiri Olsacd82a322012-03-15 20:09:17 +010054 },
55 {
Jiri Olsa4ebbcb82014-05-12 14:43:53 +020056 .desc = "Test dso data read",
Jiri Olsac81251e2012-11-10 01:46:51 +010057 .func = test__dso_data,
Jiri Olsaf7add552012-07-22 14:14:40 +020058 },
59 {
Jiri Olsa4ebbcb82014-05-12 14:43:53 +020060 .desc = "Test dso data cache",
61 .func = test__dso_data_cache,
62 },
63 {
Jiri Olsa45dc1bb52014-05-12 14:50:03 +020064 .desc = "Test dso data reopen",
65 .func = test__dso_data_reopen,
66 },
67 {
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -030068 .desc = "roundtrip evsel->name check",
Jiri Olsacfffae22012-11-10 01:46:47 +010069 .func = test__perf_evsel__roundtrip_name_test,
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -030070 },
71 {
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -030072 .desc = "Check parsing of sched tracepoints fields",
Jiri Olsa5e24a092012-11-10 01:46:48 +010073 .func = test__perf_evsel__tp_sched_test,
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -030074 },
75 {
Riku Voipio43f322b2015-04-16 16:52:53 +030076 .desc = "Generate and check syscalls:sys_enter_openat event fields",
77 .func = test__syscall_openat_tp_fields,
Arnaldo Carvalho de Meloeb2f2702012-09-26 13:23:10 -030078 },
79 {
Jiri Olsad898b242012-10-30 23:02:05 +010080 .desc = "struct perf_event_attr setup",
Jiri Olsac81251e2012-11-10 01:46:51 +010081 .func = test__attr,
Jiri Olsad898b242012-10-30 23:02:05 +010082 },
83 {
Arnaldo Carvalho de Meloffcbaa12013-05-23 12:08:38 +020084 .desc = "Test matching and linking multiple hists",
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090085 .func = test__hists_link,
86 },
87 {
WANG Chao887e73d72014-11-12 16:27:05 +080088 .desc = "Try 'import perf' in python, checking link problems",
Arnaldo Carvalho de Melo54359d32012-12-14 13:06:13 -030089 .func = test__python_use,
90 },
91 {
Jiri Olsa5a6bef42013-03-10 19:41:10 +010092 .desc = "Test breakpoint overflow signal handler",
93 .func = test__bp_signal,
94 },
95 {
Jiri Olsa06933e32013-03-10 19:41:11 +010096 .desc = "Test breakpoint overflow sampling",
97 .func = test__bp_signal_overflow,
98 },
99 {
Namhyung Kimd723a552013-03-15 14:58:11 +0900100 .desc = "Test number of exit event of a simple workload",
101 .func = test__task_exit,
102 },
103 {
Namhyung Kimbc96b362013-03-18 11:41:47 +0900104 .desc = "Test software clock events have valid period values",
105 .func = test__sw_clock_freq,
106 },
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300107#if defined(__x86_64__) || defined(__i386__)
108 {
109 .desc = "Test converting perf time to TSC",
110 .func = test__perf_time_to_tsc,
111 },
112#endif
Namhyung Kimbc96b362013-03-18 11:41:47 +0900113 {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300114 .desc = "Test object code reading",
115 .func = test__code_reading,
116 },
117 {
Adrian Hunter045f8cd82013-08-27 11:23:13 +0300118 .desc = "Test sample parsing",
119 .func = test__sample_parsing,
120 },
121 {
Adrian Hunter395c3072013-08-31 21:50:53 +0300122 .desc = "Test using a dummy software event to keep tracking",
123 .func = test__keep_tracking,
124 },
125 {
Adrian Hunter53a277e2013-09-04 23:18:16 +0300126 .desc = "Test parsing with no sample_id_all bit set",
127 .func = test__parse_no_sample_id_all,
128 },
Wang Nan3237f2812015-03-27 13:08:01 +0000129#if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__)
Jiri Olsa9ff125d2014-01-07 13:47:28 +0100130#ifdef HAVE_DWARF_UNWIND_SUPPORT
Jiri Olsaaa16b812014-01-07 13:47:22 +0100131 {
132 .desc = "Test dwarf unwind",
133 .func = test__dwarf_unwind,
134 },
135#endif
136#endif
Adrian Hunter53a277e2013-09-04 23:18:16 +0300137 {
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900138 .desc = "Test filtering hist entries",
139 .func = test__hists_filter,
140 },
141 {
Jiri Olsa4e85edf2014-03-05 17:20:31 +0100142 .desc = "Test mmap thread lookup",
143 .func = test__mmap_thread_lookup,
144 },
145 {
Jiri Olsafabf012382014-03-17 14:39:00 +0100146 .desc = "Test thread mg sharing",
147 .func = test__thread_mg_share,
148 },
149 {
Namhyung Kimf21d1812014-05-12 14:43:18 +0900150 .desc = "Test output sorting of hist entries",
151 .func = test__hists_output,
152 },
153 {
Namhyung Kim0506aec2014-05-23 18:04:42 +0900154 .desc = "Test cumulation of child hist entries",
155 .func = test__hists_cumulate,
156 },
157 {
Adrian Hunterd44bc552014-08-15 22:08:36 +0300158 .desc = "Test tracking with sched_switch",
159 .func = test__switch_tracking,
160 },
161 {
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300162 .desc = "Filter fds with revents mask in a fdarray",
163 .func = test__fdarray__filter,
Arnaldo Carvalho de Melo54dbfae2014-08-12 23:34:06 -0300164 },
165 {
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300166 .desc = "Add fd to a fdarray, making it autogrow",
167 .func = test__fdarray__add,
Arnaldo Carvalho de Melo9ae28032014-08-18 16:49:00 -0300168 },
169 {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100170 .desc = "Test kmod_path__parse function",
171 .func = test__kmod_path__parse,
172 },
173 {
Jiri Olsa134aa442015-06-26 11:29:08 +0200174 .desc = "Test thread map",
175 .func = test__thread_map,
176 },
177 {
Wang Nan9bc898c2015-07-08 10:04:02 +0000178 .desc = "Test LLVM searching and compiling",
179 .func = test__llvm,
180 },
181 {
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300182 .func = NULL,
183 },
184};
185
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200186static bool perf_test__matches(int curr, int argc, const char *argv[])
187{
188 int i;
189
190 if (argc == 0)
191 return true;
192
193 for (i = 0; i < argc; ++i) {
194 char *end;
195 long nr = strtoul(argv[i], &end, 10);
196
197 if (*end == '\0') {
198 if (nr == curr + 1)
199 return true;
200 continue;
201 }
202
203 if (strstr(tests[curr].desc, argv[i]))
204 return true;
205 }
206
207 return false;
208}
209
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200210static int run_test(struct test *test)
211{
212 int status, err = -1, child = fork();
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000213 char sbuf[STRERR_BUFSIZE];
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200214
215 if (child < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000216 pr_err("failed to fork test: %s\n",
217 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200218 return -1;
219 }
220
221 if (!child) {
222 pr_debug("test child forked, pid %d\n", getpid());
223 err = test->func();
224 exit(err);
225 }
226
227 wait(&status);
228
229 if (WIFEXITED(status)) {
He Kuang189c4662015-05-11 12:28:35 +0000230 err = (signed char)WEXITSTATUS(status);
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200231 pr_debug("test child finished with %d\n", err);
232 } else if (WIFSIGNALED(status)) {
233 err = -1;
234 pr_debug("test child interrupted\n");
235 }
236
237 return err;
238}
239
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300240static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300241{
242 int i = 0;
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200243 int width = 0;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300244
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300245 while (tests[i].func) {
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200246 int len = strlen(tests[i].desc);
247
248 if (width < len)
249 width = len;
250 ++i;
251 }
Jiri Olsa945aea22012-10-30 23:01:43 +0100252
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200253 i = 0;
254 while (tests[i].func) {
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200255 int curr = i++, err;
256
257 if (!perf_test__matches(curr, argc, argv))
258 continue;
259
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200260 pr_info("%2d: %-*s:", i, width, tests[curr].desc);
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300261
262 if (intlist__find(skiplist, i)) {
263 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
264 continue;
265 }
266
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300267 pr_debug("\n--- start ---\n");
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200268 err = run_test(&tests[curr]);
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200269 pr_debug("---- end ----\n%s:", tests[curr].desc);
Jiri Olsaf4c1ea52012-12-19 11:33:39 -0300270
271 switch (err) {
272 case TEST_OK:
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200273 pr_info(" Ok\n");
Jiri Olsaf4c1ea52012-12-19 11:33:39 -0300274 break;
275 case TEST_SKIP:
276 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
277 break;
278 case TEST_FAIL:
279 default:
280 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
281 break;
282 }
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300283 }
284
285 return 0;
286}
287
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200288static int perf_test__list(int argc, const char **argv)
289{
290 int i = 0;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300291
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200292 while (tests[i].func) {
293 int curr = i++;
294
295 if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
296 continue;
297
298 pr_info("%2d: %s\n", i, tests[curr].desc);
299 }
300
301 return 0;
302}
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300303
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300304int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300305{
Yunlong Song1f9975f2015-03-18 21:35:55 +0800306 const char *test_usage[] = {
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200307 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
308 NULL,
309 };
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300310 const char *skip = NULL;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200311 const struct option test_options[] = {
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300312 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
Namhyung Kimc30ab8a2012-01-08 02:25:26 +0900313 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200314 "be more verbose (show symbol address, etc)"),
315 OPT_END()
316 };
Yunlong Song1f9975f2015-03-18 21:35:55 +0800317 const char * const test_subcommands[] = { "list", NULL };
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300318 struct intlist *skiplist = NULL;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -0300319 int ret = hists__init();
320
321 if (ret < 0)
322 return ret;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200323
Yunlong Song1f9975f2015-03-18 21:35:55 +0800324 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200325 if (argc >= 1 && !strcmp(argv[0], "list"))
326 return perf_test__list(argc, argv);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300327
328 symbol_conf.priv_size = sizeof(int);
329 symbol_conf.sort_by_name = true;
330 symbol_conf.try_vmlinux_path = true;
331
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900332 if (symbol__init(NULL) < 0)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300333 return -1;
334
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300335 if (skip != NULL)
336 skiplist = intlist__new(skip);
337
338 return __cmd_test(argc, argv, skiplist);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300339}