builtin-test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * builtin-test.c
  3. *
  4. * Builtin regression testing command: ever growing number of sanity tests
  5. */
  6. #include "builtin.h"
  7. #include "intlist.h"
  8. #include "tests.h"
  9. #include "debug.h"
  10. #include "color.h"
  11. #include "parse-options.h"
  12. #include "symbol.h"
  13. static struct test {
  14. const char *desc;
  15. int (*func)(void);
  16. } tests[] = {
  17. {
  18. .desc = "vmlinux symtab matches kallsyms",
  19. .func = test__vmlinux_matches_kallsyms,
  20. },
  21. {
  22. .desc = "detect open syscall event",
  23. .func = test__open_syscall_event,
  24. },
  25. {
  26. .desc = "detect open syscall event on all cpus",
  27. .func = test__open_syscall_event_on_all_cpus,
  28. },
  29. {
  30. .desc = "read samples using the mmap interface",
  31. .func = test__basic_mmap,
  32. },
  33. {
  34. .desc = "parse events tests",
  35. .func = test__parse_events,
  36. },
  37. #if defined(__x86_64__) || defined(__i386__)
  38. {
  39. .desc = "x86 rdpmc test",
  40. .func = test__rdpmc,
  41. },
  42. #endif
  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 interface",
  53. .func = test__dso_data,
  54. },
  55. {
  56. .desc = "roundtrip evsel->name check",
  57. .func = test__perf_evsel__roundtrip_name_test,
  58. },
  59. {
  60. .desc = "Check parsing of sched tracepoints fields",
  61. .func = test__perf_evsel__tp_sched_test,
  62. },
  63. {
  64. .desc = "Generate and check syscalls:sys_enter_open event fields",
  65. .func = test__syscall_open_tp_fields,
  66. },
  67. {
  68. .desc = "struct perf_event_attr setup",
  69. .func = test__attr,
  70. },
  71. {
  72. .desc = "Test matching and linking multiple hists",
  73. .func = test__hists_link,
  74. },
  75. {
  76. .desc = "Try 'use perf' in python, checking link problems",
  77. .func = test__python_use,
  78. },
  79. {
  80. .desc = "Test breakpoint overflow signal handler",
  81. .func = test__bp_signal,
  82. },
  83. {
  84. .desc = "Test breakpoint overflow sampling",
  85. .func = test__bp_signal_overflow,
  86. },
  87. {
  88. .desc = "Test number of exit event of a simple workload",
  89. .func = test__task_exit,
  90. },
  91. {
  92. .desc = "Test software clock events have valid period values",
  93. .func = test__sw_clock_freq,
  94. },
  95. #if defined(__x86_64__) || defined(__i386__)
  96. {
  97. .desc = "Test converting perf time to TSC",
  98. .func = test__perf_time_to_tsc,
  99. },
  100. #endif
  101. {
  102. .desc = "Test object code reading",
  103. .func = test__code_reading,
  104. },
  105. {
  106. .desc = "Test sample parsing",
  107. .func = test__sample_parsing,
  108. },
  109. {
  110. .desc = "Test using a dummy software event to keep tracking",
  111. .func = test__keep_tracking,
  112. },
  113. {
  114. .desc = "Test parsing with no sample_id_all bit set",
  115. .func = test__parse_no_sample_id_all,
  116. },
  117. #if defined(__x86_64__) || defined(__i386__)
  118. #ifdef HAVE_DWARF_UNWIND_SUPPORT
  119. {
  120. .desc = "Test dwarf unwind",
  121. .func = test__dwarf_unwind,
  122. },
  123. #endif
  124. #endif
  125. {
  126. .func = NULL,
  127. },
  128. };
  129. static bool perf_test__matches(int curr, int argc, const char *argv[])
  130. {
  131. int i;
  132. if (argc == 0)
  133. return true;
  134. for (i = 0; i < argc; ++i) {
  135. char *end;
  136. long nr = strtoul(argv[i], &end, 10);
  137. if (*end == '\0') {
  138. if (nr == curr + 1)
  139. return true;
  140. continue;
  141. }
  142. if (strstr(tests[curr].desc, argv[i]))
  143. return true;
  144. }
  145. return false;
  146. }
  147. static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
  148. {
  149. int i = 0;
  150. int width = 0;
  151. while (tests[i].func) {
  152. int len = strlen(tests[i].desc);
  153. if (width < len)
  154. width = len;
  155. ++i;
  156. }
  157. i = 0;
  158. while (tests[i].func) {
  159. int curr = i++, err;
  160. if (!perf_test__matches(curr, argc, argv))
  161. continue;
  162. pr_info("%2d: %-*s:", i, width, tests[curr].desc);
  163. if (intlist__find(skiplist, i)) {
  164. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
  165. continue;
  166. }
  167. pr_debug("\n--- start ---\n");
  168. err = tests[curr].func();
  169. pr_debug("---- end ----\n%s:", tests[curr].desc);
  170. switch (err) {
  171. case TEST_OK:
  172. pr_info(" Ok\n");
  173. break;
  174. case TEST_SKIP:
  175. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
  176. break;
  177. case TEST_FAIL:
  178. default:
  179. color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
  180. break;
  181. }
  182. }
  183. return 0;
  184. }
  185. static int perf_test__list(int argc, const char **argv)
  186. {
  187. int i = 0;
  188. while (tests[i].func) {
  189. int curr = i++;
  190. if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
  191. continue;
  192. pr_info("%2d: %s\n", i, tests[curr].desc);
  193. }
  194. return 0;
  195. }
  196. int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
  197. {
  198. const char * const test_usage[] = {
  199. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  200. NULL,
  201. };
  202. const char *skip = NULL;
  203. const struct option test_options[] = {
  204. OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
  205. OPT_INCR('v', "verbose", &verbose,
  206. "be more verbose (show symbol address, etc)"),
  207. OPT_END()
  208. };
  209. struct intlist *skiplist = NULL;
  210. argc = parse_options(argc, argv, test_options, test_usage, 0);
  211. if (argc >= 1 && !strcmp(argv[0], "list"))
  212. return perf_test__list(argc, argv);
  213. symbol_conf.priv_size = sizeof(int);
  214. symbol_conf.sort_by_name = true;
  215. symbol_conf.try_vmlinux_path = true;
  216. if (symbol__init() < 0)
  217. return -1;
  218. if (skip != NULL)
  219. skiplist = intlist__new(skip);
  220. return __cmd_test(argc, argv, skiplist);
  221. }