of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / tools / perf / arch / x86 / tests / rdpmc.c
blob7bb0d13c235f70326afc28d726f38f1d95499055
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/tests.h"
9 #include "cloexec.h"
10 #include "arch-tests.h"
12 static u64 rdpmc(unsigned int counter)
14 unsigned int low, high;
16 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
18 return low | ((u64)high) << 32;
21 static u64 rdtsc(void)
23 unsigned int low, high;
25 asm volatile("rdtsc" : "=a" (low), "=d" (high));
27 return low | ((u64)high) << 32;
30 static u64 mmap_read_self(void *addr)
32 struct perf_event_mmap_page *pc = addr;
33 u32 seq, idx, time_mult = 0, time_shift = 0;
34 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
36 do {
37 seq = pc->lock;
38 barrier();
40 enabled = pc->time_enabled;
41 running = pc->time_running;
43 if (enabled != running) {
44 cyc = rdtsc();
45 time_mult = pc->time_mult;
46 time_shift = pc->time_shift;
47 time_offset = pc->time_offset;
50 idx = pc->index;
51 count = pc->offset;
52 if (idx)
53 count += rdpmc(idx - 1);
55 barrier();
56 } while (pc->lock != seq);
58 if (enabled != running) {
59 u64 quot, rem;
61 quot = (cyc >> time_shift);
62 rem = cyc & ((1 << time_shift) - 1);
63 delta = time_offset + quot * time_mult +
64 ((rem * time_mult) >> time_shift);
66 enabled += delta;
67 if (idx)
68 running += delta;
70 quot = count / running;
71 rem = count % running;
72 count = quot * enabled + (rem * enabled) / running;
75 return count;
79 * If the RDPMC instruction faults then signal this back to the test parent task:
81 static void segfault_handler(int sig __maybe_unused,
82 siginfo_t *info __maybe_unused,
83 void *uc __maybe_unused)
85 exit(-1);
88 static int __test__rdpmc(void)
90 volatile int tmp = 0;
91 u64 i, loops = 1000;
92 int n;
93 int fd;
94 void *addr;
95 struct perf_event_attr attr = {
96 .type = PERF_TYPE_HARDWARE,
97 .config = PERF_COUNT_HW_INSTRUCTIONS,
98 .exclude_kernel = 1,
100 u64 delta_sum = 0;
101 struct sigaction sa;
102 char sbuf[STRERR_BUFSIZE];
104 sigfillset(&sa.sa_mask);
105 sa.sa_sigaction = segfault_handler;
106 sigaction(SIGSEGV, &sa, NULL);
108 fd = sys_perf_event_open(&attr, 0, -1, -1,
109 perf_event_open_cloexec_flag());
110 if (fd < 0) {
111 pr_err("Error: sys_perf_event_open() syscall returned "
112 "with %d (%s)\n", fd,
113 strerror_r(errno, sbuf, sizeof(sbuf)));
114 return -1;
117 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
118 if (addr == (void *)(-1)) {
119 pr_err("Error: mmap() syscall returned with (%s)\n",
120 strerror_r(errno, sbuf, sizeof(sbuf)));
121 goto out_close;
124 for (n = 0; n < 6; n++) {
125 u64 stamp, now, delta;
127 stamp = mmap_read_self(addr);
129 for (i = 0; i < loops; i++)
130 tmp++;
132 now = mmap_read_self(addr);
133 loops *= 10;
135 delta = now - stamp;
136 pr_debug("%14d: %14Lu\n", n, (long long)delta);
138 delta_sum += delta;
141 munmap(addr, page_size);
142 pr_debug(" ");
143 out_close:
144 close(fd);
146 if (!delta_sum)
147 return -1;
149 return 0;
152 int test__rdpmc(int subtest __maybe_unused)
154 int status = 0;
155 int wret = 0;
156 int ret;
157 int pid;
159 pid = fork();
160 if (pid < 0)
161 return -1;
163 if (!pid) {
164 ret = __test__rdpmc();
166 exit(ret);
169 wret = waitpid(pid, &status, 0);
170 if (wret < 0 || status)
171 return -1;
173 return 0;