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 <unistd.h> 10 #include <string.h> 11 #include <sys/types.h> 12 #include <dirent.h> 13 #include <sys/wait.h> 14 #include <sys/stat.h> 15 #include "builtin.h" 16 #include "hist.h" 17 #include "intlist.h" 18 #include "tests.h" 19 #include "debug.h" 20 #include "color.h" 21 #include <subcmd/parse-options.h> 22 #include "string2.h" 23 #include "symbol.h" 24 #include <linux/kernel.h> 25 #include <subcmd/exec-cmd.h> 26 27 static bool dont_fork; 28 29 struct test __weak arch_tests[] = { 30 { 31 .func = NULL, 32 }, 33 }; 34 35 static struct test generic_tests[] = { 36 { 37 .desc = "vmlinux symtab matches kallsyms", 38 .func = test__vmlinux_matches_kallsyms, 39 }, 40 { 41 .desc = "Detect openat syscall event", 42 .func = test__openat_syscall_event, 43 }, 44 { 45 .desc = "Detect openat syscall event on all cpus", 46 .func = test__openat_syscall_event_on_all_cpus, 47 }, 48 { 49 .desc = "Read samples using the mmap interface", 50 .func = test__basic_mmap, 51 }, 52 { 53 .desc = "Test data source output", 54 .func = test__mem, 55 }, 56 { 57 .desc = "Parse event definition strings", 58 .func = test__parse_events, 59 }, 60 { 61 .desc = "Simple expression parser", 62 .func = test__expr, 63 }, 64 { 65 .desc = "PERF_RECORD_* events & perf_sample fields", 66 .func = test__PERF_RECORD, 67 }, 68 { 69 .desc = "Parse perf pmu format", 70 .func = test__pmu, 71 }, 72 { 73 .desc = "DSO data read", 74 .func = test__dso_data, 75 }, 76 { 77 .desc = "DSO data cache", 78 .func = test__dso_data_cache, 79 }, 80 { 81 .desc = "DSO data reopen", 82 .func = test__dso_data_reopen, 83 }, 84 { 85 .desc = "Roundtrip evsel->name", 86 .func = test__perf_evsel__roundtrip_name_test, 87 }, 88 { 89 .desc = "Parse sched tracepoints fields", 90 .func = test__perf_evsel__tp_sched_test, 91 }, 92 { 93 .desc = "syscalls:sys_enter_openat event fields", 94 .func = test__syscall_openat_tp_fields, 95 }, 96 { 97 .desc = "Setup struct perf_event_attr", 98 .func = test__attr, 99 }, 100 { 101 .desc = "Match and link multiple hists", 102 .func = test__hists_link, 103 }, 104 { 105 .desc = "'import perf' in python", 106 .func = test__python_use, 107 }, 108 { 109 .desc = "Breakpoint overflow signal handler", 110 .func = test__bp_signal, 111 .is_supported = test__bp_signal_is_supported, 112 }, 113 { 114 .desc = "Breakpoint overflow sampling", 115 .func = test__bp_signal_overflow, 116 .is_supported = test__bp_signal_is_supported, 117 }, 118 { 119 .desc = "Breakpoint accounting", 120 .func = test__bp_accounting, 121 .is_supported = test__bp_signal_is_supported, 122 }, 123 { 124 .desc = "Watchpoint", 125 .func = test__wp, 126 .is_supported = test__wp_is_supported, 127 .subtest = { 128 .skip_if_fail = false, 129 .get_nr = test__wp_subtest_get_nr, 130 .get_desc = test__wp_subtest_get_desc, 131 }, 132 }, 133 { 134 .desc = "Number of exit events of a simple workload", 135 .func = test__task_exit, 136 }, 137 { 138 .desc = "Software clock events period values", 139 .func = test__sw_clock_freq, 140 }, 141 { 142 .desc = "Object code reading", 143 .func = test__code_reading, 144 }, 145 { 146 .desc = "Sample parsing", 147 .func = test__sample_parsing, 148 }, 149 { 150 .desc = "Use a dummy software event to keep tracking", 151 .func = test__keep_tracking, 152 }, 153 { 154 .desc = "Parse with no sample_id_all bit set", 155 .func = test__parse_no_sample_id_all, 156 }, 157 { 158 .desc = "Filter hist entries", 159 .func = test__hists_filter, 160 }, 161 { 162 .desc = "Lookup mmap thread", 163 .func = test__mmap_thread_lookup, 164 }, 165 { 166 .desc = "Share thread mg", 167 .func = test__thread_mg_share, 168 }, 169 { 170 .desc = "Sort output of hist entries", 171 .func = test__hists_output, 172 }, 173 { 174 .desc = "Cumulate child hist entries", 175 .func = test__hists_cumulate, 176 }, 177 { 178 .desc = "Track with sched_switch", 179 .func = test__switch_tracking, 180 }, 181 { 182 .desc = "Filter fds with revents mask in a fdarray", 183 .func = test__fdarray__filter, 184 }, 185 { 186 .desc = "Add fd to a fdarray, making it autogrow", 187 .func = test__fdarray__add, 188 }, 189 { 190 .desc = "kmod_path__parse", 191 .func = test__kmod_path__parse, 192 }, 193 { 194 .desc = "Thread map", 195 .func = test__thread_map, 196 }, 197 { 198 .desc = "LLVM search and compile", 199 .func = test__llvm, 200 .subtest = { 201 .skip_if_fail = true, 202 .get_nr = test__llvm_subtest_get_nr, 203 .get_desc = test__llvm_subtest_get_desc, 204 }, 205 }, 206 { 207 .desc = "Session topology", 208 .func = test__session_topology, 209 }, 210 { 211 .desc = "BPF filter", 212 .func = test__bpf, 213 .subtest = { 214 .skip_if_fail = true, 215 .get_nr = test__bpf_subtest_get_nr, 216 .get_desc = test__bpf_subtest_get_desc, 217 }, 218 }, 219 { 220 .desc = "Synthesize thread map", 221 .func = test__thread_map_synthesize, 222 }, 223 { 224 .desc = "Remove thread map", 225 .func = test__thread_map_remove, 226 }, 227 { 228 .desc = "Synthesize cpu map", 229 .func = test__cpu_map_synthesize, 230 }, 231 { 232 .desc = "Synthesize stat config", 233 .func = test__synthesize_stat_config, 234 }, 235 { 236 .desc = "Synthesize stat", 237 .func = test__synthesize_stat, 238 }, 239 { 240 .desc = "Synthesize stat round", 241 .func = test__synthesize_stat_round, 242 }, 243 { 244 .desc = "Synthesize attr update", 245 .func = test__event_update, 246 }, 247 { 248 .desc = "Event times", 249 .func = test__event_times, 250 }, 251 { 252 .desc = "Read backward ring buffer", 253 .func = test__backward_ring_buffer, 254 }, 255 { 256 .desc = "Print cpu map", 257 .func = test__cpu_map_print, 258 }, 259 { 260 .desc = "Probe SDT events", 261 .func = test__sdt_event, 262 }, 263 { 264 .desc = "is_printable_array", 265 .func = test__is_printable_array, 266 }, 267 { 268 .desc = "Print bitmap", 269 .func = test__bitmap_print, 270 }, 271 { 272 .desc = "perf hooks", 273 .func = test__perf_hooks, 274 }, 275 { 276 .desc = "builtin clang support", 277 .func = test__clang, 278 .subtest = { 279 .skip_if_fail = true, 280 .get_nr = test__clang_subtest_get_nr, 281 .get_desc = test__clang_subtest_get_desc, 282 } 283 }, 284 { 285 .desc = "unit_number__scnprintf", 286 .func = test__unit_number__scnprint, 287 }, 288 { 289 .desc = "mem2node", 290 .func = test__mem2node, 291 }, 292 { 293 .desc = "time utils", 294 .func = test__time_utils, 295 }, 296 { 297 .desc = "map_groups__merge_in", 298 .func = test__map_groups__merge_in, 299 }, 300 { 301 .func = NULL, 302 }, 303 }; 304 305 static struct test *tests[] = { 306 generic_tests, 307 arch_tests, 308 }; 309 310 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[]) 311 { 312 int i; 313 314 if (argc == 0) 315 return true; 316 317 for (i = 0; i < argc; ++i) { 318 char *end; 319 long nr = strtoul(argv[i], &end, 10); 320 321 if (*end == '\0') { 322 if (nr == curr + 1) 323 return true; 324 continue; 325 } 326 327 if (strcasestr(test->desc, argv[i])) 328 return true; 329 } 330 331 return false; 332 } 333 334 static int run_test(struct test *test, int subtest) 335 { 336 int status, err = -1, child = dont_fork ? 0 : fork(); 337 char sbuf[STRERR_BUFSIZE]; 338 339 if (child < 0) { 340 pr_err("failed to fork test: %s\n", 341 str_error_r(errno, sbuf, sizeof(sbuf))); 342 return -1; 343 } 344 345 if (!child) { 346 if (!dont_fork) { 347 pr_debug("test child forked, pid %d\n", getpid()); 348 349 if (verbose <= 0) { 350 int nullfd = open("/dev/null", O_WRONLY); 351 352 if (nullfd >= 0) { 353 close(STDERR_FILENO); 354 close(STDOUT_FILENO); 355 356 dup2(nullfd, STDOUT_FILENO); 357 dup2(STDOUT_FILENO, STDERR_FILENO); 358 close(nullfd); 359 } 360 } else { 361 signal(SIGSEGV, sighandler_dump_stack); 362 signal(SIGFPE, sighandler_dump_stack); 363 } 364 } 365 366 err = test->func(test, subtest); 367 if (!dont_fork) 368 exit(err); 369 } 370 371 if (!dont_fork) { 372 wait(&status); 373 374 if (WIFEXITED(status)) { 375 err = (signed char)WEXITSTATUS(status); 376 pr_debug("test child finished with %d\n", err); 377 } else if (WIFSIGNALED(status)) { 378 err = -1; 379 pr_debug("test child interrupted\n"); 380 } 381 } 382 383 return err; 384 } 385 386 #define for_each_test(j, t) \ 387 for (j = 0; j < ARRAY_SIZE(tests); j++) \ 388 for (t = &tests[j][0]; t->func; t++) 389 390 static int test_and_print(struct test *t, bool force_skip, int subtest) 391 { 392 int err; 393 394 if (!force_skip) { 395 pr_debug("\n--- start ---\n"); 396 err = run_test(t, subtest); 397 pr_debug("---- end ----\n"); 398 } else { 399 pr_debug("\n--- force skipped ---\n"); 400 err = TEST_SKIP; 401 } 402 403 if (!t->subtest.get_nr) 404 pr_debug("%s:", t->desc); 405 else 406 pr_debug("%s subtest %d:", t->desc, subtest + 1); 407 408 switch (err) { 409 case TEST_OK: 410 pr_info(" Ok\n"); 411 break; 412 case TEST_SKIP: 413 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); 414 break; 415 case TEST_FAIL: 416 default: 417 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); 418 break; 419 } 420 421 return err; 422 } 423 424 static const char *shell_test__description(char *description, size_t size, 425 const char *path, const char *name) 426 { 427 FILE *fp; 428 char filename[PATH_MAX]; 429 430 path__join(filename, sizeof(filename), path, name); 431 fp = fopen(filename, "r"); 432 if (!fp) 433 return NULL; 434 435 /* Skip shebang */ 436 while (fgetc(fp) != '\n'); 437 438 description = fgets(description, size, fp); 439 fclose(fp); 440 441 return description ? trim(description + 1) : NULL; 442 } 443 444 #define for_each_shell_test(dir, base, ent) \ 445 while ((ent = readdir(dir)) != NULL) \ 446 if (!is_directory(base, ent) && ent->d_name[0] != '.') 447 448 static const char *shell_tests__dir(char *path, size_t size) 449 { 450 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", }; 451 char *exec_path; 452 unsigned int i; 453 454 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { 455 struct stat st; 456 if (!lstat(devel_dirs[i], &st)) { 457 scnprintf(path, size, "%s/shell", devel_dirs[i]); 458 if (!lstat(devel_dirs[i], &st)) 459 return path; 460 } 461 } 462 463 /* Then installed path. */ 464 exec_path = get_argv_exec_path(); 465 scnprintf(path, size, "%s/tests/shell", exec_path); 466 free(exec_path); 467 return path; 468 } 469 470 static int shell_tests__max_desc_width(void) 471 { 472 DIR *dir; 473 struct dirent *ent; 474 char path_dir[PATH_MAX]; 475 const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); 476 int width = 0; 477 478 if (path == NULL) 479 return -1; 480 481 dir = opendir(path); 482 if (!dir) 483 return -1; 484 485 for_each_shell_test(dir, path, ent) { 486 char bf[256]; 487 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name); 488 489 if (desc) { 490 int len = strlen(desc); 491 492 if (width < len) 493 width = len; 494 } 495 } 496 497 closedir(dir); 498 return width; 499 } 500 501 struct shell_test { 502 const char *dir; 503 const char *file; 504 }; 505 506 static int shell_test__run(struct test *test, int subdir __maybe_unused) 507 { 508 int err; 509 char script[PATH_MAX]; 510 struct shell_test *st = test->priv; 511 512 path__join(script, sizeof(script), st->dir, st->file); 513 514 err = system(script); 515 if (!err) 516 return TEST_OK; 517 518 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL; 519 } 520 521 static int run_shell_tests(int argc, const char *argv[], int i, int width) 522 { 523 DIR *dir; 524 struct dirent *ent; 525 char path_dir[PATH_MAX]; 526 struct shell_test st = { 527 .dir = shell_tests__dir(path_dir, sizeof(path_dir)), 528 }; 529 530 if (st.dir == NULL) 531 return -1; 532 533 dir = opendir(st.dir); 534 if (!dir) 535 return -1; 536 537 for_each_shell_test(dir, st.dir, ent) { 538 int curr = i++; 539 char desc[256]; 540 struct test test = { 541 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name), 542 .func = shell_test__run, 543 .priv = &st, 544 }; 545 546 if (!perf_test__matches(&test, curr, argc, argv)) 547 continue; 548 549 st.file = ent->d_name; 550 pr_info("%2d: %-*s:", i, width, test.desc); 551 test_and_print(&test, false, -1); 552 } 553 554 closedir(dir); 555 return 0; 556 } 557 558 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) 559 { 560 struct test *t; 561 unsigned int j; 562 int i = 0; 563 int width = shell_tests__max_desc_width(); 564 565 for_each_test(j, t) { 566 int len = strlen(t->desc); 567 568 if (width < len) 569 width = len; 570 } 571 572 for_each_test(j, t) { 573 int curr = i++, err; 574 575 if (!perf_test__matches(t, curr, argc, argv)) 576 continue; 577 578 if (t->is_supported && !t->is_supported()) { 579 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc); 580 continue; 581 } 582 583 pr_info("%2d: %-*s:", i, width, t->desc); 584 585 if (intlist__find(skiplist, i)) { 586 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 587 continue; 588 } 589 590 if (!t->subtest.get_nr) { 591 test_and_print(t, false, -1); 592 } else { 593 int subn = t->subtest.get_nr(); 594 /* 595 * minus 2 to align with normal testcases. 596 * For subtest we print additional '.x' in number. 597 * for example: 598 * 599 * 35: Test LLVM searching and compiling : 600 * 35.1: Basic BPF llvm compiling test : Ok 601 */ 602 int subw = width > 2 ? width - 2 : width; 603 bool skip = false; 604 int subi; 605 606 if (subn <= 0) { 607 color_fprintf(stderr, PERF_COLOR_YELLOW, 608 " Skip (not compiled in)\n"); 609 continue; 610 } 611 pr_info("\n"); 612 613 for (subi = 0; subi < subn; subi++) { 614 int len = strlen(t->subtest.get_desc(subi)); 615 616 if (subw < len) 617 subw = len; 618 } 619 620 for (subi = 0; subi < subn; subi++) { 621 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw, 622 t->subtest.get_desc(subi)); 623 err = test_and_print(t, skip, subi); 624 if (err != TEST_OK && t->subtest.skip_if_fail) 625 skip = true; 626 } 627 } 628 } 629 630 return run_shell_tests(argc, argv, i, width); 631 } 632 633 static int perf_test__list_shell(int argc, const char **argv, int i) 634 { 635 DIR *dir; 636 struct dirent *ent; 637 char path_dir[PATH_MAX]; 638 const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); 639 640 if (path == NULL) 641 return -1; 642 643 dir = opendir(path); 644 if (!dir) 645 return -1; 646 647 for_each_shell_test(dir, path, ent) { 648 int curr = i++; 649 char bf[256]; 650 struct test t = { 651 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name), 652 }; 653 654 if (!perf_test__matches(&t, curr, argc, argv)) 655 continue; 656 657 pr_info("%2d: %s\n", i, t.desc); 658 } 659 660 closedir(dir); 661 return 0; 662 } 663 664 static int perf_test__list(int argc, const char **argv) 665 { 666 unsigned int j; 667 struct test *t; 668 int i = 0; 669 670 for_each_test(j, t) { 671 int curr = i++; 672 673 if (!perf_test__matches(t, curr, argc, argv) || 674 (t->is_supported && !t->is_supported())) 675 continue; 676 677 pr_info("%2d: %s\n", i, t->desc); 678 679 if (t->subtest.get_nr) { 680 int subn = t->subtest.get_nr(); 681 int subi; 682 683 for (subi = 0; subi < subn; subi++) 684 pr_info("%2d:%1d: %s\n", i, subi + 1, 685 t->subtest.get_desc(subi)); 686 } 687 } 688 689 perf_test__list_shell(argc, argv, i); 690 691 return 0; 692 } 693 694 int cmd_test(int argc, const char **argv) 695 { 696 const char *test_usage[] = { 697 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 698 NULL, 699 }; 700 const char *skip = NULL; 701 const struct option test_options[] = { 702 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), 703 OPT_INCR('v', "verbose", &verbose, 704 "be more verbose (show symbol address, etc)"), 705 OPT_BOOLEAN('F', "dont-fork", &dont_fork, 706 "Do not fork for testcase"), 707 OPT_END() 708 }; 709 const char * const test_subcommands[] = { "list", NULL }; 710 struct intlist *skiplist = NULL; 711 int ret = hists__init(); 712 713 if (ret < 0) 714 return ret; 715 716 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0); 717 if (argc >= 1 && !strcmp(argv[0], "list")) 718 return perf_test__list(argc - 1, argv + 1); 719 720 symbol_conf.priv_size = sizeof(int); 721 symbol_conf.sort_by_name = true; 722 symbol_conf.try_vmlinux_path = true; 723 724 if (symbol__init(NULL) < 0) 725 return -1; 726 727 if (skip != NULL) 728 skiplist = intlist__new(skip); 729 730 return __cmd_test(argc, argv, skiplist); 731 } 732