]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - tools/perf/arch/x86/tests/rdpmc.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-kernels.git] / tools / perf / arch / x86 / tests / rdpmc.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
10ca87fd 2#include <errno.h>
bacf7e5d
JO
3#include <unistd.h>
4#include <stdlib.h>
5#include <signal.h>
6#include <sys/mman.h>
10ca87fd
ACM
7#include <sys/types.h>
8#include <sys/wait.h>
d944c4ee 9#include <linux/types.h>
bacf7e5d
JO
10#include "perf.h"
11#include "debug.h"
d8b167f9 12#include "tests/tests.h"
57480d2c 13#include "cloexec.h"
10ca87fd 14#include "util.h"
d8b167f9 15#include "arch-tests.h"
bacf7e5d 16
bacf7e5d
JO
17static u64 rdpmc(unsigned int counter)
18{
19 unsigned int low, high;
20
21 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
22
23 return low | ((u64)high) << 32;
24}
25
26static u64 rdtsc(void)
27{
28 unsigned int low, high;
29
30 asm volatile("rdtsc" : "=a" (low), "=d" (high));
31
32 return low | ((u64)high) << 32;
33}
34
35static u64 mmap_read_self(void *addr)
36{
37 struct perf_event_mmap_page *pc = addr;
38 u32 seq, idx, time_mult = 0, time_shift = 0;
39 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
40
41 do {
42 seq = pc->lock;
43 barrier();
44
45 enabled = pc->time_enabled;
46 running = pc->time_running;
47
48 if (enabled != running) {
49 cyc = rdtsc();
50 time_mult = pc->time_mult;
51 time_shift = pc->time_shift;
52 time_offset = pc->time_offset;
53 }
54
55 idx = pc->index;
56 count = pc->offset;
57 if (idx)
58 count += rdpmc(idx - 1);
59
60 barrier();
61 } while (pc->lock != seq);
62
63 if (enabled != running) {
64 u64 quot, rem;
65
66 quot = (cyc >> time_shift);
a23f96ee 67 rem = cyc & (((u64)1 << time_shift) - 1);
bacf7e5d
JO
68 delta = time_offset + quot * time_mult +
69 ((rem * time_mult) >> time_shift);
70
71 enabled += delta;
72 if (idx)
73 running += delta;
74
75 quot = count / running;
76 rem = count % running;
77 count = quot * enabled + (rem * enabled) / running;
78 }
79
80 return count;
81}
82
83/*
84 * If the RDPMC instruction faults then signal this back to the test parent task:
85 */
86static void segfault_handler(int sig __maybe_unused,
87 siginfo_t *info __maybe_unused,
88 void *uc __maybe_unused)
89{
90 exit(-1);
91}
92
93static int __test__rdpmc(void)
94{
95 volatile int tmp = 0;
96 u64 i, loops = 1000;
97 int n;
98 int fd;
99 void *addr;
100 struct perf_event_attr attr = {
101 .type = PERF_TYPE_HARDWARE,
102 .config = PERF_COUNT_HW_INSTRUCTIONS,
103 .exclude_kernel = 1,
104 };
105 u64 delta_sum = 0;
106 struct sigaction sa;
ba3dfff8 107 char sbuf[STRERR_BUFSIZE];
bacf7e5d
JO
108
109 sigfillset(&sa.sa_mask);
110 sa.sa_sigaction = segfault_handler;
e17a0e16 111 sa.sa_flags = 0;
bacf7e5d
JO
112 sigaction(SIGSEGV, &sa, NULL);
113
57480d2c
YD
114 fd = sys_perf_event_open(&attr, 0, -1, -1,
115 perf_event_open_cloexec_flag());
bacf7e5d
JO
116 if (fd < 0) {
117 pr_err("Error: sys_perf_event_open() syscall returned "
ba3dfff8 118 "with %d (%s)\n", fd,
c8b5f2c9 119 str_error_r(errno, sbuf, sizeof(sbuf)));
bacf7e5d
JO
120 return -1;
121 }
122
123 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
124 if (addr == (void *)(-1)) {
125 pr_err("Error: mmap() syscall returned with (%s)\n",
c8b5f2c9 126 str_error_r(errno, sbuf, sizeof(sbuf)));
bacf7e5d
JO
127 goto out_close;
128 }
129
130 for (n = 0; n < 6; n++) {
131 u64 stamp, now, delta;
132
133 stamp = mmap_read_self(addr);
134
135 for (i = 0; i < loops; i++)
136 tmp++;
137
138 now = mmap_read_self(addr);
139 loops *= 10;
140
141 delta = now - stamp;
142 pr_debug("%14d: %14Lu\n", n, (long long)delta);
143
144 delta_sum += delta;
145 }
146
147 munmap(addr, page_size);
148 pr_debug(" ");
149out_close:
150 close(fd);
151
152 if (!delta_sum)
153 return -1;
154
155 return 0;
156}
157
81f17c90 158int test__rdpmc(struct test *test __maybe_unused, int subtest __maybe_unused)
bacf7e5d
JO
159{
160 int status = 0;
161 int wret = 0;
162 int ret;
163 int pid;
164
165 pid = fork();
166 if (pid < 0)
167 return -1;
168
169 if (!pid) {
170 ret = __test__rdpmc();
171
172 exit(ret);
173 }
174
175 wret = waitpid(pid, &status, 0);
176 if (wret < 0 || status)
177 return -1;
178
179 return 0;
180}