builtin-test.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * builtin-test.c
  3. *
  4. * Builtin regression testing command: ever growing number of sanity tests
  5. */
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include "builtin.h"
  9. #include "hist.h"
  10. #include "intlist.h"
  11. #include "tests.h"
  12. #include "debug.h"
  13. #include "color.h"
  14. #include <subcmd/parse-options.h>
  15. #include "symbol.h"
  16. static bool dont_fork;
  17. struct test __weak arch_tests[] = {
  18. {
  19. .func = NULL,
  20. },
  21. };
  22. static struct test generic_tests[] = {
  23. {
  24. .desc = "vmlinux symtab matches kallsyms",
  25. .func = test__vmlinux_matches_kallsyms,
  26. },
  27. {
  28. .desc = "detect openat syscall event",
  29. .func = test__openat_syscall_event,
  30. },
  31. {
  32. .desc = "detect openat syscall event on all cpus",
  33. .func = test__openat_syscall_event_on_all_cpus,
  34. },
  35. {
  36. .desc = "read samples using the mmap interface",
  37. .func = test__basic_mmap,
  38. },
  39. {
  40. .desc = "parse events tests",
  41. .func = test__parse_events,
  42. },
  43. {
  44. .desc = "Validate PERF_RECORD_* events & perf_sample fields",
  45. .func = test__PERF_RECORD,
  46. },
  47. {
  48. .desc = "Test perf pmu format parsing",
  49. .func = test__pmu,
  50. },
  51. {
  52. .desc = "Test dso data read",
  53. .func = test__dso_data,
  54. },
  55. {
  56. .desc = "Test dso data cache",
  57. .func = test__dso_data_cache,
  58. },
  59. {
  60. .desc = "Test dso data reopen",
  61. .func = test__dso_data_reopen,
  62. },
  63. {
  64. .desc = "roundtrip evsel->name check",
  65. .func = test__perf_evsel__roundtrip_name_test,
  66. },
  67. {
  68. .desc = "Check parsing of sched tracepoints fields",
  69. .func = test__perf_evsel__tp_sched_test,
  70. },
  71. {
  72. .desc = "Generate and check syscalls:sys_enter_openat event fields",
  73. .func = test__syscall_openat_tp_fields,
  74. },
  75. {
  76. .desc = "struct perf_event_attr setup",
  77. .func = test__attr,
  78. },
  79. {
  80. .desc = "Test matching and linking multiple hists",
  81. .func = test__hists_link,
  82. },
  83. {
  84. .desc = "Try 'import perf' in python, checking link problems",
  85. .func = test__python_use,
  86. },
  87. {
  88. .desc = "Test breakpoint overflow signal handler",
  89. .func = test__bp_signal,
  90. },
  91. {
  92. .desc = "Test breakpoint overflow sampling",
  93. .func = test__bp_signal_overflow,
  94. },
  95. {
  96. .desc = "Test number of exit event of a simple workload",
  97. .func = test__task_exit,
  98. },
  99. {
  100. .desc = "Test software clock events have valid period values",
  101. .func = test__sw_clock_freq,
  102. },
  103. {
  104. .desc = "Test object code reading",
  105. .func = test__code_reading,
  106. },
  107. {
  108. .desc = "Test sample parsing",
  109. .func = test__sample_parsing,
  110. },
  111. {
  112. .desc = "Test using a dummy software event to keep tracking",
  113. .func = test__keep_tracking,
  114. },
  115. {
  116. .desc = "Test parsing with no sample_id_all bit set",
  117. .func = test__parse_no_sample_id_all,
  118. },
  119. {
  120. .desc = "Test filtering hist entries",
  121. .func = test__hists_filter,
  122. },
  123. {
  124. .desc = "Test mmap thread lookup",
  125. .func = test__mmap_thread_lookup,
  126. },
  127. {
  128. .desc = "Test thread mg sharing",
  129. .func = test__thread_mg_share,
  130. },
  131. {
  132. .desc = "Test output sorting of hist entries",
  133. .func = test__hists_output,
  134. },
  135. {
  136. .desc = "Test cumulation of child hist entries",
  137. .func = test__hists_cumulate,
  138. },
  139. {
  140. .desc = "Test tracking with sched_switch",
  141. .func = test__switch_tracking,
  142. },
  143. {
  144. .desc = "Filter fds with revents mask in a fdarray",
  145. .func = test__fdarray__filter,
  146. },
  147. {
  148. .desc = "Add fd to a fdarray, making it autogrow",
  149. .func = test__fdarray__add,
  150. },
  151. {
  152. .desc = "Test kmod_path__parse function",
  153. .func = test__kmod_path__parse,
  154. },
  155. {
  156. .desc = "Test thread map",
  157. .func = test__thread_map,
  158. },
  159. {
  160. .desc = "Test LLVM searching and compiling",
  161. .func = test__llvm,
  162. .subtest = {
  163. .skip_if_fail = true,
  164. .get_nr = test__llvm_subtest_get_nr,
  165. .get_desc = test__llvm_subtest_get_desc,
  166. },
  167. },
  168. {
  169. .desc = "Test topology in session",
  170. .func = test_session_topology,
  171. },
  172. {
  173. .desc = "Test BPF filter",
  174. .func = test__bpf,
  175. .subtest = {
  176. .skip_if_fail = true,
  177. .get_nr = test__bpf_subtest_get_nr,
  178. .get_desc = test__bpf_subtest_get_desc,
  179. },
  180. },
  181. {
  182. .desc = "Test thread map synthesize",
  183. .func = test__thread_map_synthesize,
  184. },
  185. {
  186. .desc = "Test cpu map synthesize",
  187. .func = test__cpu_map_synthesize,
  188. },
  189. {
  190. .desc = "Test stat config synthesize",
  191. .func = test__synthesize_stat_config,
  192. },
  193. {
  194. .desc = "Test stat synthesize",
  195. .func = test__synthesize_stat,
  196. },
  197. {
  198. .desc = "Test stat round synthesize",
  199. .func = test__synthesize_stat_round,
  200. },
  201. {
  202. .desc = "Test attr update synthesize",
  203. .func = test__event_update,
  204. },
  205. {
  206. .desc = "Test events times",
  207. .func = test__event_times,
  208. },
  209. {
  210. .desc = "Test backward reading from ring buffer",
  211. .func = test__backward_ring_buffer,
  212. },
  213. {
  214. .desc = "Test cpu map print",
  215. .func = test__cpu_map_print,
  216. },
  217. {
  218. .desc = "Test SDT event probing",
  219. .func = test__sdt_event,
  220. },
  221. {
  222. .desc = "Test is_printable_array function",
  223. .func = test__is_printable_array,
  224. },
  225. {
  226. .desc = "Test bitmap print",
  227. .func = test__bitmap_print,
  228. },
  229. {
  230. .func = NULL,
  231. },
  232. };
  233. static struct test *tests[] = {
  234. generic_tests,
  235. arch_tests,
  236. };
  237. static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
  238. {
  239. int i;
  240. if (argc == 0)
  241. return true;
  242. for (i = 0; i < argc; ++i) {
  243. char *end;
  244. long nr = strtoul(argv[i], &end, 10);
  245. if (*end == '\0') {
  246. if (nr == curr + 1)
  247. return true;
  248. continue;
  249. }
  250. if (strcasestr(test->desc, argv[i]))
  251. return true;
  252. }
  253. return false;
  254. }
  255. static int run_test(struct test *test, int subtest)
  256. {
  257. int status, err = -1, child = dont_fork ? 0 : fork();
  258. char sbuf[STRERR_BUFSIZE];
  259. if (child < 0) {
  260. pr_err("failed to fork test: %s\n",
  261. str_error_r(errno, sbuf, sizeof(sbuf)));
  262. return -1;
  263. }
  264. if (!child) {
  265. if (!dont_fork) {
  266. pr_debug("test child forked, pid %d\n", getpid());
  267. if (!verbose) {
  268. int nullfd = open("/dev/null", O_WRONLY);
  269. if (nullfd >= 0) {
  270. close(STDERR_FILENO);
  271. close(STDOUT_FILENO);
  272. dup2(nullfd, STDOUT_FILENO);
  273. dup2(STDOUT_FILENO, STDERR_FILENO);
  274. close(nullfd);
  275. }
  276. } else {
  277. signal(SIGSEGV, sighandler_dump_stack);
  278. signal(SIGFPE, sighandler_dump_stack);
  279. }
  280. }
  281. err = test->func(subtest);
  282. if (!dont_fork)
  283. exit(err);
  284. }
  285. if (!dont_fork) {
  286. wait(&status);
  287. if (WIFEXITED(status)) {
  288. err = (signed char)WEXITSTATUS(status);
  289. pr_debug("test child finished with %d\n", err);
  290. } else if (WIFSIGNALED(status)) {
  291. err = -1;
  292. pr_debug("test child interrupted\n");
  293. }
  294. }
  295. return err;
  296. }
  297. #define for_each_test(j, t) \
  298. for (j = 0; j < ARRAY_SIZE(tests); j++) \
  299. for (t = &tests[j][0]; t->func; t++)
  300. static int test_and_print(struct test *t, bool force_skip, int subtest)
  301. {
  302. int err;
  303. if (!force_skip) {
  304. pr_debug("\n--- start ---\n");
  305. err = run_test(t, subtest);
  306. pr_debug("---- end ----\n");
  307. } else {
  308. pr_debug("\n--- force skipped ---\n");
  309. err = TEST_SKIP;
  310. }
  311. if (!t->subtest.get_nr)
  312. pr_debug("%s:", t->desc);
  313. else
  314. pr_debug("%s subtest %d:", t->desc, subtest);
  315. switch (err) {
  316. case TEST_OK:
  317. pr_info(" Ok\n");
  318. break;
  319. case TEST_SKIP:
  320. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
  321. break;
  322. case TEST_FAIL:
  323. default:
  324. color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
  325. break;
  326. }
  327. return err;
  328. }
  329. static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
  330. {
  331. struct test *t;
  332. unsigned int j;
  333. int i = 0;
  334. int width = 0;
  335. for_each_test(j, t) {
  336. int len = strlen(t->desc);
  337. if (width < len)
  338. width = len;
  339. }
  340. for_each_test(j, t) {
  341. int curr = i++, err;
  342. if (!perf_test__matches(t, curr, argc, argv))
  343. continue;
  344. pr_info("%2d: %-*s:", i, width, t->desc);
  345. if (intlist__find(skiplist, i)) {
  346. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
  347. continue;
  348. }
  349. if (!t->subtest.get_nr) {
  350. test_and_print(t, false, -1);
  351. } else {
  352. int subn = t->subtest.get_nr();
  353. /*
  354. * minus 2 to align with normal testcases.
  355. * For subtest we print additional '.x' in number.
  356. * for example:
  357. *
  358. * 35: Test LLVM searching and compiling :
  359. * 35.1: Basic BPF llvm compiling test : Ok
  360. */
  361. int subw = width > 2 ? width - 2 : width;
  362. bool skip = false;
  363. int subi;
  364. if (subn <= 0) {
  365. color_fprintf(stderr, PERF_COLOR_YELLOW,
  366. " Skip (not compiled in)\n");
  367. continue;
  368. }
  369. pr_info("\n");
  370. for (subi = 0; subi < subn; subi++) {
  371. int len = strlen(t->subtest.get_desc(subi));
  372. if (subw < len)
  373. subw = len;
  374. }
  375. for (subi = 0; subi < subn; subi++) {
  376. pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
  377. t->subtest.get_desc(subi));
  378. err = test_and_print(t, skip, subi);
  379. if (err != TEST_OK && t->subtest.skip_if_fail)
  380. skip = true;
  381. }
  382. }
  383. }
  384. return 0;
  385. }
  386. static int perf_test__list(int argc, const char **argv)
  387. {
  388. unsigned int j;
  389. struct test *t;
  390. int i = 0;
  391. for_each_test(j, t) {
  392. if (argc > 1 && !strstr(t->desc, argv[1]))
  393. continue;
  394. pr_info("%2d: %s\n", ++i, t->desc);
  395. }
  396. return 0;
  397. }
  398. int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
  399. {
  400. const char *test_usage[] = {
  401. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  402. NULL,
  403. };
  404. const char *skip = NULL;
  405. const struct option test_options[] = {
  406. OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
  407. OPT_INCR('v', "verbose", &verbose,
  408. "be more verbose (show symbol address, etc)"),
  409. OPT_BOOLEAN('F', "dont-fork", &dont_fork,
  410. "Do not fork for testcase"),
  411. OPT_END()
  412. };
  413. const char * const test_subcommands[] = { "list", NULL };
  414. struct intlist *skiplist = NULL;
  415. int ret = hists__init();
  416. if (ret < 0)
  417. return ret;
  418. argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
  419. if (argc >= 1 && !strcmp(argv[0], "list"))
  420. return perf_test__list(argc, argv);
  421. symbol_conf.priv_size = sizeof(int);
  422. symbol_conf.sort_by_name = true;
  423. symbol_conf.try_vmlinux_path = true;
  424. if (symbol__init(NULL) < 0)
  425. return -1;
  426. if (skip != NULL)
  427. skiplist = intlist__new(skip);
  428. return __cmd_test(argc, argv, skiplist);
  429. }