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