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 = "Test data source output",
55 .desc = "Parse event definition strings",
56 .func = test__parse_events,
59 .desc = "Simple expression parser",
63 .desc = "PERF_RECORD_* events & perf_sample fields",
64 .func = test__PERF_RECORD,
67 .desc = "Parse perf pmu format",
71 .desc = "DSO data read",
72 .func = test__dso_data,
75 .desc = "DSO data cache",
76 .func = test__dso_data_cache,
79 .desc = "DSO data reopen",
80 .func = test__dso_data_reopen,
83 .desc = "Roundtrip evsel->name",
84 .func = test__perf_evsel__roundtrip_name_test,
87 .desc = "Parse sched tracepoints fields",
88 .func = test__perf_evsel__tp_sched_test,
91 .desc = "syscalls:sys_enter_openat event fields",
92 .func = test__syscall_openat_tp_fields,
95 .desc = "Setup struct perf_event_attr",
99 .desc = "Match and link multiple hists",
100 .func = test__hists_link,
103 .desc = "'import perf' in python",
104 .func = test__python_use,
107 .desc = "Breakpoint overflow signal handler",
108 .func = test__bp_signal,
109 .is_supported = test__bp_signal_is_supported,
112 .desc = "Breakpoint overflow sampling",
113 .func = test__bp_signal_overflow,
114 .is_supported = test__bp_signal_is_supported,
117 .desc = "Number of exit events of a simple workload",
118 .func = test__task_exit,
121 .desc = "Software clock events period values",
122 .func = test__sw_clock_freq,
125 .desc = "Object code reading",
126 .func = test__code_reading,
129 .desc = "Sample parsing",
130 .func = test__sample_parsing,
133 .desc = "Use a dummy software event to keep tracking",
134 .func = test__keep_tracking,
137 .desc = "Parse with no sample_id_all bit set",
138 .func = test__parse_no_sample_id_all,
141 .desc = "Filter hist entries",
142 .func = test__hists_filter,
145 .desc = "Lookup mmap thread",
146 .func = test__mmap_thread_lookup,
149 .desc = "Share thread mg",
150 .func = test__thread_mg_share,
153 .desc = "Sort output of hist entries",
154 .func = test__hists_output,
157 .desc = "Cumulate child hist entries",
158 .func = test__hists_cumulate,
161 .desc = "Track with sched_switch",
162 .func = test__switch_tracking,
165 .desc = "Filter fds with revents mask in a fdarray",
166 .func = test__fdarray__filter,
169 .desc = "Add fd to a fdarray, making it autogrow",
170 .func = test__fdarray__add,
173 .desc = "kmod_path__parse",
174 .func = test__kmod_path__parse,
177 .desc = "Thread map",
178 .func = test__thread_map,
181 .desc = "LLVM search and compile",
184 .skip_if_fail = true,
185 .get_nr = test__llvm_subtest_get_nr,
186 .get_desc = test__llvm_subtest_get_desc,
190 .desc = "Session topology",
191 .func = test__session_topology,
194 .desc = "BPF filter",
197 .skip_if_fail = true,
198 .get_nr = test__bpf_subtest_get_nr,
199 .get_desc = test__bpf_subtest_get_desc,
203 .desc = "Synthesize thread map",
204 .func = test__thread_map_synthesize,
207 .desc = "Remove thread map",
208 .func = test__thread_map_remove,
211 .desc = "Synthesize cpu map",
212 .func = test__cpu_map_synthesize,
215 .desc = "Synthesize stat config",
216 .func = test__synthesize_stat_config,
219 .desc = "Synthesize stat",
220 .func = test__synthesize_stat,
223 .desc = "Synthesize stat round",
224 .func = test__synthesize_stat_round,
227 .desc = "Synthesize attr update",
228 .func = test__event_update,
231 .desc = "Event times",
232 .func = test__event_times,
235 .desc = "Read backward ring buffer",
236 .func = test__backward_ring_buffer,
239 .desc = "Print cpu map",
240 .func = test__cpu_map_print,
243 .desc = "Probe SDT events",
244 .func = test__sdt_event,
247 .desc = "is_printable_array",
248 .func = test__is_printable_array,
251 .desc = "Print bitmap",
252 .func = test__bitmap_print,
255 .desc = "perf hooks",
256 .func = test__perf_hooks,
259 .desc = "builtin clang support",
262 .skip_if_fail = true,
263 .get_nr = test__clang_subtest_get_nr,
264 .get_desc = test__clang_subtest_get_desc,
268 .desc = "unit_number__scnprintf",
269 .func = test__unit_number__scnprint,
276 static struct test *tests[] = {
281 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
288 for (i = 0; i < argc; ++i) {
290 long nr = strtoul(argv[i], &end, 10);
298 if (strcasestr(test->desc, argv[i]))
305 static int run_test(struct test *test, int subtest)
307 int status, err = -1, child = dont_fork ? 0 : fork();
308 char sbuf[STRERR_BUFSIZE];
311 pr_err("failed to fork test: %s\n",
312 str_error_r(errno, sbuf, sizeof(sbuf)));
318 pr_debug("test child forked, pid %d\n", getpid());
321 int nullfd = open("/dev/null", O_WRONLY);
324 close(STDERR_FILENO);
325 close(STDOUT_FILENO);
327 dup2(nullfd, STDOUT_FILENO);
328 dup2(STDOUT_FILENO, STDERR_FILENO);
332 signal(SIGSEGV, sighandler_dump_stack);
333 signal(SIGFPE, sighandler_dump_stack);
337 err = test->func(test, subtest);
345 if (WIFEXITED(status)) {
346 err = (signed char)WEXITSTATUS(status);
347 pr_debug("test child finished with %d\n", err);
348 } else if (WIFSIGNALED(status)) {
350 pr_debug("test child interrupted\n");
357 #define for_each_test(j, t) \
358 for (j = 0; j < ARRAY_SIZE(tests); j++) \
359 for (t = &tests[j][0]; t->func; t++)
361 static int test_and_print(struct test *t, bool force_skip, int subtest)
366 pr_debug("\n--- start ---\n");
367 err = run_test(t, subtest);
368 pr_debug("---- end ----\n");
370 pr_debug("\n--- force skipped ---\n");
374 if (!t->subtest.get_nr)
375 pr_debug("%s:", t->desc);
377 pr_debug("%s subtest %d:", t->desc, subtest);
384 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
388 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
395 static const char *shell_test__description(char *description, size_t size,
396 const char *path, const char *name)
399 char filename[PATH_MAX];
401 path__join(filename, sizeof(filename), path, name);
402 fp = fopen(filename, "r");
406 description = fgets(description, size, fp);
409 return description ? trim(description + 1) : NULL;
412 #define for_each_shell_test(dir, ent) \
413 while ((ent = readdir(dir)) != NULL) \
414 if (ent->d_type == DT_REG && ent->d_name[0] != '.')
416 static const char *shell_tests__dir(char *path, size_t size)
418 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
422 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
424 if (!lstat(devel_dirs[i], &st)) {
425 scnprintf(path, size, "%s/shell", devel_dirs[i]);
426 if (!lstat(devel_dirs[i], &st))
431 /* Then installed path. */
432 exec_path = get_argv_exec_path();
433 scnprintf(path, size, "%s/tests/shell", exec_path);
438 static int shell_tests__max_desc_width(void)
442 char path_dir[PATH_MAX];
443 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
453 for_each_shell_test(dir, ent) {
455 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
458 int len = strlen(desc);
474 static int shell_test__run(struct test *test, int subdir __maybe_unused)
477 char script[PATH_MAX];
478 struct shell_test *st = test->priv;
480 path__join(script, sizeof(script), st->dir, st->file);
482 err = system(script);
486 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
489 static int run_shell_tests(int argc, const char *argv[], int i, int width)
493 char path_dir[PATH_MAX];
494 struct shell_test st = {
495 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
501 dir = opendir(st.dir);
505 for_each_shell_test(dir, ent) {
509 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
510 .func = shell_test__run,
514 if (!perf_test__matches(&test, curr, argc, argv))
517 st.file = ent->d_name;
518 pr_info("%2d: %-*s:", i, width, test.desc);
519 test_and_print(&test, false, -1);
526 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
531 int width = shell_tests__max_desc_width();
533 for_each_test(j, t) {
534 int len = strlen(t->desc);
540 for_each_test(j, t) {
543 if (!perf_test__matches(t, curr, argc, argv))
546 if (t->is_supported && !t->is_supported()) {
547 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
551 pr_info("%2d: %-*s:", i, width, t->desc);
553 if (intlist__find(skiplist, i)) {
554 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
558 if (!t->subtest.get_nr) {
559 test_and_print(t, false, -1);
561 int subn = t->subtest.get_nr();
563 * minus 2 to align with normal testcases.
564 * For subtest we print additional '.x' in number.
567 * 35: Test LLVM searching and compiling :
568 * 35.1: Basic BPF llvm compiling test : Ok
570 int subw = width > 2 ? width - 2 : width;
575 color_fprintf(stderr, PERF_COLOR_YELLOW,
576 " Skip (not compiled in)\n");
581 for (subi = 0; subi < subn; subi++) {
582 int len = strlen(t->subtest.get_desc(subi));
588 for (subi = 0; subi < subn; subi++) {
589 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
590 t->subtest.get_desc(subi));
591 err = test_and_print(t, skip, subi);
592 if (err != TEST_OK && t->subtest.skip_if_fail)
598 return run_shell_tests(argc, argv, i, width);
601 static int perf_test__list_shell(int argc, const char **argv, int i)
605 char path_dir[PATH_MAX];
606 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
615 for_each_shell_test(dir, ent) {
619 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
622 if (!perf_test__matches(&t, curr, argc, argv))
625 pr_info("%2d: %s\n", i, t.desc);
632 static int perf_test__list(int argc, const char **argv)
638 for_each_test(j, t) {
641 if (!perf_test__matches(t, curr, argc, argv) ||
642 (t->is_supported && !t->is_supported()))
645 pr_info("%2d: %s\n", i, t->desc);
648 perf_test__list_shell(argc, argv, i);
653 int cmd_test(int argc, const char **argv)
655 const char *test_usage[] = {
656 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
659 const char *skip = NULL;
660 const struct option test_options[] = {
661 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
662 OPT_INCR('v', "verbose", &verbose,
663 "be more verbose (show symbol address, etc)"),
664 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
665 "Do not fork for testcase"),
668 const char * const test_subcommands[] = { "list", NULL };
669 struct intlist *skiplist = NULL;
670 int ret = hists__init();
675 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
676 if (argc >= 1 && !strcmp(argv[0], "list"))
677 return perf_test__list(argc - 1, argv + 1);
679 symbol_conf.priv_size = sizeof(int);
680 symbol_conf.sort_by_name = true;
681 symbol_conf.try_vmlinux_path = true;
683 if (symbol__init(NULL) < 0)
687 skiplist = intlist__new(skip);
689 return __cmd_test(argc, argv, skiplist);