4 * Builtin regression testing command: ever growing number of sanity tests
19 #include <subcmd/parse-options.h>
22 #include <linux/kernel.h>
23 #include <subcmd/exec-cmd.h>
25 static bool dont_fork
;
27 struct test __weak arch_tests
[] = {
33 static struct test generic_tests
[] = {
35 .desc
= "vmlinux symtab matches kallsyms",
36 .func
= test__vmlinux_matches_kallsyms
,
39 .desc
= "Detect openat syscall event",
40 .func
= test__openat_syscall_event
,
43 .desc
= "Detect openat syscall event on all cpus",
44 .func
= test__openat_syscall_event_on_all_cpus
,
47 .desc
= "Read samples using the mmap interface",
48 .func
= test__basic_mmap
,
51 .desc
= "Parse event definition strings",
52 .func
= test__parse_events
,
55 .desc
= "Simple expression parser",
59 .desc
= "PERF_RECORD_* events & perf_sample fields",
60 .func
= test__PERF_RECORD
,
63 .desc
= "Parse perf pmu format",
67 .desc
= "DSO data read",
68 .func
= test__dso_data
,
71 .desc
= "DSO data cache",
72 .func
= test__dso_data_cache
,
75 .desc
= "DSO data reopen",
76 .func
= test__dso_data_reopen
,
79 .desc
= "Roundtrip evsel->name",
80 .func
= test__perf_evsel__roundtrip_name_test
,
83 .desc
= "Parse sched tracepoints fields",
84 .func
= test__perf_evsel__tp_sched_test
,
87 .desc
= "syscalls:sys_enter_openat event fields",
88 .func
= test__syscall_openat_tp_fields
,
91 .desc
= "Setup struct perf_event_attr",
95 .desc
= "Match and link multiple hists",
96 .func
= test__hists_link
,
99 .desc
= "'import perf' in python",
100 .func
= test__python_use
,
103 .desc
= "Breakpoint overflow signal handler",
104 .func
= test__bp_signal
,
105 .is_supported
= test__bp_signal_is_supported
,
108 .desc
= "Breakpoint overflow sampling",
109 .func
= test__bp_signal_overflow
,
110 .is_supported
= test__bp_signal_is_supported
,
113 .desc
= "Number of exit events of a simple workload",
114 .func
= test__task_exit
,
117 .desc
= "Software clock events period values",
118 .func
= test__sw_clock_freq
,
121 .desc
= "Object code reading",
122 .func
= test__code_reading
,
125 .desc
= "Sample parsing",
126 .func
= test__sample_parsing
,
129 .desc
= "Use a dummy software event to keep tracking",
130 .func
= test__keep_tracking
,
133 .desc
= "Parse with no sample_id_all bit set",
134 .func
= test__parse_no_sample_id_all
,
137 .desc
= "Filter hist entries",
138 .func
= test__hists_filter
,
141 .desc
= "Lookup mmap thread",
142 .func
= test__mmap_thread_lookup
,
145 .desc
= "Share thread mg",
146 .func
= test__thread_mg_share
,
149 .desc
= "Sort output of hist entries",
150 .func
= test__hists_output
,
153 .desc
= "Cumulate child hist entries",
154 .func
= test__hists_cumulate
,
157 .desc
= "Track with sched_switch",
158 .func
= test__switch_tracking
,
161 .desc
= "Filter fds with revents mask in a fdarray",
162 .func
= test__fdarray__filter
,
165 .desc
= "Add fd to a fdarray, making it autogrow",
166 .func
= test__fdarray__add
,
169 .desc
= "kmod_path__parse",
170 .func
= test__kmod_path__parse
,
173 .desc
= "Thread map",
174 .func
= test__thread_map
,
177 .desc
= "LLVM search and compile",
180 .skip_if_fail
= true,
181 .get_nr
= test__llvm_subtest_get_nr
,
182 .get_desc
= test__llvm_subtest_get_desc
,
186 .desc
= "Session topology",
187 .func
= test__session_topology
,
190 .desc
= "BPF filter",
193 .skip_if_fail
= true,
194 .get_nr
= test__bpf_subtest_get_nr
,
195 .get_desc
= test__bpf_subtest_get_desc
,
199 .desc
= "Synthesize thread map",
200 .func
= test__thread_map_synthesize
,
203 .desc
= "Remove thread map",
204 .func
= test__thread_map_remove
,
207 .desc
= "Synthesize cpu map",
208 .func
= test__cpu_map_synthesize
,
211 .desc
= "Synthesize stat config",
212 .func
= test__synthesize_stat_config
,
215 .desc
= "Synthesize stat",
216 .func
= test__synthesize_stat
,
219 .desc
= "Synthesize stat round",
220 .func
= test__synthesize_stat_round
,
223 .desc
= "Synthesize attr update",
224 .func
= test__event_update
,
227 .desc
= "Event times",
228 .func
= test__event_times
,
231 .desc
= "Read backward ring buffer",
232 .func
= test__backward_ring_buffer
,
235 .desc
= "Print cpu map",
236 .func
= test__cpu_map_print
,
239 .desc
= "Probe SDT events",
240 .func
= test__sdt_event
,
243 .desc
= "is_printable_array",
244 .func
= test__is_printable_array
,
247 .desc
= "Print bitmap",
248 .func
= test__bitmap_print
,
251 .desc
= "perf hooks",
252 .func
= test__perf_hooks
,
255 .desc
= "builtin clang support",
258 .skip_if_fail
= true,
259 .get_nr
= test__clang_subtest_get_nr
,
260 .get_desc
= test__clang_subtest_get_desc
,
264 .desc
= "unit_number__scnprintf",
265 .func
= test__unit_number__scnprint
,
272 static struct test
*tests
[] = {
277 static bool perf_test__matches(struct test
*test
, int curr
, int argc
, const char *argv
[])
284 for (i
= 0; i
< argc
; ++i
) {
286 long nr
= strtoul(argv
[i
], &end
, 10);
294 if (strcasestr(test
->desc
, argv
[i
]))
301 static int run_test(struct test
*test
, int subtest
)
303 int status
, err
= -1, child
= dont_fork
? 0 : fork();
304 char sbuf
[STRERR_BUFSIZE
];
307 pr_err("failed to fork test: %s\n",
308 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
314 pr_debug("test child forked, pid %d\n", getpid());
317 int nullfd
= open("/dev/null", O_WRONLY
);
320 close(STDERR_FILENO
);
321 close(STDOUT_FILENO
);
323 dup2(nullfd
, STDOUT_FILENO
);
324 dup2(STDOUT_FILENO
, STDERR_FILENO
);
328 signal(SIGSEGV
, sighandler_dump_stack
);
329 signal(SIGFPE
, sighandler_dump_stack
);
333 err
= test
->func(test
, subtest
);
341 if (WIFEXITED(status
)) {
342 err
= (signed char)WEXITSTATUS(status
);
343 pr_debug("test child finished with %d\n", err
);
344 } else if (WIFSIGNALED(status
)) {
346 pr_debug("test child interrupted\n");
353 #define for_each_test(j, t) \
354 for (j = 0; j < ARRAY_SIZE(tests); j++) \
355 for (t = &tests[j][0]; t->func; t++)
357 static int test_and_print(struct test
*t
, bool force_skip
, int subtest
)
362 pr_debug("\n--- start ---\n");
363 err
= run_test(t
, subtest
);
364 pr_debug("---- end ----\n");
366 pr_debug("\n--- force skipped ---\n");
370 if (!t
->subtest
.get_nr
)
371 pr_debug("%s:", t
->desc
);
373 pr_debug("%s subtest %d:", t
->desc
, subtest
);
380 color_fprintf(stderr
, PERF_COLOR_YELLOW
, " Skip\n");
384 color_fprintf(stderr
, PERF_COLOR_RED
, " FAILED!\n");
391 static const char *shell_test__description(char *description
, size_t size
,
392 const char *path
, const char *name
)
395 char filename
[PATH_MAX
];
397 path__join(filename
, sizeof(filename
), path
, name
);
398 fp
= fopen(filename
, "r");
402 description
= fgets(description
, size
, fp
);
405 return description
? trim(description
+ 1) : NULL
;
408 #define for_each_shell_test(dir, ent) \
409 while ((ent = readdir(dir)) != NULL) \
410 if (ent->d_type == DT_REG && ent->d_name[0] != '.')
412 static const char *shell_tests__dir(char *path
, size_t size
)
414 const char *devel_dirs
[] = { "./tools/perf/tests", "./tests", };
418 for (i
= 0; i
< ARRAY_SIZE(devel_dirs
); ++i
) {
420 if (!lstat(devel_dirs
[i
], &st
)) {
421 scnprintf(path
, size
, "%s/shell", devel_dirs
[i
]);
422 if (!lstat(devel_dirs
[i
], &st
))
427 /* Then installed path. */
428 exec_path
= get_argv_exec_path();
429 scnprintf(path
, size
, "%s/tests/shell", exec_path
);
434 static int shell_tests__max_desc_width(void)
438 char path_dir
[PATH_MAX
];
439 const char *path
= shell_tests__dir(path_dir
, sizeof(path_dir
));
449 for_each_shell_test(dir
, ent
) {
451 const char *desc
= shell_test__description(bf
, sizeof(bf
), path
, ent
->d_name
);
454 int len
= strlen(desc
);
470 static int shell_test__run(struct test
*test
, int subdir __maybe_unused
)
473 char script
[PATH_MAX
];
474 struct shell_test
*st
= test
->priv
;
476 path__join(script
, sizeof(script
), st
->dir
, st
->file
);
478 err
= system(script
);
482 return WEXITSTATUS(err
) == 2 ? TEST_SKIP
: TEST_FAIL
;
485 static int run_shell_tests(int argc
, const char *argv
[], int i
, int width
)
489 char path_dir
[PATH_MAX
];
490 struct shell_test st
= {
491 .dir
= shell_tests__dir(path_dir
, sizeof(path_dir
)),
497 dir
= opendir(st
.dir
);
501 for_each_shell_test(dir
, ent
) {
505 .desc
= shell_test__description(desc
, sizeof(desc
), st
.dir
, ent
->d_name
),
506 .func
= shell_test__run
,
510 if (!perf_test__matches(&test
, curr
, argc
, argv
))
513 st
.file
= ent
->d_name
;
514 pr_info("%2d: %-*s:", i
, width
, test
.desc
);
515 test_and_print(&test
, false, -1);
522 static int __cmd_test(int argc
, const char *argv
[], struct intlist
*skiplist
)
527 int width
= shell_tests__max_desc_width();
529 for_each_test(j
, t
) {
530 int len
= strlen(t
->desc
);
536 for_each_test(j
, t
) {
539 if (!perf_test__matches(t
, curr
, argc
, argv
))
542 if (t
->is_supported
&& !t
->is_supported()) {
543 pr_debug("%2d: %-*s: Disabled\n", i
, width
, t
->desc
);
547 pr_info("%2d: %-*s:", i
, width
, t
->desc
);
549 if (intlist__find(skiplist
, i
)) {
550 color_fprintf(stderr
, PERF_COLOR_YELLOW
, " Skip (user override)\n");
554 if (!t
->subtest
.get_nr
) {
555 test_and_print(t
, false, -1);
557 int subn
= t
->subtest
.get_nr();
559 * minus 2 to align with normal testcases.
560 * For subtest we print additional '.x' in number.
563 * 35: Test LLVM searching and compiling :
564 * 35.1: Basic BPF llvm compiling test : Ok
566 int subw
= width
> 2 ? width
- 2 : width
;
571 color_fprintf(stderr
, PERF_COLOR_YELLOW
,
572 " Skip (not compiled in)\n");
577 for (subi
= 0; subi
< subn
; subi
++) {
578 int len
= strlen(t
->subtest
.get_desc(subi
));
584 for (subi
= 0; subi
< subn
; subi
++) {
585 pr_info("%2d.%1d: %-*s:", i
, subi
+ 1, subw
,
586 t
->subtest
.get_desc(subi
));
587 err
= test_and_print(t
, skip
, subi
);
588 if (err
!= TEST_OK
&& t
->subtest
.skip_if_fail
)
594 return run_shell_tests(argc
, argv
, i
, width
);
597 static int perf_test__list_shell(int argc
, const char **argv
, int i
)
601 char path_dir
[PATH_MAX
];
602 const char *path
= shell_tests__dir(path_dir
, sizeof(path_dir
));
611 for_each_shell_test(dir
, ent
) {
613 const char *desc
= shell_test__description(bf
, sizeof(bf
), path
, ent
->d_name
);
617 if (argc
> 1 && !strstr(desc
, argv
[1]))
620 pr_info("%2d: %s\n", i
, desc
);
627 static int perf_test__list(int argc
, const char **argv
)
633 for_each_test(j
, t
) {
636 if (argc
> 1 && !strstr(t
->desc
, argv
[1]))
639 pr_info("%2d: %s\n", i
, t
->desc
);
642 perf_test__list_shell(argc
, argv
, i
);
647 int cmd_test(int argc
, const char **argv
)
649 const char *test_usage
[] = {
650 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
653 const char *skip
= NULL
;
654 const struct option test_options
[] = {
655 OPT_STRING('s', "skip", &skip
, "tests", "tests to skip"),
656 OPT_INCR('v', "verbose", &verbose
,
657 "be more verbose (show symbol address, etc)"),
658 OPT_BOOLEAN('F', "dont-fork", &dont_fork
,
659 "Do not fork for testcase"),
662 const char * const test_subcommands
[] = { "list", NULL
};
663 struct intlist
*skiplist
= NULL
;
664 int ret
= hists__init();
669 argc
= parse_options_subcommand(argc
, argv
, test_options
, test_subcommands
, test_usage
, 0);
670 if (argc
>= 1 && !strcmp(argv
[0], "list"))
671 return perf_test__list(argc
, argv
);
673 symbol_conf
.priv_size
= sizeof(int);
674 symbol_conf
.sort_by_name
= true;
675 symbol_conf
.try_vmlinux_path
= true;
677 if (symbol__init(NULL
) < 0)
681 skiplist
= intlist__new(skip
);
683 return __cmd_test(argc
, argv
, skiplist
);