rdpmc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <errno.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <sys/mman.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <linux/types.h>
  9. #include "perf.h"
  10. #include "debug.h"
  11. #include "tests/tests.h"
  12. #include "cloexec.h"
  13. #include "util.h"
  14. #include "arch-tests.h"
  15. static u64 rdpmc(unsigned int counter)
  16. {
  17. unsigned int low, high;
  18. asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
  19. return low | ((u64)high) << 32;
  20. }
  21. static u64 rdtsc(void)
  22. {
  23. unsigned int low, high;
  24. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  25. return low | ((u64)high) << 32;
  26. }
  27. static u64 mmap_read_self(void *addr)
  28. {
  29. struct perf_event_mmap_page *pc = addr;
  30. u32 seq, idx, time_mult = 0, time_shift = 0;
  31. u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
  32. do {
  33. seq = pc->lock;
  34. barrier();
  35. enabled = pc->time_enabled;
  36. running = pc->time_running;
  37. if (enabled != running) {
  38. cyc = rdtsc();
  39. time_mult = pc->time_mult;
  40. time_shift = pc->time_shift;
  41. time_offset = pc->time_offset;
  42. }
  43. idx = pc->index;
  44. count = pc->offset;
  45. if (idx)
  46. count += rdpmc(idx - 1);
  47. barrier();
  48. } while (pc->lock != seq);
  49. if (enabled != running) {
  50. u64 quot, rem;
  51. quot = (cyc >> time_shift);
  52. rem = cyc & (((u64)1 << time_shift) - 1);
  53. delta = time_offset + quot * time_mult +
  54. ((rem * time_mult) >> time_shift);
  55. enabled += delta;
  56. if (idx)
  57. running += delta;
  58. quot = count / running;
  59. rem = count % running;
  60. count = quot * enabled + (rem * enabled) / running;
  61. }
  62. return count;
  63. }
  64. /*
  65. * If the RDPMC instruction faults then signal this back to the test parent task:
  66. */
  67. static void segfault_handler(int sig __maybe_unused,
  68. siginfo_t *info __maybe_unused,
  69. void *uc __maybe_unused)
  70. {
  71. exit(-1);
  72. }
  73. static int __test__rdpmc(void)
  74. {
  75. volatile int tmp = 0;
  76. u64 i, loops = 1000;
  77. int n;
  78. int fd;
  79. void *addr;
  80. struct perf_event_attr attr = {
  81. .type = PERF_TYPE_HARDWARE,
  82. .config = PERF_COUNT_HW_INSTRUCTIONS,
  83. .exclude_kernel = 1,
  84. };
  85. u64 delta_sum = 0;
  86. struct sigaction sa;
  87. char sbuf[STRERR_BUFSIZE];
  88. sigfillset(&sa.sa_mask);
  89. sa.sa_sigaction = segfault_handler;
  90. sa.sa_flags = 0;
  91. sigaction(SIGSEGV, &sa, NULL);
  92. fd = sys_perf_event_open(&attr, 0, -1, -1,
  93. perf_event_open_cloexec_flag());
  94. if (fd < 0) {
  95. pr_err("Error: sys_perf_event_open() syscall returned "
  96. "with %d (%s)\n", fd,
  97. str_error_r(errno, sbuf, sizeof(sbuf)));
  98. return -1;
  99. }
  100. addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
  101. if (addr == (void *)(-1)) {
  102. pr_err("Error: mmap() syscall returned with (%s)\n",
  103. str_error_r(errno, sbuf, sizeof(sbuf)));
  104. goto out_close;
  105. }
  106. for (n = 0; n < 6; n++) {
  107. u64 stamp, now, delta;
  108. stamp = mmap_read_self(addr);
  109. for (i = 0; i < loops; i++)
  110. tmp++;
  111. now = mmap_read_self(addr);
  112. loops *= 10;
  113. delta = now - stamp;
  114. pr_debug("%14d: %14Lu\n", n, (long long)delta);
  115. delta_sum += delta;
  116. }
  117. munmap(addr, page_size);
  118. pr_debug(" ");
  119. out_close:
  120. close(fd);
  121. if (!delta_sum)
  122. return -1;
  123. return 0;
  124. }
  125. int test__rdpmc(int subtest __maybe_unused)
  126. {
  127. int status = 0;
  128. int wret = 0;
  129. int ret;
  130. int pid;
  131. pid = fork();
  132. if (pid < 0)
  133. return -1;
  134. if (!pid) {
  135. ret = __test__rdpmc();
  136. exit(ret);
  137. }
  138. wret = waitpid(pid, &status, 0);
  139. if (wret < 0 || status)
  140. return -1;
  141. return 0;
  142. }