rdpmc.c 3.2 KB

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