builtin-test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * builtin-test.c
  3. *
  4. * Builtin regression testing command: ever growing number of sanity tests
  5. */
  6. #include "builtin.h"
  7. #include "tests.h"
  8. #include "debug.h"
  9. #include "color.h"
  10. #include "parse-options.h"
  11. #include "symbol.h"
  12. static struct test {
  13. const char *desc;
  14. int (*func)(void);
  15. } tests[] = {
  16. {
  17. .desc = "vmlinux symtab matches kallsyms",
  18. .func = test__vmlinux_matches_kallsyms,
  19. },
  20. {
  21. .desc = "detect open syscall event",
  22. .func = test__open_syscall_event,
  23. },
  24. {
  25. .desc = "detect open syscall event on all cpus",
  26. .func = test__open_syscall_event_on_all_cpus,
  27. },
  28. {
  29. .desc = "read samples using the mmap interface",
  30. .func = test__basic_mmap,
  31. },
  32. {
  33. .desc = "parse events tests",
  34. .func = test__parse_events,
  35. },
  36. #if defined(__x86_64__) || defined(__i386__)
  37. {
  38. .desc = "x86 rdpmc test",
  39. .func = test__rdpmc,
  40. },
  41. #endif
  42. {
  43. .desc = "Validate PERF_RECORD_* events & perf_sample fields",
  44. .func = test__PERF_RECORD,
  45. },
  46. {
  47. .desc = "Test perf pmu format parsing",
  48. .func = test__pmu,
  49. },
  50. {
  51. .desc = "Test dso data interface",
  52. .func = test__dso_data,
  53. },
  54. {
  55. .desc = "roundtrip evsel->name check",
  56. .func = test__perf_evsel__roundtrip_name_test,
  57. },
  58. {
  59. .desc = "Check parsing of sched tracepoints fields",
  60. .func = test__perf_evsel__tp_sched_test,
  61. },
  62. {
  63. .desc = "Generate and check syscalls:sys_enter_open event fields",
  64. .func = test__syscall_open_tp_fields,
  65. },
  66. {
  67. .desc = "struct perf_event_attr setup",
  68. .func = test__attr,
  69. },
  70. {
  71. .desc = "Test matching and linking mutliple hists",
  72. .func = test__hists_link,
  73. },
  74. {
  75. .desc = "Try 'use perf' in python, checking link problems",
  76. .func = test__python_use,
  77. },
  78. {
  79. .func = NULL,
  80. },
  81. };
  82. static bool perf_test__matches(int curr, int argc, const char *argv[])
  83. {
  84. int i;
  85. if (argc == 0)
  86. return true;
  87. for (i = 0; i < argc; ++i) {
  88. char *end;
  89. long nr = strtoul(argv[i], &end, 10);
  90. if (*end == '\0') {
  91. if (nr == curr + 1)
  92. return true;
  93. continue;
  94. }
  95. if (strstr(tests[curr].desc, argv[i]))
  96. return true;
  97. }
  98. return false;
  99. }
  100. static int __cmd_test(int argc, const char *argv[])
  101. {
  102. int i = 0;
  103. int width = 0;
  104. while (tests[i].func) {
  105. int len = strlen(tests[i].desc);
  106. if (width < len)
  107. width = len;
  108. ++i;
  109. }
  110. i = 0;
  111. while (tests[i].func) {
  112. int curr = i++, err;
  113. if (!perf_test__matches(curr, argc, argv))
  114. continue;
  115. pr_info("%2d: %-*s:", i, width, tests[curr].desc);
  116. pr_debug("\n--- start ---\n");
  117. err = tests[curr].func();
  118. pr_debug("---- end ----\n%s:", tests[curr].desc);
  119. switch (err) {
  120. case TEST_OK:
  121. pr_info(" Ok\n");
  122. break;
  123. case TEST_SKIP:
  124. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
  125. break;
  126. case TEST_FAIL:
  127. default:
  128. color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
  129. break;
  130. }
  131. }
  132. return 0;
  133. }
  134. static int perf_test__list(int argc, const char **argv)
  135. {
  136. int i = 0;
  137. while (tests[i].func) {
  138. int curr = i++;
  139. if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
  140. continue;
  141. pr_info("%2d: %s\n", i, tests[curr].desc);
  142. }
  143. return 0;
  144. }
  145. int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
  146. {
  147. const char * const test_usage[] = {
  148. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  149. NULL,
  150. };
  151. const struct option test_options[] = {
  152. OPT_INCR('v', "verbose", &verbose,
  153. "be more verbose (show symbol address, etc)"),
  154. OPT_END()
  155. };
  156. argc = parse_options(argc, argv, test_options, test_usage, 0);
  157. if (argc >= 1 && !strcmp(argv[0], "list"))
  158. return perf_test__list(argc, argv);
  159. symbol_conf.priv_size = sizeof(int);
  160. symbol_conf.sort_by_name = true;
  161. symbol_conf.try_vmlinux_path = true;
  162. if (symbol__init() < 0)
  163. return -1;
  164. return __cmd_test(argc, argv);
  165. }