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 <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 and 39 * making them easier to debug. 40 */ 41 static bool dont_fork; 42 /* Don't fork the tests in parallel and wait for their completion. */ 43 static 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 */ 46 static bool parallel; 47 const char *dso_to_test; 48 const char *test_objdump_path = "objdump"; 49 50 /* 51 * List of architecture specific tests. Not a weak symbol as the array length is 52 * dependent on the initialization, as such GCC with LTO complains of 53 * conflicting definitions with a weak symbol. 54 */ 55 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) 56 extern struct test_suite *arch_tests[]; 57 #else 58 static struct test_suite *arch_tests[] = { 59 NULL, 60 }; 61 #endif 62 63 static struct test_suite *generic_tests[] = { 64 &suite__vmlinux_matches_kallsyms, 65 #ifdef HAVE_LIBTRACEEVENT 66 &suite__openat_syscall_event, 67 &suite__openat_syscall_event_on_all_cpus, 68 &suite__basic_mmap, 69 #endif 70 &suite__mem, 71 &suite__parse_events, 72 &suite__expr, 73 &suite__PERF_RECORD, 74 &suite__pmu, 75 &suite__pmu_events, 76 &suite__tool_pmu, 77 &suite__dso_data, 78 &suite__perf_evsel__roundtrip_name_test, 79 #ifdef HAVE_LIBTRACEEVENT 80 &suite__perf_evsel__tp_sched_test, 81 &suite__syscall_openat_tp_fields, 82 #endif 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_LIBTRACEEVENT 101 &suite__switch_tracking, 102 #endif 103 &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 142 static struct test_suite **tests[] = { 143 generic_tests, 144 arch_tests, 145 NULL, /* shell tests created at runtime. */ 146 }; 147 148 static 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 158 #define workloads__for_each(workload) \ 159 for (unsigned i = 0; i < ARRAY_SIZE(workloads) && ({ workload = workloads[i]; 1; }); i++) 160 161 static int num_subtests(const struct test_suite *t) 162 { 163 int num; 164 165 if (!t->test_cases) 166 return 0; 167 168 num = 0; 169 while (t->test_cases[num].name) 170 num++; 171 172 return num; 173 } 174 175 static bool has_subtests(const struct test_suite *t) 176 { 177 return num_subtests(t) > 1; 178 } 179 180 static const char *skip_reason(const struct test_suite *t, int subtest) 181 { 182 if (!t->test_cases) 183 return NULL; 184 185 return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason; 186 } 187 188 static const char *test_description(const struct test_suite *t, int subtest) 189 { 190 if (t->test_cases && subtest >= 0) 191 return t->test_cases[subtest].desc; 192 193 return t->desc; 194 } 195 196 static test_fnptr test_function(const struct test_suite *t, int subtest) 197 { 198 if (subtest <= 0) 199 return t->test_cases[0].run_case; 200 201 return t->test_cases[subtest].run_case; 202 } 203 204 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[]) 205 { 206 int i; 207 208 if (argc == 0) 209 return true; 210 211 for (i = 0; i < argc; ++i) { 212 char *end; 213 long nr = strtoul(argv[i], &end, 10); 214 215 if (*end == '\0') { 216 if (nr == curr + 1) 217 return true; 218 continue; 219 } 220 221 if (strcasestr(desc, argv[i])) 222 return true; 223 } 224 225 return false; 226 } 227 228 struct child_test { 229 struct child_process process; 230 struct test_suite *test; 231 int test_num; 232 int subtest; 233 }; 234 235 static int run_test_child(struct child_process *process) 236 { 237 struct child_test *child = container_of(process, struct child_test, process); 238 int err; 239 240 pr_debug("--- start ---\n"); 241 pr_debug("test child forked, pid %d\n", getpid()); 242 err = test_function(child->test, child->subtest)(child->test, child->subtest); 243 pr_debug("---- end(%d) ----\n", err); 244 fflush(NULL); 245 return -err; 246 } 247 248 static int print_test_result(struct test_suite *t, int i, int subtest, int result, int width) 249 { 250 if (has_subtests(t)) { 251 int subw = width > 2 ? width - 2 : width; 252 253 pr_info("%3d.%1d: %-*s:", i + 1, subtest + 1, subw, test_description(t, subtest)); 254 } else 255 pr_info("%3d: %-*s:", i + 1, width, test_description(t, subtest)); 256 257 switch (result) { 258 case TEST_OK: 259 pr_info(" Ok\n"); 260 break; 261 case TEST_SKIP: { 262 const char *reason = skip_reason(t, subtest); 263 264 if (reason) 265 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason); 266 else 267 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); 268 } 269 break; 270 case TEST_FAIL: 271 default: 272 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); 273 break; 274 } 275 276 return 0; 277 } 278 279 static int finish_test(struct child_test *child_test, int width) 280 { 281 struct test_suite *t = child_test->test; 282 int i = child_test->test_num; 283 int subi = child_test->subtest; 284 int err = child_test->process.err; 285 bool err_done = err <= 0; 286 struct strbuf err_output = STRBUF_INIT; 287 int ret; 288 289 /* 290 * For test suites with subtests, display the suite name ahead of the 291 * sub test names. 292 */ 293 if (has_subtests(t) && subi == 0) 294 pr_info("%3d: %-*s:\n", i + 1, width, test_description(t, -1)); 295 296 /* 297 * Busy loop reading from the child's stdout/stderr that are set to be 298 * non-blocking until EOF. 299 */ 300 if (!err_done) 301 fcntl(err, F_SETFL, O_NONBLOCK); 302 if (verbose > 1) { 303 if (has_subtests(t)) 304 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi)); 305 else 306 pr_info("%3d: %s:\n", i + 1, test_description(t, -1)); 307 } 308 while (!err_done) { 309 struct pollfd pfds[1] = { 310 { .fd = err, 311 .events = POLLIN | POLLERR | POLLHUP | POLLNVAL, 312 }, 313 }; 314 char buf[512]; 315 ssize_t len; 316 317 /* Poll to avoid excessive spinning, timeout set for 100ms. */ 318 poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/100); 319 if (!err_done && pfds[0].revents) { 320 errno = 0; 321 len = read(err, buf, sizeof(buf) - 1); 322 323 if (len <= 0) { 324 err_done = errno != EAGAIN; 325 } else { 326 buf[len] = '\0'; 327 if (verbose > 1) 328 fprintf(stdout, "%s", buf); 329 else 330 strbuf_addstr(&err_output, buf); 331 } 332 } 333 } 334 /* Clean up child process. */ 335 ret = finish_command(&child_test->process); 336 if (verbose == 1 && ret == TEST_FAIL) { 337 /* Add header for test that was skipped above. */ 338 if (has_subtests(t)) 339 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi)); 340 else 341 pr_info("%3d: %s:\n", i + 1, test_description(t, -1)); 342 fprintf(stderr, "%s", err_output.buf); 343 } 344 strbuf_release(&err_output); 345 print_test_result(t, i, subi, ret, width); 346 if (err > 0) 347 close(err); 348 return 0; 349 } 350 351 static int start_test(struct test_suite *test, int i, int subi, struct child_test **child, 352 int width) 353 { 354 int err; 355 356 *child = NULL; 357 if (dont_fork) { 358 pr_debug("--- start ---\n"); 359 err = test_function(test, subi)(test, subi); 360 pr_debug("---- end ----\n"); 361 print_test_result(test, i, subi, err, width); 362 return 0; 363 } 364 365 *child = zalloc(sizeof(**child)); 366 if (!*child) 367 return -ENOMEM; 368 369 (*child)->test = test; 370 (*child)->test_num = i; 371 (*child)->subtest = subi; 372 (*child)->process.pid = -1; 373 (*child)->process.no_stdin = 1; 374 if (verbose <= 0) { 375 (*child)->process.no_stdout = 1; 376 (*child)->process.no_stderr = 1; 377 } else { 378 (*child)->process.stdout_to_stderr = 1; 379 (*child)->process.out = -1; 380 (*child)->process.err = -1; 381 } 382 (*child)->process.no_exec_cmd = run_test_child; 383 err = start_command(&(*child)->process); 384 if (err || !sequential) 385 return err; 386 return finish_test(*child, width); 387 } 388 389 #define for_each_test(j, k, t) \ 390 for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \ 391 while ((t = tests[j][k++]) != NULL) 392 393 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) 394 { 395 struct test_suite *t; 396 unsigned int j, k; 397 int i = 0; 398 int width = 0; 399 size_t num_tests = 0; 400 struct child_test **child_tests; 401 int child_test_num = 0; 402 403 for_each_test(j, k, t) { 404 int len = strlen(test_description(t, -1)); 405 406 if (width < len) 407 width = len; 408 409 if (has_subtests(t)) { 410 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) { 411 len = strlen(test_description(t, subi)); 412 if (width < len) 413 width = len; 414 num_tests++; 415 } 416 } else { 417 num_tests++; 418 } 419 } 420 child_tests = calloc(num_tests, sizeof(*child_tests)); 421 if (!child_tests) 422 return -ENOMEM; 423 424 for_each_test(j, k, t) { 425 int curr = i++; 426 427 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) { 428 bool skip = true; 429 430 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) { 431 if (perf_test__matches(test_description(t, subi), 432 curr, argc, argv)) 433 skip = false; 434 } 435 436 if (skip) 437 continue; 438 } 439 440 if (intlist__find(skiplist, i)) { 441 pr_info("%3d: %-*s:", curr + 1, width, test_description(t, -1)); 442 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 443 continue; 444 } 445 446 if (!has_subtests(t)) { 447 int err = start_test(t, curr, -1, &child_tests[child_test_num++], width); 448 449 if (err) { 450 /* TODO: if !sequential waitpid the already forked children. */ 451 free(child_tests); 452 return err; 453 } 454 } else { 455 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) { 456 int err; 457 458 if (!perf_test__matches(test_description(t, subi), 459 curr, argc, argv)) 460 continue; 461 462 err = start_test(t, curr, subi, &child_tests[child_test_num++], 463 width); 464 if (err) 465 return err; 466 } 467 } 468 } 469 for (i = 0; i < child_test_num; i++) { 470 if (!sequential) { 471 int ret = finish_test(child_tests[i], width); 472 473 if (ret) 474 return ret; 475 } 476 free(child_tests[i]); 477 } 478 free(child_tests); 479 return 0; 480 } 481 482 static int perf_test__list(int argc, const char **argv) 483 { 484 unsigned int j, k; 485 struct test_suite *t; 486 int i = 0; 487 488 for_each_test(j, k, t) { 489 int curr = i++; 490 491 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) 492 continue; 493 494 pr_info("%3d: %s\n", i, test_description(t, -1)); 495 496 if (has_subtests(t)) { 497 int subn = num_subtests(t); 498 int subi; 499 500 for (subi = 0; subi < subn; subi++) 501 pr_info("%3d:%1d: %s\n", i, subi + 1, 502 test_description(t, subi)); 503 } 504 } 505 return 0; 506 } 507 508 static int workloads__fprintf_list(FILE *fp) 509 { 510 struct test_workload *twl; 511 int printed = 0; 512 513 workloads__for_each(twl) 514 printed += fprintf(fp, "%s\n", twl->name); 515 516 return printed; 517 } 518 519 static int run_workload(const char *work, int argc, const char **argv) 520 { 521 struct test_workload *twl; 522 523 workloads__for_each(twl) { 524 if (!strcmp(twl->name, work)) 525 return twl->func(argc, argv); 526 } 527 528 pr_info("No workload found: %s\n", work); 529 return -1; 530 } 531 532 static int perf_test__config(const char *var, const char *value, 533 void *data __maybe_unused) 534 { 535 if (!strcmp(var, "annotate.objdump")) 536 test_objdump_path = value; 537 538 return 0; 539 } 540 541 int cmd_test(int argc, const char **argv) 542 { 543 const char *test_usage[] = { 544 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 545 NULL, 546 }; 547 const char *skip = NULL; 548 const char *workload = NULL; 549 bool list_workloads = false; 550 const struct option test_options[] = { 551 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), 552 OPT_INCR('v', "verbose", &verbose, 553 "be more verbose (show symbol address, etc)"), 554 OPT_BOOLEAN('F', "dont-fork", &dont_fork, 555 "Do not fork for testcase"), 556 OPT_BOOLEAN('p', "parallel", ¶llel, "Run the tests in parallel"), 557 OPT_BOOLEAN('S', "sequential", &sequential, 558 "Run the tests one after another rather than in parallel"), 559 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing, use '--list-workloads' to list the available ones."), 560 OPT_BOOLEAN(0, "list-workloads", &list_workloads, "List the available builtin workloads to use with -w/--workload"), 561 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"), 562 OPT_STRING(0, "objdump", &test_objdump_path, "path", 563 "objdump binary to use for disassembly and annotations"), 564 OPT_END() 565 }; 566 const char * const test_subcommands[] = { "list", NULL }; 567 struct intlist *skiplist = NULL; 568 int ret = hists__init(); 569 570 if (ret < 0) 571 return ret; 572 573 perf_config(perf_test__config, NULL); 574 575 /* Unbuffered output */ 576 setvbuf(stdout, NULL, _IONBF, 0); 577 578 tests[2] = create_script_test_suites(); 579 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0); 580 if (argc >= 1 && !strcmp(argv[0], "list")) 581 return perf_test__list(argc - 1, argv + 1); 582 583 if (workload) 584 return run_workload(workload, argc, argv); 585 586 if (list_workloads) { 587 workloads__fprintf_list(stdout); 588 return 0; 589 } 590 591 if (dont_fork) 592 sequential = true; 593 else if (parallel) 594 sequential = false; 595 596 symbol_conf.priv_size = sizeof(int); 597 symbol_conf.try_vmlinux_path = true; 598 599 if (symbol__init(NULL) < 0) 600 return -1; 601 602 if (skip != NULL) 603 skiplist = intlist__new(skip); 604 /* 605 * Tests that create BPF maps, for instance, need more than the 64K 606 * default: 607 */ 608 rlimit__bump_memlock(); 609 610 return __cmd_test(argc, argv, skiplist); 611 } 612