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 static bool dont_fork;
33 struct test_suite *__weak arch_tests[] = {
37 static struct test_suite *generic_tests[] = {
38 &suite__vmlinux_matches_kallsyms,
39 &suite__openat_syscall_event,
40 &suite__openat_syscall_event_on_all_cpus,
49 &suite__dso_data_cache,
50 &suite__dso_data_reopen,
51 &suite__perf_evsel__roundtrip_name_test,
52 &suite__perf_evsel__tp_sched_test,
53 &suite__syscall_openat_tp_fields,
58 &suite__bp_signal_overflow,
59 &suite__bp_accounting,
62 &suite__sw_clock_freq,
64 &suite__sample_parsing,
65 &suite__keep_tracking,
66 &suite__parse_no_sample_id_all,
68 &suite__mmap_thread_lookup,
69 &suite__thread_maps_share,
71 &suite__hists_cumulate,
72 &suite__switch_tracking,
73 &suite__fdarray__filter,
75 &suite__kmod_path__parse,
78 &suite__session_topology,
80 &suite__thread_map_synthesize,
81 &suite__thread_map_remove,
82 &suite__cpu_map_synthesize,
83 &suite__synthesize_stat_config,
84 &suite__synthesize_stat,
85 &suite__synthesize_stat_round,
88 &suite__backward_ring_buffer,
89 &suite__cpu_map_print,
90 &suite__cpu_map_merge,
92 &suite__is_printable_array,
96 &suite__unit_number__scnprint,
99 &suite__jit_write_elf,
102 &suite__maps__merge_in,
103 &suite__demangle_java,
104 &suite__demangle_ocaml,
105 &suite__parse_metric,
106 &suite__pe_file_parsing,
107 &suite__expand_cgroup_events,
108 &suite__perf_time_to_tsc,
114 static struct test_suite **tests[] = {
119 static int num_subtests(const struct test_suite *t)
127 while (t->test_cases[num].name)
133 static bool has_subtests(const struct test_suite *t)
135 return num_subtests(t) > 1;
138 static const char *skip_reason(const struct test_suite *t, int subtest)
143 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;
146 static const char *test_description(const struct test_suite *t, int subtest)
148 if (t->test_cases && subtest >= 0)
149 return t->test_cases[subtest].desc;
154 static test_fnptr test_function(const struct test_suite *t, int subtest)
157 return t->test_cases[0].run_case;
159 return t->test_cases[subtest].run_case;
162 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
169 for (i = 0; i < argc; ++i) {
171 long nr = strtoul(argv[i], &end, 10);
179 if (strcasestr(desc, argv[i]))
186 static int run_test(struct test_suite *test, int subtest)
188 int status, err = -1, child = dont_fork ? 0 : fork();
189 char sbuf[STRERR_BUFSIZE];
192 pr_err("failed to fork test: %s\n",
193 str_error_r(errno, sbuf, sizeof(sbuf)));
199 pr_debug("test child forked, pid %d\n", getpid());
202 int nullfd = open("/dev/null", O_WRONLY);
205 close(STDERR_FILENO);
206 close(STDOUT_FILENO);
208 dup2(nullfd, STDOUT_FILENO);
209 dup2(STDOUT_FILENO, STDERR_FILENO);
213 signal(SIGSEGV, sighandler_dump_stack);
214 signal(SIGFPE, sighandler_dump_stack);
218 err = test_function(test, subtest)(test, subtest);
226 if (WIFEXITED(status)) {
227 err = (signed char)WEXITSTATUS(status);
228 pr_debug("test child finished with %d\n", err);
229 } else if (WIFSIGNALED(status)) {
231 pr_debug("test child interrupted\n");
238 #define for_each_test(j, k, t) \
239 for (j = 0; j < ARRAY_SIZE(tests); j++) \
240 for (k = 0, t = tests[j][k]; tests[j][k]; k++, t = tests[j][k])
242 static int test_and_print(struct test_suite *t, int subtest)
246 pr_debug("\n--- start ---\n");
247 err = run_test(t, subtest);
248 pr_debug("---- end ----\n");
250 if (!has_subtests(t))
251 pr_debug("%s:", t->desc);
253 pr_debug("%s subtest %d:", t->desc, subtest + 1);
260 const char *reason = skip_reason(t, subtest);
263 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
265 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
270 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
277 static const char *shell_test__description(char *description, size_t size,
278 const char *path, const char *name)
281 char filename[PATH_MAX];
284 path__join(filename, sizeof(filename), path, name);
285 fp = fopen(filename, "r");
292 } while (ch != EOF && ch != '\n');
294 description = fgets(description, size, fp);
297 return description ? strim(description + 1) : NULL;
300 #define for_each_shell_test(entlist, nr, base, ent) \
301 for (int __i = 0; __i < nr && (ent = entlist[__i]); __i++) \
302 if (!is_directory(base, ent) && \
303 is_executable_file(base, ent) && \
304 ent->d_name[0] != '.')
306 static const char *shell_tests__dir(char *path, size_t size)
308 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
312 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
314 if (!lstat(devel_dirs[i], &st)) {
315 scnprintf(path, size, "%s/shell", devel_dirs[i]);
316 if (!lstat(devel_dirs[i], &st))
321 /* Then installed path. */
322 exec_path = get_argv_exec_path();
323 scnprintf(path, size, "%s/tests/shell", exec_path);
328 static int shell_tests__max_desc_width(void)
330 struct dirent **entlist;
333 char path_dir[PATH_MAX];
334 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
340 n_dirs = scandir(path, &entlist, NULL, alphasort);
344 for_each_shell_test(entlist, n_dirs, path, ent) {
346 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
349 int len = strlen(desc);
356 for (e = 0; e < n_dirs; e++)
367 static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
370 char script[PATH_MAX];
371 struct shell_test *st = test->priv;
373 path__join(script, sizeof(script) - 3, st->dir, st->file);
376 strncat(script, " -v", sizeof(script) - strlen(script) - 1);
378 err = system(script);
382 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
385 static int run_shell_tests(int argc, const char *argv[], int i, int width,
386 struct intlist *skiplist)
388 struct dirent **entlist;
391 char path_dir[PATH_MAX];
392 struct shell_test st = {
393 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
399 n_dirs = scandir(st.dir, &entlist, NULL, alphasort);
401 pr_err("failed to open shell test directory: %s\n",
406 for_each_shell_test(entlist, n_dirs, st.dir, ent) {
409 struct test_case test_cases[] = {
411 .desc = shell_test__description(desc,
415 .run_case = shell_test__run,
419 struct test_suite test_suite = {
420 .desc = test_cases[0].desc,
421 .test_cases = test_cases,
425 if (test_suite.desc == NULL ||
426 !perf_test__matches(test_suite.desc, curr, argc, argv))
429 st.file = ent->d_name;
430 pr_info("%3d: %-*s:", i, width, test_suite.desc);
432 if (intlist__find(skiplist, i)) {
433 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
437 test_and_print(&test_suite, 0);
440 for (e = 0; e < n_dirs; e++)
446 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
448 struct test_suite *t;
451 int width = shell_tests__max_desc_width();
453 for_each_test(j, k, t) {
454 int len = strlen(test_description(t, -1));
460 for_each_test(j, k, t) {
464 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
468 subn = num_subtests(t);
470 for (subi = 0; subi < subn; subi++) {
471 if (perf_test__matches(test_description(t, subi),
480 pr_info("%3d: %-*s:", i, width, test_description(t, -1));
482 if (intlist__find(skiplist, i)) {
483 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
487 if (!has_subtests(t)) {
488 test_and_print(t, -1);
490 int subn = num_subtests(t);
492 * minus 2 to align with normal testcases.
493 * For subtest we print additional '.x' in number.
496 * 35: Test LLVM searching and compiling :
497 * 35.1: Basic BPF llvm compiling test : Ok
499 int subw = width > 2 ? width - 2 : width;
502 color_fprintf(stderr, PERF_COLOR_YELLOW,
503 " Skip (not compiled in)\n");
508 for (subi = 0; subi < subn; subi++) {
509 int len = strlen(test_description(t, subi));
515 for (subi = 0; subi < subn; subi++) {
516 if (!perf_test__matches(test_description(t, subi),
520 pr_info("%3d.%1d: %-*s:", i, subi + 1, subw,
521 test_description(t, subi));
522 test_and_print(t, subi);
527 return run_shell_tests(argc, argv, i, width, skiplist);
530 static int perf_test__list_shell(int argc, const char **argv, int i)
532 struct dirent **entlist;
535 char path_dir[PATH_MAX];
536 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
541 n_dirs = scandir(path, &entlist, NULL, alphasort);
545 for_each_shell_test(entlist, n_dirs, path, ent) {
548 struct test_suite t = {
549 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
552 if (!perf_test__matches(t.desc, curr, argc, argv))
555 pr_info("%3d: %s\n", i, t.desc);
559 for (e = 0; e < n_dirs; e++)
565 static int perf_test__list(int argc, const char **argv)
568 struct test_suite *t;
571 for_each_test(j, k, t) {
574 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
577 pr_info("%3d: %s\n", i, test_description(t, -1));
579 if (has_subtests(t)) {
580 int subn = num_subtests(t);
583 for (subi = 0; subi < subn; subi++)
584 pr_info("%3d:%1d: %s\n", i, subi + 1,
585 test_description(t, subi));
589 perf_test__list_shell(argc, argv, i);
594 int cmd_test(int argc, const char **argv)
596 const char *test_usage[] = {
597 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
600 const char *skip = NULL;
601 const struct option test_options[] = {
602 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
603 OPT_INCR('v', "verbose", &verbose,
604 "be more verbose (show symbol address, etc)"),
605 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
606 "Do not fork for testcase"),
609 const char * const test_subcommands[] = { "list", NULL };
610 struct intlist *skiplist = NULL;
611 int ret = hists__init();
616 /* Unbuffered output */
617 setvbuf(stdout, NULL, _IONBF, 0);
619 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
620 if (argc >= 1 && !strcmp(argv[0], "list"))
621 return perf_test__list(argc - 1, argv + 1);
623 symbol_conf.priv_size = sizeof(int);
624 symbol_conf.sort_by_name = true;
625 symbol_conf.try_vmlinux_path = true;
627 if (symbol__init(NULL) < 0)
631 skiplist = intlist__new(skip);
633 * Tests that create BPF maps, for instance, need more than the 64K
636 rlimit__bump_memlock();
638 return __cmd_test(argc, argv, skiplist);