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