perf-time-to-tsc.c 3.7 KB

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