1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * builtin-test.c 4 * 5 * Builtin regression testing command: ever growing number of sanity tests 6 */ 7 #include <fcntl.h> 8 #include <errno.h> 9 #include <poll.h> 10 #include <unistd.h> 11 #include <setjmp.h> 12 #include <string.h> 13 #include <stdlib.h> 14 #include <sys/types.h> 15 #include <dirent.h> 16 #include <sys/wait.h> 17 #include <sys/stat.h> 18 #include "builtin.h" 19 #include "config.h" 20 #include "hist.h" 21 #include "intlist.h" 22 #include "tests.h" 23 #include "debug.h" 24 #include "color.h" 25 #include <subcmd/parse-options.h> 26 #include <subcmd/run-command.h> 27 #include "string2.h" 28 #include "symbol.h" 29 #include "util/rlimit.h" 30 #include "util/strbuf.h" 31 #include <linux/kernel.h> 32 #include <linux/string.h> 33 #include <subcmd/exec-cmd.h> 34 #include <linux/zalloc.h> 35 36 #include "tests-scripts.h" 37 38 /* 39 * Command line option to not fork the test running in the same process and 40 * making them easier to debug. 41 */ 42 static bool dont_fork; 43 /* Fork the tests in parallel and wait for their completion. */ 44 static bool sequential; 45 const char *dso_to_test; 46 const char *test_objdump_path = "objdump"; 47 48 /* 49 * List of architecture specific tests. Not a weak symbol as the array length is 50 * dependent on the initialization, as such GCC with LTO complains of 51 * conflicting definitions with a weak symbol. 52 */ 53 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) 54 extern struct test_suite *arch_tests[]; 55 #else 56 static struct test_suite *arch_tests[] = { 57 NULL, 58 }; 59 #endif 60 61 static struct test_suite *generic_tests[] = { 62 &suite__vmlinux_matches_kallsyms, 63 &suite__openat_syscall_event, 64 &suite__openat_syscall_event_on_all_cpus, 65 &suite__basic_mmap, 66 &suite__mem, 67 &suite__parse_events, 68 &suite__expr, 69 &suite__PERF_RECORD, 70 &suite__pmu, 71 &suite__pmu_events, 72 &suite__hwmon_pmu, 73 &suite__tool_pmu, 74 &suite__dso_data, 75 &suite__perf_evsel__roundtrip_name_test, 76 #ifdef HAVE_LIBTRACEEVENT 77 &suite__perf_evsel__tp_sched_test, 78 &suite__syscall_openat_tp_fields, 79 #endif 80 &suite__hists_link, 81 &suite__python_use, 82 &suite__bp_signal, 83 &suite__bp_signal_overflow, 84 &suite__bp_accounting, 85 &suite__wp, 86 &suite__task_exit, 87 &suite__sw_clock_freq, 88 &suite__code_reading, 89 &suite__sample_parsing, 90 &suite__keep_tracking, 91 &suite__parse_no_sample_id_all, 92 &suite__hists_filter, 93 &suite__mmap_thread_lookup, 94 &suite__thread_maps_share, 95 &suite__hists_output, 96 &suite__hists_cumulate, 97 #ifdef HAVE_LIBTRACEEVENT 98 &suite__switch_tracking, 99 #endif 100 &suite__fdarray__filter, 101 &suite__fdarray__add, 102 &suite__kmod_path__parse, 103 &suite__thread_map, 104 &suite__session_topology, 105 &suite__thread_map_synthesize, 106 &suite__thread_map_remove, 107 &suite__cpu_map, 108 &suite__synthesize_stat_config, 109 &suite__synthesize_stat, 110 &suite__synthesize_stat_round, 111 &suite__event_update, 112 &suite__event_times, 113 &suite__backward_ring_buffer, 114 &suite__sdt_event, 115 &suite__is_printable_array, 116 &suite__bitmap_print, 117 &suite__perf_hooks, 118 &suite__unit_number__scnprint, 119 &suite__mem2node, 120 &suite__time_utils, 121 &suite__jit_write_elf, 122 &suite__pfm, 123 &suite__api_io, 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, 131 &suite__dlfilter, 132 &suite__sigtrap, 133 &suite__event_groups, 134 &suite__symbols, 135 &suite__util, 136 NULL, 137 }; 138 139 static struct test_workload *workloads[] = { 140 &workload__noploop, 141 &workload__thloop, 142 &workload__leafloop, 143 &workload__sqrtloop, 144 &workload__brstack, 145 &workload__datasym, 146 &workload__landlock, 147 }; 148 149 #define workloads__for_each(workload) \ 150 for (unsigned i = 0; i < ARRAY_SIZE(workloads) && ({ workload = workloads[i]; 1; }); i++) 151 152 static int num_subtests(const struct test_suite *t) 153 { 154 int num; 155 156 if (!t->test_cases) 157 return 0; 158 159 num = 0; 160 while (t->test_cases[num].name) 161 num++; 162 163 return num; 164 } 165 166 static bool has_subtests(const struct test_suite *t) 167 { 168 return num_subtests(t) > 1; 169 } 170 171 static const char *skip_reason(const struct test_suite *t, int subtest) 172 { 173 if (!t->test_cases) 174 return NULL; 175 176 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason; 177 } 178 179 static const char *test_description(const struct test_suite *t, int subtest) 180 { 181 if (t->test_cases && subtest >= 0) 182 return t->test_cases[subtest].desc; 183 184 return t->desc; 185 } 186 187 static test_fnptr test_function(const struct test_suite *t, int subtest) 188 { 189 if (subtest <= 0) 190 return t->test_cases[0].run_case; 191 192 return t->test_cases[subtest].run_case; 193 } 194 195 static bool test_exclusive(const struct test_suite *t, int subtest) 196 { 197 if (subtest <= 0) 198 return t->test_cases[0].exclusive; 199 200 return t->test_cases[subtest].exclusive; 201 } 202 203 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[]) 204 { 205 int i; 206 207 if (argc == 0) 208 return true; 209 210 for (i = 0; i < argc; ++i) { 211 char *end; 212 long nr = strtoul(argv[i], &end, 10); 213 214 if (*end == '\0') { 215 if (nr == curr + 1) 216 return true; 217 continue; 218 } 219 220 if (strcasestr(desc, argv[i])) 221 return true; 222 } 223 224 return false; 225 } 226 227 struct child_test { 228 struct child_process process; 229 struct test_suite *test; 230 int test_num; 231 int subtest; 232 }; 233 234 static jmp_buf run_test_jmp_buf; 235 236 static void child_test_sig_handler(int sig) 237 { 238 siglongjmp(run_test_jmp_buf, sig); 239 } 240 241 static int run_test_child(struct child_process *process) 242 { 243 const int signals[] = { 244 SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGINT, SIGPIPE, SIGQUIT, SIGSEGV, SIGTERM, 245 }; 246 struct child_test *child = container_of(process, struct child_test, process); 247 int err; 248 249 err = sigsetjmp(run_test_jmp_buf, 1); 250 if (err) { 251 fprintf(stderr, "\n---- unexpected signal (%d) ----\n", err); 252 err = err > 0 ? -err : -1; 253 goto err_out; 254 } 255 256 for (size_t i = 0; i < ARRAY_SIZE(signals); i++) 257 signal(signals[i], child_test_sig_handler); 258 259 pr_debug("--- start ---\n"); 260 pr_debug("test child forked, pid %d\n", getpid()); 261 err = test_function(child->test, child->subtest)(child->test, child->subtest); 262 pr_debug("---- end(%d) ----\n", err); 263 264 err_out: 265 fflush(NULL); 266 for (size_t i = 0; i < ARRAY_SIZE(signals); i++) 267 signal(signals[i], SIG_DFL); 268 return -err; 269 } 270 271 #define TEST_RUNNING -3 272 273 static int print_test_result(struct test_suite *t, int i, int subtest, int result, int width, 274 int running) 275 { 276 if (has_subtests(t)) { 277 int subw = width > 2 ? width - 2 : width; 278 279 pr_info("%3d.%1d: %-*s:", i + 1, subtest + 1, subw, test_description(t, subtest)); 280 } else 281 pr_info("%3d: %-*s:", i + 1, width, test_description(t, subtest)); 282 283 switch (result) { 284 case TEST_RUNNING: 285 color_fprintf(stderr, PERF_COLOR_YELLOW, " Running (%d active)\n", running); 286 break; 287 case TEST_OK: 288 pr_info(" Ok\n"); 289 break; 290 case TEST_SKIP: { 291 const char *reason = skip_reason(t, subtest); 292 293 if (reason) 294 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason); 295 else 296 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); 297 } 298 break; 299 case TEST_FAIL: 300 default: 301 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); 302 break; 303 } 304 305 return 0; 306 } 307 308 static void finish_test(struct child_test **child_tests, int running_test, int child_test_num, 309 int width) 310 { 311 struct child_test *child_test = child_tests[running_test]; 312 struct test_suite *t; 313 int i, subi, err; 314 bool err_done = false; 315 struct strbuf err_output = STRBUF_INIT; 316 int last_running = -1; 317 int ret; 318 319 if (child_test == NULL) { 320 /* Test wasn't started. */ 321 return; 322 } 323 t = child_test->test; 324 i = child_test->test_num; 325 subi = child_test->subtest; 326 err = child_test->process.err; 327 /* 328 * For test suites with subtests, display the suite name ahead of the 329 * sub test names. 330 */ 331 if (has_subtests(t) && subi == 0) 332 pr_info("%3d: %-*s:\n", i + 1, width, test_description(t, -1)); 333 334 /* 335 * Busy loop reading from the child's stdout/stderr that are set to be 336 * non-blocking until EOF. 337 */ 338 if (err > 0) 339 fcntl(err, F_SETFL, O_NONBLOCK); 340 if (verbose > 1) { 341 if (has_subtests(t)) 342 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi)); 343 else 344 pr_info("%3d: %s:\n", i + 1, test_description(t, -1)); 345 } 346 while (!err_done) { 347 struct pollfd pfds[1] = { 348 { .fd = err, 349 .events = POLLIN | POLLERR | POLLHUP | POLLNVAL, 350 }, 351 }; 352 if (perf_use_color_default) { 353 int running = 0; 354 355 for (int y = running_test; y < child_test_num; y++) { 356 if (child_tests[y] == NULL) 357 continue; 358 if (check_if_command_finished(&child_tests[y]->process) == 0) 359 running++; 360 } 361 if (running != last_running) { 362 if (last_running != -1) { 363 /* 364 * Erase "Running (.. active)" line 365 * printed before poll/sleep. 366 */ 367 fprintf(debug_file(), PERF_COLOR_DELETE_LINE); 368 } 369 print_test_result(t, i, subi, TEST_RUNNING, width, running); 370 last_running = running; 371 } 372 } 373 374 err_done = true; 375 if (err <= 0) { 376 /* No child stderr to poll, sleep for 10ms for child to complete. */ 377 usleep(10 * 1000); 378 } else { 379 /* Poll to avoid excessive spinning, timeout set for 100ms. */ 380 poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/100); 381 if (pfds[0].revents) { 382 char buf[512]; 383 ssize_t len; 384 385 len = read(err, buf, sizeof(buf) - 1); 386 387 if (len > 0) { 388 err_done = false; 389 buf[len] = '\0'; 390 strbuf_addstr(&err_output, buf); 391 } 392 } 393 } 394 if (err_done) 395 err_done = check_if_command_finished(&child_test->process); 396 } 397 if (perf_use_color_default && last_running != -1) { 398 /* Erase "Running (.. active)" line printed before poll/sleep. */ 399 fprintf(debug_file(), PERF_COLOR_DELETE_LINE); 400 } 401 /* Clean up child process. */ 402 ret = finish_command(&child_test->process); 403 if (verbose > 1 || (verbose == 1 && ret == TEST_FAIL)) 404 fprintf(stderr, "%s", err_output.buf); 405 406 strbuf_release(&err_output); 407 print_test_result(t, i, subi, ret, width, /*running=*/0); 408 if (err > 0) 409 close(err); 410 zfree(&child_tests[running_test]); 411 } 412 413 static int start_test(struct test_suite *test, int i, int subi, struct child_test **child, 414 int width, int pass) 415 { 416 int err; 417 418 *child = NULL; 419 if (dont_fork) { 420 if (pass == 1) { 421 pr_debug("--- start ---\n"); 422 err = test_function(test, subi)(test, subi); 423 pr_debug("---- end ----\n"); 424 print_test_result(test, i, subi, err, width, /*running=*/0); 425 } 426 return 0; 427 } 428 if (pass == 1 && !sequential && test_exclusive(test, subi)) { 429 /* When parallel, skip exclusive tests on the first pass. */ 430 return 0; 431 } 432 if (pass != 1 && (sequential || !test_exclusive(test, subi))) { 433 /* Sequential and non-exclusive tests were run on the first pass. */ 434 return 0; 435 } 436 *child = zalloc(sizeof(**child)); 437 if (!*child) 438 return -ENOMEM; 439 440 (*child)->test = test; 441 (*child)->test_num = i; 442 (*child)->subtest = subi; 443 (*child)->process.pid = -1; 444 (*child)->process.no_stdin = 1; 445 if (verbose <= 0) { 446 (*child)->process.no_stdout = 1; 447 (*child)->process.no_stderr = 1; 448 } else { 449 (*child)->process.stdout_to_stderr = 1; 450 (*child)->process.out = -1; 451 (*child)->process.err = -1; 452 } 453 (*child)->process.no_exec_cmd = run_test_child; 454 if (sequential || pass == 2) { 455 err = start_command(&(*child)->process); 456 if (err) 457 return err; 458 finish_test(child, /*running_test=*/0, /*child_test_num=*/1, width); 459 return 0; 460 } 461 return start_command(&(*child)->process); 462 } 463 464 /* State outside of __cmd_test for the sake of the signal handler. */ 465 466 static size_t num_tests; 467 static struct child_test **child_tests; 468 static jmp_buf cmd_test_jmp_buf; 469 470 static void cmd_test_sig_handler(int sig) 471 { 472 siglongjmp(cmd_test_jmp_buf, sig); 473 } 474 475 static int __cmd_test(struct test_suite **suites, int argc, const char *argv[], 476 struct intlist *skiplist) 477 { 478 static int width = 0; 479 int err = 0; 480 481 for (struct test_suite **t = suites; *t; t++) { 482 int len = strlen(test_description(*t, -1)); 483 484 if (width < len) 485 width = len; 486 487 if (has_subtests(*t)) { 488 for (int subi = 0, subn = num_subtests(*t); subi < subn; subi++) { 489 len = strlen(test_description(*t, subi)); 490 if (width < len) 491 width = len; 492 num_tests++; 493 } 494 } else { 495 num_tests++; 496 } 497 } 498 child_tests = calloc(num_tests, sizeof(*child_tests)); 499 if (!child_tests) 500 return -ENOMEM; 501 502 err = sigsetjmp(cmd_test_jmp_buf, 1); 503 if (err) { 504 pr_err("\nSignal (%d) while running tests.\nTerminating tests with the same signal\n", 505 err); 506 for (size_t x = 0; x < num_tests; x++) { 507 struct child_test *child_test = child_tests[x]; 508 509 if (!child_test || child_test->process.pid <= 0) 510 continue; 511 512 pr_debug3("Killing %d pid %d\n", 513 child_test->test_num + 1, 514 child_test->process.pid); 515 kill(child_test->process.pid, err); 516 } 517 goto err_out; 518 } 519 signal(SIGINT, cmd_test_sig_handler); 520 signal(SIGTERM, cmd_test_sig_handler); 521 522 /* 523 * In parallel mode pass 1 runs non-exclusive tests in parallel, pass 2 524 * runs the exclusive tests sequentially. In other modes all tests are 525 * run in pass 1. 526 */ 527 for (int pass = 1; pass <= 2; pass++) { 528 int child_test_num = 0; 529 int i = 0; 530 531 for (struct test_suite **t = suites; *t; t++) { 532 int curr = i++; 533 534 if (!perf_test__matches(test_description(*t, -1), curr, argc, argv)) { 535 /* 536 * Test suite shouldn't be run based on 537 * description. See if subtest should. 538 */ 539 bool skip = true; 540 541 for (int subi = 0, subn = num_subtests(*t); subi < subn; subi++) { 542 if (perf_test__matches(test_description(*t, subi), 543 curr, argc, argv)) 544 skip = false; 545 } 546 547 if (skip) 548 continue; 549 } 550 551 if (intlist__find(skiplist, i)) { 552 pr_info("%3d: %-*s:", curr + 1, width, test_description(*t, -1)); 553 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 554 continue; 555 } 556 557 if (!has_subtests(*t)) { 558 err = start_test(*t, curr, -1, &child_tests[child_test_num++], 559 width, pass); 560 if (err) 561 goto err_out; 562 continue; 563 } 564 for (int subi = 0, subn = num_subtests(*t); subi < subn; subi++) { 565 if (!perf_test__matches(test_description(*t, subi), 566 curr, argc, argv)) 567 continue; 568 569 err = start_test(*t, curr, subi, &child_tests[child_test_num++], 570 width, pass); 571 if (err) 572 goto err_out; 573 } 574 } 575 if (!sequential) { 576 /* Parallel mode starts tests but doesn't finish them. Do that now. */ 577 for (size_t x = 0; x < num_tests; x++) 578 finish_test(child_tests, x, num_tests, width); 579 } 580 } 581 err_out: 582 signal(SIGINT, SIG_DFL); 583 signal(SIGTERM, SIG_DFL); 584 if (err) { 585 pr_err("Internal test harness failure. Completing any started tests:\n:"); 586 for (size_t x = 0; x < num_tests; x++) 587 finish_test(child_tests, x, num_tests, width); 588 } 589 free(child_tests); 590 return err; 591 } 592 593 static int perf_test__list(struct test_suite **suites, int argc, const char **argv) 594 { 595 int i = 0; 596 597 for (struct test_suite **t = suites; *t; t++) { 598 int curr = i++; 599 600 if (!perf_test__matches(test_description(*t, -1), curr, argc, argv)) 601 continue; 602 603 pr_info("%3d: %s\n", i, test_description(*t, -1)); 604 605 if (has_subtests(*t)) { 606 int subn = num_subtests(*t); 607 int subi; 608 609 for (subi = 0; subi < subn; subi++) 610 pr_info("%3d:%1d: %s\n", i, subi + 1, 611 test_description(*t, subi)); 612 } 613 } 614 return 0; 615 } 616 617 static int workloads__fprintf_list(FILE *fp) 618 { 619 struct test_workload *twl; 620 int printed = 0; 621 622 workloads__for_each(twl) 623 printed += fprintf(fp, "%s\n", twl->name); 624 625 return printed; 626 } 627 628 static int run_workload(const char *work, int argc, const char **argv) 629 { 630 struct test_workload *twl; 631 632 workloads__for_each(twl) { 633 if (!strcmp(twl->name, work)) 634 return twl->func(argc, argv); 635 } 636 637 pr_info("No workload found: %s\n", work); 638 return -1; 639 } 640 641 static int perf_test__config(const char *var, const char *value, 642 void *data __maybe_unused) 643 { 644 if (!strcmp(var, "annotate.objdump")) 645 test_objdump_path = value; 646 647 return 0; 648 } 649 650 static struct test_suite **build_suites(void) 651 { 652 /* 653 * TODO: suites is static to avoid needing to clean up the scripts tests 654 * for leak sanitizer. 655 */ 656 static struct test_suite **suites[] = { 657 generic_tests, 658 arch_tests, 659 NULL, 660 }; 661 struct test_suite **result; 662 struct test_suite *t; 663 size_t n = 0, num_suites = 0; 664 665 if (suites[2] == NULL) 666 suites[2] = create_script_test_suites(); 667 668 #define for_each_test(t) \ 669 for (size_t i = 0, j = 0; i < ARRAY_SIZE(suites); i++, j = 0) \ 670 while ((t = suites[i][j++]) != NULL) 671 672 for_each_test(t) 673 num_suites++; 674 675 result = calloc(num_suites + 1, sizeof(struct test_suite *)); 676 677 for (int pass = 1; pass <= 2; pass++) { 678 for_each_test(t) { 679 bool exclusive = false; 680 681 if (!has_subtests(t)) { 682 exclusive = test_exclusive(t, -1); 683 } else { 684 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) { 685 if (test_exclusive(t, subi)) { 686 exclusive = true; 687 break; 688 } 689 } 690 } 691 if ((!exclusive && pass == 1) || (exclusive && pass == 2)) 692 result[n++] = t; 693 } 694 } 695 return result; 696 #undef for_each_test 697 } 698 699 int cmd_test(int argc, const char **argv) 700 { 701 const char *test_usage[] = { 702 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 703 NULL, 704 }; 705 const char *skip = NULL; 706 const char *workload = NULL; 707 bool list_workloads = false; 708 const struct option test_options[] = { 709 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), 710 OPT_INCR('v', "verbose", &verbose, 711 "be more verbose (show symbol address, etc)"), 712 OPT_BOOLEAN('F', "dont-fork", &dont_fork, 713 "Do not fork for testcase"), 714 OPT_BOOLEAN('S', "sequential", &sequential, 715 "Run the tests one after another rather than in parallel"), 716 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing, use '--list-workloads' to list the available ones."), 717 OPT_BOOLEAN(0, "list-workloads", &list_workloads, "List the available builtin workloads to use with -w/--workload"), 718 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"), 719 OPT_STRING(0, "objdump", &test_objdump_path, "path", 720 "objdump binary to use for disassembly and annotations"), 721 OPT_END() 722 }; 723 const char * const test_subcommands[] = { "list", NULL }; 724 struct intlist *skiplist = NULL; 725 int ret = hists__init(); 726 struct test_suite **suites; 727 728 if (ret < 0) 729 return ret; 730 731 perf_config(perf_test__config, NULL); 732 733 /* Unbuffered output */ 734 setvbuf(stdout, NULL, _IONBF, 0); 735 736 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0); 737 if (argc >= 1 && !strcmp(argv[0], "list")) { 738 suites = build_suites(); 739 ret = perf_test__list(suites, argc - 1, argv + 1); 740 free(suites); 741 return ret; 742 } 743 744 if (workload) 745 return run_workload(workload, argc, argv); 746 747 if (list_workloads) { 748 workloads__fprintf_list(stdout); 749 return 0; 750 } 751 752 if (dont_fork) 753 sequential = true; 754 755 symbol_conf.priv_size = sizeof(int); 756 symbol_conf.try_vmlinux_path = true; 757 758 759 if (symbol__init(NULL) < 0) 760 return -1; 761 762 if (skip != NULL) 763 skiplist = intlist__new(skip); 764 /* 765 * Tests that create BPF maps, for instance, need more than the 64K 766 * default: 767 */ 768 rlimit__bump_memlock(); 769 770 suites = build_suites(); 771 ret = __cmd_test(suites, argc, argv, skiplist); 772 free(suites); 773 return ret; 774 } 775