perf-time-to-tsc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <limits.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <linux/types.h>
  9. #include <sys/prctl.h>
  10. #include <perf/cpumap.h>
  11. #include <perf/evlist.h>
  12. #include <perf/mmap.h>
  13. #include "debug.h"
  14. #include "parse-events.h"
  15. #include "evlist.h"
  16. #include "evsel.h"
  17. #include "thread_map.h"
  18. #include "record.h"
  19. #include "tsc.h"
  20. #include "mmap.h"
  21. #include "tests.h"
  22. #include "util/sample.h"
  23. /*
  24. * Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just
  25. * enable the test for x86_64/i386 and Arm64 archs.
  26. */
  27. #if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__)
  28. #define TSC_IS_SUPPORTED 1
  29. #else
  30. #define TSC_IS_SUPPORTED 0
  31. #endif
  32. #define CHECK__(x) { \
  33. while ((x) < 0) { \
  34. pr_debug(#x " failed!\n"); \
  35. goto out_err; \
  36. } \
  37. }
  38. #define CHECK_NOT_NULL__(x) { \
  39. while ((x) == NULL) { \
  40. pr_debug(#x " failed!\n"); \
  41. goto out_err; \
  42. } \
  43. }
  44. static int test__tsc_is_supported(struct test_suite *test __maybe_unused,
  45. int subtest __maybe_unused)
  46. {
  47. if (!TSC_IS_SUPPORTED) {
  48. pr_debug("Test not supported on this architecture\n");
  49. return TEST_SKIP;
  50. }
  51. return TEST_OK;
  52. }
  53. /**
  54. * test__perf_time_to_tsc - test converting perf time to TSC.
  55. *
  56. * This function implements a test that checks that the conversion of perf time
  57. * to and from TSC is consistent with the order of events. If the test passes
  58. * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
  59. * supported then the test passes but " (not supported)" is printed.
  60. */
  61. static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  62. {
  63. struct record_opts opts = {
  64. .mmap_pages = UINT_MAX,
  65. .user_freq = UINT_MAX,
  66. .user_interval = ULLONG_MAX,
  67. .target = {
  68. .uses_mmap = true,
  69. },
  70. .sample_time = true,
  71. };
  72. struct perf_thread_map *threads = NULL;
  73. struct perf_cpu_map *cpus = NULL;
  74. struct evlist *evlist = NULL;
  75. struct evsel *evsel = NULL;
  76. int err = TEST_FAIL, ret, i;
  77. const char *comm1, *comm2;
  78. struct perf_tsc_conversion tc;
  79. struct perf_event_mmap_page *pc;
  80. union perf_event *event;
  81. u64 test_tsc, comm1_tsc, comm2_tsc;
  82. u64 test_time, comm1_time = 0, comm2_time = 0;
  83. struct mmap *md;
  84. threads = thread_map__new_by_tid(getpid());
  85. CHECK_NOT_NULL__(threads);
  86. cpus = perf_cpu_map__new_online_cpus();
  87. CHECK_NOT_NULL__(cpus);
  88. evlist = evlist__new();
  89. CHECK_NOT_NULL__(evlist);
  90. perf_evlist__set_maps(&evlist->core, cpus, threads);
  91. CHECK__(parse_event(evlist, "cpu-cycles:u"));
  92. evlist__config(evlist, &opts, NULL);
  93. /* For hybrid "cpu-cycles:u", it creates two events */
  94. evlist__for_each_entry(evlist, evsel) {
  95. evsel->core.attr.comm = 1;
  96. evsel->core.attr.disabled = 1;
  97. evsel->core.attr.enable_on_exec = 0;
  98. }
  99. ret = evlist__open(evlist);
  100. if (ret < 0) {
  101. if (ret == -ENOENT)
  102. err = TEST_SKIP;
  103. else
  104. pr_debug("evlist__open() failed\n");
  105. goto out_err;
  106. }
  107. CHECK__(evlist__mmap(evlist, UINT_MAX));
  108. pc = evlist->mmap[0].core.base;
  109. ret = perf_read_tsc_conversion(pc, &tc);
  110. if (ret) {
  111. if (ret == -EOPNOTSUPP) {
  112. pr_debug("perf_read_tsc_conversion is not supported in current kernel\n");
  113. err = TEST_SKIP;
  114. }
  115. goto out_err;
  116. }
  117. evlist__enable(evlist);
  118. comm1 = "Test COMM 1";
  119. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
  120. test_tsc = rdtsc();
  121. comm2 = "Test COMM 2";
  122. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
  123. evlist__disable(evlist);
  124. for (i = 0; i < evlist->core.nr_mmaps; i++) {
  125. md = &evlist->mmap[i];
  126. if (perf_mmap__read_init(&md->core) < 0)
  127. continue;
  128. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  129. struct perf_sample sample;
  130. perf_sample__init(&sample, /*all=*/false);
  131. if (event->header.type != PERF_RECORD_COMM ||
  132. (pid_t)event->comm.pid != getpid() ||
  133. (pid_t)event->comm.tid != getpid())
  134. goto next_event;
  135. if (strcmp(event->comm.comm, comm1) == 0) {
  136. CHECK_NOT_NULL__(evsel = evlist__event2evsel(evlist, event));
  137. CHECK__(evsel__parse_sample(evsel, event, &sample));
  138. comm1_time = sample.time;
  139. }
  140. if (strcmp(event->comm.comm, comm2) == 0) {
  141. CHECK_NOT_NULL__(evsel = evlist__event2evsel(evlist, event));
  142. CHECK__(evsel__parse_sample(evsel, event, &sample));
  143. comm2_time = sample.time;
  144. }
  145. next_event:
  146. perf_mmap__consume(&md->core);
  147. perf_sample__exit(&sample);
  148. }
  149. perf_mmap__read_done(&md->core);
  150. }
  151. if (!comm1_time || !comm2_time)
  152. goto out_err;
  153. test_time = tsc_to_perf_time(test_tsc, &tc);
  154. comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
  155. comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
  156. pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
  157. comm1_time, comm1_tsc);
  158. pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n",
  159. test_time, test_tsc);
  160. pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
  161. comm2_time, comm2_tsc);
  162. if (test_time <= comm1_time ||
  163. test_time >= comm2_time)
  164. goto out_err;
  165. if (test_tsc <= comm1_tsc ||
  166. test_tsc >= comm2_tsc)
  167. goto out_err;
  168. err = TEST_OK;
  169. out_err:
  170. evlist__delete(evlist);
  171. perf_cpu_map__put(cpus);
  172. perf_thread_map__put(threads);
  173. return err;
  174. }
  175. static struct test_case time_to_tsc_tests[] = {
  176. TEST_CASE_REASON("TSC support", tsc_is_supported,
  177. "This architecture does not support"),
  178. TEST_CASE_REASON("Perf time to TSC", perf_time_to_tsc,
  179. "perf_read_tsc_conversion is not supported"),
  180. { .name = NULL, }
  181. };
  182. struct test_suite suite__perf_time_to_tsc = {
  183. .desc = "Convert perf time to TSC",
  184. .test_cases = time_to_tsc_tests,
  185. };