builtin-test.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 event definition strings",
  41. .func = test__parse_events,
  42. },
  43. {
  44. .desc = "PERF_RECORD_* events & perf_sample fields",
  45. .func = test__PERF_RECORD,
  46. },
  47. {
  48. .desc = "Parse perf pmu format",
  49. .func = test__pmu,
  50. },
  51. {
  52. .desc = "DSO data read",
  53. .func = test__dso_data,
  54. },
  55. {
  56. .desc = "DSO data cache",
  57. .func = test__dso_data_cache,
  58. },
  59. {
  60. .desc = "DSO data reopen",
  61. .func = test__dso_data_reopen,
  62. },
  63. {
  64. .desc = "Roundtrip evsel->name",
  65. .func = test__perf_evsel__roundtrip_name_test,
  66. },
  67. {
  68. .desc = "Parse sched tracepoints fields",
  69. .func = test__perf_evsel__tp_sched_test,
  70. },
  71. {
  72. .desc = "syscalls:sys_enter_openat event fields",
  73. .func = test__syscall_openat_tp_fields,
  74. },
  75. {
  76. .desc = "Setup struct perf_event_attr",
  77. .func = test__attr,
  78. },
  79. {
  80. .desc = "Match and link multiple hists",
  81. .func = test__hists_link,
  82. },
  83. {
  84. .desc = "'import perf' in python",
  85. .func = test__python_use,
  86. },
  87. {
  88. .desc = "Breakpoint overflow signal handler",
  89. .func = test__bp_signal,
  90. },
  91. {
  92. .desc = "Breakpoint overflow sampling",
  93. .func = test__bp_signal_overflow,
  94. },
  95. {
  96. .desc = "Number of exit events of a simple workload",
  97. .func = test__task_exit,
  98. },
  99. {
  100. .desc = "Software clock events period values",
  101. .func = test__sw_clock_freq,
  102. },
  103. {
  104. .desc = "Object code reading",
  105. .func = test__code_reading,
  106. },
  107. {
  108. .desc = "Sample parsing",
  109. .func = test__sample_parsing,
  110. },
  111. {
  112. .desc = "Use a dummy software event to keep tracking",
  113. .func = test__keep_tracking,
  114. },
  115. {
  116. .desc = "Parse with no sample_id_all bit set",
  117. .func = test__parse_no_sample_id_all,
  118. },
  119. {
  120. .desc = "Filter hist entries",
  121. .func = test__hists_filter,
  122. },
  123. {
  124. .desc = "Lookup mmap thread",
  125. .func = test__mmap_thread_lookup,
  126. },
  127. {
  128. .desc = "Share thread mg",
  129. .func = test__thread_mg_share,
  130. },
  131. {
  132. .desc = "Sort output of hist entries",
  133. .func = test__hists_output,
  134. },
  135. {
  136. .desc = "Cumulate child hist entries",
  137. .func = test__hists_cumulate,
  138. },
  139. {
  140. .desc = "Track 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 = "kmod_path__parse",
  153. .func = test__kmod_path__parse,
  154. },
  155. {
  156. .desc = "Thread map",
  157. .func = test__thread_map,
  158. },
  159. {
  160. .desc = "LLVM search and compile",
  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 = "Session topology",
  170. .func = test_session_topology,
  171. },
  172. {
  173. .desc = "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 = "Synthesize thread map",
  183. .func = test__thread_map_synthesize,
  184. },
  185. {
  186. .desc = "Synthesize cpu map",
  187. .func = test__cpu_map_synthesize,
  188. },
  189. {
  190. .desc = "Synthesize stat config",
  191. .func = test__synthesize_stat_config,
  192. },
  193. {
  194. .desc = "Synthesize stat",
  195. .func = test__synthesize_stat,
  196. },
  197. {
  198. .desc = "Synthesize stat round",
  199. .func = test__synthesize_stat_round,
  200. },
  201. {
  202. .desc = "Synthesize attr update",
  203. .func = test__event_update,
  204. },
  205. {
  206. .desc = "Event times",
  207. .func = test__event_times,
  208. },
  209. {
  210. .desc = "Read backward ring buffer",
  211. .func = test__backward_ring_buffer,
  212. },
  213. {
  214. .desc = "Print cpu map",
  215. .func = test__cpu_map_print,
  216. },
  217. {
  218. .desc = "Probe SDT events",
  219. .func = test__sdt_event,
  220. },
  221. {
  222. .desc = "is_printable_array",
  223. .func = test__is_printable_array,
  224. },
  225. {
  226. .desc = "Print bitmap",
  227. .func = test__bitmap_print,
  228. },
  229. {
  230. .desc = "perf hooks",
  231. .func = test__perf_hooks,
  232. },
  233. {
  234. .func = NULL,
  235. },
  236. };
  237. static struct test *tests[] = {
  238. generic_tests,
  239. arch_tests,
  240. };
  241. static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
  242. {
  243. int i;
  244. if (argc == 0)
  245. return true;
  246. for (i = 0; i < argc; ++i) {
  247. char *end;
  248. long nr = strtoul(argv[i], &end, 10);
  249. if (*end == '\0') {
  250. if (nr == curr + 1)
  251. return true;
  252. continue;
  253. }
  254. if (strcasestr(test->desc, argv[i]))
  255. return true;
  256. }
  257. return false;
  258. }
  259. static int run_test(struct test *test, int subtest)
  260. {
  261. int status, err = -1, child = dont_fork ? 0 : fork();
  262. char sbuf[STRERR_BUFSIZE];
  263. if (child < 0) {
  264. pr_err("failed to fork test: %s\n",
  265. str_error_r(errno, sbuf, sizeof(sbuf)));
  266. return -1;
  267. }
  268. if (!child) {
  269. if (!dont_fork) {
  270. pr_debug("test child forked, pid %d\n", getpid());
  271. if (!verbose) {
  272. int nullfd = open("/dev/null", O_WRONLY);
  273. if (nullfd >= 0) {
  274. close(STDERR_FILENO);
  275. close(STDOUT_FILENO);
  276. dup2(nullfd, STDOUT_FILENO);
  277. dup2(STDOUT_FILENO, STDERR_FILENO);
  278. close(nullfd);
  279. }
  280. } else {
  281. signal(SIGSEGV, sighandler_dump_stack);
  282. signal(SIGFPE, sighandler_dump_stack);
  283. }
  284. }
  285. err = test->func(subtest);
  286. if (!dont_fork)
  287. exit(err);
  288. }
  289. if (!dont_fork) {
  290. wait(&status);
  291. if (WIFEXITED(status)) {
  292. err = (signed char)WEXITSTATUS(status);
  293. pr_debug("test child finished with %d\n", err);
  294. } else if (WIFSIGNALED(status)) {
  295. err = -1;
  296. pr_debug("test child interrupted\n");
  297. }
  298. }
  299. return err;
  300. }
  301. #define for_each_test(j, t) \
  302. for (j = 0; j < ARRAY_SIZE(tests); j++) \
  303. for (t = &tests[j][0]; t->func; t++)
  304. static int test_and_print(struct test *t, bool force_skip, int subtest)
  305. {
  306. int err;
  307. if (!force_skip) {
  308. pr_debug("\n--- start ---\n");
  309. err = run_test(t, subtest);
  310. pr_debug("---- end ----\n");
  311. } else {
  312. pr_debug("\n--- force skipped ---\n");
  313. err = TEST_SKIP;
  314. }
  315. if (!t->subtest.get_nr)
  316. pr_debug("%s:", t->desc);
  317. else
  318. pr_debug("%s subtest %d:", t->desc, subtest);
  319. switch (err) {
  320. case TEST_OK:
  321. pr_info(" Ok\n");
  322. break;
  323. case TEST_SKIP:
  324. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
  325. break;
  326. case TEST_FAIL:
  327. default:
  328. color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
  329. break;
  330. }
  331. return err;
  332. }
  333. static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
  334. {
  335. struct test *t;
  336. unsigned int j;
  337. int i = 0;
  338. int width = 0;
  339. for_each_test(j, t) {
  340. int len = strlen(t->desc);
  341. if (width < len)
  342. width = len;
  343. }
  344. for_each_test(j, t) {
  345. int curr = i++, err;
  346. if (!perf_test__matches(t, curr, argc, argv))
  347. continue;
  348. pr_info("%2d: %-*s:", i, width, t->desc);
  349. if (intlist__find(skiplist, i)) {
  350. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
  351. continue;
  352. }
  353. if (!t->subtest.get_nr) {
  354. test_and_print(t, false, -1);
  355. } else {
  356. int subn = t->subtest.get_nr();
  357. /*
  358. * minus 2 to align with normal testcases.
  359. * For subtest we print additional '.x' in number.
  360. * for example:
  361. *
  362. * 35: Test LLVM searching and compiling :
  363. * 35.1: Basic BPF llvm compiling test : Ok
  364. */
  365. int subw = width > 2 ? width - 2 : width;
  366. bool skip = false;
  367. int subi;
  368. if (subn <= 0) {
  369. color_fprintf(stderr, PERF_COLOR_YELLOW,
  370. " Skip (not compiled in)\n");
  371. continue;
  372. }
  373. pr_info("\n");
  374. for (subi = 0; subi < subn; subi++) {
  375. int len = strlen(t->subtest.get_desc(subi));
  376. if (subw < len)
  377. subw = len;
  378. }
  379. for (subi = 0; subi < subn; subi++) {
  380. pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
  381. t->subtest.get_desc(subi));
  382. err = test_and_print(t, skip, subi);
  383. if (err != TEST_OK && t->subtest.skip_if_fail)
  384. skip = true;
  385. }
  386. }
  387. }
  388. return 0;
  389. }
  390. static int perf_test__list(int argc, const char **argv)
  391. {
  392. unsigned int j;
  393. struct test *t;
  394. int i = 0;
  395. for_each_test(j, t) {
  396. if (argc > 1 && !strstr(t->desc, argv[1]))
  397. continue;
  398. pr_info("%2d: %s\n", ++i, t->desc);
  399. }
  400. return 0;
  401. }
  402. int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
  403. {
  404. const char *test_usage[] = {
  405. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  406. NULL,
  407. };
  408. const char *skip = NULL;
  409. const struct option test_options[] = {
  410. OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
  411. OPT_INCR('v', "verbose", &verbose,
  412. "be more verbose (show symbol address, etc)"),
  413. OPT_BOOLEAN('F', "dont-fork", &dont_fork,
  414. "Do not fork for testcase"),
  415. OPT_END()
  416. };
  417. const char * const test_subcommands[] = { "list", NULL };
  418. struct intlist *skiplist = NULL;
  419. int ret = hists__init();
  420. if (ret < 0)
  421. return ret;
  422. argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
  423. if (argc >= 1 && !strcmp(argv[0], "list"))
  424. return perf_test__list(argc, argv);
  425. symbol_conf.priv_size = sizeof(int);
  426. symbol_conf.sort_by_name = true;
  427. symbol_conf.try_vmlinux_path = true;
  428. if (symbol__init(NULL) < 0)
  429. return -1;
  430. if (skip != NULL)
  431. skiplist = intlist__new(skip);
  432. return __cmd_test(argc, argv, skiplist);
  433. }