1 // SPDX-License-Identifier: GPL-2.0
5 * Builtin regression testing command: ever growing number of sanity tests
13 #include <sys/types.h>
24 #include <subcmd/parse-options.h>
25 #include <subcmd/run-command.h>
28 #include "util/rlimit.h"
29 #include "util/strbuf.h"
30 #include <linux/kernel.h>
31 #include <linux/string.h>
32 #include <subcmd/exec-cmd.h>
33 #include <linux/zalloc.h>
35 #include "tests-scripts.h"
38 * Command line option to not fork the test running in the same process and
39 * making them easier to debug.
41 static bool dont_fork;
42 /* Fork the tests in parallel and then wait for their completion. */
44 const char *dso_to_test;
45 const char *test_objdump_path = "objdump";
48 * List of architecture specific tests. Not a weak symbol as the array length is
49 * dependent on the initialization, as such GCC with LTO complains of
50 * conflicting definitions with a weak symbol.
52 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
53 extern struct test_suite *arch_tests[];
55 static struct test_suite *arch_tests[] = {
60 static struct test_suite *generic_tests[] = {
61 &suite__vmlinux_matches_kallsyms,
62 #ifdef HAVE_LIBTRACEEVENT
63 &suite__openat_syscall_event,
64 &suite__openat_syscall_event_on_all_cpus,
74 &suite__perf_evsel__roundtrip_name_test,
75 #ifdef HAVE_LIBTRACEEVENT
76 &suite__perf_evsel__tp_sched_test,
77 &suite__syscall_openat_tp_fields,
83 &suite__bp_signal_overflow,
84 &suite__bp_accounting,
87 &suite__sw_clock_freq,
89 &suite__sample_parsing,
90 &suite__keep_tracking,
91 &suite__parse_no_sample_id_all,
93 &suite__mmap_thread_lookup,
94 &suite__thread_maps_share,
96 &suite__hists_cumulate,
97 #ifdef HAVE_LIBTRACEEVENT
98 &suite__switch_tracking,
100 &suite__fdarray__filter,
101 &suite__fdarray__add,
102 &suite__kmod_path__parse,
104 &suite__session_topology,
105 &suite__thread_map_synthesize,
106 &suite__thread_map_remove,
108 &suite__synthesize_stat_config,
109 &suite__synthesize_stat,
110 &suite__synthesize_stat_round,
111 &suite__event_update,
113 &suite__backward_ring_buffer,
115 &suite__is_printable_array,
116 &suite__bitmap_print,
118 &suite__unit_number__scnprint,
121 &suite__jit_write_elf,
124 &suite__maps__merge_in,
125 &suite__demangle_java,
126 &suite__demangle_ocaml,
127 &suite__parse_metric,
128 &suite__pe_file_parsing,
129 &suite__expand_cgroup_events,
130 &suite__perf_time_to_tsc,
133 &suite__event_groups,
139 static struct test_suite **tests[] = {
142 NULL, /* shell tests created at runtime. */
145 static struct test_workload *workloads[] = {
154 static int num_subtests(const struct test_suite *t)
162 while (t->test_cases[num].name)
168 static bool has_subtests(const struct test_suite *t)
170 return num_subtests(t) > 1;
173 static const char *skip_reason(const struct test_suite *t, int subtest)
178 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;
181 static const char *test_description(const struct test_suite *t, int subtest)
183 if (t->test_cases && subtest >= 0)
184 return t->test_cases[subtest].desc;
189 static test_fnptr test_function(const struct test_suite *t, int subtest)
192 return t->test_cases[0].run_case;
194 return t->test_cases[subtest].run_case;
197 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
204 for (i = 0; i < argc; ++i) {
206 long nr = strtoul(argv[i], &end, 10);
214 if (strcasestr(desc, argv[i]))
222 struct child_process process;
223 struct test_suite *test;
228 static int run_test_child(struct child_process *process)
230 struct child_test *child = container_of(process, struct child_test, process);
233 pr_debug("--- start ---\n");
234 pr_debug("test child forked, pid %d\n", getpid());
235 err = test_function(child->test, child->subtest)(child->test, child->subtest);
236 pr_debug("---- end(%d) ----\n", err);
241 static int print_test_result(struct test_suite *t, int i, int subtest, int result, int width)
243 if (has_subtests(t)) {
244 int subw = width > 2 ? width - 2 : width;
246 pr_info("%3d.%1d: %-*s:", i + 1, subtest + 1, subw, test_description(t, subtest));
248 pr_info("%3d: %-*s:", i + 1, width, test_description(t, subtest));
255 const char *reason = skip_reason(t, subtest);
258 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
260 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
265 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
272 static int finish_test(struct child_test *child_test, int width)
274 struct test_suite *t = child_test->test;
275 int i = child_test->test_num;
276 int subi = child_test->subtest;
277 int out = child_test->process.out;
278 int err = child_test->process.err;
279 bool out_done = out <= 0;
280 bool err_done = err <= 0;
281 struct strbuf out_output = STRBUF_INIT;
282 struct strbuf err_output = STRBUF_INIT;
286 * For test suites with subtests, display the suite name ahead of the
289 if (has_subtests(t) && subi == 0)
290 pr_info("%3d: %-*s:\n", i + 1, width, test_description(t, -1));
293 * Busy loop reading from the child's stdout and stderr that are set to
294 * be non-blocking until EOF.
297 fcntl(out, F_SETFL, O_NONBLOCK);
299 fcntl(err, F_SETFL, O_NONBLOCK);
302 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));
304 pr_info("%3d: %s:\n", i + 1, test_description(t, -1));
306 while (!out_done || !err_done) {
307 struct pollfd pfds[2] = {
309 .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,
312 .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,
318 /* Poll to avoid excessive spinning, timeout set for 1000ms. */
319 poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/1000);
320 if (!out_done && pfds[0].revents) {
322 len = read(out, buf, sizeof(buf) - 1);
325 out_done = errno != EAGAIN;
329 fprintf(stdout, "%s", buf);
331 strbuf_addstr(&out_output, buf);
334 if (!err_done && pfds[1].revents) {
336 len = read(err, buf, sizeof(buf) - 1);
339 err_done = errno != EAGAIN;
343 fprintf(stdout, "%s", buf);
345 strbuf_addstr(&err_output, buf);
349 /* Clean up child process. */
350 ret = finish_command(&child_test->process);
351 if (verbose == 1 && ret == TEST_FAIL) {
352 /* Add header for test that was skipped above. */
354 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));
356 pr_info("%3d: %s:\n", i + 1, test_description(t, -1));
357 fprintf(stdout, "%s", out_output.buf);
358 fprintf(stderr, "%s", err_output.buf);
360 strbuf_release(&out_output);
361 strbuf_release(&err_output);
362 print_test_result(t, i, subi, ret, width);
370 static int start_test(struct test_suite *test, int i, int subi, struct child_test **child,
377 pr_debug("--- start ---\n");
378 err = test_function(test, subi)(test, subi);
379 pr_debug("---- end ----\n");
380 print_test_result(test, i, subi, err, width);
384 *child = zalloc(sizeof(**child));
388 (*child)->test = test;
389 (*child)->test_num = i;
390 (*child)->subtest = subi;
391 (*child)->process.pid = -1;
392 (*child)->process.no_stdin = 1;
394 (*child)->process.no_stdout = 1;
395 (*child)->process.no_stderr = 1;
397 (*child)->process.out = -1;
398 (*child)->process.err = -1;
400 (*child)->process.no_exec_cmd = run_test_child;
401 err = start_command(&(*child)->process);
404 return finish_test(*child, width);
407 #define for_each_test(j, k, t) \
408 for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \
409 while ((t = tests[j][k++]) != NULL)
411 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
413 struct test_suite *t;
417 size_t num_tests = 0;
418 struct child_test **child_tests;
419 int child_test_num = 0;
421 for_each_test(j, k, t) {
422 int len = strlen(test_description(t, -1));
427 if (has_subtests(t)) {
428 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {
429 len = strlen(test_description(t, subi));
438 child_tests = calloc(num_tests, sizeof(*child_tests));
442 for_each_test(j, k, t) {
445 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
448 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {
449 if (perf_test__matches(test_description(t, subi),
458 if (intlist__find(skiplist, i)) {
459 pr_info("%3d: %-*s:", curr + 1, width, test_description(t, -1));
460 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
464 if (!has_subtests(t)) {
465 int err = start_test(t, curr, -1, &child_tests[child_test_num++], width);
468 /* TODO: if parallel waitpid the already forked children. */
473 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {
476 if (!perf_test__matches(test_description(t, subi),
480 err = start_test(t, curr, subi, &child_tests[child_test_num++],
487 for (i = 0; i < child_test_num; i++) {
489 int ret = finish_test(child_tests[i], width);
494 free(child_tests[i]);
500 static int perf_test__list(int argc, const char **argv)
503 struct test_suite *t;
506 for_each_test(j, k, t) {
509 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
512 pr_info("%3d: %s\n", i, test_description(t, -1));
514 if (has_subtests(t)) {
515 int subn = num_subtests(t);
518 for (subi = 0; subi < subn; subi++)
519 pr_info("%3d:%1d: %s\n", i, subi + 1,
520 test_description(t, subi));
526 static int run_workload(const char *work, int argc, const char **argv)
529 struct test_workload *twl;
531 for (i = 0; i < ARRAY_SIZE(workloads); i++) {
533 if (!strcmp(twl->name, work))
534 return twl->func(argc, argv);
537 pr_info("No workload found: %s\n", work);
541 static int perf_test__config(const char *var, const char *value,
542 void *data __maybe_unused)
544 if (!strcmp(var, "annotate.objdump"))
545 test_objdump_path = value;
550 int cmd_test(int argc, const char **argv)
552 const char *test_usage[] = {
553 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
556 const char *skip = NULL;
557 const char *workload = NULL;
558 const struct option test_options[] = {
559 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
560 OPT_INCR('v', "verbose", &verbose,
561 "be more verbose (show symbol address, etc)"),
562 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
563 "Do not fork for testcase"),
564 OPT_BOOLEAN('p', "parallel", ¶llel,
565 "Run the tests altogether in parallel"),
566 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),
567 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
568 OPT_STRING(0, "objdump", &test_objdump_path, "path",
569 "objdump binary to use for disassembly and annotations"),
572 const char * const test_subcommands[] = { "list", NULL };
573 struct intlist *skiplist = NULL;
574 int ret = hists__init();
579 perf_config(perf_test__config, NULL);
581 /* Unbuffered output */
582 setvbuf(stdout, NULL, _IONBF, 0);
584 tests[2] = create_script_test_suites();
585 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
586 if (argc >= 1 && !strcmp(argv[0], "list"))
587 return perf_test__list(argc - 1, argv + 1);
590 return run_workload(workload, argc, argv);
592 symbol_conf.priv_size = sizeof(int);
593 symbol_conf.try_vmlinux_path = true;
595 if (symbol__init(NULL) < 0)
599 skiplist = intlist__new(skip);
601 * Tests that create BPF maps, for instance, need more than the 64K
604 rlimit__bump_memlock();
606 return __cmd_test(argc, argv, skiplist);