4 * Builtin regression testing command: ever growing number of sanity tests
16 #include <subcmd/parse-options.h>
18 #include <linux/kernel.h>
20 static bool dont_fork;
22 struct test __weak arch_tests[] = {
28 static struct test generic_tests[] = {
30 .desc = "vmlinux symtab matches kallsyms",
31 .func = test__vmlinux_matches_kallsyms,
34 .desc = "Detect openat syscall event",
35 .func = test__openat_syscall_event,
38 .desc = "Detect openat syscall event on all cpus",
39 .func = test__openat_syscall_event_on_all_cpus,
42 .desc = "Read samples using the mmap interface",
43 .func = test__basic_mmap,
46 .desc = "Parse event definition strings",
47 .func = test__parse_events,
50 .desc = "Simple expression parser",
54 .desc = "PERF_RECORD_* events & perf_sample fields",
55 .func = test__PERF_RECORD,
58 .desc = "Parse perf pmu format",
62 .desc = "DSO data read",
63 .func = test__dso_data,
66 .desc = "DSO data cache",
67 .func = test__dso_data_cache,
70 .desc = "DSO data reopen",
71 .func = test__dso_data_reopen,
74 .desc = "Roundtrip evsel->name",
75 .func = test__perf_evsel__roundtrip_name_test,
78 .desc = "Parse sched tracepoints fields",
79 .func = test__perf_evsel__tp_sched_test,
82 .desc = "syscalls:sys_enter_openat event fields",
83 .func = test__syscall_openat_tp_fields,
86 .desc = "Setup struct perf_event_attr",
90 .desc = "Match and link multiple hists",
91 .func = test__hists_link,
94 .desc = "'import perf' in python",
95 .func = test__python_use,
98 .desc = "Breakpoint overflow signal handler",
99 .func = test__bp_signal,
100 .is_supported = test__bp_signal_is_supported,
103 .desc = "Breakpoint overflow sampling",
104 .func = test__bp_signal_overflow,
105 .is_supported = test__bp_signal_is_supported,
108 .desc = "Number of exit events of a simple workload",
109 .func = test__task_exit,
112 .desc = "Software clock events period values",
113 .func = test__sw_clock_freq,
116 .desc = "Object code reading",
117 .func = test__code_reading,
120 .desc = "Sample parsing",
121 .func = test__sample_parsing,
124 .desc = "Use a dummy software event to keep tracking",
125 .func = test__keep_tracking,
128 .desc = "Parse with no sample_id_all bit set",
129 .func = test__parse_no_sample_id_all,
132 .desc = "Filter hist entries",
133 .func = test__hists_filter,
136 .desc = "Lookup mmap thread",
137 .func = test__mmap_thread_lookup,
140 .desc = "Share thread mg",
141 .func = test__thread_mg_share,
144 .desc = "Sort output of hist entries",
145 .func = test__hists_output,
148 .desc = "Cumulate child hist entries",
149 .func = test__hists_cumulate,
152 .desc = "Track with sched_switch",
153 .func = test__switch_tracking,
156 .desc = "Filter fds with revents mask in a fdarray",
157 .func = test__fdarray__filter,
160 .desc = "Add fd to a fdarray, making it autogrow",
161 .func = test__fdarray__add,
164 .desc = "kmod_path__parse",
165 .func = test__kmod_path__parse,
168 .desc = "Thread map",
169 .func = test__thread_map,
172 .desc = "LLVM search and compile",
175 .skip_if_fail = true,
176 .get_nr = test__llvm_subtest_get_nr,
177 .get_desc = test__llvm_subtest_get_desc,
181 .desc = "Session topology",
182 .func = test_session_topology,
185 .desc = "BPF filter",
188 .skip_if_fail = true,
189 .get_nr = test__bpf_subtest_get_nr,
190 .get_desc = test__bpf_subtest_get_desc,
194 .desc = "Synthesize thread map",
195 .func = test__thread_map_synthesize,
198 .desc = "Remove thread map",
199 .func = test__thread_map_remove,
202 .desc = "Synthesize cpu map",
203 .func = test__cpu_map_synthesize,
206 .desc = "Synthesize stat config",
207 .func = test__synthesize_stat_config,
210 .desc = "Synthesize stat",
211 .func = test__synthesize_stat,
214 .desc = "Synthesize stat round",
215 .func = test__synthesize_stat_round,
218 .desc = "Synthesize attr update",
219 .func = test__event_update,
222 .desc = "Event times",
223 .func = test__event_times,
226 .desc = "Read backward ring buffer",
227 .func = test__backward_ring_buffer,
230 .desc = "Print cpu map",
231 .func = test__cpu_map_print,
234 .desc = "Probe SDT events",
235 .func = test__sdt_event,
238 .desc = "is_printable_array",
239 .func = test__is_printable_array,
242 .desc = "Print bitmap",
243 .func = test__bitmap_print,
246 .desc = "perf hooks",
247 .func = test__perf_hooks,
250 .desc = "builtin clang support",
253 .skip_if_fail = true,
254 .get_nr = test__clang_subtest_get_nr,
255 .get_desc = test__clang_subtest_get_desc,
259 .desc = "unit_number__scnprintf",
260 .func = test__unit_number__scnprint,
267 static struct test *tests[] = {
272 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
279 for (i = 0; i < argc; ++i) {
281 long nr = strtoul(argv[i], &end, 10);
289 if (strcasestr(test->desc, argv[i]))
296 static int run_test(struct test *test, int subtest)
298 int status, err = -1, child = dont_fork ? 0 : fork();
299 char sbuf[STRERR_BUFSIZE];
302 pr_err("failed to fork test: %s\n",
303 str_error_r(errno, sbuf, sizeof(sbuf)));
309 pr_debug("test child forked, pid %d\n", getpid());
312 int nullfd = open("/dev/null", O_WRONLY);
315 close(STDERR_FILENO);
316 close(STDOUT_FILENO);
318 dup2(nullfd, STDOUT_FILENO);
319 dup2(STDOUT_FILENO, STDERR_FILENO);
323 signal(SIGSEGV, sighandler_dump_stack);
324 signal(SIGFPE, sighandler_dump_stack);
328 err = test->func(subtest);
336 if (WIFEXITED(status)) {
337 err = (signed char)WEXITSTATUS(status);
338 pr_debug("test child finished with %d\n", err);
339 } else if (WIFSIGNALED(status)) {
341 pr_debug("test child interrupted\n");
348 #define for_each_test(j, t) \
349 for (j = 0; j < ARRAY_SIZE(tests); j++) \
350 for (t = &tests[j][0]; t->func; t++)
352 static int test_and_print(struct test *t, bool force_skip, int subtest)
357 pr_debug("\n--- start ---\n");
358 err = run_test(t, subtest);
359 pr_debug("---- end ----\n");
361 pr_debug("\n--- force skipped ---\n");
365 if (!t->subtest.get_nr)
366 pr_debug("%s:", t->desc);
368 pr_debug("%s subtest %d:", t->desc, subtest);
375 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
379 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
386 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
393 for_each_test(j, t) {
394 int len = strlen(t->desc);
400 for_each_test(j, t) {
403 if (!perf_test__matches(t, curr, argc, argv))
406 if (t->is_supported && !t->is_supported()) {
407 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
411 pr_info("%2d: %-*s:", i, width, t->desc);
413 if (intlist__find(skiplist, i)) {
414 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
418 if (!t->subtest.get_nr) {
419 test_and_print(t, false, -1);
421 int subn = t->subtest.get_nr();
423 * minus 2 to align with normal testcases.
424 * For subtest we print additional '.x' in number.
427 * 35: Test LLVM searching and compiling :
428 * 35.1: Basic BPF llvm compiling test : Ok
430 int subw = width > 2 ? width - 2 : width;
435 color_fprintf(stderr, PERF_COLOR_YELLOW,
436 " Skip (not compiled in)\n");
441 for (subi = 0; subi < subn; subi++) {
442 int len = strlen(t->subtest.get_desc(subi));
448 for (subi = 0; subi < subn; subi++) {
449 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
450 t->subtest.get_desc(subi));
451 err = test_and_print(t, skip, subi);
452 if (err != TEST_OK && t->subtest.skip_if_fail)
461 static int perf_test__list(int argc, const char **argv)
467 for_each_test(j, t) {
468 if (argc > 1 && !strstr(t->desc, argv[1]))
471 pr_info("%2d: %s\n", ++i, t->desc);
477 int cmd_test(int argc, const char **argv)
479 const char *test_usage[] = {
480 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
483 const char *skip = NULL;
484 const struct option test_options[] = {
485 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
486 OPT_INCR('v', "verbose", &verbose,
487 "be more verbose (show symbol address, etc)"),
488 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
489 "Do not fork for testcase"),
492 const char * const test_subcommands[] = { "list", NULL };
493 struct intlist *skiplist = NULL;
494 int ret = hists__init();
499 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
500 if (argc >= 1 && !strcmp(argv[0], "list"))
501 return perf_test__list(argc, argv);
503 symbol_conf.priv_size = sizeof(int);
504 symbol_conf.sort_by_name = true;
505 symbol_conf.try_vmlinux_path = true;
507 if (symbol__init(NULL) < 0)
511 skiplist = intlist__new(skip);
513 return __cmd_test(argc, argv, skiplist);