builtin-test.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. .func = NULL,
  72. },
  73. };
  74. static bool perf_test__matches(int curr, int argc, const char *argv[])
  75. {
  76. int i;
  77. if (argc == 0)
  78. return true;
  79. for (i = 0; i < argc; ++i) {
  80. char *end;
  81. long nr = strtoul(argv[i], &end, 10);
  82. if (*end == '\0') {
  83. if (nr == curr + 1)
  84. return true;
  85. continue;
  86. }
  87. if (strstr(tests[curr].desc, argv[i]))
  88. return true;
  89. }
  90. return false;
  91. }
  92. static int __cmd_test(int argc, const char *argv[])
  93. {
  94. int i = 0;
  95. int width = 0;
  96. while (tests[i].func) {
  97. int len = strlen(tests[i].desc);
  98. if (width < len)
  99. width = len;
  100. ++i;
  101. }
  102. i = 0;
  103. while (tests[i].func) {
  104. int curr = i++, err;
  105. if (!perf_test__matches(curr, argc, argv))
  106. continue;
  107. pr_info("%2d: %-*s:", i, width, tests[curr].desc);
  108. pr_debug("\n--- start ---\n");
  109. err = tests[curr].func();
  110. pr_debug("---- end ----\n%s:", tests[curr].desc);
  111. if (err)
  112. color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
  113. else
  114. pr_info(" Ok\n");
  115. }
  116. return 0;
  117. }
  118. static int perf_test__list(int argc, const char **argv)
  119. {
  120. int i = 0;
  121. while (tests[i].func) {
  122. int curr = i++;
  123. if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
  124. continue;
  125. pr_info("%2d: %s\n", i, tests[curr].desc);
  126. }
  127. return 0;
  128. }
  129. int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
  130. {
  131. const char * const test_usage[] = {
  132. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  133. NULL,
  134. };
  135. const struct option test_options[] = {
  136. OPT_INCR('v', "verbose", &verbose,
  137. "be more verbose (show symbol address, etc)"),
  138. OPT_END()
  139. };
  140. argc = parse_options(argc, argv, test_options, test_usage, 0);
  141. if (argc >= 1 && !strcmp(argv[0], "list"))
  142. return perf_test__list(argc, argv);
  143. symbol_conf.priv_size = sizeof(int);
  144. symbol_conf.sort_by_name = true;
  145. symbol_conf.try_vmlinux_path = true;
  146. if (symbol__init() < 0)
  147. return -1;
  148. return __cmd_test(argc, argv);
  149. }