rdpmc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. sigfillset(&sa.sa_mask);
  84. sa.sa_sigaction = segfault_handler;
  85. sigaction(SIGSEGV, &sa, NULL);
  86. fd = sys_perf_event_open(&attr, 0, -1, -1,
  87. perf_event_open_cloexec_flag());
  88. if (fd < 0) {
  89. pr_err("Error: sys_perf_event_open() syscall returned "
  90. "with %d (%s)\n", fd, strerror(errno));
  91. return -1;
  92. }
  93. addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
  94. if (addr == (void *)(-1)) {
  95. pr_err("Error: mmap() syscall returned with (%s)\n",
  96. strerror(errno));
  97. goto out_close;
  98. }
  99. for (n = 0; n < 6; n++) {
  100. u64 stamp, now, delta;
  101. stamp = mmap_read_self(addr);
  102. for (i = 0; i < loops; i++)
  103. tmp++;
  104. now = mmap_read_self(addr);
  105. loops *= 10;
  106. delta = now - stamp;
  107. pr_debug("%14d: %14Lu\n", n, (long long)delta);
  108. delta_sum += delta;
  109. }
  110. munmap(addr, page_size);
  111. pr_debug(" ");
  112. out_close:
  113. close(fd);
  114. if (!delta_sum)
  115. return -1;
  116. return 0;
  117. }
  118. int test__rdpmc(void)
  119. {
  120. int status = 0;
  121. int wret = 0;
  122. int ret;
  123. int pid;
  124. pid = fork();
  125. if (pid < 0)
  126. return -1;
  127. if (!pid) {
  128. ret = __test__rdpmc();
  129. exit(ret);
  130. }
  131. wret = waitpid(pid, &status, 0);
  132. if (wret < 0 || status)
  133. return -1;
  134. return 0;
  135. }
  136. #endif