1 // SPDX-License-Identifier: GPL-2.0
5 * Builtin regression testing command: ever growing number of sanity tests
11 #include <sys/types.h>
21 #include <subcmd/parse-options.h>
24 #include <linux/kernel.h>
25 #include <subcmd/exec-cmd.h>
27 static bool dont_fork
;
29 struct test __weak arch_tests
[] = {
35 static struct test generic_tests
[] = {
37 .desc
= "vmlinux symtab matches kallsyms",
38 .func
= test__vmlinux_matches_kallsyms
,
41 .desc
= "Detect openat syscall event",
42 .func
= test__openat_syscall_event
,
45 .desc
= "Detect openat syscall event on all cpus",
46 .func
= test__openat_syscall_event_on_all_cpus
,
49 .desc
= "Read samples using the mmap interface",
50 .func
= test__basic_mmap
,
53 .desc
= "Test data source output",
57 .desc
= "Parse event definition strings",
58 .func
= test__parse_events
,
61 .desc
= "Simple expression parser",
65 .desc
= "PERF_RECORD_* events & perf_sample fields",
66 .func
= test__PERF_RECORD
,
69 .desc
= "Parse perf pmu format",
73 .desc
= "DSO data read",
74 .func
= test__dso_data
,
77 .desc
= "DSO data cache",
78 .func
= test__dso_data_cache
,
81 .desc
= "DSO data reopen",
82 .func
= test__dso_data_reopen
,
85 .desc
= "Roundtrip evsel->name",
86 .func
= test__perf_evsel__roundtrip_name_test
,
89 .desc
= "Parse sched tracepoints fields",
90 .func
= test__perf_evsel__tp_sched_test
,
93 .desc
= "syscalls:sys_enter_openat event fields",
94 .func
= test__syscall_openat_tp_fields
,
97 .desc
= "Setup struct perf_event_attr",
101 .desc
= "Match and link multiple hists",
102 .func
= test__hists_link
,
105 .desc
= "'import perf' in python",
106 .func
= test__python_use
,
109 .desc
= "Breakpoint overflow signal handler",
110 .func
= test__bp_signal
,
111 .is_supported
= test__bp_signal_is_supported
,
114 .desc
= "Breakpoint overflow sampling",
115 .func
= test__bp_signal_overflow
,
116 .is_supported
= test__bp_signal_is_supported
,
119 .desc
= "Breakpoint accounting",
120 .func
= test__bp_accounting
,
121 .is_supported
= test__bp_signal_is_supported
,
124 .desc
= "Watchpoint",
126 .is_supported
= test__wp_is_supported
,
128 .skip_if_fail
= false,
129 .get_nr
= test__wp_subtest_get_nr
,
130 .get_desc
= test__wp_subtest_get_desc
,
134 .desc
= "Number of exit events of a simple workload",
135 .func
= test__task_exit
,
138 .desc
= "Software clock events period values",
139 .func
= test__sw_clock_freq
,
142 .desc
= "Object code reading",
143 .func
= test__code_reading
,
146 .desc
= "Sample parsing",
147 .func
= test__sample_parsing
,
150 .desc
= "Use a dummy software event to keep tracking",
151 .func
= test__keep_tracking
,
154 .desc
= "Parse with no sample_id_all bit set",
155 .func
= test__parse_no_sample_id_all
,
158 .desc
= "Filter hist entries",
159 .func
= test__hists_filter
,
162 .desc
= "Lookup mmap thread",
163 .func
= test__mmap_thread_lookup
,
166 .desc
= "Share thread mg",
167 .func
= test__thread_mg_share
,
170 .desc
= "Sort output of hist entries",
171 .func
= test__hists_output
,
174 .desc
= "Cumulate child hist entries",
175 .func
= test__hists_cumulate
,
178 .desc
= "Track with sched_switch",
179 .func
= test__switch_tracking
,
182 .desc
= "Filter fds with revents mask in a fdarray",
183 .func
= test__fdarray__filter
,
186 .desc
= "Add fd to a fdarray, making it autogrow",
187 .func
= test__fdarray__add
,
190 .desc
= "kmod_path__parse",
191 .func
= test__kmod_path__parse
,
194 .desc
= "Thread map",
195 .func
= test__thread_map
,
198 .desc
= "LLVM search and compile",
201 .skip_if_fail
= true,
202 .get_nr
= test__llvm_subtest_get_nr
,
203 .get_desc
= test__llvm_subtest_get_desc
,
207 .desc
= "Session topology",
208 .func
= test__session_topology
,
211 .desc
= "BPF filter",
214 .skip_if_fail
= true,
215 .get_nr
= test__bpf_subtest_get_nr
,
216 .get_desc
= test__bpf_subtest_get_desc
,
220 .desc
= "Synthesize thread map",
221 .func
= test__thread_map_synthesize
,
224 .desc
= "Remove thread map",
225 .func
= test__thread_map_remove
,
228 .desc
= "Synthesize cpu map",
229 .func
= test__cpu_map_synthesize
,
232 .desc
= "Synthesize stat config",
233 .func
= test__synthesize_stat_config
,
236 .desc
= "Synthesize stat",
237 .func
= test__synthesize_stat
,
240 .desc
= "Synthesize stat round",
241 .func
= test__synthesize_stat_round
,
244 .desc
= "Synthesize attr update",
245 .func
= test__event_update
,
248 .desc
= "Event times",
249 .func
= test__event_times
,
252 .desc
= "Read backward ring buffer",
253 .func
= test__backward_ring_buffer
,
256 .desc
= "Print cpu map",
257 .func
= test__cpu_map_print
,
260 .desc
= "Probe SDT events",
261 .func
= test__sdt_event
,
264 .desc
= "is_printable_array",
265 .func
= test__is_printable_array
,
268 .desc
= "Print bitmap",
269 .func
= test__bitmap_print
,
272 .desc
= "perf hooks",
273 .func
= test__perf_hooks
,
276 .desc
= "builtin clang support",
279 .skip_if_fail
= true,
280 .get_nr
= test__clang_subtest_get_nr
,
281 .get_desc
= test__clang_subtest_get_desc
,
285 .desc
= "unit_number__scnprintf",
286 .func
= test__unit_number__scnprint
,
290 .func
= test__mem2node
,
297 static struct test
*tests
[] = {
302 static bool perf_test__matches(struct test
*test
, int curr
, int argc
, const char *argv
[])
309 for (i
= 0; i
< argc
; ++i
) {
311 long nr
= strtoul(argv
[i
], &end
, 10);
319 if (strcasestr(test
->desc
, argv
[i
]))
326 static int run_test(struct test
*test
, int subtest
)
328 int status
, err
= -1, child
= dont_fork
? 0 : fork();
329 char sbuf
[STRERR_BUFSIZE
];
332 pr_err("failed to fork test: %s\n",
333 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
339 pr_debug("test child forked, pid %d\n", getpid());
342 int nullfd
= open("/dev/null", O_WRONLY
);
345 close(STDERR_FILENO
);
346 close(STDOUT_FILENO
);
348 dup2(nullfd
, STDOUT_FILENO
);
349 dup2(STDOUT_FILENO
, STDERR_FILENO
);
353 signal(SIGSEGV
, sighandler_dump_stack
);
354 signal(SIGFPE
, sighandler_dump_stack
);
358 err
= test
->func(test
, subtest
);
366 if (WIFEXITED(status
)) {
367 err
= (signed char)WEXITSTATUS(status
);
368 pr_debug("test child finished with %d\n", err
);
369 } else if (WIFSIGNALED(status
)) {
371 pr_debug("test child interrupted\n");
378 #define for_each_test(j, t) \
379 for (j = 0; j < ARRAY_SIZE(tests); j++) \
380 for (t = &tests[j][0]; t->func; t++)
382 static int test_and_print(struct test
*t
, bool force_skip
, int subtest
)
387 pr_debug("\n--- start ---\n");
388 err
= run_test(t
, subtest
);
389 pr_debug("---- end ----\n");
391 pr_debug("\n--- force skipped ---\n");
395 if (!t
->subtest
.get_nr
)
396 pr_debug("%s:", t
->desc
);
398 pr_debug("%s subtest %d:", t
->desc
, subtest
+ 1);
405 color_fprintf(stderr
, PERF_COLOR_YELLOW
, " Skip\n");
409 color_fprintf(stderr
, PERF_COLOR_RED
, " FAILED!\n");
416 static const char *shell_test__description(char *description
, size_t size
,
417 const char *path
, const char *name
)
420 char filename
[PATH_MAX
];
422 path__join(filename
, sizeof(filename
), path
, name
);
423 fp
= fopen(filename
, "r");
428 while (fgetc(fp
) != '\n');
430 description
= fgets(description
, size
, fp
);
433 return description
? trim(description
+ 1) : NULL
;
436 #define for_each_shell_test(dir, base, ent) \
437 while ((ent = readdir(dir)) != NULL) \
438 if (!is_directory(base, ent) && ent->d_name[0] != '.')
440 static const char *shell_tests__dir(char *path
, size_t size
)
442 const char *devel_dirs
[] = { "./tools/perf/tests", "./tests", };
446 for (i
= 0; i
< ARRAY_SIZE(devel_dirs
); ++i
) {
448 if (!lstat(devel_dirs
[i
], &st
)) {
449 scnprintf(path
, size
, "%s/shell", devel_dirs
[i
]);
450 if (!lstat(devel_dirs
[i
], &st
))
455 /* Then installed path. */
456 exec_path
= get_argv_exec_path();
457 scnprintf(path
, size
, "%s/tests/shell", exec_path
);
462 static int shell_tests__max_desc_width(void)
466 char path_dir
[PATH_MAX
];
467 const char *path
= shell_tests__dir(path_dir
, sizeof(path_dir
));
477 for_each_shell_test(dir
, path
, ent
) {
479 const char *desc
= shell_test__description(bf
, sizeof(bf
), path
, ent
->d_name
);
482 int len
= strlen(desc
);
498 static int shell_test__run(struct test
*test
, int subdir __maybe_unused
)
501 char script
[PATH_MAX
];
502 struct shell_test
*st
= test
->priv
;
504 path__join(script
, sizeof(script
), st
->dir
, st
->file
);
506 err
= system(script
);
510 return WEXITSTATUS(err
) == 2 ? TEST_SKIP
: TEST_FAIL
;
513 static int run_shell_tests(int argc
, const char *argv
[], int i
, int width
)
517 char path_dir
[PATH_MAX
];
518 struct shell_test st
= {
519 .dir
= shell_tests__dir(path_dir
, sizeof(path_dir
)),
525 dir
= opendir(st
.dir
);
529 for_each_shell_test(dir
, st
.dir
, ent
) {
533 .desc
= shell_test__description(desc
, sizeof(desc
), st
.dir
, ent
->d_name
),
534 .func
= shell_test__run
,
538 if (!perf_test__matches(&test
, curr
, argc
, argv
))
541 st
.file
= ent
->d_name
;
542 pr_info("%2d: %-*s:", i
, width
, test
.desc
);
543 test_and_print(&test
, false, -1);
550 static int __cmd_test(int argc
, const char *argv
[], struct intlist
*skiplist
)
555 int width
= shell_tests__max_desc_width();
557 for_each_test(j
, t
) {
558 int len
= strlen(t
->desc
);
564 for_each_test(j
, t
) {
567 if (!perf_test__matches(t
, curr
, argc
, argv
))
570 if (t
->is_supported
&& !t
->is_supported()) {
571 pr_debug("%2d: %-*s: Disabled\n", i
, width
, t
->desc
);
575 pr_info("%2d: %-*s:", i
, width
, t
->desc
);
577 if (intlist__find(skiplist
, i
)) {
578 color_fprintf(stderr
, PERF_COLOR_YELLOW
, " Skip (user override)\n");
582 if (!t
->subtest
.get_nr
) {
583 test_and_print(t
, false, -1);
585 int subn
= t
->subtest
.get_nr();
587 * minus 2 to align with normal testcases.
588 * For subtest we print additional '.x' in number.
591 * 35: Test LLVM searching and compiling :
592 * 35.1: Basic BPF llvm compiling test : Ok
594 int subw
= width
> 2 ? width
- 2 : width
;
599 color_fprintf(stderr
, PERF_COLOR_YELLOW
,
600 " Skip (not compiled in)\n");
605 for (subi
= 0; subi
< subn
; subi
++) {
606 int len
= strlen(t
->subtest
.get_desc(subi
));
612 for (subi
= 0; subi
< subn
; subi
++) {
613 pr_info("%2d.%1d: %-*s:", i
, subi
+ 1, subw
,
614 t
->subtest
.get_desc(subi
));
615 err
= test_and_print(t
, skip
, subi
);
616 if (err
!= TEST_OK
&& t
->subtest
.skip_if_fail
)
622 return run_shell_tests(argc
, argv
, i
, width
);
625 static int perf_test__list_shell(int argc
, const char **argv
, int i
)
629 char path_dir
[PATH_MAX
];
630 const char *path
= shell_tests__dir(path_dir
, sizeof(path_dir
));
639 for_each_shell_test(dir
, path
, ent
) {
643 .desc
= shell_test__description(bf
, sizeof(bf
), path
, ent
->d_name
),
646 if (!perf_test__matches(&t
, curr
, argc
, argv
))
649 pr_info("%2d: %s\n", i
, t
.desc
);
656 static int perf_test__list(int argc
, const char **argv
)
662 for_each_test(j
, t
) {
665 if (!perf_test__matches(t
, curr
, argc
, argv
) ||
666 (t
->is_supported
&& !t
->is_supported()))
669 pr_info("%2d: %s\n", i
, t
->desc
);
671 if (t
->subtest
.get_nr
) {
672 int subn
= t
->subtest
.get_nr();
675 for (subi
= 0; subi
< subn
; subi
++)
676 pr_info("%2d:%1d: %s\n", i
, subi
+ 1,
677 t
->subtest
.get_desc(subi
));
681 perf_test__list_shell(argc
, argv
, i
);
686 int cmd_test(int argc
, const char **argv
)
688 const char *test_usage
[] = {
689 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
692 const char *skip
= NULL
;
693 const struct option test_options
[] = {
694 OPT_STRING('s', "skip", &skip
, "tests", "tests to skip"),
695 OPT_INCR('v', "verbose", &verbose
,
696 "be more verbose (show symbol address, etc)"),
697 OPT_BOOLEAN('F', "dont-fork", &dont_fork
,
698 "Do not fork for testcase"),
701 const char * const test_subcommands
[] = { "list", NULL
};
702 struct intlist
*skiplist
= NULL
;
703 int ret
= hists__init();
708 argc
= parse_options_subcommand(argc
, argv
, test_options
, test_subcommands
, test_usage
, 0);
709 if (argc
>= 1 && !strcmp(argv
[0], "list"))
710 return perf_test__list(argc
- 1, argv
+ 1);
712 symbol_conf
.priv_size
= sizeof(int);
713 symbol_conf
.sort_by_name
= true;
714 symbol_conf
.try_vmlinux_path
= true;
716 if (symbol__init(NULL
) < 0)
720 skiplist
= intlist__new(skip
);
722 return __cmd_test(argc
, argv
, skiplist
);