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