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 "util/rlimit.h"
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <subcmd/exec-cmd.h>
29 static bool dont_fork;
31 struct test __weak arch_tests[] = {
37 static struct test generic_tests[] = {
39 .desc = "vmlinux symtab matches kallsyms",
40 .func = test__vmlinux_matches_kallsyms,
43 .desc = "Detect openat syscall event",
44 .func = test__openat_syscall_event,
47 .desc = "Detect openat syscall event on all cpus",
48 .func = test__openat_syscall_event_on_all_cpus,
51 .desc = "Read samples using the mmap interface",
52 .func = test__basic_mmap,
55 .desc = "Test data source output",
59 .desc = "Parse event definition strings",
60 .func = test__parse_events,
63 .desc = "Simple expression parser",
67 .desc = "PERF_RECORD_* events & perf_sample fields",
68 .func = test__PERF_RECORD,
71 .desc = "Parse perf pmu format",
75 .desc = "DSO data read",
76 .func = test__dso_data,
79 .desc = "DSO data cache",
80 .func = test__dso_data_cache,
83 .desc = "DSO data reopen",
84 .func = test__dso_data_reopen,
87 .desc = "Roundtrip evsel->name",
88 .func = test__perf_evsel__roundtrip_name_test,
91 .desc = "Parse sched tracepoints fields",
92 .func = test__perf_evsel__tp_sched_test,
95 .desc = "syscalls:sys_enter_openat event fields",
96 .func = test__syscall_openat_tp_fields,
99 .desc = "Setup struct perf_event_attr",
103 .desc = "Match and link multiple hists",
104 .func = test__hists_link,
107 .desc = "'import perf' in python",
108 .func = test__python_use,
111 .desc = "Breakpoint overflow signal handler",
112 .func = test__bp_signal,
113 .is_supported = test__bp_signal_is_supported,
116 .desc = "Breakpoint overflow sampling",
117 .func = test__bp_signal_overflow,
118 .is_supported = test__bp_signal_is_supported,
121 .desc = "Breakpoint accounting",
122 .func = test__bp_accounting,
123 .is_supported = test__bp_signal_is_supported,
126 .desc = "Watchpoint",
128 .is_supported = test__wp_is_supported,
130 .skip_if_fail = false,
131 .get_nr = test__wp_subtest_get_nr,
132 .get_desc = test__wp_subtest_get_desc,
136 .desc = "Number of exit events of a simple workload",
137 .func = test__task_exit,
140 .desc = "Software clock events period values",
141 .func = test__sw_clock_freq,
144 .desc = "Object code reading",
145 .func = test__code_reading,
148 .desc = "Sample parsing",
149 .func = test__sample_parsing,
152 .desc = "Use a dummy software event to keep tracking",
153 .func = test__keep_tracking,
156 .desc = "Parse with no sample_id_all bit set",
157 .func = test__parse_no_sample_id_all,
160 .desc = "Filter hist entries",
161 .func = test__hists_filter,
164 .desc = "Lookup mmap thread",
165 .func = test__mmap_thread_lookup,
168 .desc = "Share thread mg",
169 .func = test__thread_mg_share,
172 .desc = "Sort output of hist entries",
173 .func = test__hists_output,
176 .desc = "Cumulate child hist entries",
177 .func = test__hists_cumulate,
180 .desc = "Track with sched_switch",
181 .func = test__switch_tracking,
184 .desc = "Filter fds with revents mask in a fdarray",
185 .func = test__fdarray__filter,
188 .desc = "Add fd to a fdarray, making it autogrow",
189 .func = test__fdarray__add,
192 .desc = "kmod_path__parse",
193 .func = test__kmod_path__parse,
196 .desc = "Thread map",
197 .func = test__thread_map,
200 .desc = "LLVM search and compile",
203 .skip_if_fail = true,
204 .get_nr = test__llvm_subtest_get_nr,
205 .get_desc = test__llvm_subtest_get_desc,
209 .desc = "Session topology",
210 .func = test__session_topology,
213 .desc = "BPF filter",
216 .skip_if_fail = true,
217 .get_nr = test__bpf_subtest_get_nr,
218 .get_desc = test__bpf_subtest_get_desc,
222 .desc = "Synthesize thread map",
223 .func = test__thread_map_synthesize,
226 .desc = "Remove thread map",
227 .func = test__thread_map_remove,
230 .desc = "Synthesize cpu map",
231 .func = test__cpu_map_synthesize,
234 .desc = "Synthesize stat config",
235 .func = test__synthesize_stat_config,
238 .desc = "Synthesize stat",
239 .func = test__synthesize_stat,
242 .desc = "Synthesize stat round",
243 .func = test__synthesize_stat_round,
246 .desc = "Synthesize attr update",
247 .func = test__event_update,
250 .desc = "Event times",
251 .func = test__event_times,
254 .desc = "Read backward ring buffer",
255 .func = test__backward_ring_buffer,
258 .desc = "Print cpu map",
259 .func = test__cpu_map_print,
262 .desc = "Probe SDT events",
263 .func = test__sdt_event,
266 .desc = "is_printable_array",
267 .func = test__is_printable_array,
270 .desc = "Print bitmap",
271 .func = test__bitmap_print,
274 .desc = "perf hooks",
275 .func = test__perf_hooks,
278 .desc = "builtin clang support",
281 .skip_if_fail = true,
282 .get_nr = test__clang_subtest_get_nr,
283 .get_desc = test__clang_subtest_get_desc,
287 .desc = "unit_number__scnprintf",
288 .func = test__unit_number__scnprint,
292 .func = test__mem2node,
295 .desc = "time utils",
296 .func = test__time_utils,
299 .desc = "map_groups__merge_in",
300 .func = test__map_groups__merge_in,
307 static struct test *tests[] = {
312 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
319 for (i = 0; i < argc; ++i) {
321 long nr = strtoul(argv[i], &end, 10);
329 if (strcasestr(test->desc, argv[i]))
336 static int run_test(struct test *test, int subtest)
338 int status, err = -1, child = dont_fork ? 0 : fork();
339 char sbuf[STRERR_BUFSIZE];
342 pr_err("failed to fork test: %s\n",
343 str_error_r(errno, sbuf, sizeof(sbuf)));
349 pr_debug("test child forked, pid %d\n", getpid());
352 int nullfd = open("/dev/null", O_WRONLY);
355 close(STDERR_FILENO);
356 close(STDOUT_FILENO);
358 dup2(nullfd, STDOUT_FILENO);
359 dup2(STDOUT_FILENO, STDERR_FILENO);
363 signal(SIGSEGV, sighandler_dump_stack);
364 signal(SIGFPE, sighandler_dump_stack);
368 err = test->func(test, subtest);
376 if (WIFEXITED(status)) {
377 err = (signed char)WEXITSTATUS(status);
378 pr_debug("test child finished with %d\n", err);
379 } else if (WIFSIGNALED(status)) {
381 pr_debug("test child interrupted\n");
388 #define for_each_test(j, t) \
389 for (j = 0; j < ARRAY_SIZE(tests); j++) \
390 for (t = &tests[j][0]; t->func; t++)
392 static int test_and_print(struct test *t, bool force_skip, int subtest)
397 pr_debug("\n--- start ---\n");
398 err = run_test(t, subtest);
399 pr_debug("---- end ----\n");
401 pr_debug("\n--- force skipped ---\n");
405 if (!t->subtest.get_nr)
406 pr_debug("%s:", t->desc);
408 pr_debug("%s subtest %d:", t->desc, subtest + 1);
415 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
419 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
426 static const char *shell_test__description(char *description, size_t size,
427 const char *path, const char *name)
430 char filename[PATH_MAX];
432 path__join(filename, sizeof(filename), path, name);
433 fp = fopen(filename, "r");
438 while (fgetc(fp) != '\n');
440 description = fgets(description, size, fp);
443 return description ? strim(description + 1) : NULL;
446 #define for_each_shell_test(dir, base, ent) \
447 while ((ent = readdir(dir)) != NULL) \
448 if (!is_directory(base, ent) && ent->d_name[0] != '.')
450 static const char *shell_tests__dir(char *path, size_t size)
452 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
456 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
458 if (!lstat(devel_dirs[i], &st)) {
459 scnprintf(path, size, "%s/shell", devel_dirs[i]);
460 if (!lstat(devel_dirs[i], &st))
465 /* Then installed path. */
466 exec_path = get_argv_exec_path();
467 scnprintf(path, size, "%s/tests/shell", exec_path);
472 static int shell_tests__max_desc_width(void)
476 char path_dir[PATH_MAX];
477 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
487 for_each_shell_test(dir, path, ent) {
489 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
492 int len = strlen(desc);
508 static int shell_test__run(struct test *test, int subdir __maybe_unused)
511 char script[PATH_MAX];
512 struct shell_test *st = test->priv;
514 path__join(script, sizeof(script), st->dir, st->file);
516 err = system(script);
520 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
523 static int run_shell_tests(int argc, const char *argv[], int i, int width)
527 char path_dir[PATH_MAX];
528 struct shell_test st = {
529 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
535 dir = opendir(st.dir);
539 for_each_shell_test(dir, st.dir, ent) {
543 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
544 .func = shell_test__run,
548 if (!perf_test__matches(&test, curr, argc, argv))
551 st.file = ent->d_name;
552 pr_info("%2d: %-*s:", i, width, test.desc);
553 test_and_print(&test, false, -1);
560 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
565 int width = shell_tests__max_desc_width();
567 for_each_test(j, t) {
568 int len = strlen(t->desc);
574 for_each_test(j, t) {
577 if (!perf_test__matches(t, curr, argc, argv))
580 if (t->is_supported && !t->is_supported()) {
581 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
585 pr_info("%2d: %-*s:", i, width, t->desc);
587 if (intlist__find(skiplist, i)) {
588 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
592 if (!t->subtest.get_nr) {
593 test_and_print(t, false, -1);
595 int subn = t->subtest.get_nr();
597 * minus 2 to align with normal testcases.
598 * For subtest we print additional '.x' in number.
601 * 35: Test LLVM searching and compiling :
602 * 35.1: Basic BPF llvm compiling test : Ok
604 int subw = width > 2 ? width - 2 : width;
609 color_fprintf(stderr, PERF_COLOR_YELLOW,
610 " Skip (not compiled in)\n");
615 for (subi = 0; subi < subn; subi++) {
616 int len = strlen(t->subtest.get_desc(subi));
622 for (subi = 0; subi < subn; subi++) {
623 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
624 t->subtest.get_desc(subi));
625 err = test_and_print(t, skip, subi);
626 if (err != TEST_OK && t->subtest.skip_if_fail)
632 return run_shell_tests(argc, argv, i, width);
635 static int perf_test__list_shell(int argc, const char **argv, int i)
639 char path_dir[PATH_MAX];
640 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
649 for_each_shell_test(dir, path, ent) {
653 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
656 if (!perf_test__matches(&t, curr, argc, argv))
659 pr_info("%2d: %s\n", i, t.desc);
666 static int perf_test__list(int argc, const char **argv)
672 for_each_test(j, t) {
675 if (!perf_test__matches(t, curr, argc, argv) ||
676 (t->is_supported && !t->is_supported()))
679 pr_info("%2d: %s\n", i, t->desc);
681 if (t->subtest.get_nr) {
682 int subn = t->subtest.get_nr();
685 for (subi = 0; subi < subn; subi++)
686 pr_info("%2d:%1d: %s\n", i, subi + 1,
687 t->subtest.get_desc(subi));
691 perf_test__list_shell(argc, argv, i);
696 int cmd_test(int argc, const char **argv)
698 const char *test_usage[] = {
699 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
702 const char *skip = NULL;
703 const struct option test_options[] = {
704 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
705 OPT_INCR('v', "verbose", &verbose,
706 "be more verbose (show symbol address, etc)"),
707 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
708 "Do not fork for testcase"),
711 const char * const test_subcommands[] = { "list", NULL };
712 struct intlist *skiplist = NULL;
713 int ret = hists__init();
718 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
719 if (argc >= 1 && !strcmp(argv[0], "list"))
720 return perf_test__list(argc - 1, argv + 1);
722 symbol_conf.priv_size = sizeof(int);
723 symbol_conf.sort_by_name = true;
724 symbol_conf.try_vmlinux_path = true;
726 if (symbol__init(NULL) < 0)
730 skiplist = intlist__new(skip);
732 * Tests that create BPF maps, for instance, need more than the 64K
735 rlimit__bump_memlock();
737 return __cmd_test(argc, argv, skiplist);