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;
36 struct test_suite *__weak arch_tests[] = {
40 static struct test_suite *generic_tests[] = {
41 &suite__vmlinux_matches_kallsyms,
42 #ifdef HAVE_LIBTRACEEVENT
43 &suite__openat_syscall_event,
44 &suite__openat_syscall_event_on_all_cpus,
54 &suite__dso_data_cache,
55 &suite__dso_data_reopen,
56 &suite__perf_evsel__roundtrip_name_test,
57 #ifdef HAVE_LIBTRACEEVENT
58 &suite__perf_evsel__tp_sched_test,
59 &suite__syscall_openat_tp_fields,
65 &suite__bp_signal_overflow,
66 &suite__bp_accounting,
69 &suite__sw_clock_freq,
71 &suite__sample_parsing,
72 &suite__keep_tracking,
73 &suite__parse_no_sample_id_all,
75 &suite__mmap_thread_lookup,
76 &suite__thread_maps_share,
78 &suite__hists_cumulate,
79 #ifdef HAVE_LIBTRACEEVENT
80 &suite__switch_tracking,
82 &suite__fdarray__filter,
84 &suite__kmod_path__parse,
87 &suite__session_topology,
89 &suite__thread_map_synthesize,
90 &suite__thread_map_remove,
92 &suite__synthesize_stat_config,
93 &suite__synthesize_stat,
94 &suite__synthesize_stat_round,
97 &suite__backward_ring_buffer,
99 &suite__is_printable_array,
100 &suite__bitmap_print,
103 &suite__unit_number__scnprint,
106 &suite__jit_write_elf,
109 &suite__maps__merge_in,
110 &suite__demangle_java,
111 &suite__demangle_ocaml,
112 &suite__parse_metric,
113 &suite__pe_file_parsing,
114 &suite__expand_cgroup_events,
115 &suite__perf_time_to_tsc,
118 &suite__event_groups,
123 static struct test_suite **tests[] = {
128 static struct test_workload *workloads[] = {
137 static int num_subtests(const struct test_suite *t)
145 while (t->test_cases[num].name)
151 static bool has_subtests(const struct test_suite *t)
153 return num_subtests(t) > 1;
156 static const char *skip_reason(const struct test_suite *t, int subtest)
161 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;
164 static const char *test_description(const struct test_suite *t, int subtest)
166 if (t->test_cases && subtest >= 0)
167 return t->test_cases[subtest].desc;
172 static test_fnptr test_function(const struct test_suite *t, int subtest)
175 return t->test_cases[0].run_case;
177 return t->test_cases[subtest].run_case;
180 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
187 for (i = 0; i < argc; ++i) {
189 long nr = strtoul(argv[i], &end, 10);
197 if (strcasestr(desc, argv[i]))
204 static int run_test(struct test_suite *test, int subtest)
206 int status, err = -1, child = dont_fork ? 0 : fork();
207 char sbuf[STRERR_BUFSIZE];
210 pr_err("failed to fork test: %s\n",
211 str_error_r(errno, sbuf, sizeof(sbuf)));
217 pr_debug("test child forked, pid %d\n", getpid());
220 int nullfd = open("/dev/null", O_WRONLY);
223 close(STDERR_FILENO);
224 close(STDOUT_FILENO);
226 dup2(nullfd, STDOUT_FILENO);
227 dup2(STDOUT_FILENO, STDERR_FILENO);
231 signal(SIGSEGV, sighandler_dump_stack);
232 signal(SIGFPE, sighandler_dump_stack);
236 err = test_function(test, subtest)(test, subtest);
244 if (WIFEXITED(status)) {
245 err = (signed char)WEXITSTATUS(status);
246 pr_debug("test child finished with %d\n", err);
247 } else if (WIFSIGNALED(status)) {
249 pr_debug("test child interrupted\n");
256 #define for_each_test(j, k, t) \
257 for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \
258 while ((t = tests[j][k++]) != NULL)
260 static int test_and_print(struct test_suite *t, int subtest)
264 pr_debug("\n--- start ---\n");
265 err = run_test(t, subtest);
266 pr_debug("---- end ----\n");
268 if (!has_subtests(t))
269 pr_debug("%s:", t->desc);
271 pr_debug("%s subtest %d:", t->desc, subtest + 1);
278 const char *reason = skip_reason(t, subtest);
281 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
283 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
288 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
300 static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
303 char script[PATH_MAX];
304 struct shell_test *st = test->priv;
306 path__join(script, sizeof(script) - 3, st->dir, st->file);
309 strncat(script, " -v", sizeof(script) - strlen(script) - 1);
311 err = system(script);
315 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
318 static int run_shell_tests(int argc, const char *argv[], int i, int width,
319 struct intlist *skiplist)
321 struct shell_test st;
322 const struct script_file *files, *file;
324 files = list_script_files();
327 for (file = files; file->dir; file++) {
329 struct test_case test_cases[] = {
332 .run_case = shell_test__run,
336 struct test_suite test_suite = {
337 .desc = test_cases[0].desc,
338 .test_cases = test_cases,
343 if (test_suite.desc == NULL ||
344 !perf_test__matches(test_suite.desc, curr, argc, argv))
347 st.file = file->file;
348 pr_info("%3d: %-*s:", i, width, test_suite.desc);
350 if (intlist__find(skiplist, i)) {
351 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
355 test_and_print(&test_suite, 0);
360 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
362 struct test_suite *t;
365 int width = list_script_max_width();
367 for_each_test(j, k, t) {
368 int len = strlen(test_description(t, -1));
374 for_each_test(j, k, t) {
378 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
382 subn = num_subtests(t);
384 for (subi = 0; subi < subn; subi++) {
385 if (perf_test__matches(test_description(t, subi),
394 pr_info("%3d: %-*s:", i, width, test_description(t, -1));
396 if (intlist__find(skiplist, i)) {
397 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
401 if (!has_subtests(t)) {
402 test_and_print(t, -1);
404 int subn = num_subtests(t);
406 * minus 2 to align with normal testcases.
407 * For subtest we print additional '.x' in number.
410 * 35: Test LLVM searching and compiling :
411 * 35.1: Basic BPF llvm compiling test : Ok
413 int subw = width > 2 ? width - 2 : width;
416 color_fprintf(stderr, PERF_COLOR_YELLOW,
417 " Skip (not compiled in)\n");
422 for (subi = 0; subi < subn; subi++) {
423 int len = strlen(test_description(t, subi));
429 for (subi = 0; subi < subn; subi++) {
430 if (!perf_test__matches(test_description(t, subi),
434 pr_info("%3d.%1d: %-*s:", i, subi + 1, subw,
435 test_description(t, subi));
436 test_and_print(t, subi);
441 return run_shell_tests(argc, argv, i, width, skiplist);
444 static int perf_test__list_shell(int argc, const char **argv, int i)
446 const struct script_file *files, *file;
448 files = list_script_files();
451 for (file = files; file->dir; file++) {
453 struct test_suite t = {
457 if (!perf_test__matches(t.desc, curr, argc, argv))
460 pr_info("%3d: %s\n", i, t.desc);
465 static int perf_test__list(int argc, const char **argv)
468 struct test_suite *t;
471 for_each_test(j, k, t) {
474 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
477 pr_info("%3d: %s\n", i, test_description(t, -1));
479 if (has_subtests(t)) {
480 int subn = num_subtests(t);
483 for (subi = 0; subi < subn; subi++)
484 pr_info("%3d:%1d: %s\n", i, subi + 1,
485 test_description(t, subi));
489 perf_test__list_shell(argc, argv, i);
494 static int run_workload(const char *work, int argc, const char **argv)
497 struct test_workload *twl;
499 for (i = 0; i < ARRAY_SIZE(workloads); i++) {
501 if (!strcmp(twl->name, work))
502 return twl->func(argc, argv);
505 pr_info("No workload found: %s\n", work);
509 int cmd_test(int argc, const char **argv)
511 const char *test_usage[] = {
512 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
515 const char *skip = NULL;
516 const char *workload = NULL;
517 const struct option test_options[] = {
518 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
519 OPT_INCR('v', "verbose", &verbose,
520 "be more verbose (show symbol address, etc)"),
521 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
522 "Do not fork for testcase"),
523 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),
524 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
527 const char * const test_subcommands[] = { "list", NULL };
528 struct intlist *skiplist = NULL;
529 int ret = hists__init();
534 /* Unbuffered output */
535 setvbuf(stdout, NULL, _IONBF, 0);
537 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
538 if (argc >= 1 && !strcmp(argv[0], "list"))
539 return perf_test__list(argc - 1, argv + 1);
542 return run_workload(workload, argc, argv);
544 symbol_conf.priv_size = sizeof(int);
545 symbol_conf.try_vmlinux_path = true;
547 if (symbol__init(NULL) < 0)
551 skiplist = intlist__new(skip);
553 * Tests that create BPF maps, for instance, need more than the 64K
556 rlimit__bump_memlock();
558 return __cmd_test(argc, argv, skiplist);