Merge tag 'armsoc-arm64' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[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 <subcmd/parse-options.h>
15 #include "symbol.h"
16
17 static bool dont_fork;
18
19 struct test __weak arch_tests[] = {
20         {
21                 .func = NULL,
22         },
23 };
24
25 static struct test generic_tests[] = {
26         {
27                 .desc = "vmlinux symtab matches kallsyms",
28                 .func = test__vmlinux_matches_kallsyms,
29         },
30         {
31                 .desc = "detect openat syscall event",
32                 .func = test__openat_syscall_event,
33         },
34         {
35                 .desc = "detect openat syscall event on all cpus",
36                 .func = test__openat_syscall_event_on_all_cpus,
37         },
38         {
39                 .desc = "read samples using the mmap interface",
40                 .func = test__basic_mmap,
41         },
42         {
43                 .desc = "parse events tests",
44                 .func = test__parse_events,
45         },
46         {
47                 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
48                 .func = test__PERF_RECORD,
49         },
50         {
51                 .desc = "Test perf pmu format parsing",
52                 .func = test__pmu,
53         },
54         {
55                 .desc = "Test dso data read",
56                 .func = test__dso_data,
57         },
58         {
59                 .desc = "Test dso data cache",
60                 .func = test__dso_data_cache,
61         },
62         {
63                 .desc = "Test dso data reopen",
64                 .func = test__dso_data_reopen,
65         },
66         {
67                 .desc = "roundtrip evsel->name check",
68                 .func = test__perf_evsel__roundtrip_name_test,
69         },
70         {
71                 .desc = "Check parsing of sched tracepoints fields",
72                 .func = test__perf_evsel__tp_sched_test,
73         },
74         {
75                 .desc = "Generate and check syscalls:sys_enter_openat event fields",
76                 .func = test__syscall_openat_tp_fields,
77         },
78         {
79                 .desc = "struct perf_event_attr setup",
80                 .func = test__attr,
81         },
82         {
83                 .desc = "Test matching and linking multiple hists",
84                 .func = test__hists_link,
85         },
86         {
87                 .desc = "Try 'import perf' in python, checking link problems",
88                 .func = test__python_use,
89         },
90         {
91                 .desc = "Test breakpoint overflow signal handler",
92                 .func = test__bp_signal,
93         },
94         {
95                 .desc = "Test breakpoint overflow sampling",
96                 .func = test__bp_signal_overflow,
97         },
98         {
99                 .desc = "Test number of exit event of a simple workload",
100                 .func = test__task_exit,
101         },
102         {
103                 .desc = "Test software clock events have valid period values",
104                 .func = test__sw_clock_freq,
105         },
106         {
107                 .desc = "Test object code reading",
108                 .func = test__code_reading,
109         },
110         {
111                 .desc = "Test sample parsing",
112                 .func = test__sample_parsing,
113         },
114         {
115                 .desc = "Test using a dummy software event to keep tracking",
116                 .func = test__keep_tracking,
117         },
118         {
119                 .desc = "Test parsing with no sample_id_all bit set",
120                 .func = test__parse_no_sample_id_all,
121         },
122         {
123                 .desc = "Test filtering hist entries",
124                 .func = test__hists_filter,
125         },
126         {
127                 .desc = "Test mmap thread lookup",
128                 .func = test__mmap_thread_lookup,
129         },
130         {
131                 .desc = "Test thread mg sharing",
132                 .func = test__thread_mg_share,
133         },
134         {
135                 .desc = "Test output sorting of hist entries",
136                 .func = test__hists_output,
137         },
138         {
139                 .desc = "Test cumulation of child hist entries",
140                 .func = test__hists_cumulate,
141         },
142         {
143                 .desc = "Test tracking with sched_switch",
144                 .func = test__switch_tracking,
145         },
146         {
147                 .desc = "Filter fds with revents mask in a fdarray",
148                 .func = test__fdarray__filter,
149         },
150         {
151                 .desc = "Add fd to a fdarray, making it autogrow",
152                 .func = test__fdarray__add,
153         },
154         {
155                 .desc = "Test kmod_path__parse function",
156                 .func = test__kmod_path__parse,
157         },
158         {
159                 .desc = "Test thread map",
160                 .func = test__thread_map,
161         },
162         {
163                 .desc = "Test LLVM searching and compiling",
164                 .func = test__llvm,
165                 .subtest = {
166                         .skip_if_fail   = true,
167                         .get_nr         = test__llvm_subtest_get_nr,
168                         .get_desc       = test__llvm_subtest_get_desc,
169                 },
170         },
171         {
172                 .desc = "Test topology in session",
173                 .func = test_session_topology,
174         },
175         {
176                 .desc = "Test BPF filter",
177                 .func = test__bpf,
178                 .subtest = {
179                         .skip_if_fail   = true,
180                         .get_nr         = test__bpf_subtest_get_nr,
181                         .get_desc       = test__bpf_subtest_get_desc,
182                 },
183         },
184         {
185                 .desc = "Test thread map synthesize",
186                 .func = test__thread_map_synthesize,
187         },
188         {
189                 .desc = "Test cpu map synthesize",
190                 .func = test__cpu_map_synthesize,
191         },
192         {
193                 .desc = "Test stat config synthesize",
194                 .func = test__synthesize_stat_config,
195         },
196         {
197                 .desc = "Test stat synthesize",
198                 .func = test__synthesize_stat,
199         },
200         {
201                 .desc = "Test stat round synthesize",
202                 .func = test__synthesize_stat_round,
203         },
204         {
205                 .desc = "Test attr update synthesize",
206                 .func = test__event_update,
207         },
208         {
209                 .desc = "Test events times",
210                 .func = test__event_times,
211         },
212         {
213                 .desc = "Test backward reading from ring buffer",
214                 .func = test__backward_ring_buffer,
215         },
216         {
217                 .desc = "Test cpu map print",
218                 .func = test__cpu_map_print,
219         },
220         {
221                 .desc = "Test SDT event probing",
222                 .func = test__sdt_event,
223         },
224         {
225                 .desc = "Test is_printable_array function",
226                 .func = test__is_printable_array,
227         },
228         {
229                 .func = NULL,
230         },
231 };
232
233 static struct test *tests[] = {
234         generic_tests,
235         arch_tests,
236 };
237
238 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
239 {
240         int i;
241
242         if (argc == 0)
243                 return true;
244
245         for (i = 0; i < argc; ++i) {
246                 char *end;
247                 long nr = strtoul(argv[i], &end, 10);
248
249                 if (*end == '\0') {
250                         if (nr == curr + 1)
251                                 return true;
252                         continue;
253                 }
254
255                 if (strcasestr(test->desc, argv[i]))
256                         return true;
257         }
258
259         return false;
260 }
261
262 static int run_test(struct test *test, int subtest)
263 {
264         int status, err = -1, child = dont_fork ? 0 : fork();
265         char sbuf[STRERR_BUFSIZE];
266
267         if (child < 0) {
268                 pr_err("failed to fork test: %s\n",
269                         str_error_r(errno, sbuf, sizeof(sbuf)));
270                 return -1;
271         }
272
273         if (!child) {
274                 if (!dont_fork) {
275                         pr_debug("test child forked, pid %d\n", getpid());
276
277                         if (!verbose) {
278                                 int nullfd = open("/dev/null", O_WRONLY);
279
280                                 if (nullfd >= 0) {
281                                         close(STDERR_FILENO);
282                                         close(STDOUT_FILENO);
283
284                                         dup2(nullfd, STDOUT_FILENO);
285                                         dup2(STDOUT_FILENO, STDERR_FILENO);
286                                         close(nullfd);
287                                 }
288                         } else {
289                                 signal(SIGSEGV, sighandler_dump_stack);
290                                 signal(SIGFPE, sighandler_dump_stack);
291                         }
292                 }
293
294                 err = test->func(subtest);
295                 if (!dont_fork)
296                         exit(err);
297         }
298
299         if (!dont_fork) {
300                 wait(&status);
301
302                 if (WIFEXITED(status)) {
303                         err = (signed char)WEXITSTATUS(status);
304                         pr_debug("test child finished with %d\n", err);
305                 } else if (WIFSIGNALED(status)) {
306                         err = -1;
307                         pr_debug("test child interrupted\n");
308                 }
309         }
310
311         return err;
312 }
313
314 #define for_each_test(j, t)                                     \
315         for (j = 0; j < ARRAY_SIZE(tests); j++) \
316                 for (t = &tests[j][0]; t->func; t++)
317
318 static int test_and_print(struct test *t, bool force_skip, int subtest)
319 {
320         int err;
321
322         if (!force_skip) {
323                 pr_debug("\n--- start ---\n");
324                 err = run_test(t, subtest);
325                 pr_debug("---- end ----\n");
326         } else {
327                 pr_debug("\n--- force skipped ---\n");
328                 err = TEST_SKIP;
329         }
330
331         if (!t->subtest.get_nr)
332                 pr_debug("%s:", t->desc);
333         else
334                 pr_debug("%s subtest %d:", t->desc, subtest);
335
336         switch (err) {
337         case TEST_OK:
338                 pr_info(" Ok\n");
339                 break;
340         case TEST_SKIP:
341                 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
342                 break;
343         case TEST_FAIL:
344         default:
345                 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
346                 break;
347         }
348
349         return err;
350 }
351
352 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
353 {
354         struct test *t;
355         unsigned int j;
356         int i = 0;
357         int width = 0;
358
359         for_each_test(j, t) {
360                 int len = strlen(t->desc);
361
362                 if (width < len)
363                         width = len;
364         }
365
366         for_each_test(j, t) {
367                 int curr = i++, err;
368
369                 if (!perf_test__matches(t, curr, argc, argv))
370                         continue;
371
372                 pr_info("%2d: %-*s:", i, width, t->desc);
373
374                 if (intlist__find(skiplist, i)) {
375                         color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
376                         continue;
377                 }
378
379                 if (!t->subtest.get_nr) {
380                         test_and_print(t, false, -1);
381                 } else {
382                         int subn = t->subtest.get_nr();
383                         /*
384                          * minus 2 to align with normal testcases.
385                          * For subtest we print additional '.x' in number.
386                          * for example:
387                          *
388                          * 35: Test LLVM searching and compiling                        :
389                          * 35.1: Basic BPF llvm compiling test                          : Ok
390                          */
391                         int subw = width > 2 ? width - 2 : width;
392                         bool skip = false;
393                         int subi;
394
395                         if (subn <= 0) {
396                                 color_fprintf(stderr, PERF_COLOR_YELLOW,
397                                               " Skip (not compiled in)\n");
398                                 continue;
399                         }
400                         pr_info("\n");
401
402                         for (subi = 0; subi < subn; subi++) {
403                                 int len = strlen(t->subtest.get_desc(subi));
404
405                                 if (subw < len)
406                                         subw = len;
407                         }
408
409                         for (subi = 0; subi < subn; subi++) {
410                                 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
411                                         t->subtest.get_desc(subi));
412                                 err = test_and_print(t, skip, subi);
413                                 if (err != TEST_OK && t->subtest.skip_if_fail)
414                                         skip = true;
415                         }
416                 }
417         }
418
419         return 0;
420 }
421
422 static int perf_test__list(int argc, const char **argv)
423 {
424         unsigned int j;
425         struct test *t;
426         int i = 0;
427
428         for_each_test(j, t) {
429                 if (argc > 1 && !strstr(t->desc, argv[1]))
430                         continue;
431
432                 pr_info("%2d: %s\n", ++i, t->desc);
433         }
434
435         return 0;
436 }
437
438 int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
439 {
440         const char *test_usage[] = {
441         "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
442         NULL,
443         };
444         const char *skip = NULL;
445         const struct option test_options[] = {
446         OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
447         OPT_INCR('v', "verbose", &verbose,
448                     "be more verbose (show symbol address, etc)"),
449         OPT_BOOLEAN('F', "dont-fork", &dont_fork,
450                     "Do not fork for testcase"),
451         OPT_END()
452         };
453         const char * const test_subcommands[] = { "list", NULL };
454         struct intlist *skiplist = NULL;
455         int ret = hists__init();
456
457         if (ret < 0)
458                 return ret;
459
460         argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
461         if (argc >= 1 && !strcmp(argv[0], "list"))
462                 return perf_test__list(argc, argv);
463
464         symbol_conf.priv_size = sizeof(int);
465         symbol_conf.sort_by_name = true;
466         symbol_conf.try_vmlinux_path = true;
467
468         if (symbol__init(NULL) < 0)
469                 return -1;
470
471         if (skip != NULL)
472                 skiplist = intlist__new(skip);
473
474         return __cmd_test(argc, argv, skiplist);
475 }