4 * Builtin regression testing command: ever growing number of sanity tests
14 #include <subcmd/parse-options.h>
16 #include <linux/kernel.h>
18 static bool dont_fork;
20 struct test __weak arch_tests[] = {
26 static struct test generic_tests[] = {
28 .desc = "vmlinux symtab matches kallsyms",
29 .func = test__vmlinux_matches_kallsyms,
32 .desc = "Detect openat syscall event",
33 .func = test__openat_syscall_event,
36 .desc = "Detect openat syscall event on all cpus",
37 .func = test__openat_syscall_event_on_all_cpus,
40 .desc = "Read samples using the mmap interface",
41 .func = test__basic_mmap,
44 .desc = "Parse event definition strings",
45 .func = test__parse_events,
48 .desc = "Simple expression parser",
52 .desc = "PERF_RECORD_* events & perf_sample fields",
53 .func = test__PERF_RECORD,
56 .desc = "Parse perf pmu format",
60 .desc = "DSO data read",
61 .func = test__dso_data,
64 .desc = "DSO data cache",
65 .func = test__dso_data_cache,
68 .desc = "DSO data reopen",
69 .func = test__dso_data_reopen,
72 .desc = "Roundtrip evsel->name",
73 .func = test__perf_evsel__roundtrip_name_test,
76 .desc = "Parse sched tracepoints fields",
77 .func = test__perf_evsel__tp_sched_test,
80 .desc = "syscalls:sys_enter_openat event fields",
81 .func = test__syscall_openat_tp_fields,
84 .desc = "Setup struct perf_event_attr",
88 .desc = "Match and link multiple hists",
89 .func = test__hists_link,
92 .desc = "'import perf' in python",
93 .func = test__python_use,
96 .desc = "Breakpoint overflow signal handler",
97 .func = test__bp_signal,
100 .desc = "Breakpoint overflow sampling",
101 .func = test__bp_signal_overflow,
104 .desc = "Number of exit events of a simple workload",
105 .func = test__task_exit,
108 .desc = "Software clock events period values",
109 .func = test__sw_clock_freq,
112 .desc = "Object code reading",
113 .func = test__code_reading,
116 .desc = "Sample parsing",
117 .func = test__sample_parsing,
120 .desc = "Use a dummy software event to keep tracking",
121 .func = test__keep_tracking,
124 .desc = "Parse with no sample_id_all bit set",
125 .func = test__parse_no_sample_id_all,
128 .desc = "Filter hist entries",
129 .func = test__hists_filter,
132 .desc = "Lookup mmap thread",
133 .func = test__mmap_thread_lookup,
136 .desc = "Share thread mg",
137 .func = test__thread_mg_share,
140 .desc = "Sort output of hist entries",
141 .func = test__hists_output,
144 .desc = "Cumulate child hist entries",
145 .func = test__hists_cumulate,
148 .desc = "Track with sched_switch",
149 .func = test__switch_tracking,
152 .desc = "Filter fds with revents mask in a fdarray",
153 .func = test__fdarray__filter,
156 .desc = "Add fd to a fdarray, making it autogrow",
157 .func = test__fdarray__add,
160 .desc = "kmod_path__parse",
161 .func = test__kmod_path__parse,
164 .desc = "Thread map",
165 .func = test__thread_map,
168 .desc = "LLVM search and compile",
171 .skip_if_fail = true,
172 .get_nr = test__llvm_subtest_get_nr,
173 .get_desc = test__llvm_subtest_get_desc,
177 .desc = "Session topology",
178 .func = test_session_topology,
181 .desc = "BPF filter",
184 .skip_if_fail = true,
185 .get_nr = test__bpf_subtest_get_nr,
186 .get_desc = test__bpf_subtest_get_desc,
190 .desc = "Synthesize thread map",
191 .func = test__thread_map_synthesize,
194 .desc = "Remove thread map",
195 .func = test__thread_map_remove,
198 .desc = "Synthesize cpu map",
199 .func = test__cpu_map_synthesize,
202 .desc = "Synthesize stat config",
203 .func = test__synthesize_stat_config,
206 .desc = "Synthesize stat",
207 .func = test__synthesize_stat,
210 .desc = "Synthesize stat round",
211 .func = test__synthesize_stat_round,
214 .desc = "Synthesize attr update",
215 .func = test__event_update,
218 .desc = "Event times",
219 .func = test__event_times,
222 .desc = "Read backward ring buffer",
223 .func = test__backward_ring_buffer,
226 .desc = "Print cpu map",
227 .func = test__cpu_map_print,
230 .desc = "Probe SDT events",
231 .func = test__sdt_event,
234 .desc = "is_printable_array",
235 .func = test__is_printable_array,
238 .desc = "Print bitmap",
239 .func = test__bitmap_print,
242 .desc = "perf hooks",
243 .func = test__perf_hooks,
246 .desc = "builtin clang support",
249 .skip_if_fail = true,
250 .get_nr = test__clang_subtest_get_nr,
251 .get_desc = test__clang_subtest_get_desc,
255 .desc = "unit_number__scnprintf",
256 .func = test__unit_number__scnprint,
263 static struct test *tests[] = {
268 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
275 for (i = 0; i < argc; ++i) {
277 long nr = strtoul(argv[i], &end, 10);
285 if (strcasestr(test->desc, argv[i]))
292 static int run_test(struct test *test, int subtest)
294 int status, err = -1, child = dont_fork ? 0 : fork();
295 char sbuf[STRERR_BUFSIZE];
298 pr_err("failed to fork test: %s\n",
299 str_error_r(errno, sbuf, sizeof(sbuf)));
305 pr_debug("test child forked, pid %d\n", getpid());
308 int nullfd = open("/dev/null", O_WRONLY);
311 close(STDERR_FILENO);
312 close(STDOUT_FILENO);
314 dup2(nullfd, STDOUT_FILENO);
315 dup2(STDOUT_FILENO, STDERR_FILENO);
319 signal(SIGSEGV, sighandler_dump_stack);
320 signal(SIGFPE, sighandler_dump_stack);
324 err = test->func(subtest);
332 if (WIFEXITED(status)) {
333 err = (signed char)WEXITSTATUS(status);
334 pr_debug("test child finished with %d\n", err);
335 } else if (WIFSIGNALED(status)) {
337 pr_debug("test child interrupted\n");
344 #define for_each_test(j, t) \
345 for (j = 0; j < ARRAY_SIZE(tests); j++) \
346 for (t = &tests[j][0]; t->func; t++)
348 static int test_and_print(struct test *t, bool force_skip, int subtest)
353 pr_debug("\n--- start ---\n");
354 err = run_test(t, subtest);
355 pr_debug("---- end ----\n");
357 pr_debug("\n--- force skipped ---\n");
361 if (!t->subtest.get_nr)
362 pr_debug("%s:", t->desc);
364 pr_debug("%s subtest %d:", t->desc, subtest);
371 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
375 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
382 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
389 for_each_test(j, t) {
390 int len = strlen(t->desc);
396 for_each_test(j, t) {
399 if (!perf_test__matches(t, curr, argc, argv))
402 pr_info("%2d: %-*s:", i, width, t->desc);
404 if (intlist__find(skiplist, i)) {
405 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
409 if (!t->subtest.get_nr) {
410 test_and_print(t, false, -1);
412 int subn = t->subtest.get_nr();
414 * minus 2 to align with normal testcases.
415 * For subtest we print additional '.x' in number.
418 * 35: Test LLVM searching and compiling :
419 * 35.1: Basic BPF llvm compiling test : Ok
421 int subw = width > 2 ? width - 2 : width;
426 color_fprintf(stderr, PERF_COLOR_YELLOW,
427 " Skip (not compiled in)\n");
432 for (subi = 0; subi < subn; subi++) {
433 int len = strlen(t->subtest.get_desc(subi));
439 for (subi = 0; subi < subn; subi++) {
440 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
441 t->subtest.get_desc(subi));
442 err = test_and_print(t, skip, subi);
443 if (err != TEST_OK && t->subtest.skip_if_fail)
452 static int perf_test__list(int argc, const char **argv)
458 for_each_test(j, t) {
459 if (argc > 1 && !strstr(t->desc, argv[1]))
462 pr_info("%2d: %s\n", ++i, t->desc);
468 int cmd_test(int argc, const char **argv)
470 const char *test_usage[] = {
471 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
474 const char *skip = NULL;
475 const struct option test_options[] = {
476 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
477 OPT_INCR('v', "verbose", &verbose,
478 "be more verbose (show symbol address, etc)"),
479 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
480 "Do not fork for testcase"),
483 const char * const test_subcommands[] = { "list", NULL };
484 struct intlist *skiplist = NULL;
485 int ret = hists__init();
490 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
491 if (argc >= 1 && !strcmp(argv[0], "list"))
492 return perf_test__list(argc, argv);
494 symbol_conf.priv_size = sizeof(int);
495 symbol_conf.sort_by_name = true;
496 symbol_conf.try_vmlinux_path = true;
498 if (symbol__init(NULL) < 0)
502 skiplist = intlist__new(skip);
504 return __cmd_test(argc, argv, skiplist);