593 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * builtin-test.c4 *5 * Builtin regression testing command: ever growing number of sanity tests6 */7#include <fcntl.h>8#include <errno.h>9#include <poll.h>10#include <unistd.h>11#include <string.h>12#include <stdlib.h>13#include <sys/types.h>14#include <dirent.h>15#include <sys/wait.h>16#include <sys/stat.h>17#include "builtin.h"18#include "config.h"19#include "hist.h"20#include "intlist.h"21#include "tests.h"22#include "debug.h"23#include "color.h"24#include <subcmd/parse-options.h>25#include <subcmd/run-command.h>26#include "string2.h"27#include "symbol.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>34 35#include "tests-scripts.h"36 37/*38 * Command line option to not fork the test running in the same process and39 * making them easier to debug.40 */41static bool dont_fork;42/* Don't fork the tests in parallel and wait for their completion. */43static bool sequential = true;44/* Do it in parallel, lacks infrastructure to avoid running tests that clash for resources,45 * So leave it as the developers choice to enable while working on the needed infra */46static bool parallel;47const char *dso_to_test;48const char *test_objdump_path = "objdump";49 50/*51 * List of architecture specific tests. Not a weak symbol as the array length is52 * dependent on the initialization, as such GCC with LTO complains of53 * conflicting definitions with a weak symbol.54 */55#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)56extern struct test_suite *arch_tests[];57#else58static struct test_suite *arch_tests[] = {59 NULL,60};61#endif62 63static struct test_suite *generic_tests[] = {64 &suite__vmlinux_matches_kallsyms,65#ifdef HAVE_LIBTRACEEVENT66 &suite__openat_syscall_event,67 &suite__openat_syscall_event_on_all_cpus,68 &suite__basic_mmap,69#endif70 &suite__mem,71 &suite__parse_events,72 &suite__expr,73 &suite__PERF_RECORD,74 &suite__pmu,75 &suite__pmu_events,76 &suite__dso_data,77 &suite__perf_evsel__roundtrip_name_test,78#ifdef HAVE_LIBTRACEEVENT79 &suite__perf_evsel__tp_sched_test,80 &suite__syscall_openat_tp_fields,81#endif82 &suite__attr,83 &suite__hists_link,84 &suite__python_use,85 &suite__bp_signal,86 &suite__bp_signal_overflow,87 &suite__bp_accounting,88 &suite__wp,89 &suite__task_exit,90 &suite__sw_clock_freq,91 &suite__code_reading,92 &suite__sample_parsing,93 &suite__keep_tracking,94 &suite__parse_no_sample_id_all,95 &suite__hists_filter,96 &suite__mmap_thread_lookup,97 &suite__thread_maps_share,98 &suite__hists_output,99 &suite__hists_cumulate,100#ifdef HAVE_LIBTRACEEVENT101 &suite__switch_tracking,102#endif103 &suite__fdarray__filter,104 &suite__fdarray__add,105 &suite__kmod_path__parse,106 &suite__thread_map,107 &suite__session_topology,108 &suite__thread_map_synthesize,109 &suite__thread_map_remove,110 &suite__cpu_map,111 &suite__synthesize_stat_config,112 &suite__synthesize_stat,113 &suite__synthesize_stat_round,114 &suite__event_update,115 &suite__event_times,116 &suite__backward_ring_buffer,117 &suite__sdt_event,118 &suite__is_printable_array,119 &suite__bitmap_print,120 &suite__perf_hooks,121 &suite__unit_number__scnprint,122 &suite__mem2node,123 &suite__time_utils,124 &suite__jit_write_elf,125 &suite__pfm,126 &suite__api_io,127 &suite__maps__merge_in,128 &suite__demangle_java,129 &suite__demangle_ocaml,130 &suite__parse_metric,131 &suite__pe_file_parsing,132 &suite__expand_cgroup_events,133 &suite__perf_time_to_tsc,134 &suite__dlfilter,135 &suite__sigtrap,136 &suite__event_groups,137 &suite__symbols,138 &suite__util,139 NULL,140};141 142static struct test_suite **tests[] = {143 generic_tests,144 arch_tests,145 NULL, /* shell tests created at runtime. */146};147 148static struct test_workload *workloads[] = {149 &workload__noploop,150 &workload__thloop,151 &workload__leafloop,152 &workload__sqrtloop,153 &workload__brstack,154 &workload__datasym,155 &workload__landlock,156};157 158static int num_subtests(const struct test_suite *t)159{160 int num;161 162 if (!t->test_cases)163 return 0;164 165 num = 0;166 while (t->test_cases[num].name)167 num++;168 169 return num;170}171 172static bool has_subtests(const struct test_suite *t)173{174 return num_subtests(t) > 1;175}176 177static const char *skip_reason(const struct test_suite *t, int subtest)178{179 if (!t->test_cases)180 return NULL;181 182 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;183}184 185static const char *test_description(const struct test_suite *t, int subtest)186{187 if (t->test_cases && subtest >= 0)188 return t->test_cases[subtest].desc;189 190 return t->desc;191}192 193static test_fnptr test_function(const struct test_suite *t, int subtest)194{195 if (subtest <= 0)196 return t->test_cases[0].run_case;197 198 return t->test_cases[subtest].run_case;199}200 201static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])202{203 int i;204 205 if (argc == 0)206 return true;207 208 for (i = 0; i < argc; ++i) {209 char *end;210 long nr = strtoul(argv[i], &end, 10);211 212 if (*end == '\0') {213 if (nr == curr + 1)214 return true;215 continue;216 }217 218 if (strcasestr(desc, argv[i]))219 return true;220 }221 222 return false;223}224 225struct child_test {226 struct child_process process;227 struct test_suite *test;228 int test_num;229 int subtest;230};231 232static int run_test_child(struct child_process *process)233{234 struct child_test *child = container_of(process, struct child_test, process);235 int err;236 237 pr_debug("--- start ---\n");238 pr_debug("test child forked, pid %d\n", getpid());239 err = test_function(child->test, child->subtest)(child->test, child->subtest);240 pr_debug("---- end(%d) ----\n", err);241 fflush(NULL);242 return -err;243}244 245static int print_test_result(struct test_suite *t, int i, int subtest, int result, int width)246{247 if (has_subtests(t)) {248 int subw = width > 2 ? width - 2 : width;249 250 pr_info("%3d.%1d: %-*s:", i + 1, subtest + 1, subw, test_description(t, subtest));251 } else252 pr_info("%3d: %-*s:", i + 1, width, test_description(t, subtest));253 254 switch (result) {255 case TEST_OK:256 pr_info(" Ok\n");257 break;258 case TEST_SKIP: {259 const char *reason = skip_reason(t, subtest);260 261 if (reason)262 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);263 else264 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");265 }266 break;267 case TEST_FAIL:268 default:269 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");270 break;271 }272 273 return 0;274}275 276static int finish_test(struct child_test *child_test, int width)277{278 struct test_suite *t = child_test->test;279 int i = child_test->test_num;280 int subi = child_test->subtest;281 int err = child_test->process.err;282 bool err_done = err <= 0;283 struct strbuf err_output = STRBUF_INIT;284 int ret;285 286 /*287 * For test suites with subtests, display the suite name ahead of the288 * sub test names.289 */290 if (has_subtests(t) && subi == 0)291 pr_info("%3d: %-*s:\n", i + 1, width, test_description(t, -1));292 293 /*294 * Busy loop reading from the child's stdout/stderr that are set to be295 * non-blocking until EOF.296 */297 if (!err_done)298 fcntl(err, F_SETFL, O_NONBLOCK);299 if (verbose > 1) {300 if (has_subtests(t))301 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));302 else303 pr_info("%3d: %s:\n", i + 1, test_description(t, -1));304 }305 while (!err_done) {306 struct pollfd pfds[1] = {307 { .fd = err,308 .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,309 },310 };311 char buf[512];312 ssize_t len;313 314 /* Poll to avoid excessive spinning, timeout set for 100ms. */315 poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/100);316 if (!err_done && pfds[0].revents) {317 errno = 0;318 len = read(err, buf, sizeof(buf) - 1);319 320 if (len <= 0) {321 err_done = errno != EAGAIN;322 } else {323 buf[len] = '\0';324 if (verbose > 1)325 fprintf(stdout, "%s", buf);326 else327 strbuf_addstr(&err_output, buf);328 }329 }330 }331 /* Clean up child process. */332 ret = finish_command(&child_test->process);333 if (verbose == 1 && ret == TEST_FAIL) {334 /* Add header for test that was skipped above. */335 if (has_subtests(t))336 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));337 else338 pr_info("%3d: %s:\n", i + 1, test_description(t, -1));339 fprintf(stderr, "%s", err_output.buf);340 }341 strbuf_release(&err_output);342 print_test_result(t, i, subi, ret, width);343 if (err > 0)344 close(err);345 return 0;346}347 348static int start_test(struct test_suite *test, int i, int subi, struct child_test **child,349 int width)350{351 int err;352 353 *child = NULL;354 if (dont_fork) {355 pr_debug("--- start ---\n");356 err = test_function(test, subi)(test, subi);357 pr_debug("---- end ----\n");358 print_test_result(test, i, subi, err, width);359 return 0;360 }361 362 *child = zalloc(sizeof(**child));363 if (!*child)364 return -ENOMEM;365 366 (*child)->test = test;367 (*child)->test_num = i;368 (*child)->subtest = subi;369 (*child)->process.pid = -1;370 (*child)->process.no_stdin = 1;371 if (verbose <= 0) {372 (*child)->process.no_stdout = 1;373 (*child)->process.no_stderr = 1;374 } else {375 (*child)->process.stdout_to_stderr = 1;376 (*child)->process.out = -1;377 (*child)->process.err = -1;378 }379 (*child)->process.no_exec_cmd = run_test_child;380 err = start_command(&(*child)->process);381 if (err || !sequential)382 return err;383 return finish_test(*child, width);384}385 386#define for_each_test(j, k, t) \387 for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \388 while ((t = tests[j][k++]) != NULL)389 390static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)391{392 struct test_suite *t;393 unsigned int j, k;394 int i = 0;395 int width = 0;396 size_t num_tests = 0;397 struct child_test **child_tests;398 int child_test_num = 0;399 400 for_each_test(j, k, t) {401 int len = strlen(test_description(t, -1));402 403 if (width < len)404 width = len;405 406 if (has_subtests(t)) {407 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {408 len = strlen(test_description(t, subi));409 if (width < len)410 width = len;411 num_tests++;412 }413 } else {414 num_tests++;415 }416 }417 child_tests = calloc(num_tests, sizeof(*child_tests));418 if (!child_tests)419 return -ENOMEM;420 421 for_each_test(j, k, t) {422 int curr = i++;423 424 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {425 bool skip = true;426 427 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {428 if (perf_test__matches(test_description(t, subi),429 curr, argc, argv))430 skip = false;431 }432 433 if (skip)434 continue;435 }436 437 if (intlist__find(skiplist, i)) {438 pr_info("%3d: %-*s:", curr + 1, width, test_description(t, -1));439 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");440 continue;441 }442 443 if (!has_subtests(t)) {444 int err = start_test(t, curr, -1, &child_tests[child_test_num++], width);445 446 if (err) {447 /* TODO: if !sequential waitpid the already forked children. */448 free(child_tests);449 return err;450 }451 } else {452 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {453 int err;454 455 if (!perf_test__matches(test_description(t, subi),456 curr, argc, argv))457 continue;458 459 err = start_test(t, curr, subi, &child_tests[child_test_num++],460 width);461 if (err)462 return err;463 }464 }465 }466 for (i = 0; i < child_test_num; i++) {467 if (!sequential) {468 int ret = finish_test(child_tests[i], width);469 470 if (ret)471 return ret;472 }473 free(child_tests[i]);474 }475 free(child_tests);476 return 0;477}478 479static int perf_test__list(int argc, const char **argv)480{481 unsigned int j, k;482 struct test_suite *t;483 int i = 0;484 485 for_each_test(j, k, t) {486 int curr = i++;487 488 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))489 continue;490 491 pr_info("%3d: %s\n", i, test_description(t, -1));492 493 if (has_subtests(t)) {494 int subn = num_subtests(t);495 int subi;496 497 for (subi = 0; subi < subn; subi++)498 pr_info("%3d:%1d: %s\n", i, subi + 1,499 test_description(t, subi));500 }501 }502 return 0;503}504 505static int run_workload(const char *work, int argc, const char **argv)506{507 unsigned int i = 0;508 struct test_workload *twl;509 510 for (i = 0; i < ARRAY_SIZE(workloads); i++) {511 twl = workloads[i];512 if (!strcmp(twl->name, work))513 return twl->func(argc, argv);514 }515 516 pr_info("No workload found: %s\n", work);517 return -1;518}519 520static int perf_test__config(const char *var, const char *value,521 void *data __maybe_unused)522{523 if (!strcmp(var, "annotate.objdump"))524 test_objdump_path = value;525 526 return 0;527}528 529int cmd_test(int argc, const char **argv)530{531 const char *test_usage[] = {532 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",533 NULL,534 };535 const char *skip = NULL;536 const char *workload = NULL;537 const struct option test_options[] = {538 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),539 OPT_INCR('v', "verbose", &verbose,540 "be more verbose (show symbol address, etc)"),541 OPT_BOOLEAN('F', "dont-fork", &dont_fork,542 "Do not fork for testcase"),543 OPT_BOOLEAN('p', "parallel", ¶llel, "Run the tests in parallel"),544 OPT_BOOLEAN('S', "sequential", &sequential,545 "Run the tests one after another rather than in parallel"),546 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),547 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),548 OPT_STRING(0, "objdump", &test_objdump_path, "path",549 "objdump binary to use for disassembly and annotations"),550 OPT_END()551 };552 const char * const test_subcommands[] = { "list", NULL };553 struct intlist *skiplist = NULL;554 int ret = hists__init();555 556 if (ret < 0)557 return ret;558 559 perf_config(perf_test__config, NULL);560 561 /* Unbuffered output */562 setvbuf(stdout, NULL, _IONBF, 0);563 564 tests[2] = create_script_test_suites();565 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);566 if (argc >= 1 && !strcmp(argv[0], "list"))567 return perf_test__list(argc - 1, argv + 1);568 569 if (workload)570 return run_workload(workload, argc, argv);571 572 if (dont_fork)573 sequential = true;574 else if (parallel)575 sequential = false;576 577 symbol_conf.priv_size = sizeof(int);578 symbol_conf.try_vmlinux_path = true;579 580 if (symbol__init(NULL) < 0)581 return -1;582 583 if (skip != NULL)584 skiplist = intlist__new(skip);585 /*586 * Tests that create BPF maps, for instance, need more than the 64K587 * default:588 */589 rlimit__bump_memlock();590 591 return __cmd_test(argc, argv, skiplist);592}593