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