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 = "Number of exit events of a simple workload",
125 .func = test__task_exit,
128 .desc = "Software clock events period values",
129 .func = test__sw_clock_freq,
132 .desc = "Object code reading",
133 .func = test__code_reading,
136 .desc = "Sample parsing",
137 .func = test__sample_parsing,
140 .desc = "Use a dummy software event to keep tracking",
141 .func = test__keep_tracking,
144 .desc = "Parse with no sample_id_all bit set",
145 .func = test__parse_no_sample_id_all,
148 .desc = "Filter hist entries",
149 .func = test__hists_filter,
152 .desc = "Lookup mmap thread",
153 .func = test__mmap_thread_lookup,
156 .desc = "Share thread mg",
157 .func = test__thread_mg_share,
160 .desc = "Sort output of hist entries",
161 .func = test__hists_output,
164 .desc = "Cumulate child hist entries",
165 .func = test__hists_cumulate,
168 .desc = "Track with sched_switch",
169 .func = test__switch_tracking,
172 .desc = "Filter fds with revents mask in a fdarray",
173 .func = test__fdarray__filter,
176 .desc = "Add fd to a fdarray, making it autogrow",
177 .func = test__fdarray__add,
180 .desc = "kmod_path__parse",
181 .func = test__kmod_path__parse,
184 .desc = "Thread map",
185 .func = test__thread_map,
188 .desc = "LLVM search and compile",
191 .skip_if_fail = true,
192 .get_nr = test__llvm_subtest_get_nr,
193 .get_desc = test__llvm_subtest_get_desc,
197 .desc = "Session topology",
198 .func = test__session_topology,
201 .desc = "BPF filter",
204 .skip_if_fail = true,
205 .get_nr = test__bpf_subtest_get_nr,
206 .get_desc = test__bpf_subtest_get_desc,
210 .desc = "Synthesize thread map",
211 .func = test__thread_map_synthesize,
214 .desc = "Remove thread map",
215 .func = test__thread_map_remove,
218 .desc = "Synthesize cpu map",
219 .func = test__cpu_map_synthesize,
222 .desc = "Synthesize stat config",
223 .func = test__synthesize_stat_config,
226 .desc = "Synthesize stat",
227 .func = test__synthesize_stat,
230 .desc = "Synthesize stat round",
231 .func = test__synthesize_stat_round,
234 .desc = "Synthesize attr update",
235 .func = test__event_update,
238 .desc = "Event times",
239 .func = test__event_times,
242 .desc = "Read backward ring buffer",
243 .func = test__backward_ring_buffer,
246 .desc = "Print cpu map",
247 .func = test__cpu_map_print,
250 .desc = "Probe SDT events",
251 .func = test__sdt_event,
254 .desc = "is_printable_array",
255 .func = test__is_printable_array,
258 .desc = "Print bitmap",
259 .func = test__bitmap_print,
262 .desc = "perf hooks",
263 .func = test__perf_hooks,
266 .desc = "builtin clang support",
269 .skip_if_fail = true,
270 .get_nr = test__clang_subtest_get_nr,
271 .get_desc = test__clang_subtest_get_desc,
275 .desc = "unit_number__scnprintf",
276 .func = test__unit_number__scnprint,
280 .func = test__mem2node,
287 static struct test *tests[] = {
292 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
299 for (i = 0; i < argc; ++i) {
301 long nr = strtoul(argv[i], &end, 10);
309 if (strcasestr(test->desc, argv[i]))
316 static int run_test(struct test *test, int subtest)
318 int status, err = -1, child = dont_fork ? 0 : fork();
319 char sbuf[STRERR_BUFSIZE];
322 pr_err("failed to fork test: %s\n",
323 str_error_r(errno, sbuf, sizeof(sbuf)));
329 pr_debug("test child forked, pid %d\n", getpid());
332 int nullfd = open("/dev/null", O_WRONLY);
335 close(STDERR_FILENO);
336 close(STDOUT_FILENO);
338 dup2(nullfd, STDOUT_FILENO);
339 dup2(STDOUT_FILENO, STDERR_FILENO);
343 signal(SIGSEGV, sighandler_dump_stack);
344 signal(SIGFPE, sighandler_dump_stack);
348 err = test->func(test, subtest);
356 if (WIFEXITED(status)) {
357 err = (signed char)WEXITSTATUS(status);
358 pr_debug("test child finished with %d\n", err);
359 } else if (WIFSIGNALED(status)) {
361 pr_debug("test child interrupted\n");
368 #define for_each_test(j, t) \
369 for (j = 0; j < ARRAY_SIZE(tests); j++) \
370 for (t = &tests[j][0]; t->func; t++)
372 static int test_and_print(struct test *t, bool force_skip, int subtest)
377 pr_debug("\n--- start ---\n");
378 err = run_test(t, subtest);
379 pr_debug("---- end ----\n");
381 pr_debug("\n--- force skipped ---\n");
385 if (!t->subtest.get_nr)
386 pr_debug("%s:", t->desc);
388 pr_debug("%s subtest %d:", t->desc, subtest);
395 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
399 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
406 static const char *shell_test__description(char *description, size_t size,
407 const char *path, const char *name)
410 char filename[PATH_MAX];
412 path__join(filename, sizeof(filename), path, name);
413 fp = fopen(filename, "r");
417 description = fgets(description, size, fp);
420 return description ? trim(description + 1) : NULL;
423 #define for_each_shell_test(dir, base, ent) \
424 while ((ent = readdir(dir)) != NULL) \
425 if (!is_directory(base, ent) && ent->d_name[0] != '.')
427 static const char *shell_tests__dir(char *path, size_t size)
429 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
433 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
435 if (!lstat(devel_dirs[i], &st)) {
436 scnprintf(path, size, "%s/shell", devel_dirs[i]);
437 if (!lstat(devel_dirs[i], &st))
442 /* Then installed path. */
443 exec_path = get_argv_exec_path();
444 scnprintf(path, size, "%s/tests/shell", exec_path);
449 static int shell_tests__max_desc_width(void)
453 char path_dir[PATH_MAX];
454 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
464 for_each_shell_test(dir, path, ent) {
466 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
469 int len = strlen(desc);
485 static int shell_test__run(struct test *test, int subdir __maybe_unused)
488 char script[PATH_MAX];
489 struct shell_test *st = test->priv;
491 path__join(script, sizeof(script), st->dir, st->file);
493 err = system(script);
497 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
500 static int run_shell_tests(int argc, const char *argv[], int i, int width)
504 char path_dir[PATH_MAX];
505 struct shell_test st = {
506 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
512 dir = opendir(st.dir);
516 for_each_shell_test(dir, st.dir, ent) {
520 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
521 .func = shell_test__run,
525 if (!perf_test__matches(&test, curr, argc, argv))
528 st.file = ent->d_name;
529 pr_info("%2d: %-*s:", i, width, test.desc);
530 test_and_print(&test, false, -1);
537 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
542 int width = shell_tests__max_desc_width();
544 for_each_test(j, t) {
545 int len = strlen(t->desc);
551 for_each_test(j, t) {
554 if (!perf_test__matches(t, curr, argc, argv))
557 if (t->is_supported && !t->is_supported()) {
558 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
562 pr_info("%2d: %-*s:", i, width, t->desc);
564 if (intlist__find(skiplist, i)) {
565 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
569 if (!t->subtest.get_nr) {
570 test_and_print(t, false, -1);
572 int subn = t->subtest.get_nr();
574 * minus 2 to align with normal testcases.
575 * For subtest we print additional '.x' in number.
578 * 35: Test LLVM searching and compiling :
579 * 35.1: Basic BPF llvm compiling test : Ok
581 int subw = width > 2 ? width - 2 : width;
586 color_fprintf(stderr, PERF_COLOR_YELLOW,
587 " Skip (not compiled in)\n");
592 for (subi = 0; subi < subn; subi++) {
593 int len = strlen(t->subtest.get_desc(subi));
599 for (subi = 0; subi < subn; subi++) {
600 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
601 t->subtest.get_desc(subi));
602 err = test_and_print(t, skip, subi + 1);
603 if (err != TEST_OK && t->subtest.skip_if_fail)
609 return run_shell_tests(argc, argv, i, width);
612 static int perf_test__list_shell(int argc, const char **argv, int i)
616 char path_dir[PATH_MAX];
617 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
626 for_each_shell_test(dir, path, ent) {
630 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
633 if (!perf_test__matches(&t, curr, argc, argv))
636 pr_info("%2d: %s\n", i, t.desc);
643 static int perf_test__list(int argc, const char **argv)
649 for_each_test(j, t) {
652 if (!perf_test__matches(t, curr, argc, argv) ||
653 (t->is_supported && !t->is_supported()))
656 pr_info("%2d: %s\n", i, t->desc);
658 if (t->subtest.get_nr) {
659 int subn = t->subtest.get_nr();
662 for (subi = 0; subi < subn; subi++)
663 pr_info("%2d:%1d: %s\n", i, subi + 1,
664 t->subtest.get_desc(subi));
668 perf_test__list_shell(argc, argv, i);
673 int cmd_test(int argc, const char **argv)
675 const char *test_usage[] = {
676 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
679 const char *skip = NULL;
680 const struct option test_options[] = {
681 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
682 OPT_INCR('v', "verbose", &verbose,
683 "be more verbose (show symbol address, etc)"),
684 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
685 "Do not fork for testcase"),
688 const char * const test_subcommands[] = { "list", NULL };
689 struct intlist *skiplist = NULL;
690 int ret = hists__init();
695 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
696 if (argc >= 1 && !strcmp(argv[0], "list"))
697 return perf_test__list(argc - 1, argv + 1);
699 symbol_conf.priv_size = sizeof(int);
700 symbol_conf.sort_by_name = true;
701 symbol_conf.try_vmlinux_path = true;
703 if (symbol__init(NULL) < 0)
707 skiplist = intlist__new(skip);
709 return __cmd_test(argc, argv, skiplist);