builtin-test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. static bool dont_fork;
  27. struct test __weak arch_tests[] = {
  28. {
  29. .func = NULL,
  30. },
  31. };
  32. static struct test generic_tests[] = {
  33. {
  34. .desc = "vmlinux symtab matches kallsyms",
  35. .func = test__vmlinux_matches_kallsyms,
  36. },
  37. {
  38. .desc = "Detect openat syscall event",
  39. .func = test__openat_syscall_event,
  40. },
  41. {
  42. .desc = "Detect openat syscall event on all cpus",
  43. .func = test__openat_syscall_event_on_all_cpus,
  44. },
  45. {
  46. .desc = "Read samples using the mmap interface",
  47. .func = test__basic_mmap,
  48. },
  49. {
  50. .desc = "Test data source output",
  51. .func = test__mem,
  52. },
  53. {
  54. .desc = "Parse event definition strings",
  55. .func = test__parse_events,
  56. },
  57. {
  58. .desc = "Simple expression parser",
  59. .func = test__expr,
  60. },
  61. {
  62. .desc = "PERF_RECORD_* events & perf_sample fields",
  63. .func = test__PERF_RECORD,
  64. },
  65. {
  66. .desc = "Parse perf pmu format",
  67. .func = test__pmu,
  68. },
  69. {
  70. .desc = "DSO data read",
  71. .func = test__dso_data,
  72. },
  73. {
  74. .desc = "DSO data cache",
  75. .func = test__dso_data_cache,
  76. },
  77. {
  78. .desc = "DSO data reopen",
  79. .func = test__dso_data_reopen,
  80. },
  81. {
  82. .desc = "Roundtrip evsel->name",
  83. .func = test__perf_evsel__roundtrip_name_test,
  84. },
  85. {
  86. .desc = "Parse sched tracepoints fields",
  87. .func = test__perf_evsel__tp_sched_test,
  88. },
  89. {
  90. .desc = "syscalls:sys_enter_openat event fields",
  91. .func = test__syscall_openat_tp_fields,
  92. },
  93. {
  94. .desc = "Setup struct perf_event_attr",
  95. .func = test__attr,
  96. },
  97. {
  98. .desc = "Match and link multiple hists",
  99. .func = test__hists_link,
  100. },
  101. {
  102. .desc = "'import perf' in python",
  103. .func = test__python_use,
  104. },
  105. {
  106. .desc = "Breakpoint overflow signal handler",
  107. .func = test__bp_signal,
  108. .is_supported = test__bp_signal_is_supported,
  109. },
  110. {
  111. .desc = "Breakpoint overflow sampling",
  112. .func = test__bp_signal_overflow,
  113. .is_supported = test__bp_signal_is_supported,
  114. },
  115. {
  116. .desc = "Breakpoint accounting",
  117. .func = test__bp_accounting,
  118. .is_supported = test__bp_signal_is_supported,
  119. },
  120. {
  121. .desc = "Number of exit events of a simple workload",
  122. .func = test__task_exit,
  123. },
  124. {
  125. .desc = "Software clock events period values",
  126. .func = test__sw_clock_freq,
  127. },
  128. {
  129. .desc = "Object code reading",
  130. .func = test__code_reading,
  131. },
  132. {
  133. .desc = "Sample parsing",
  134. .func = test__sample_parsing,
  135. },
  136. {
  137. .desc = "Use a dummy software event to keep tracking",
  138. .func = test__keep_tracking,
  139. },
  140. {
  141. .desc = "Parse with no sample_id_all bit set",
  142. .func = test__parse_no_sample_id_all,
  143. },
  144. {
  145. .desc = "Filter hist entries",
  146. .func = test__hists_filter,
  147. },
  148. {
  149. .desc = "Lookup mmap thread",
  150. .func = test__mmap_thread_lookup,
  151. },
  152. {
  153. .desc = "Share thread mg",
  154. .func = test__thread_mg_share,
  155. },
  156. {
  157. .desc = "Sort output of hist entries",
  158. .func = test__hists_output,
  159. },
  160. {
  161. .desc = "Cumulate child hist entries",
  162. .func = test__hists_cumulate,
  163. },
  164. {
  165. .desc = "Track with sched_switch",
  166. .func = test__switch_tracking,
  167. },
  168. {
  169. .desc = "Filter fds with revents mask in a fdarray",
  170. .func = test__fdarray__filter,
  171. },
  172. {
  173. .desc = "Add fd to a fdarray, making it autogrow",
  174. .func = test__fdarray__add,
  175. },
  176. {
  177. .desc = "kmod_path__parse",
  178. .func = test__kmod_path__parse,
  179. },
  180. {
  181. .desc = "Thread map",
  182. .func = test__thread_map,
  183. },
  184. {
  185. .desc = "LLVM search and compile",
  186. .func = test__llvm,
  187. .subtest = {
  188. .skip_if_fail = true,
  189. .get_nr = test__llvm_subtest_get_nr,
  190. .get_desc = test__llvm_subtest_get_desc,
  191. },
  192. },
  193. {
  194. .desc = "Session topology",
  195. .func = test__session_topology,
  196. },
  197. {
  198. .desc = "BPF filter",
  199. .func = test__bpf,
  200. .subtest = {
  201. .skip_if_fail = true,
  202. .get_nr = test__bpf_subtest_get_nr,
  203. .get_desc = test__bpf_subtest_get_desc,
  204. },
  205. },
  206. {
  207. .desc = "Synthesize thread map",
  208. .func = test__thread_map_synthesize,
  209. },
  210. {
  211. .desc = "Remove thread map",
  212. .func = test__thread_map_remove,
  213. },
  214. {
  215. .desc = "Synthesize cpu map",
  216. .func = test__cpu_map_synthesize,
  217. },
  218. {
  219. .desc = "Synthesize stat config",
  220. .func = test__synthesize_stat_config,
  221. },
  222. {
  223. .desc = "Synthesize stat",
  224. .func = test__synthesize_stat,
  225. },
  226. {
  227. .desc = "Synthesize stat round",
  228. .func = test__synthesize_stat_round,
  229. },
  230. {
  231. .desc = "Synthesize attr update",
  232. .func = test__event_update,
  233. },
  234. {
  235. .desc = "Event times",
  236. .func = test__event_times,
  237. },
  238. {
  239. .desc = "Read backward ring buffer",
  240. .func = test__backward_ring_buffer,
  241. },
  242. {
  243. .desc = "Print cpu map",
  244. .func = test__cpu_map_print,
  245. },
  246. {
  247. .desc = "Probe SDT events",
  248. .func = test__sdt_event,
  249. },
  250. {
  251. .desc = "is_printable_array",
  252. .func = test__is_printable_array,
  253. },
  254. {
  255. .desc = "Print bitmap",
  256. .func = test__bitmap_print,
  257. },
  258. {
  259. .desc = "perf hooks",
  260. .func = test__perf_hooks,
  261. },
  262. {
  263. .desc = "builtin clang support",
  264. .func = test__clang,
  265. .subtest = {
  266. .skip_if_fail = true,
  267. .get_nr = test__clang_subtest_get_nr,
  268. .get_desc = test__clang_subtest_get_desc,
  269. }
  270. },
  271. {
  272. .desc = "unit_number__scnprintf",
  273. .func = test__unit_number__scnprint,
  274. },
  275. {
  276. .desc = "mem2node",
  277. .func = test__mem2node,
  278. },
  279. {
  280. .func = NULL,
  281. },
  282. };
  283. static struct test *tests[] = {
  284. generic_tests,
  285. arch_tests,
  286. };
  287. static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
  288. {
  289. int i;
  290. if (argc == 0)
  291. return true;
  292. for (i = 0; i < argc; ++i) {
  293. char *end;
  294. long nr = strtoul(argv[i], &end, 10);
  295. if (*end == '\0') {
  296. if (nr == curr + 1)
  297. return true;
  298. continue;
  299. }
  300. if (strcasestr(test->desc, argv[i]))
  301. return true;
  302. }
  303. return false;
  304. }
  305. static int run_test(struct test *test, int subtest)
  306. {
  307. int status, err = -1, child = dont_fork ? 0 : fork();
  308. char sbuf[STRERR_BUFSIZE];
  309. if (child < 0) {
  310. pr_err("failed to fork test: %s\n",
  311. str_error_r(errno, sbuf, sizeof(sbuf)));
  312. return -1;
  313. }
  314. if (!child) {
  315. if (!dont_fork) {
  316. pr_debug("test child forked, pid %d\n", getpid());
  317. if (verbose <= 0) {
  318. int nullfd = open("/dev/null", O_WRONLY);
  319. if (nullfd >= 0) {
  320. close(STDERR_FILENO);
  321. close(STDOUT_FILENO);
  322. dup2(nullfd, STDOUT_FILENO);
  323. dup2(STDOUT_FILENO, STDERR_FILENO);
  324. close(nullfd);
  325. }
  326. } else {
  327. signal(SIGSEGV, sighandler_dump_stack);
  328. signal(SIGFPE, sighandler_dump_stack);
  329. }
  330. }
  331. err = test->func(test, subtest);
  332. if (!dont_fork)
  333. exit(err);
  334. }
  335. if (!dont_fork) {
  336. wait(&status);
  337. if (WIFEXITED(status)) {
  338. err = (signed char)WEXITSTATUS(status);
  339. pr_debug("test child finished with %d\n", err);
  340. } else if (WIFSIGNALED(status)) {
  341. err = -1;
  342. pr_debug("test child interrupted\n");
  343. }
  344. }
  345. return err;
  346. }
  347. #define for_each_test(j, t) \
  348. for (j = 0; j < ARRAY_SIZE(tests); j++) \
  349. for (t = &tests[j][0]; t->func; t++)
  350. static int test_and_print(struct test *t, bool force_skip, int subtest)
  351. {
  352. int err;
  353. if (!force_skip) {
  354. pr_debug("\n--- start ---\n");
  355. err = run_test(t, subtest);
  356. pr_debug("---- end ----\n");
  357. } else {
  358. pr_debug("\n--- force skipped ---\n");
  359. err = TEST_SKIP;
  360. }
  361. if (!t->subtest.get_nr)
  362. pr_debug("%s:", t->desc);
  363. else
  364. pr_debug("%s subtest %d:", t->desc, subtest + 1);
  365. switch (err) {
  366. case TEST_OK:
  367. pr_info(" Ok\n");
  368. break;
  369. case TEST_SKIP:
  370. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
  371. break;
  372. case TEST_FAIL:
  373. default:
  374. color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
  375. break;
  376. }
  377. return err;
  378. }
  379. static const char *shell_test__description(char *description, size_t size,
  380. const char *path, const char *name)
  381. {
  382. FILE *fp;
  383. char filename[PATH_MAX];
  384. path__join(filename, sizeof(filename), path, name);
  385. fp = fopen(filename, "r");
  386. if (!fp)
  387. return NULL;
  388. description = fgets(description, size, fp);
  389. fclose(fp);
  390. return description ? trim(description + 1) : NULL;
  391. }
  392. #define for_each_shell_test(dir, base, ent) \
  393. while ((ent = readdir(dir)) != NULL) \
  394. if (!is_directory(base, ent) && ent->d_name[0] != '.')
  395. static const char *shell_tests__dir(char *path, size_t size)
  396. {
  397. const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
  398. char *exec_path;
  399. unsigned int i;
  400. for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
  401. struct stat st;
  402. if (!lstat(devel_dirs[i], &st)) {
  403. scnprintf(path, size, "%s/shell", devel_dirs[i]);
  404. if (!lstat(devel_dirs[i], &st))
  405. return path;
  406. }
  407. }
  408. /* Then installed path. */
  409. exec_path = get_argv_exec_path();
  410. scnprintf(path, size, "%s/tests/shell", exec_path);
  411. free(exec_path);
  412. return path;
  413. }
  414. static int shell_tests__max_desc_width(void)
  415. {
  416. DIR *dir;
  417. struct dirent *ent;
  418. char path_dir[PATH_MAX];
  419. const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
  420. int width = 0;
  421. if (path == NULL)
  422. return -1;
  423. dir = opendir(path);
  424. if (!dir)
  425. return -1;
  426. for_each_shell_test(dir, path, ent) {
  427. char bf[256];
  428. const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
  429. if (desc) {
  430. int len = strlen(desc);
  431. if (width < len)
  432. width = len;
  433. }
  434. }
  435. closedir(dir);
  436. return width;
  437. }
  438. struct shell_test {
  439. const char *dir;
  440. const char *file;
  441. };
  442. static int shell_test__run(struct test *test, int subdir __maybe_unused)
  443. {
  444. int err;
  445. char script[PATH_MAX];
  446. struct shell_test *st = test->priv;
  447. path__join(script, sizeof(script), st->dir, st->file);
  448. err = system(script);
  449. if (!err)
  450. return TEST_OK;
  451. return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
  452. }
  453. static int run_shell_tests(int argc, const char *argv[], int i, int width)
  454. {
  455. DIR *dir;
  456. struct dirent *ent;
  457. char path_dir[PATH_MAX];
  458. struct shell_test st = {
  459. .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
  460. };
  461. if (st.dir == NULL)
  462. return -1;
  463. dir = opendir(st.dir);
  464. if (!dir)
  465. return -1;
  466. for_each_shell_test(dir, st.dir, ent) {
  467. int curr = i++;
  468. char desc[256];
  469. struct test test = {
  470. .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
  471. .func = shell_test__run,
  472. .priv = &st,
  473. };
  474. if (!perf_test__matches(&test, curr, argc, argv))
  475. continue;
  476. st.file = ent->d_name;
  477. pr_info("%2d: %-*s:", i, width, test.desc);
  478. test_and_print(&test, false, -1);
  479. }
  480. closedir(dir);
  481. return 0;
  482. }
  483. static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
  484. {
  485. struct test *t;
  486. unsigned int j;
  487. int i = 0;
  488. int width = shell_tests__max_desc_width();
  489. for_each_test(j, t) {
  490. int len = strlen(t->desc);
  491. if (width < len)
  492. width = len;
  493. }
  494. for_each_test(j, t) {
  495. int curr = i++, err;
  496. if (!perf_test__matches(t, curr, argc, argv))
  497. continue;
  498. if (t->is_supported && !t->is_supported()) {
  499. pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
  500. continue;
  501. }
  502. pr_info("%2d: %-*s:", i, width, t->desc);
  503. if (intlist__find(skiplist, i)) {
  504. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
  505. continue;
  506. }
  507. if (!t->subtest.get_nr) {
  508. test_and_print(t, false, -1);
  509. } else {
  510. int subn = t->subtest.get_nr();
  511. /*
  512. * minus 2 to align with normal testcases.
  513. * For subtest we print additional '.x' in number.
  514. * for example:
  515. *
  516. * 35: Test LLVM searching and compiling :
  517. * 35.1: Basic BPF llvm compiling test : Ok
  518. */
  519. int subw = width > 2 ? width - 2 : width;
  520. bool skip = false;
  521. int subi;
  522. if (subn <= 0) {
  523. color_fprintf(stderr, PERF_COLOR_YELLOW,
  524. " Skip (not compiled in)\n");
  525. continue;
  526. }
  527. pr_info("\n");
  528. for (subi = 0; subi < subn; subi++) {
  529. int len = strlen(t->subtest.get_desc(subi));
  530. if (subw < len)
  531. subw = len;
  532. }
  533. for (subi = 0; subi < subn; subi++) {
  534. pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
  535. t->subtest.get_desc(subi));
  536. err = test_and_print(t, skip, subi);
  537. if (err != TEST_OK && t->subtest.skip_if_fail)
  538. skip = true;
  539. }
  540. }
  541. }
  542. return run_shell_tests(argc, argv, i, width);
  543. }
  544. static int perf_test__list_shell(int argc, const char **argv, int i)
  545. {
  546. DIR *dir;
  547. struct dirent *ent;
  548. char path_dir[PATH_MAX];
  549. const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
  550. if (path == NULL)
  551. return -1;
  552. dir = opendir(path);
  553. if (!dir)
  554. return -1;
  555. for_each_shell_test(dir, path, ent) {
  556. int curr = i++;
  557. char bf[256];
  558. struct test t = {
  559. .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
  560. };
  561. if (!perf_test__matches(&t, curr, argc, argv))
  562. continue;
  563. pr_info("%2d: %s\n", i, t.desc);
  564. }
  565. closedir(dir);
  566. return 0;
  567. }
  568. static int perf_test__list(int argc, const char **argv)
  569. {
  570. unsigned int j;
  571. struct test *t;
  572. int i = 0;
  573. for_each_test(j, t) {
  574. int curr = i++;
  575. if (!perf_test__matches(t, curr, argc, argv) ||
  576. (t->is_supported && !t->is_supported()))
  577. continue;
  578. pr_info("%2d: %s\n", i, t->desc);
  579. if (t->subtest.get_nr) {
  580. int subn = t->subtest.get_nr();
  581. int subi;
  582. for (subi = 0; subi < subn; subi++)
  583. pr_info("%2d:%1d: %s\n", i, subi + 1,
  584. t->subtest.get_desc(subi));
  585. }
  586. }
  587. perf_test__list_shell(argc, argv, i);
  588. return 0;
  589. }
  590. int cmd_test(int argc, const char **argv)
  591. {
  592. const char *test_usage[] = {
  593. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  594. NULL,
  595. };
  596. const char *skip = NULL;
  597. const struct option test_options[] = {
  598. OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
  599. OPT_INCR('v', "verbose", &verbose,
  600. "be more verbose (show symbol address, etc)"),
  601. OPT_BOOLEAN('F', "dont-fork", &dont_fork,
  602. "Do not fork for testcase"),
  603. OPT_END()
  604. };
  605. const char * const test_subcommands[] = { "list", NULL };
  606. struct intlist *skiplist = NULL;
  607. int ret = hists__init();
  608. if (ret < 0)
  609. return ret;
  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. symbol_conf.priv_size = sizeof(int);
  614. symbol_conf.sort_by_name = true;
  615. symbol_conf.try_vmlinux_path = true;
  616. if (symbol__init(NULL) < 0)
  617. return -1;
  618. if (skip != NULL)
  619. skiplist = intlist__new(skip);
  620. return __cmd_test(argc, argv, skiplist);
  621. }