builtin-test.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "parse-options.h"
  15. #include "symbol.h"
  16. static struct test {
  17. const char *desc;
  18. int (*func)(void);
  19. } tests[] = {
  20. {
  21. .desc = "vmlinux symtab matches kallsyms",
  22. .func = test__vmlinux_matches_kallsyms,
  23. },
  24. {
  25. .desc = "detect openat syscall event",
  26. .func = test__openat_syscall_event,
  27. },
  28. {
  29. .desc = "detect openat syscall event on all cpus",
  30. .func = test__openat_syscall_event_on_all_cpus,
  31. },
  32. {
  33. .desc = "read samples using the mmap interface",
  34. .func = test__basic_mmap,
  35. },
  36. {
  37. .desc = "parse events tests",
  38. .func = test__parse_events,
  39. },
  40. #if defined(__x86_64__) || defined(__i386__)
  41. {
  42. .desc = "x86 rdpmc test",
  43. .func = test__rdpmc,
  44. },
  45. #endif
  46. {
  47. .desc = "Validate PERF_RECORD_* events & perf_sample fields",
  48. .func = test__PERF_RECORD,
  49. },
  50. {
  51. .desc = "Test perf pmu format parsing",
  52. .func = test__pmu,
  53. },
  54. {
  55. .desc = "Test dso data read",
  56. .func = test__dso_data,
  57. },
  58. {
  59. .desc = "Test dso data cache",
  60. .func = test__dso_data_cache,
  61. },
  62. {
  63. .desc = "Test dso data reopen",
  64. .func = test__dso_data_reopen,
  65. },
  66. {
  67. .desc = "roundtrip evsel->name check",
  68. .func = test__perf_evsel__roundtrip_name_test,
  69. },
  70. {
  71. .desc = "Check parsing of sched tracepoints fields",
  72. .func = test__perf_evsel__tp_sched_test,
  73. },
  74. {
  75. .desc = "Generate and check syscalls:sys_enter_openat event fields",
  76. .func = test__syscall_openat_tp_fields,
  77. },
  78. {
  79. .desc = "struct perf_event_attr setup",
  80. .func = test__attr,
  81. },
  82. {
  83. .desc = "Test matching and linking multiple hists",
  84. .func = test__hists_link,
  85. },
  86. {
  87. .desc = "Try 'import perf' in python, checking link problems",
  88. .func = test__python_use,
  89. },
  90. {
  91. .desc = "Test breakpoint overflow signal handler",
  92. .func = test__bp_signal,
  93. },
  94. {
  95. .desc = "Test breakpoint overflow sampling",
  96. .func = test__bp_signal_overflow,
  97. },
  98. {
  99. .desc = "Test number of exit event of a simple workload",
  100. .func = test__task_exit,
  101. },
  102. {
  103. .desc = "Test software clock events have valid period values",
  104. .func = test__sw_clock_freq,
  105. },
  106. #if defined(__x86_64__) || defined(__i386__)
  107. {
  108. .desc = "Test converting perf time to TSC",
  109. .func = test__perf_time_to_tsc,
  110. },
  111. #endif
  112. {
  113. .desc = "Test object code reading",
  114. .func = test__code_reading,
  115. },
  116. {
  117. .desc = "Test sample parsing",
  118. .func = test__sample_parsing,
  119. },
  120. {
  121. .desc = "Test using a dummy software event to keep tracking",
  122. .func = test__keep_tracking,
  123. },
  124. {
  125. .desc = "Test parsing with no sample_id_all bit set",
  126. .func = test__parse_no_sample_id_all,
  127. },
  128. #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__)
  129. #ifdef HAVE_DWARF_UNWIND_SUPPORT
  130. {
  131. .desc = "Test dwarf unwind",
  132. .func = test__dwarf_unwind,
  133. },
  134. #endif
  135. #endif
  136. {
  137. .desc = "Test filtering hist entries",
  138. .func = test__hists_filter,
  139. },
  140. {
  141. .desc = "Test mmap thread lookup",
  142. .func = test__mmap_thread_lookup,
  143. },
  144. {
  145. .desc = "Test thread mg sharing",
  146. .func = test__thread_mg_share,
  147. },
  148. {
  149. .desc = "Test output sorting of hist entries",
  150. .func = test__hists_output,
  151. },
  152. {
  153. .desc = "Test cumulation of child hist entries",
  154. .func = test__hists_cumulate,
  155. },
  156. {
  157. .desc = "Test tracking with sched_switch",
  158. .func = test__switch_tracking,
  159. },
  160. {
  161. .desc = "Filter fds with revents mask in a fdarray",
  162. .func = test__fdarray__filter,
  163. },
  164. {
  165. .desc = "Add fd to a fdarray, making it autogrow",
  166. .func = test__fdarray__add,
  167. },
  168. {
  169. .desc = "Test kmod_path__parse function",
  170. .func = test__kmod_path__parse,
  171. },
  172. {
  173. .desc = "Test thread map",
  174. .func = test__thread_map,
  175. },
  176. {
  177. .desc = "Test LLVM searching and compiling",
  178. .func = test__llvm,
  179. },
  180. {
  181. .func = NULL,
  182. },
  183. };
  184. static bool perf_test__matches(int curr, int argc, const char *argv[])
  185. {
  186. int i;
  187. if (argc == 0)
  188. return true;
  189. for (i = 0; i < argc; ++i) {
  190. char *end;
  191. long nr = strtoul(argv[i], &end, 10);
  192. if (*end == '\0') {
  193. if (nr == curr + 1)
  194. return true;
  195. continue;
  196. }
  197. if (strstr(tests[curr].desc, argv[i]))
  198. return true;
  199. }
  200. return false;
  201. }
  202. static int run_test(struct test *test)
  203. {
  204. int status, err = -1, child = fork();
  205. char sbuf[STRERR_BUFSIZE];
  206. if (child < 0) {
  207. pr_err("failed to fork test: %s\n",
  208. strerror_r(errno, sbuf, sizeof(sbuf)));
  209. return -1;
  210. }
  211. if (!child) {
  212. pr_debug("test child forked, pid %d\n", getpid());
  213. err = test->func();
  214. exit(err);
  215. }
  216. wait(&status);
  217. if (WIFEXITED(status)) {
  218. err = (signed char)WEXITSTATUS(status);
  219. pr_debug("test child finished with %d\n", err);
  220. } else if (WIFSIGNALED(status)) {
  221. err = -1;
  222. pr_debug("test child interrupted\n");
  223. }
  224. return err;
  225. }
  226. static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
  227. {
  228. int i = 0;
  229. int width = 0;
  230. while (tests[i].func) {
  231. int len = strlen(tests[i].desc);
  232. if (width < len)
  233. width = len;
  234. ++i;
  235. }
  236. i = 0;
  237. while (tests[i].func) {
  238. int curr = i++, err;
  239. if (!perf_test__matches(curr, argc, argv))
  240. continue;
  241. pr_info("%2d: %-*s:", i, width, tests[curr].desc);
  242. if (intlist__find(skiplist, i)) {
  243. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
  244. continue;
  245. }
  246. pr_debug("\n--- start ---\n");
  247. err = run_test(&tests[curr]);
  248. pr_debug("---- end ----\n%s:", tests[curr].desc);
  249. switch (err) {
  250. case TEST_OK:
  251. pr_info(" Ok\n");
  252. break;
  253. case TEST_SKIP:
  254. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
  255. break;
  256. case TEST_FAIL:
  257. default:
  258. color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
  259. break;
  260. }
  261. }
  262. return 0;
  263. }
  264. static int perf_test__list(int argc, const char **argv)
  265. {
  266. int i = 0;
  267. while (tests[i].func) {
  268. int curr = i++;
  269. if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
  270. continue;
  271. pr_info("%2d: %s\n", i, tests[curr].desc);
  272. }
  273. return 0;
  274. }
  275. int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
  276. {
  277. const char *test_usage[] = {
  278. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  279. NULL,
  280. };
  281. const char *skip = NULL;
  282. const struct option test_options[] = {
  283. OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
  284. OPT_INCR('v', "verbose", &verbose,
  285. "be more verbose (show symbol address, etc)"),
  286. OPT_END()
  287. };
  288. const char * const test_subcommands[] = { "list", NULL };
  289. struct intlist *skiplist = NULL;
  290. int ret = hists__init();
  291. if (ret < 0)
  292. return ret;
  293. argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
  294. if (argc >= 1 && !strcmp(argv[0], "list"))
  295. return perf_test__list(argc, argv);
  296. symbol_conf.priv_size = sizeof(int);
  297. symbol_conf.sort_by_name = true;
  298. symbol_conf.try_vmlinux_path = true;
  299. if (symbol__init(NULL) < 0)
  300. return -1;
  301. if (skip != NULL)
  302. skiplist = intlist__new(skip);
  303. return __cmd_test(argc, argv, skiplist);
  304. }