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