1 // SPDX-License-Identifier: GPL-2.0
5 * Builtin regression testing command: ever growing number of sanity tests
10 #include <sys/types.h>
20 #include <subcmd/parse-options.h>
23 #include <linux/kernel.h>
24 #include <subcmd/exec-cmd.h>
26 static bool dont_fork;
28 struct test __weak arch_tests[] = {
34 static struct test generic_tests[] = {
36 .desc = "vmlinux symtab matches kallsyms",
37 .func = test__vmlinux_matches_kallsyms,
40 .desc = "Detect openat syscall event",
41 .func = test__openat_syscall_event,
44 .desc = "Detect openat syscall event on all cpus",
45 .func = test__openat_syscall_event_on_all_cpus,
48 .desc = "Read samples using the mmap interface",
49 .func = test__basic_mmap,
52 .desc = "Test data source output",
56 .desc = "Parse event definition strings",
57 .func = test__parse_events,
60 .desc = "Simple expression parser",
64 .desc = "PERF_RECORD_* events & perf_sample fields",
65 .func = test__PERF_RECORD,
68 .desc = "Parse perf pmu format",
72 .desc = "DSO data read",
73 .func = test__dso_data,
76 .desc = "DSO data cache",
77 .func = test__dso_data_cache,
80 .desc = "DSO data reopen",
81 .func = test__dso_data_reopen,
84 .desc = "Roundtrip evsel->name",
85 .func = test__perf_evsel__roundtrip_name_test,
88 .desc = "Parse sched tracepoints fields",
89 .func = test__perf_evsel__tp_sched_test,
92 .desc = "syscalls:sys_enter_openat event fields",
93 .func = test__syscall_openat_tp_fields,
96 .desc = "Setup struct perf_event_attr",
100 .desc = "Match and link multiple hists",
101 .func = test__hists_link,
104 .desc = "'import perf' in python",
105 .func = test__python_use,
108 .desc = "Breakpoint overflow signal handler",
109 .func = test__bp_signal,
110 .is_supported = test__bp_signal_is_supported,
113 .desc = "Breakpoint overflow sampling",
114 .func = test__bp_signal_overflow,
115 .is_supported = test__bp_signal_is_supported,
118 .desc = "Number of exit events of a simple workload",
119 .func = test__task_exit,
122 .desc = "Software clock events period values",
123 .func = test__sw_clock_freq,
126 .desc = "Object code reading",
127 .func = test__code_reading,
130 .desc = "Sample parsing",
131 .func = test__sample_parsing,
134 .desc = "Use a dummy software event to keep tracking",
135 .func = test__keep_tracking,
138 .desc = "Parse with no sample_id_all bit set",
139 .func = test__parse_no_sample_id_all,
142 .desc = "Filter hist entries",
143 .func = test__hists_filter,
146 .desc = "Lookup mmap thread",
147 .func = test__mmap_thread_lookup,
150 .desc = "Share thread mg",
151 .func = test__thread_mg_share,
154 .desc = "Sort output of hist entries",
155 .func = test__hists_output,
158 .desc = "Cumulate child hist entries",
159 .func = test__hists_cumulate,
162 .desc = "Track with sched_switch",
163 .func = test__switch_tracking,
166 .desc = "Filter fds with revents mask in a fdarray",
167 .func = test__fdarray__filter,
170 .desc = "Add fd to a fdarray, making it autogrow",
171 .func = test__fdarray__add,
174 .desc = "kmod_path__parse",
175 .func = test__kmod_path__parse,
178 .desc = "Thread map",
179 .func = test__thread_map,
182 .desc = "LLVM search and compile",
185 .skip_if_fail = true,
186 .get_nr = test__llvm_subtest_get_nr,
187 .get_desc = test__llvm_subtest_get_desc,
191 .desc = "Session topology",
192 .func = test__session_topology,
195 .desc = "BPF filter",
198 .skip_if_fail = true,
199 .get_nr = test__bpf_subtest_get_nr,
200 .get_desc = test__bpf_subtest_get_desc,
204 .desc = "Synthesize thread map",
205 .func = test__thread_map_synthesize,
208 .desc = "Remove thread map",
209 .func = test__thread_map_remove,
212 .desc = "Synthesize cpu map",
213 .func = test__cpu_map_synthesize,
216 .desc = "Synthesize stat config",
217 .func = test__synthesize_stat_config,
220 .desc = "Synthesize stat",
221 .func = test__synthesize_stat,
224 .desc = "Synthesize stat round",
225 .func = test__synthesize_stat_round,
228 .desc = "Synthesize attr update",
229 .func = test__event_update,
232 .desc = "Event times",
233 .func = test__event_times,
236 .desc = "Read backward ring buffer",
237 .func = test__backward_ring_buffer,
240 .desc = "Print cpu map",
241 .func = test__cpu_map_print,
244 .desc = "Probe SDT events",
245 .func = test__sdt_event,
248 .desc = "is_printable_array",
249 .func = test__is_printable_array,
252 .desc = "Print bitmap",
253 .func = test__bitmap_print,
256 .desc = "perf hooks",
257 .func = test__perf_hooks,
260 .desc = "builtin clang support",
263 .skip_if_fail = true,
264 .get_nr = test__clang_subtest_get_nr,
265 .get_desc = test__clang_subtest_get_desc,
269 .desc = "unit_number__scnprintf",
270 .func = test__unit_number__scnprint,
277 static struct test *tests[] = {
282 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
289 for (i = 0; i < argc; ++i) {
291 long nr = strtoul(argv[i], &end, 10);
299 if (strcasestr(test->desc, argv[i]))
306 static int run_test(struct test *test, int subtest)
308 int status, err = -1, child = dont_fork ? 0 : fork();
309 char sbuf[STRERR_BUFSIZE];
312 pr_err("failed to fork test: %s\n",
313 str_error_r(errno, sbuf, sizeof(sbuf)));
319 pr_debug("test child forked, pid %d\n", getpid());
322 int nullfd = open("/dev/null", O_WRONLY);
325 close(STDERR_FILENO);
326 close(STDOUT_FILENO);
328 dup2(nullfd, STDOUT_FILENO);
329 dup2(STDOUT_FILENO, STDERR_FILENO);
333 signal(SIGSEGV, sighandler_dump_stack);
334 signal(SIGFPE, sighandler_dump_stack);
338 err = test->func(test, subtest);
346 if (WIFEXITED(status)) {
347 err = (signed char)WEXITSTATUS(status);
348 pr_debug("test child finished with %d\n", err);
349 } else if (WIFSIGNALED(status)) {
351 pr_debug("test child interrupted\n");
358 #define for_each_test(j, t) \
359 for (j = 0; j < ARRAY_SIZE(tests); j++) \
360 for (t = &tests[j][0]; t->func; t++)
362 static int test_and_print(struct test *t, bool force_skip, int subtest)
367 pr_debug("\n--- start ---\n");
368 err = run_test(t, subtest);
369 pr_debug("---- end ----\n");
371 pr_debug("\n--- force skipped ---\n");
375 if (!t->subtest.get_nr)
376 pr_debug("%s:", t->desc);
378 pr_debug("%s subtest %d:", t->desc, subtest + 1);
385 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
389 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
396 static const char *shell_test__description(char *description, size_t size,
397 const char *path, const char *name)
400 char filename[PATH_MAX];
402 path__join(filename, sizeof(filename), path, name);
403 fp = fopen(filename, "r");
407 description = fgets(description, size, fp);
410 return description ? trim(description + 1) : NULL;
413 #define for_each_shell_test(dir, ent) \
414 while ((ent = readdir(dir)) != NULL) \
415 if (ent->d_type == DT_REG && ent->d_name[0] != '.')
417 static const char *shell_tests__dir(char *path, size_t size)
419 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
423 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
425 if (!lstat(devel_dirs[i], &st)) {
426 scnprintf(path, size, "%s/shell", devel_dirs[i]);
427 if (!lstat(devel_dirs[i], &st))
432 /* Then installed path. */
433 exec_path = get_argv_exec_path();
434 scnprintf(path, size, "%s/tests/shell", exec_path);
439 static int shell_tests__max_desc_width(void)
443 char path_dir[PATH_MAX];
444 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
454 for_each_shell_test(dir, ent) {
456 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
459 int len = strlen(desc);
475 static int shell_test__run(struct test *test, int subdir __maybe_unused)
478 char script[PATH_MAX];
479 struct shell_test *st = test->priv;
481 path__join(script, sizeof(script), st->dir, st->file);
483 err = system(script);
487 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
490 static int run_shell_tests(int argc, const char *argv[], int i, int width)
494 char path_dir[PATH_MAX];
495 struct shell_test st = {
496 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
502 dir = opendir(st.dir);
506 for_each_shell_test(dir, ent) {
510 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
511 .func = shell_test__run,
515 if (!perf_test__matches(&test, curr, argc, argv))
518 st.file = ent->d_name;
519 pr_info("%2d: %-*s:", i, width, test.desc);
520 test_and_print(&test, false, -1);
527 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
532 int width = shell_tests__max_desc_width();
534 for_each_test(j, t) {
535 int len = strlen(t->desc);
541 for_each_test(j, t) {
544 if (!perf_test__matches(t, curr, argc, argv))
547 if (t->is_supported && !t->is_supported()) {
548 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
552 pr_info("%2d: %-*s:", i, width, t->desc);
554 if (intlist__find(skiplist, i)) {
555 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
559 if (!t->subtest.get_nr) {
560 test_and_print(t, false, -1);
562 int subn = t->subtest.get_nr();
564 * minus 2 to align with normal testcases.
565 * For subtest we print additional '.x' in number.
568 * 35: Test LLVM searching and compiling :
569 * 35.1: Basic BPF llvm compiling test : Ok
571 int subw = width > 2 ? width - 2 : width;
576 color_fprintf(stderr, PERF_COLOR_YELLOW,
577 " Skip (not compiled in)\n");
582 for (subi = 0; subi < subn; subi++) {
583 int len = strlen(t->subtest.get_desc(subi));
589 for (subi = 0; subi < subn; subi++) {
590 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
591 t->subtest.get_desc(subi));
592 err = test_and_print(t, skip, subi);
593 if (err != TEST_OK && t->subtest.skip_if_fail)
599 return run_shell_tests(argc, argv, i, width);
602 static int perf_test__list_shell(int argc, const char **argv, int i)
606 char path_dir[PATH_MAX];
607 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
616 for_each_shell_test(dir, ent) {
620 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
623 if (!perf_test__matches(&t, curr, argc, argv))
626 pr_info("%2d: %s\n", i, t.desc);
633 static int perf_test__list(int argc, const char **argv)
639 for_each_test(j, t) {
642 if (!perf_test__matches(t, curr, argc, argv) ||
643 (t->is_supported && !t->is_supported()))
646 pr_info("%2d: %s\n", i, t->desc);
649 perf_test__list_shell(argc, argv, i);
654 int cmd_test(int argc, const char **argv)
656 const char *test_usage[] = {
657 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
660 const char *skip = NULL;
661 const struct option test_options[] = {
662 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
663 OPT_INCR('v', "verbose", &verbose,
664 "be more verbose (show symbol address, etc)"),
665 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
666 "Do not fork for testcase"),
669 const char * const test_subcommands[] = { "list", NULL };
670 struct intlist *skiplist = NULL;
671 int ret = hists__init();
676 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
677 if (argc >= 1 && !strcmp(argv[0], "list"))
678 return perf_test__list(argc - 1, argv + 1);
680 symbol_conf.priv_size = sizeof(int);
681 symbol_conf.sort_by_name = true;
682 symbol_conf.try_vmlinux_path = true;
684 if (symbol__init(NULL) < 0)
688 skiplist = intlist__new(skip);
690 return __cmd_test(argc, argv, skiplist);