4 * Builtin regression testing command: ever growing number of sanity tests
15 #include <subcmd/parse-options.h>
17 #include <linux/kernel.h>
19 static bool dont_fork;
21 struct test __weak arch_tests[] = {
27 static struct test generic_tests[] = {
29 .desc = "vmlinux symtab matches kallsyms",
30 .func = test__vmlinux_matches_kallsyms,
33 .desc = "Detect openat syscall event",
34 .func = test__openat_syscall_event,
37 .desc = "Detect openat syscall event on all cpus",
38 .func = test__openat_syscall_event_on_all_cpus,
41 .desc = "Read samples using the mmap interface",
42 .func = test__basic_mmap,
45 .desc = "Parse event definition strings",
46 .func = test__parse_events,
49 .desc = "Simple expression parser",
53 .desc = "PERF_RECORD_* events & perf_sample fields",
54 .func = test__PERF_RECORD,
57 .desc = "Parse perf pmu format",
61 .desc = "DSO data read",
62 .func = test__dso_data,
65 .desc = "DSO data cache",
66 .func = test__dso_data_cache,
69 .desc = "DSO data reopen",
70 .func = test__dso_data_reopen,
73 .desc = "Roundtrip evsel->name",
74 .func = test__perf_evsel__roundtrip_name_test,
77 .desc = "Parse sched tracepoints fields",
78 .func = test__perf_evsel__tp_sched_test,
81 .desc = "syscalls:sys_enter_openat event fields",
82 .func = test__syscall_openat_tp_fields,
85 .desc = "Setup struct perf_event_attr",
89 .desc = "Match and link multiple hists",
90 .func = test__hists_link,
93 .desc = "'import perf' in python",
94 .func = test__python_use,
97 .desc = "Breakpoint overflow signal handler",
98 .func = test__bp_signal,
101 .desc = "Breakpoint overflow sampling",
102 .func = test__bp_signal_overflow,
105 .desc = "Number of exit events of a simple workload",
106 .func = test__task_exit,
109 .desc = "Software clock events period values",
110 .func = test__sw_clock_freq,
113 .desc = "Object code reading",
114 .func = test__code_reading,
117 .desc = "Sample parsing",
118 .func = test__sample_parsing,
121 .desc = "Use a dummy software event to keep tracking",
122 .func = test__keep_tracking,
125 .desc = "Parse with no sample_id_all bit set",
126 .func = test__parse_no_sample_id_all,
129 .desc = "Filter hist entries",
130 .func = test__hists_filter,
133 .desc = "Lookup mmap thread",
134 .func = test__mmap_thread_lookup,
137 .desc = "Share thread mg",
138 .func = test__thread_mg_share,
141 .desc = "Sort output of hist entries",
142 .func = test__hists_output,
145 .desc = "Cumulate child hist entries",
146 .func = test__hists_cumulate,
149 .desc = "Track with sched_switch",
150 .func = test__switch_tracking,
153 .desc = "Filter fds with revents mask in a fdarray",
154 .func = test__fdarray__filter,
157 .desc = "Add fd to a fdarray, making it autogrow",
158 .func = test__fdarray__add,
161 .desc = "kmod_path__parse",
162 .func = test__kmod_path__parse,
165 .desc = "Thread map",
166 .func = test__thread_map,
169 .desc = "LLVM search and compile",
172 .skip_if_fail = true,
173 .get_nr = test__llvm_subtest_get_nr,
174 .get_desc = test__llvm_subtest_get_desc,
178 .desc = "Session topology",
179 .func = test_session_topology,
182 .desc = "BPF filter",
185 .skip_if_fail = true,
186 .get_nr = test__bpf_subtest_get_nr,
187 .get_desc = test__bpf_subtest_get_desc,
191 .desc = "Synthesize thread map",
192 .func = test__thread_map_synthesize,
195 .desc = "Remove thread map",
196 .func = test__thread_map_remove,
199 .desc = "Synthesize cpu map",
200 .func = test__cpu_map_synthesize,
203 .desc = "Synthesize stat config",
204 .func = test__synthesize_stat_config,
207 .desc = "Synthesize stat",
208 .func = test__synthesize_stat,
211 .desc = "Synthesize stat round",
212 .func = test__synthesize_stat_round,
215 .desc = "Synthesize attr update",
216 .func = test__event_update,
219 .desc = "Event times",
220 .func = test__event_times,
223 .desc = "Read backward ring buffer",
224 .func = test__backward_ring_buffer,
227 .desc = "Print cpu map",
228 .func = test__cpu_map_print,
231 .desc = "Probe SDT events",
232 .func = test__sdt_event,
235 .desc = "is_printable_array",
236 .func = test__is_printable_array,
239 .desc = "Print bitmap",
240 .func = test__bitmap_print,
243 .desc = "perf hooks",
244 .func = test__perf_hooks,
247 .desc = "builtin clang support",
250 .skip_if_fail = true,
251 .get_nr = test__clang_subtest_get_nr,
252 .get_desc = test__clang_subtest_get_desc,
256 .desc = "unit_number__scnprintf",
257 .func = test__unit_number__scnprint,
264 static struct test *tests[] = {
269 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
276 for (i = 0; i < argc; ++i) {
278 long nr = strtoul(argv[i], &end, 10);
286 if (strcasestr(test->desc, argv[i]))
293 static int run_test(struct test *test, int subtest)
295 int status, err = -1, child = dont_fork ? 0 : fork();
296 char sbuf[STRERR_BUFSIZE];
299 pr_err("failed to fork test: %s\n",
300 str_error_r(errno, sbuf, sizeof(sbuf)));
306 pr_debug("test child forked, pid %d\n", getpid());
309 int nullfd = open("/dev/null", O_WRONLY);
312 close(STDERR_FILENO);
313 close(STDOUT_FILENO);
315 dup2(nullfd, STDOUT_FILENO);
316 dup2(STDOUT_FILENO, STDERR_FILENO);
320 signal(SIGSEGV, sighandler_dump_stack);
321 signal(SIGFPE, sighandler_dump_stack);
325 err = test->func(subtest);
333 if (WIFEXITED(status)) {
334 err = (signed char)WEXITSTATUS(status);
335 pr_debug("test child finished with %d\n", err);
336 } else if (WIFSIGNALED(status)) {
338 pr_debug("test child interrupted\n");
345 #define for_each_test(j, t) \
346 for (j = 0; j < ARRAY_SIZE(tests); j++) \
347 for (t = &tests[j][0]; t->func; t++)
349 static int test_and_print(struct test *t, bool force_skip, int subtest)
354 pr_debug("\n--- start ---\n");
355 err = run_test(t, subtest);
356 pr_debug("---- end ----\n");
358 pr_debug("\n--- force skipped ---\n");
362 if (!t->subtest.get_nr)
363 pr_debug("%s:", t->desc);
365 pr_debug("%s subtest %d:", t->desc, subtest);
372 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
376 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
383 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
390 for_each_test(j, t) {
391 int len = strlen(t->desc);
397 for_each_test(j, t) {
400 if (!perf_test__matches(t, curr, argc, argv))
403 pr_info("%2d: %-*s:", i, width, t->desc);
405 if (intlist__find(skiplist, i)) {
406 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
410 if (!t->subtest.get_nr) {
411 test_and_print(t, false, -1);
413 int subn = t->subtest.get_nr();
415 * minus 2 to align with normal testcases.
416 * For subtest we print additional '.x' in number.
419 * 35: Test LLVM searching and compiling :
420 * 35.1: Basic BPF llvm compiling test : Ok
422 int subw = width > 2 ? width - 2 : width;
427 color_fprintf(stderr, PERF_COLOR_YELLOW,
428 " Skip (not compiled in)\n");
433 for (subi = 0; subi < subn; subi++) {
434 int len = strlen(t->subtest.get_desc(subi));
440 for (subi = 0; subi < subn; subi++) {
441 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
442 t->subtest.get_desc(subi));
443 err = test_and_print(t, skip, subi);
444 if (err != TEST_OK && t->subtest.skip_if_fail)
453 static int perf_test__list(int argc, const char **argv)
459 for_each_test(j, t) {
460 if (argc > 1 && !strstr(t->desc, argv[1]))
463 pr_info("%2d: %s\n", ++i, t->desc);
469 int cmd_test(int argc, const char **argv)
471 const char *test_usage[] = {
472 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
475 const char *skip = NULL;
476 const struct option test_options[] = {
477 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
478 OPT_INCR('v', "verbose", &verbose,
479 "be more verbose (show symbol address, etc)"),
480 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
481 "Do not fork for testcase"),
484 const char * const test_subcommands[] = { "list", NULL };
485 struct intlist *skiplist = NULL;
486 int ret = hists__init();
491 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
492 if (argc >= 1 && !strcmp(argv[0], "list"))
493 return perf_test__list(argc, argv);
495 symbol_conf.priv_size = sizeof(int);
496 symbol_conf.sort_by_name = true;
497 symbol_conf.try_vmlinux_path = true;
499 if (symbol__init(NULL) < 0)
503 skiplist = intlist__new(skip);
505 return __cmd_test(argc, argv, skiplist);