1 // SPDX-License-Identifier: GPL-2.0
5 * Builtin regression testing command: ever growing number of sanity tests
12 #include <sys/types.h>
22 #include <subcmd/parse-options.h>
25 #include "util/rlimit.h"
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <subcmd/exec-cmd.h>
29 #include <linux/zalloc.h>
31 #include "builtin-test-list.h"
33 static bool dont_fork;
35 struct test_suite *__weak arch_tests[] = {
39 static struct test_suite *generic_tests[] = {
40 &suite__vmlinux_matches_kallsyms,
41 &suite__openat_syscall_event,
42 &suite__openat_syscall_event_on_all_cpus,
51 &suite__dso_data_cache,
52 &suite__dso_data_reopen,
53 &suite__perf_evsel__roundtrip_name_test,
54 &suite__perf_evsel__tp_sched_test,
55 &suite__syscall_openat_tp_fields,
60 &suite__bp_signal_overflow,
61 &suite__bp_accounting,
64 &suite__sw_clock_freq,
66 &suite__sample_parsing,
67 &suite__keep_tracking,
68 &suite__parse_no_sample_id_all,
70 &suite__mmap_thread_lookup,
71 &suite__thread_maps_share,
73 &suite__hists_cumulate,
74 &suite__switch_tracking,
75 &suite__fdarray__filter,
77 &suite__kmod_path__parse,
80 &suite__session_topology,
82 &suite__thread_map_synthesize,
83 &suite__thread_map_remove,
84 &suite__cpu_map_synthesize,
85 &suite__synthesize_stat_config,
86 &suite__synthesize_stat,
87 &suite__synthesize_stat_round,
90 &suite__backward_ring_buffer,
91 &suite__cpu_map_print,
92 &suite__cpu_map_merge,
94 &suite__is_printable_array,
98 &suite__unit_number__scnprint,
101 &suite__jit_write_elf,
104 &suite__maps__merge_in,
105 &suite__demangle_java,
106 &suite__demangle_ocaml,
107 &suite__parse_metric,
108 &suite__pe_file_parsing,
109 &suite__expand_cgroup_events,
110 &suite__perf_time_to_tsc,
116 static struct test_suite **tests[] = {
121 static int num_subtests(const struct test_suite *t)
129 while (t->test_cases[num].name)
135 static bool has_subtests(const struct test_suite *t)
137 return num_subtests(t) > 1;
140 static const char *skip_reason(const struct test_suite *t, int subtest)
145 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;
148 static const char *test_description(const struct test_suite *t, int subtest)
150 if (t->test_cases && subtest >= 0)
151 return t->test_cases[subtest].desc;
156 static test_fnptr test_function(const struct test_suite *t, int subtest)
159 return t->test_cases[0].run_case;
161 return t->test_cases[subtest].run_case;
164 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
171 for (i = 0; i < argc; ++i) {
173 long nr = strtoul(argv[i], &end, 10);
181 if (strcasestr(desc, argv[i]))
188 static int run_test(struct test_suite *test, int subtest)
190 int status, err = -1, child = dont_fork ? 0 : fork();
191 char sbuf[STRERR_BUFSIZE];
194 pr_err("failed to fork test: %s\n",
195 str_error_r(errno, sbuf, sizeof(sbuf)));
201 pr_debug("test child forked, pid %d\n", getpid());
204 int nullfd = open("/dev/null", O_WRONLY);
207 close(STDERR_FILENO);
208 close(STDOUT_FILENO);
210 dup2(nullfd, STDOUT_FILENO);
211 dup2(STDOUT_FILENO, STDERR_FILENO);
215 signal(SIGSEGV, sighandler_dump_stack);
216 signal(SIGFPE, sighandler_dump_stack);
220 err = test_function(test, subtest)(test, subtest);
228 if (WIFEXITED(status)) {
229 err = (signed char)WEXITSTATUS(status);
230 pr_debug("test child finished with %d\n", err);
231 } else if (WIFSIGNALED(status)) {
233 pr_debug("test child interrupted\n");
240 #define for_each_test(j, k, t) \
241 for (j = 0; j < ARRAY_SIZE(tests); j++) \
242 for (k = 0, t = tests[j][k]; tests[j][k]; k++, t = tests[j][k])
244 static int test_and_print(struct test_suite *t, int subtest)
248 pr_debug("\n--- start ---\n");
249 err = run_test(t, subtest);
250 pr_debug("---- end ----\n");
252 if (!has_subtests(t))
253 pr_debug("%s:", t->desc);
255 pr_debug("%s subtest %d:", t->desc, subtest + 1);
262 const char *reason = skip_reason(t, subtest);
265 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
267 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
272 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
284 static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
287 char script[PATH_MAX];
288 struct shell_test *st = test->priv;
290 path__join(script, sizeof(script) - 3, st->dir, st->file);
293 strncat(script, " -v", sizeof(script) - strlen(script) - 1);
295 err = system(script);
299 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
302 static int run_shell_tests(int argc, const char *argv[], int i, int width,
303 struct intlist *skiplist)
305 struct shell_test st;
306 const struct script_file *files, *file;
308 files = list_script_files();
311 for (file = files; file->dir; file++) {
313 struct test_case test_cases[] = {
316 .run_case = shell_test__run,
320 struct test_suite test_suite = {
321 .desc = test_cases[0].desc,
322 .test_cases = test_cases,
327 if (test_suite.desc == NULL ||
328 !perf_test__matches(test_suite.desc, curr, argc, argv))
331 st.file = file->file;
332 pr_info("%3d: %-*s:", i, width, test_suite.desc);
334 if (intlist__find(skiplist, i)) {
335 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
339 test_and_print(&test_suite, 0);
344 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
346 struct test_suite *t;
349 int width = list_script_max_width();
351 for_each_test(j, k, t) {
352 int len = strlen(test_description(t, -1));
358 for_each_test(j, k, t) {
362 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
366 subn = num_subtests(t);
368 for (subi = 0; subi < subn; subi++) {
369 if (perf_test__matches(test_description(t, subi),
378 pr_info("%3d: %-*s:", i, width, test_description(t, -1));
380 if (intlist__find(skiplist, i)) {
381 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
385 if (!has_subtests(t)) {
386 test_and_print(t, -1);
388 int subn = num_subtests(t);
390 * minus 2 to align with normal testcases.
391 * For subtest we print additional '.x' in number.
394 * 35: Test LLVM searching and compiling :
395 * 35.1: Basic BPF llvm compiling test : Ok
397 int subw = width > 2 ? width - 2 : width;
400 color_fprintf(stderr, PERF_COLOR_YELLOW,
401 " Skip (not compiled in)\n");
406 for (subi = 0; subi < subn; subi++) {
407 int len = strlen(test_description(t, subi));
413 for (subi = 0; subi < subn; subi++) {
414 if (!perf_test__matches(test_description(t, subi),
418 pr_info("%3d.%1d: %-*s:", i, subi + 1, subw,
419 test_description(t, subi));
420 test_and_print(t, subi);
425 return run_shell_tests(argc, argv, i, width, skiplist);
428 static int perf_test__list_shell(int argc, const char **argv, int i)
430 const struct script_file *files, *file;
432 files = list_script_files();
435 for (file = files; file->dir; file++) {
437 struct test_suite t = {
441 if (!perf_test__matches(t.desc, curr, argc, argv))
444 pr_info("%3d: %s\n", i, t.desc);
449 static int perf_test__list(int argc, const char **argv)
452 struct test_suite *t;
455 for_each_test(j, k, t) {
458 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
461 pr_info("%3d: %s\n", i, test_description(t, -1));
463 if (has_subtests(t)) {
464 int subn = num_subtests(t);
467 for (subi = 0; subi < subn; subi++)
468 pr_info("%3d:%1d: %s\n", i, subi + 1,
469 test_description(t, subi));
473 perf_test__list_shell(argc, argv, i);
478 int cmd_test(int argc, const char **argv)
480 const char *test_usage[] = {
481 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
484 const char *skip = NULL;
485 const struct option test_options[] = {
486 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
487 OPT_INCR('v', "verbose", &verbose,
488 "be more verbose (show symbol address, etc)"),
489 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
490 "Do not fork for testcase"),
493 const char * const test_subcommands[] = { "list", NULL };
494 struct intlist *skiplist = NULL;
495 int ret = hists__init();
500 /* Unbuffered output */
501 setvbuf(stdout, NULL, _IONBF, 0);
503 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
504 if (argc >= 1 && !strcmp(argv[0], "list"))
505 return perf_test__list(argc - 1, argv + 1);
507 symbol_conf.priv_size = sizeof(int);
508 symbol_conf.sort_by_name = true;
509 symbol_conf.try_vmlinux_path = true;
511 if (symbol__init(NULL) < 0)
515 skiplist = intlist__new(skip);
517 * Tests that create BPF maps, for instance, need more than the 64K
520 rlimit__bump_memlock();
522 return __cmd_test(argc, argv, skiplist);