perf tests: Add arch tests
[cascardo/linux.git] / tools / perf / tests / builtin-test.c
1 /*
2  * builtin-test.c
3  *
4  * Builtin regression testing command: ever growing number of sanity tests
5  */
6 #include <unistd.h>
7 #include <string.h>
8 #include "builtin.h"
9 #include "hist.h"
10 #include "intlist.h"
11 #include "tests.h"
12 #include "debug.h"
13 #include "color.h"
14 #include "parse-options.h"
15 #include "symbol.h"
16
17 struct test __weak arch_tests[] = {
18         {
19                 .func = NULL,
20         },
21 };
22
23 static struct test generic_tests[] = {
24         {
25                 .desc = "vmlinux symtab matches kallsyms",
26                 .func = test__vmlinux_matches_kallsyms,
27         },
28         {
29                 .desc = "detect openat syscall event",
30                 .func = test__openat_syscall_event,
31         },
32         {
33                 .desc = "detect openat syscall event on all cpus",
34                 .func = test__openat_syscall_event_on_all_cpus,
35         },
36         {
37                 .desc = "read samples using the mmap interface",
38                 .func = test__basic_mmap,
39         },
40         {
41                 .desc = "parse events tests",
42                 .func = test__parse_events,
43         },
44 #if defined(__x86_64__) || defined(__i386__)
45         {
46                 .desc = "x86 rdpmc test",
47                 .func = test__rdpmc,
48         },
49 #endif
50         {
51                 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
52                 .func = test__PERF_RECORD,
53         },
54         {
55                 .desc = "Test perf pmu format parsing",
56                 .func = test__pmu,
57         },
58         {
59                 .desc = "Test dso data read",
60                 .func = test__dso_data,
61         },
62         {
63                 .desc = "Test dso data cache",
64                 .func = test__dso_data_cache,
65         },
66         {
67                 .desc = "Test dso data reopen",
68                 .func = test__dso_data_reopen,
69         },
70         {
71                 .desc = "roundtrip evsel->name check",
72                 .func = test__perf_evsel__roundtrip_name_test,
73         },
74         {
75                 .desc = "Check parsing of sched tracepoints fields",
76                 .func = test__perf_evsel__tp_sched_test,
77         },
78         {
79                 .desc = "Generate and check syscalls:sys_enter_openat event fields",
80                 .func = test__syscall_openat_tp_fields,
81         },
82         {
83                 .desc = "struct perf_event_attr setup",
84                 .func = test__attr,
85         },
86         {
87                 .desc = "Test matching and linking multiple hists",
88                 .func = test__hists_link,
89         },
90         {
91                 .desc = "Try 'import perf' in python, checking link problems",
92                 .func = test__python_use,
93         },
94         {
95                 .desc = "Test breakpoint overflow signal handler",
96                 .func = test__bp_signal,
97         },
98         {
99                 .desc = "Test breakpoint overflow sampling",
100                 .func = test__bp_signal_overflow,
101         },
102         {
103                 .desc = "Test number of exit event of a simple workload",
104                 .func = test__task_exit,
105         },
106         {
107                 .desc = "Test software clock events have valid period values",
108                 .func = test__sw_clock_freq,
109         },
110 #if defined(__x86_64__) || defined(__i386__)
111         {
112                 .desc = "Test converting perf time to TSC",
113                 .func = test__perf_time_to_tsc,
114         },
115 #endif
116         {
117                 .desc = "Test object code reading",
118                 .func = test__code_reading,
119         },
120         {
121                 .desc = "Test sample parsing",
122                 .func = test__sample_parsing,
123         },
124         {
125                 .desc = "Test using a dummy software event to keep tracking",
126                 .func = test__keep_tracking,
127         },
128         {
129                 .desc = "Test parsing with no sample_id_all bit set",
130                 .func = test__parse_no_sample_id_all,
131         },
132 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__)
133 #ifdef HAVE_DWARF_UNWIND_SUPPORT
134         {
135                 .desc = "Test dwarf unwind",
136                 .func = test__dwarf_unwind,
137         },
138 #endif
139 #endif
140         {
141                 .desc = "Test filtering hist entries",
142                 .func = test__hists_filter,
143         },
144         {
145                 .desc = "Test mmap thread lookup",
146                 .func = test__mmap_thread_lookup,
147         },
148         {
149                 .desc = "Test thread mg sharing",
150                 .func = test__thread_mg_share,
151         },
152         {
153                 .desc = "Test output sorting of hist entries",
154                 .func = test__hists_output,
155         },
156         {
157                 .desc = "Test cumulation of child hist entries",
158                 .func = test__hists_cumulate,
159         },
160         {
161                 .desc = "Test tracking with sched_switch",
162                 .func = test__switch_tracking,
163         },
164         {
165                 .desc = "Filter fds with revents mask in a fdarray",
166                 .func = test__fdarray__filter,
167         },
168         {
169                 .desc = "Add fd to a fdarray, making it autogrow",
170                 .func = test__fdarray__add,
171         },
172         {
173                 .desc = "Test kmod_path__parse function",
174                 .func = test__kmod_path__parse,
175         },
176         {
177                 .desc = "Test thread map",
178                 .func = test__thread_map,
179         },
180         {
181                 .desc = "Test LLVM searching and compiling",
182                 .func = test__llvm,
183         },
184 #ifdef HAVE_AUXTRACE_SUPPORT
185 #if defined(__x86_64__) || defined(__i386__)
186         {
187                 .desc = "Test x86 instruction decoder - new instructions",
188                 .func = test__insn_x86,
189         },
190 #endif
191 #endif
192         {
193                 .desc = "Test topology in session",
194                 .func = test_session_topology,
195         },
196         {
197                 .func = NULL,
198         },
199 };
200
201 static struct test *tests[] = {
202         generic_tests,
203         arch_tests,
204 };
205
206 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
207 {
208         int i;
209
210         if (argc == 0)
211                 return true;
212
213         for (i = 0; i < argc; ++i) {
214                 char *end;
215                 long nr = strtoul(argv[i], &end, 10);
216
217                 if (*end == '\0') {
218                         if (nr == curr + 1)
219                                 return true;
220                         continue;
221                 }
222
223                 if (strstr(test->desc, argv[i]))
224                         return true;
225         }
226
227         return false;
228 }
229
230 static int run_test(struct test *test)
231 {
232         int status, err = -1, child = fork();
233         char sbuf[STRERR_BUFSIZE];
234
235         if (child < 0) {
236                 pr_err("failed to fork test: %s\n",
237                         strerror_r(errno, sbuf, sizeof(sbuf)));
238                 return -1;
239         }
240
241         if (!child) {
242                 pr_debug("test child forked, pid %d\n", getpid());
243                 err = test->func();
244                 exit(err);
245         }
246
247         wait(&status);
248
249         if (WIFEXITED(status)) {
250                 err = (signed char)WEXITSTATUS(status);
251                 pr_debug("test child finished with %d\n", err);
252         } else if (WIFSIGNALED(status)) {
253                 err = -1;
254                 pr_debug("test child interrupted\n");
255         }
256
257         return err;
258 }
259
260 #define for_each_test(j, t)                                     \
261         for (j = 0; j < ARRAY_SIZE(tests); j++) \
262                 for (t = &tests[j][0]; t->func; t++)
263
264 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
265 {
266         struct test *t;
267         unsigned int j;
268         int i = 0;
269         int width = 0;
270
271         for_each_test(j, t) {
272                 int len = strlen(t->desc);
273
274                 if (width < len)
275                         width = len;
276         }
277
278         for_each_test(j, t) {
279                 int curr = i++, err;
280
281                 if (!perf_test__matches(t, curr, argc, argv))
282                         continue;
283
284                 pr_info("%2d: %-*s:", i, width, t->desc);
285
286                 if (intlist__find(skiplist, i)) {
287                         color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
288                         continue;
289                 }
290
291                 pr_debug("\n--- start ---\n");
292                 err = run_test(t);
293                 pr_debug("---- end ----\n%s:", t->desc);
294
295                 switch (err) {
296                 case TEST_OK:
297                         pr_info(" Ok\n");
298                         break;
299                 case TEST_SKIP:
300                         color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
301                         break;
302                 case TEST_FAIL:
303                 default:
304                         color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
305                         break;
306                 }
307         }
308
309         return 0;
310 }
311
312 static int perf_test__list(int argc, const char **argv)
313 {
314         unsigned int j;
315         struct test *t;
316         int i = 0;
317
318         for_each_test(j, t) {
319                 if (argc > 1 && !strstr(t->desc, argv[1]))
320                         continue;
321
322                 pr_info("%2d: %s\n", ++i, t->desc);
323         }
324
325         return 0;
326 }
327
328 int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
329 {
330         const char *test_usage[] = {
331         "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
332         NULL,
333         };
334         const char *skip = NULL;
335         const struct option test_options[] = {
336         OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
337         OPT_INCR('v', "verbose", &verbose,
338                     "be more verbose (show symbol address, etc)"),
339         OPT_END()
340         };
341         const char * const test_subcommands[] = { "list", NULL };
342         struct intlist *skiplist = NULL;
343         int ret = hists__init();
344
345         if (ret < 0)
346                 return ret;
347
348         argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
349         if (argc >= 1 && !strcmp(argv[0], "list"))
350                 return perf_test__list(argc, argv);
351
352         symbol_conf.priv_size = sizeof(int);
353         symbol_conf.sort_by_name = true;
354         symbol_conf.try_vmlinux_path = true;
355
356         if (symbol__init(NULL) < 0)
357                 return -1;
358
359         if (skip != NULL)
360                 skiplist = intlist__new(skip);
361
362         return __cmd_test(argc, argv, skiplist);
363 }