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;
34 const char *dso_to_test;
37 * List of architecture specific tests. Not a weak symbol as the array length is
38 * dependent on the initialization, as such GCC with LTO complains of
39 * conflicting definitions with a weak symbol.
41 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
42 extern struct test_suite *arch_tests[];
44 static struct test_suite *arch_tests[] = {
49 static struct test_suite *generic_tests[] = {
50 &suite__vmlinux_matches_kallsyms,
51 #ifdef HAVE_LIBTRACEEVENT
52 &suite__openat_syscall_event,
53 &suite__openat_syscall_event_on_all_cpus,
63 &suite__dso_data_cache,
64 &suite__dso_data_reopen,
65 &suite__perf_evsel__roundtrip_name_test,
66 #ifdef HAVE_LIBTRACEEVENT
67 &suite__perf_evsel__tp_sched_test,
68 &suite__syscall_openat_tp_fields,
74 &suite__bp_signal_overflow,
75 &suite__bp_accounting,
78 &suite__sw_clock_freq,
80 &suite__sample_parsing,
81 &suite__keep_tracking,
82 &suite__parse_no_sample_id_all,
84 &suite__mmap_thread_lookup,
85 &suite__thread_maps_share,
87 &suite__hists_cumulate,
88 #ifdef HAVE_LIBTRACEEVENT
89 &suite__switch_tracking,
91 &suite__fdarray__filter,
93 &suite__kmod_path__parse,
95 &suite__session_topology,
96 &suite__thread_map_synthesize,
97 &suite__thread_map_remove,
99 &suite__synthesize_stat_config,
100 &suite__synthesize_stat,
101 &suite__synthesize_stat_round,
102 &suite__event_update,
104 &suite__backward_ring_buffer,
106 &suite__is_printable_array,
107 &suite__bitmap_print,
109 &suite__unit_number__scnprint,
112 &suite__jit_write_elf,
115 &suite__maps__merge_in,
116 &suite__demangle_java,
117 &suite__demangle_ocaml,
118 &suite__parse_metric,
119 &suite__pe_file_parsing,
120 &suite__expand_cgroup_events,
121 &suite__perf_time_to_tsc,
124 &suite__event_groups,
129 static struct test_suite **tests[] = {
134 static struct test_workload *workloads[] = {
143 static int num_subtests(const struct test_suite *t)
151 while (t->test_cases[num].name)
157 static bool has_subtests(const struct test_suite *t)
159 return num_subtests(t) > 1;
162 static const char *skip_reason(const struct test_suite *t, int subtest)
167 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;
170 static const char *test_description(const struct test_suite *t, int subtest)
172 if (t->test_cases && subtest >= 0)
173 return t->test_cases[subtest].desc;
178 static test_fnptr test_function(const struct test_suite *t, int subtest)
181 return t->test_cases[0].run_case;
183 return t->test_cases[subtest].run_case;
186 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
193 for (i = 0; i < argc; ++i) {
195 long nr = strtoul(argv[i], &end, 10);
203 if (strcasestr(desc, argv[i]))
210 static int run_test(struct test_suite *test, int subtest)
212 int status, err = -1, child = dont_fork ? 0 : fork();
213 char sbuf[STRERR_BUFSIZE];
216 pr_err("failed to fork test: %s\n",
217 str_error_r(errno, sbuf, sizeof(sbuf)));
223 pr_debug("test child forked, pid %d\n", getpid());
226 int nullfd = open("/dev/null", O_WRONLY);
229 close(STDERR_FILENO);
230 close(STDOUT_FILENO);
232 dup2(nullfd, STDOUT_FILENO);
233 dup2(STDOUT_FILENO, STDERR_FILENO);
237 signal(SIGSEGV, sighandler_dump_stack);
238 signal(SIGFPE, sighandler_dump_stack);
242 err = test_function(test, subtest)(test, subtest);
250 if (WIFEXITED(status)) {
251 err = (signed char)WEXITSTATUS(status);
252 pr_debug("test child finished with %d\n", err);
253 } else if (WIFSIGNALED(status)) {
255 pr_debug("test child interrupted\n");
262 #define for_each_test(j, k, t) \
263 for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \
264 while ((t = tests[j][k++]) != NULL)
266 static int test_and_print(struct test_suite *t, int subtest)
270 pr_debug("\n--- start ---\n");
271 err = run_test(t, subtest);
272 pr_debug("---- end ----\n");
274 if (!has_subtests(t))
275 pr_debug("%s:", t->desc);
277 pr_debug("%s subtest %d:", t->desc, subtest + 1);
284 const char *reason = skip_reason(t, subtest);
287 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
289 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
294 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
306 static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
309 char script[PATH_MAX];
310 struct shell_test *st = test->priv;
312 path__join(script, sizeof(script) - 3, st->dir, st->file);
315 strncat(script, " -v", sizeof(script) - strlen(script) - 1);
317 err = system(script);
321 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
324 static int run_shell_tests(int argc, const char *argv[], int i, int width,
325 struct intlist *skiplist)
327 struct shell_test st;
328 const struct script_file *files, *file;
330 files = list_script_files();
333 for (file = files; file->dir; file++) {
335 struct test_case test_cases[] = {
338 .run_case = shell_test__run,
342 struct test_suite test_suite = {
343 .desc = test_cases[0].desc,
344 .test_cases = test_cases,
349 if (test_suite.desc == NULL ||
350 !perf_test__matches(test_suite.desc, curr, argc, argv))
353 st.file = file->file;
354 pr_info("%3d: %-*s:", i, width, test_suite.desc);
356 if (intlist__find(skiplist, i)) {
357 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
361 test_and_print(&test_suite, 0);
366 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
368 struct test_suite *t;
371 int width = list_script_max_width();
373 for_each_test(j, k, t) {
374 int len = strlen(test_description(t, -1));
380 for_each_test(j, k, t) {
384 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
388 subn = num_subtests(t);
390 for (subi = 0; subi < subn; subi++) {
391 if (perf_test__matches(test_description(t, subi),
400 pr_info("%3d: %-*s:", i, width, test_description(t, -1));
402 if (intlist__find(skiplist, i)) {
403 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
407 if (!has_subtests(t)) {
408 test_and_print(t, -1);
410 int subn = num_subtests(t);
412 * minus 2 to align with normal testcases.
413 * For subtest we print additional '.x' in number.
416 * 35: Test LLVM searching and compiling :
417 * 35.1: Basic BPF llvm compiling test : Ok
419 int subw = width > 2 ? width - 2 : width;
422 color_fprintf(stderr, PERF_COLOR_YELLOW,
423 " Skip (not compiled in)\n");
428 for (subi = 0; subi < subn; subi++) {
429 int len = strlen(test_description(t, subi));
435 for (subi = 0; subi < subn; subi++) {
436 if (!perf_test__matches(test_description(t, subi),
440 pr_info("%3d.%1d: %-*s:", i, subi + 1, subw,
441 test_description(t, subi));
442 test_and_print(t, subi);
447 return run_shell_tests(argc, argv, i, width, skiplist);
450 static int perf_test__list_shell(int argc, const char **argv, int i)
452 const struct script_file *files, *file;
454 files = list_script_files();
457 for (file = files; file->dir; file++) {
459 struct test_suite t = {
463 if (!perf_test__matches(t.desc, curr, argc, argv))
466 pr_info("%3d: %s\n", i, t.desc);
471 static int perf_test__list(int argc, const char **argv)
474 struct test_suite *t;
477 for_each_test(j, k, t) {
480 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
483 pr_info("%3d: %s\n", i, test_description(t, -1));
485 if (has_subtests(t)) {
486 int subn = num_subtests(t);
489 for (subi = 0; subi < subn; subi++)
490 pr_info("%3d:%1d: %s\n", i, subi + 1,
491 test_description(t, subi));
495 perf_test__list_shell(argc, argv, i);
500 static int run_workload(const char *work, int argc, const char **argv)
503 struct test_workload *twl;
505 for (i = 0; i < ARRAY_SIZE(workloads); i++) {
507 if (!strcmp(twl->name, work))
508 return twl->func(argc, argv);
511 pr_info("No workload found: %s\n", work);
515 int cmd_test(int argc, const char **argv)
517 const char *test_usage[] = {
518 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
521 const char *skip = NULL;
522 const char *workload = NULL;
523 const struct option test_options[] = {
524 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
525 OPT_INCR('v', "verbose", &verbose,
526 "be more verbose (show symbol address, etc)"),
527 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
528 "Do not fork for testcase"),
529 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),
530 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
533 const char * const test_subcommands[] = { "list", NULL };
534 struct intlist *skiplist = NULL;
535 int ret = hists__init();
540 /* Unbuffered output */
541 setvbuf(stdout, NULL, _IONBF, 0);
543 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
544 if (argc >= 1 && !strcmp(argv[0], "list"))
545 return perf_test__list(argc - 1, argv + 1);
548 return run_workload(workload, argc, argv);
550 symbol_conf.priv_size = sizeof(int);
551 symbol_conf.try_vmlinux_path = true;
553 if (symbol__init(NULL) < 0)
557 skiplist = intlist__new(skip);
559 * Tests that create BPF maps, for instance, need more than the 64K
562 rlimit__bump_memlock();
564 return __cmd_test(argc, argv, skiplist);