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 <stdlib.h> 12 #include <sys/types.h> 13 #include <dirent.h> 14 #include <sys/wait.h> 15 #include <sys/stat.h> 16 #include "builtin.h" 17 #include "hist.h" 18 #include "intlist.h" 19 #include "tests.h" 20 #include "debug.h" 21 #include "color.h" 22 #include <subcmd/parse-options.h> 23 #include "string2.h" 24 #include "symbol.h" 25 #include "util/rlimit.h" 26 #include <linux/kernel.h> 27 #include <linux/string.h> 28 #include <subcmd/exec-cmd.h> 29 #include <linux/zalloc.h> 30 31 static bool dont_fork; 32 33 struct test_suite *__weak arch_tests[] = { 34 NULL, 35 }; 36 37 static struct test_suite *generic_tests[] = { 38 &suite__vmlinux_matches_kallsyms, 39 &suite__openat_syscall_event, 40 &suite__openat_syscall_event_on_all_cpus, 41 &suite__basic_mmap, 42 &suite__mem, 43 &suite__parse_events, 44 &suite__expr, 45 &suite__PERF_RECORD, 46 &suite__pmu, 47 &suite__pmu_events, 48 &suite__dso_data, 49 &suite__dso_data_cache, 50 &suite__dso_data_reopen, 51 &suite__perf_evsel__roundtrip_name_test, 52 &suite__perf_evsel__tp_sched_test, 53 &suite__syscall_openat_tp_fields, 54 &suite__attr, 55 &suite__hists_link, 56 &suite__python_use, 57 &suite__bp_signal, 58 &suite__bp_signal_overflow, 59 &suite__bp_accounting, 60 &suite__wp, 61 &suite__task_exit, 62 &suite__sw_clock_freq, 63 &suite__code_reading, 64 &suite__sample_parsing, 65 &suite__keep_tracking, 66 &suite__parse_no_sample_id_all, 67 &suite__hists_filter, 68 &suite__mmap_thread_lookup, 69 &suite__thread_maps_share, 70 &suite__hists_output, 71 &suite__hists_cumulate, 72 &suite__switch_tracking, 73 &suite__fdarray__filter, 74 &suite__fdarray__add, 75 &suite__kmod_path__parse, 76 &suite__thread_map, 77 &suite__llvm, 78 &suite__session_topology, 79 &suite__bpf, 80 &suite__thread_map_synthesize, 81 &suite__thread_map_remove, 82 &suite__cpu_map_synthesize, 83 &suite__synthesize_stat_config, 84 &suite__synthesize_stat, 85 &suite__synthesize_stat_round, 86 &suite__event_update, 87 &suite__event_times, 88 &suite__backward_ring_buffer, 89 &suite__cpu_map_print, 90 &suite__cpu_map_merge, 91 &suite__sdt_event, 92 &suite__is_printable_array, 93 &suite__bitmap_print, 94 &suite__perf_hooks, 95 &suite__clang, 96 &suite__unit_number__scnprint, 97 &suite__mem2node, 98 &suite__time_utils, 99 &suite__jit_write_elf, 100 &suite__pfm, 101 &suite__api_io, 102 &suite__maps__merge_in, 103 &suite__demangle_java, 104 &suite__demangle_ocaml, 105 &suite__parse_metric, 106 &suite__pe_file_parsing, 107 &suite__expand_cgroup_events, 108 &suite__perf_time_to_tsc, 109 &suite__dlfilter, 110 NULL, 111 }; 112 113 static struct test_suite **tests[] = { 114 generic_tests, 115 arch_tests, 116 }; 117 118 static int num_subtests(const struct test_suite *t) 119 { 120 if (t->subtest.get_nr) 121 return t->subtest.get_nr(); 122 123 return 0; 124 } 125 126 static bool has_subtests(const struct test_suite *t) 127 { 128 return t->subtest.get_nr || num_subtests(t) > 1; 129 } 130 131 static const char *skip_reason(const struct test_suite *t, int subtest) 132 { 133 if (t->subtest.skip_reason) 134 return t->subtest.skip_reason(subtest); 135 136 return NULL; 137 } 138 139 static const char *test_description(const struct test_suite *t, int subtest) 140 { 141 if (subtest < 0 || !t->subtest.get_desc) 142 return t->desc; 143 144 return t->subtest.get_desc(subtest); 145 } 146 147 static bool is_supported(const struct test_suite *t) 148 { 149 return !t->is_supported || t->is_supported(); 150 } 151 152 static test_fnptr test_function(const struct test_suite *t, int subtest __maybe_unused) 153 { 154 return t->func; 155 } 156 157 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[]) 158 { 159 int i; 160 161 if (argc == 0) 162 return true; 163 164 for (i = 0; i < argc; ++i) { 165 char *end; 166 long nr = strtoul(argv[i], &end, 10); 167 168 if (*end == '\0') { 169 if (nr == curr + 1) 170 return true; 171 continue; 172 } 173 174 if (strcasestr(desc, argv[i])) 175 return true; 176 } 177 178 return false; 179 } 180 181 static int run_test(struct test_suite *test, int subtest) 182 { 183 int status, err = -1, child = dont_fork ? 0 : fork(); 184 char sbuf[STRERR_BUFSIZE]; 185 186 if (child < 0) { 187 pr_err("failed to fork test: %s\n", 188 str_error_r(errno, sbuf, sizeof(sbuf))); 189 return -1; 190 } 191 192 if (!child) { 193 if (!dont_fork) { 194 pr_debug("test child forked, pid %d\n", getpid()); 195 196 if (verbose <= 0) { 197 int nullfd = open("/dev/null", O_WRONLY); 198 199 if (nullfd >= 0) { 200 close(STDERR_FILENO); 201 close(STDOUT_FILENO); 202 203 dup2(nullfd, STDOUT_FILENO); 204 dup2(STDOUT_FILENO, STDERR_FILENO); 205 close(nullfd); 206 } 207 } else { 208 signal(SIGSEGV, sighandler_dump_stack); 209 signal(SIGFPE, sighandler_dump_stack); 210 } 211 } 212 213 err = test_function(test, subtest)(test, subtest); 214 if (!dont_fork) 215 exit(err); 216 } 217 218 if (!dont_fork) { 219 wait(&status); 220 221 if (WIFEXITED(status)) { 222 err = (signed char)WEXITSTATUS(status); 223 pr_debug("test child finished with %d\n", err); 224 } else if (WIFSIGNALED(status)) { 225 err = -1; 226 pr_debug("test child interrupted\n"); 227 } 228 } 229 230 return err; 231 } 232 233 #define for_each_test(j, k, t) \ 234 for (j = 0; j < ARRAY_SIZE(tests); j++) \ 235 for (k = 0, t = tests[j][k]; tests[j][k]; k++, t = tests[j][k]) 236 237 static int test_and_print(struct test_suite *t, bool force_skip, int subtest) 238 { 239 int err; 240 241 if (!force_skip) { 242 pr_debug("\n--- start ---\n"); 243 err = run_test(t, subtest); 244 pr_debug("---- end ----\n"); 245 } else { 246 pr_debug("\n--- force skipped ---\n"); 247 err = TEST_SKIP; 248 } 249 250 if (!has_subtests(t)) 251 pr_debug("%s:", t->desc); 252 else 253 pr_debug("%s subtest %d:", t->desc, subtest + 1); 254 255 switch (err) { 256 case TEST_OK: 257 pr_info(" Ok\n"); 258 break; 259 case TEST_SKIP: { 260 const char *reason = skip_reason(t, subtest); 261 262 if (reason) 263 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason); 264 else 265 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); 266 } 267 break; 268 case TEST_FAIL: 269 default: 270 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); 271 break; 272 } 273 274 return err; 275 } 276 277 static const char *shell_test__description(char *description, size_t size, 278 const char *path, const char *name) 279 { 280 FILE *fp; 281 char filename[PATH_MAX]; 282 283 path__join(filename, sizeof(filename), path, name); 284 fp = fopen(filename, "r"); 285 if (!fp) 286 return NULL; 287 288 /* Skip shebang */ 289 while (fgetc(fp) != '\n'); 290 291 description = fgets(description, size, fp); 292 fclose(fp); 293 294 return description ? strim(description + 1) : NULL; 295 } 296 297 #define for_each_shell_test(entlist, nr, base, ent) \ 298 for (int __i = 0; __i < nr && (ent = entlist[__i]); __i++) \ 299 if (!is_directory(base, ent) && ent->d_name[0] != '.') 300 301 static const char *shell_tests__dir(char *path, size_t size) 302 { 303 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", }; 304 char *exec_path; 305 unsigned int i; 306 307 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { 308 struct stat st; 309 if (!lstat(devel_dirs[i], &st)) { 310 scnprintf(path, size, "%s/shell", devel_dirs[i]); 311 if (!lstat(devel_dirs[i], &st)) 312 return path; 313 } 314 } 315 316 /* Then installed path. */ 317 exec_path = get_argv_exec_path(); 318 scnprintf(path, size, "%s/tests/shell", exec_path); 319 free(exec_path); 320 return path; 321 } 322 323 static int shell_tests__max_desc_width(void) 324 { 325 struct dirent **entlist; 326 struct dirent *ent; 327 int n_dirs, e; 328 char path_dir[PATH_MAX]; 329 const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); 330 int width = 0; 331 332 if (path == NULL) 333 return -1; 334 335 n_dirs = scandir(path, &entlist, NULL, alphasort); 336 if (n_dirs == -1) 337 return -1; 338 339 for_each_shell_test(entlist, n_dirs, path, ent) { 340 char bf[256]; 341 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name); 342 343 if (desc) { 344 int len = strlen(desc); 345 346 if (width < len) 347 width = len; 348 } 349 } 350 351 for (e = 0; e < n_dirs; e++) 352 zfree(&entlist[e]); 353 free(entlist); 354 return width; 355 } 356 357 struct shell_test { 358 const char *dir; 359 const char *file; 360 }; 361 362 static int shell_test__run(struct test_suite *test, int subdir __maybe_unused) 363 { 364 int err; 365 char script[PATH_MAX]; 366 struct shell_test *st = test->priv; 367 368 path__join(script, sizeof(script) - 3, st->dir, st->file); 369 370 if (verbose) 371 strncat(script, " -v", sizeof(script) - strlen(script) - 1); 372 373 err = system(script); 374 if (!err) 375 return TEST_OK; 376 377 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL; 378 } 379 380 static int run_shell_tests(int argc, const char *argv[], int i, int width, 381 struct intlist *skiplist) 382 { 383 struct dirent **entlist; 384 struct dirent *ent; 385 int n_dirs, e; 386 char path_dir[PATH_MAX]; 387 struct shell_test st = { 388 .dir = shell_tests__dir(path_dir, sizeof(path_dir)), 389 }; 390 391 if (st.dir == NULL) 392 return -1; 393 394 n_dirs = scandir(st.dir, &entlist, NULL, alphasort); 395 if (n_dirs == -1) { 396 pr_err("failed to open shell test directory: %s\n", 397 st.dir); 398 return -1; 399 } 400 401 for_each_shell_test(entlist, n_dirs, st.dir, ent) { 402 int curr = i++; 403 char desc[256]; 404 struct test_suite test = { 405 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name), 406 .func = shell_test__run, 407 .priv = &st, 408 }; 409 410 if (!perf_test__matches(test.desc, curr, argc, argv)) 411 continue; 412 413 st.file = ent->d_name; 414 pr_info("%2d: %-*s:", i, width, test.desc); 415 416 if (intlist__find(skiplist, i)) { 417 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 418 continue; 419 } 420 421 test_and_print(&test, false, -1); 422 } 423 424 for (e = 0; e < n_dirs; e++) 425 zfree(&entlist[e]); 426 free(entlist); 427 return 0; 428 } 429 430 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) 431 { 432 struct test_suite *t; 433 unsigned int j, k; 434 int i = 0; 435 int width = shell_tests__max_desc_width(); 436 437 for_each_test(j, k, t) { 438 int len = strlen(test_description(t, -1)); 439 440 if (width < len) 441 width = len; 442 } 443 444 for_each_test(j, k, t) { 445 int curr = i++, err; 446 int subi; 447 448 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) { 449 bool skip = true; 450 int subn; 451 452 subn = num_subtests(t); 453 454 for (subi = 0; subi < subn; subi++) { 455 if (perf_test__matches(test_description(t, subi), 456 curr, argc, argv)) 457 skip = false; 458 } 459 460 if (skip) 461 continue; 462 } 463 464 if (!is_supported(t)) { 465 pr_debug("%2d: %-*s: Disabled\n", i, width, 466 test_description(t, -1)); 467 continue; 468 } 469 470 pr_info("%2d: %-*s:", i, width, test_description(t, -1)); 471 472 if (intlist__find(skiplist, i)) { 473 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 474 continue; 475 } 476 477 if (!has_subtests(t)) { 478 test_and_print(t, false, -1); 479 } else { 480 int subn = num_subtests(t); 481 /* 482 * minus 2 to align with normal testcases. 483 * For subtest we print additional '.x' in number. 484 * for example: 485 * 486 * 35: Test LLVM searching and compiling : 487 * 35.1: Basic BPF llvm compiling test : Ok 488 */ 489 int subw = width > 2 ? width - 2 : width; 490 bool skip = false; 491 492 if (subn <= 0) { 493 color_fprintf(stderr, PERF_COLOR_YELLOW, 494 " Skip (not compiled in)\n"); 495 continue; 496 } 497 pr_info("\n"); 498 499 for (subi = 0; subi < subn; subi++) { 500 int len = strlen(test_description(t, subi)); 501 502 if (subw < len) 503 subw = len; 504 } 505 506 for (subi = 0; subi < subn; subi++) { 507 if (!perf_test__matches(test_description(t, subi), 508 curr, argc, argv)) 509 continue; 510 511 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw, 512 test_description(t, subi)); 513 err = test_and_print(t, skip, subi); 514 if (err != TEST_OK && t->subtest.skip_if_fail) 515 skip = true; 516 } 517 } 518 } 519 520 return run_shell_tests(argc, argv, i, width, skiplist); 521 } 522 523 static int perf_test__list_shell(int argc, const char **argv, int i) 524 { 525 struct dirent **entlist; 526 struct dirent *ent; 527 int n_dirs, e; 528 char path_dir[PATH_MAX]; 529 const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); 530 531 if (path == NULL) 532 return -1; 533 534 n_dirs = scandir(path, &entlist, NULL, alphasort); 535 if (n_dirs == -1) 536 return -1; 537 538 for_each_shell_test(entlist, n_dirs, path, ent) { 539 int curr = i++; 540 char bf[256]; 541 struct test_suite t = { 542 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name), 543 }; 544 545 if (!perf_test__matches(t.desc, curr, argc, argv)) 546 continue; 547 548 pr_info("%2d: %s\n", i, t.desc); 549 550 } 551 552 for (e = 0; e < n_dirs; e++) 553 zfree(&entlist[e]); 554 free(entlist); 555 return 0; 556 } 557 558 static int perf_test__list(int argc, const char **argv) 559 { 560 unsigned int j, k; 561 struct test_suite *t; 562 int i = 0; 563 564 for_each_test(j, k, t) { 565 int curr = i++; 566 567 if (!perf_test__matches(test_description(t, -1), curr, argc, argv) || 568 !is_supported(t)) 569 continue; 570 571 pr_info("%2d: %s\n", i, test_description(t, -1)); 572 573 if (has_subtests(t)) { 574 int subn = num_subtests(t); 575 int subi; 576 577 for (subi = 0; subi < subn; subi++) 578 pr_info("%2d:%1d: %s\n", i, subi + 1, 579 test_description(t, subi)); 580 } 581 } 582 583 perf_test__list_shell(argc, argv, i); 584 585 return 0; 586 } 587 588 int cmd_test(int argc, const char **argv) 589 { 590 const char *test_usage[] = { 591 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 592 NULL, 593 }; 594 const char *skip = NULL; 595 const struct option test_options[] = { 596 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), 597 OPT_INCR('v', "verbose", &verbose, 598 "be more verbose (show symbol address, etc)"), 599 OPT_BOOLEAN('F', "dont-fork", &dont_fork, 600 "Do not fork for testcase"), 601 OPT_END() 602 }; 603 const char * const test_subcommands[] = { "list", NULL }; 604 struct intlist *skiplist = NULL; 605 int ret = hists__init(); 606 607 if (ret < 0) 608 return ret; 609 610 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0); 611 if (argc >= 1 && !strcmp(argv[0], "list")) 612 return perf_test__list(argc - 1, argv + 1); 613 614 symbol_conf.priv_size = sizeof(int); 615 symbol_conf.sort_by_name = true; 616 symbol_conf.try_vmlinux_path = true; 617 618 if (symbol__init(NULL) < 0) 619 return -1; 620 621 if (skip != NULL) 622 skiplist = intlist__new(skip); 623 /* 624 * Tests that create BPF maps, for instance, need more than the 64K 625 * default: 626 */ 627 rlimit__bump_memlock(); 628 629 return __cmd_test(argc, argv, skiplist); 630 } 631