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