perf-time-to-tsc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <inttypes.h>
  5. #include <sys/prctl.h>
  6. #include "parse-events.h"
  7. #include "evlist.h"
  8. #include "evsel.h"
  9. #include "thread_map.h"
  10. #include "cpumap.h"
  11. #include "tests.h"
  12. #include "../arch/x86/util/tsc.h"
  13. #define CHECK__(x) { \
  14. while ((x) < 0) { \
  15. pr_debug(#x " failed!\n"); \
  16. goto out_err; \
  17. } \
  18. }
  19. #define CHECK_NOT_NULL__(x) { \
  20. while ((x) == NULL) { \
  21. pr_debug(#x " failed!\n"); \
  22. goto out_err; \
  23. } \
  24. }
  25. static u64 rdtsc(void)
  26. {
  27. unsigned int low, high;
  28. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  29. return low | ((u64)high) << 32;
  30. }
  31. /**
  32. * test__perf_time_to_tsc - test converting perf time to TSC.
  33. *
  34. * This function implements a test that checks that the conversion of perf time
  35. * to and from TSC is consistent with the order of events. If the test passes
  36. * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
  37. * supported then then the test passes but " (not supported)" is printed.
  38. */
  39. int test__perf_time_to_tsc(void)
  40. {
  41. struct perf_record_opts opts = {
  42. .mmap_pages = UINT_MAX,
  43. .user_freq = UINT_MAX,
  44. .user_interval = ULLONG_MAX,
  45. .freq = 4000,
  46. .target = {
  47. .uses_mmap = true,
  48. },
  49. .sample_time = true,
  50. };
  51. struct thread_map *threads = NULL;
  52. struct cpu_map *cpus = NULL;
  53. struct perf_evlist *evlist = NULL;
  54. struct perf_evsel *evsel = NULL;
  55. int err = -1, ret, i;
  56. const char *comm1, *comm2;
  57. struct perf_tsc_conversion tc;
  58. struct perf_event_mmap_page *pc;
  59. union perf_event *event;
  60. u64 test_tsc, comm1_tsc, comm2_tsc;
  61. u64 test_time, comm1_time = 0, comm2_time = 0;
  62. threads = thread_map__new(-1, getpid(), UINT_MAX);
  63. CHECK_NOT_NULL__(threads);
  64. cpus = cpu_map__new(NULL);
  65. CHECK_NOT_NULL__(cpus);
  66. evlist = perf_evlist__new();
  67. CHECK_NOT_NULL__(evlist);
  68. perf_evlist__set_maps(evlist, cpus, threads);
  69. CHECK__(parse_events(evlist, "cycles:u"));
  70. perf_evlist__config(evlist, &opts);
  71. evsel = perf_evlist__first(evlist);
  72. evsel->attr.comm = 1;
  73. evsel->attr.disabled = 1;
  74. evsel->attr.enable_on_exec = 0;
  75. CHECK__(perf_evlist__open(evlist));
  76. CHECK__(perf_evlist__mmap(evlist, UINT_MAX, false));
  77. pc = evlist->mmap[0].base;
  78. ret = perf_read_tsc_conversion(pc, &tc);
  79. if (ret) {
  80. if (ret == -EOPNOTSUPP) {
  81. fprintf(stderr, " (not supported)");
  82. return 0;
  83. }
  84. goto out_err;
  85. }
  86. perf_evlist__enable(evlist);
  87. comm1 = "Test COMM 1";
  88. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
  89. test_tsc = rdtsc();
  90. comm2 = "Test COMM 2";
  91. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
  92. perf_evlist__disable(evlist);
  93. for (i = 0; i < evlist->nr_mmaps; i++) {
  94. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  95. struct perf_sample sample;
  96. if (event->header.type != PERF_RECORD_COMM ||
  97. (pid_t)event->comm.pid != getpid() ||
  98. (pid_t)event->comm.tid != getpid())
  99. continue;
  100. if (strcmp(event->comm.comm, comm1) == 0) {
  101. CHECK__(perf_evsel__parse_sample(evsel, event,
  102. &sample));
  103. comm1_time = sample.time;
  104. }
  105. if (strcmp(event->comm.comm, comm2) == 0) {
  106. CHECK__(perf_evsel__parse_sample(evsel, event,
  107. &sample));
  108. comm2_time = sample.time;
  109. }
  110. }
  111. }
  112. if (!comm1_time || !comm2_time)
  113. goto out_err;
  114. test_time = tsc_to_perf_time(test_tsc, &tc);
  115. comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
  116. comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
  117. pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
  118. comm1_time, comm1_tsc);
  119. pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n",
  120. test_time, test_tsc);
  121. pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
  122. comm2_time, comm2_tsc);
  123. if (test_time <= comm1_time ||
  124. test_time >= comm2_time)
  125. goto out_err;
  126. if (test_tsc <= comm1_tsc ||
  127. test_tsc >= comm2_tsc)
  128. goto out_err;
  129. err = 0;
  130. out_err:
  131. if (evlist) {
  132. perf_evlist__disable(evlist);
  133. perf_evlist__munmap(evlist);
  134. perf_evlist__close(evlist);
  135. perf_evlist__delete(evlist);
  136. }
  137. if (cpus)
  138. cpu_map__delete(cpus);
  139. if (threads)
  140. thread_map__delete(threads);
  141. return err;
  142. }