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,
96 &suite__session_topology,
98 &suite__thread_map_synthesize,
99 &suite__thread_map_remove,
101 &suite__synthesize_stat_config,
102 &suite__synthesize_stat,
103 &suite__synthesize_stat_round,
104 &suite__event_update,
106 &suite__backward_ring_buffer,
108 &suite__is_printable_array,
109 &suite__bitmap_print,
112 &suite__unit_number__scnprint,
115 &suite__jit_write_elf,
118 &suite__maps__merge_in,
119 &suite__demangle_java,
120 &suite__demangle_ocaml,
121 &suite__parse_metric,
122 &suite__pe_file_parsing,
123 &suite__expand_cgroup_events,
124 &suite__perf_time_to_tsc,
127 &suite__event_groups,
132 static struct test_suite **tests[] = {
137 static struct test_workload *workloads[] = {
146 static int num_subtests(const struct test_suite *t)
154 while (t->test_cases[num].name)
160 static bool has_subtests(const struct test_suite *t)
162 return num_subtests(t) > 1;
165 static const char *skip_reason(const struct test_suite *t, int subtest)
170 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;
173 static const char *test_description(const struct test_suite *t, int subtest)
175 if (t->test_cases && subtest >= 0)
176 return t->test_cases[subtest].desc;
181 static test_fnptr test_function(const struct test_suite *t, int subtest)
184 return t->test_cases[0].run_case;
186 return t->test_cases[subtest].run_case;
189 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
196 for (i = 0; i < argc; ++i) {
198 long nr = strtoul(argv[i], &end, 10);
206 if (strcasestr(desc, argv[i]))
213 static int run_test(struct test_suite *test, int subtest)
215 int status, err = -1, child = dont_fork ? 0 : fork();
216 char sbuf[STRERR_BUFSIZE];
219 pr_err("failed to fork test: %s\n",
220 str_error_r(errno, sbuf, sizeof(sbuf)));
226 pr_debug("test child forked, pid %d\n", getpid());
229 int nullfd = open("/dev/null", O_WRONLY);
232 close(STDERR_FILENO);
233 close(STDOUT_FILENO);
235 dup2(nullfd, STDOUT_FILENO);
236 dup2(STDOUT_FILENO, STDERR_FILENO);
240 signal(SIGSEGV, sighandler_dump_stack);
241 signal(SIGFPE, sighandler_dump_stack);
245 err = test_function(test, subtest)(test, subtest);
253 if (WIFEXITED(status)) {
254 err = (signed char)WEXITSTATUS(status);
255 pr_debug("test child finished with %d\n", err);
256 } else if (WIFSIGNALED(status)) {
258 pr_debug("test child interrupted\n");
265 #define for_each_test(j, k, t) \
266 for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \
267 while ((t = tests[j][k++]) != NULL)
269 static int test_and_print(struct test_suite *t, int subtest)
273 pr_debug("\n--- start ---\n");
274 err = run_test(t, subtest);
275 pr_debug("---- end ----\n");
277 if (!has_subtests(t))
278 pr_debug("%s:", t->desc);
280 pr_debug("%s subtest %d:", t->desc, subtest + 1);
287 const char *reason = skip_reason(t, subtest);
290 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
292 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
297 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
309 static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
312 char script[PATH_MAX];
313 struct shell_test *st = test->priv;
315 path__join(script, sizeof(script) - 3, st->dir, st->file);
318 strncat(script, " -v", sizeof(script) - strlen(script) - 1);
320 err = system(script);
324 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
327 static int run_shell_tests(int argc, const char *argv[], int i, int width,
328 struct intlist *skiplist)
330 struct shell_test st;
331 const struct script_file *files, *file;
333 files = list_script_files();
336 for (file = files; file->dir; file++) {
338 struct test_case test_cases[] = {
341 .run_case = shell_test__run,
345 struct test_suite test_suite = {
346 .desc = test_cases[0].desc,
347 .test_cases = test_cases,
352 if (test_suite.desc == NULL ||
353 !perf_test__matches(test_suite.desc, curr, argc, argv))
356 st.file = file->file;
357 pr_info("%3d: %-*s:", i, width, test_suite.desc);
359 if (intlist__find(skiplist, i)) {
360 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
364 test_and_print(&test_suite, 0);
369 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
371 struct test_suite *t;
374 int width = list_script_max_width();
376 for_each_test(j, k, t) {
377 int len = strlen(test_description(t, -1));
383 for_each_test(j, k, t) {
387 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
391 subn = num_subtests(t);
393 for (subi = 0; subi < subn; subi++) {
394 if (perf_test__matches(test_description(t, subi),
403 pr_info("%3d: %-*s:", i, width, test_description(t, -1));
405 if (intlist__find(skiplist, i)) {
406 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
410 if (!has_subtests(t)) {
411 test_and_print(t, -1);
413 int subn = num_subtests(t);
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;
425 color_fprintf(stderr, PERF_COLOR_YELLOW,
426 " Skip (not compiled in)\n");
431 for (subi = 0; subi < subn; subi++) {
432 int len = strlen(test_description(t, subi));
438 for (subi = 0; subi < subn; subi++) {
439 if (!perf_test__matches(test_description(t, subi),
443 pr_info("%3d.%1d: %-*s:", i, subi + 1, subw,
444 test_description(t, subi));
445 test_and_print(t, subi);
450 return run_shell_tests(argc, argv, i, width, skiplist);
453 static int perf_test__list_shell(int argc, const char **argv, int i)
455 const struct script_file *files, *file;
457 files = list_script_files();
460 for (file = files; file->dir; file++) {
462 struct test_suite t = {
466 if (!perf_test__matches(t.desc, curr, argc, argv))
469 pr_info("%3d: %s\n", i, t.desc);
474 static int perf_test__list(int argc, const char **argv)
477 struct test_suite *t;
480 for_each_test(j, k, t) {
483 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
486 pr_info("%3d: %s\n", i, test_description(t, -1));
488 if (has_subtests(t)) {
489 int subn = num_subtests(t);
492 for (subi = 0; subi < subn; subi++)
493 pr_info("%3d:%1d: %s\n", i, subi + 1,
494 test_description(t, subi));
498 perf_test__list_shell(argc, argv, i);
503 static int run_workload(const char *work, int argc, const char **argv)
506 struct test_workload *twl;
508 for (i = 0; i < ARRAY_SIZE(workloads); i++) {
510 if (!strcmp(twl->name, work))
511 return twl->func(argc, argv);
514 pr_info("No workload found: %s\n", work);
518 int cmd_test(int argc, const char **argv)
520 const char *test_usage[] = {
521 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
524 const char *skip = NULL;
525 const char *workload = NULL;
526 const struct option test_options[] = {
527 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
528 OPT_INCR('v', "verbose", &verbose,
529 "be more verbose (show symbol address, etc)"),
530 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
531 "Do not fork for testcase"),
532 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),
533 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
536 const char * const test_subcommands[] = { "list", NULL };
537 struct intlist *skiplist = NULL;
538 int ret = hists__init();
543 /* Unbuffered output */
544 setvbuf(stdout, NULL, _IONBF, 0);
546 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
547 if (argc >= 1 && !strcmp(argv[0], "list"))
548 return perf_test__list(argc - 1, argv + 1);
551 return run_workload(workload, argc, argv);
553 symbol_conf.priv_size = sizeof(int);
554 symbol_conf.try_vmlinux_path = true;
556 if (symbol__init(NULL) < 0)
560 skiplist = intlist__new(skip);
562 * Tests that create BPF maps, for instance, need more than the 64K
565 rlimit__bump_memlock();
567 return __cmd_test(argc, argv, skiplist);