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