1*d8b80295SApple OSS Distributions // Copyright (c) 2021-2022 Apple Inc. All rights reserved.
2*d8b80295SApple OSS Distributions
3*d8b80295SApple OSS Distributions #include <darwintest.h>
4*d8b80295SApple OSS Distributions #include "test_utils.h"
5*d8b80295SApple OSS Distributions #include <fcntl.h>
6*d8b80295SApple OSS Distributions #include <inttypes.h>
7*d8b80295SApple OSS Distributions #ifndef PRIVATE
8*d8b80295SApple OSS Distributions /*
9*d8b80295SApple OSS Distributions * Need new CPU families.
10*d8b80295SApple OSS Distributions */
11*d8b80295SApple OSS Distributions #define PRIVATE
12*d8b80295SApple OSS Distributions #include <mach/machine.h>
13*d8b80295SApple OSS Distributions #undef PRIVATE
14*d8b80295SApple OSS Distributions #else /* !defined(PRIVATE) */
15*d8b80295SApple OSS Distributions #include <mach/machine.h>
16*d8b80295SApple OSS Distributions #endif /* defined(PRIVATE) */
17*d8b80295SApple OSS Distributions #include <stdint.h>
18*d8b80295SApple OSS Distributions #include <System/sys/guarded.h>
19*d8b80295SApple OSS Distributions #include <System/sys/monotonic.h>
20*d8b80295SApple OSS Distributions #include <sys/ioctl.h>
21*d8b80295SApple OSS Distributions #include <sys/sysctl.h>
22*d8b80295SApple OSS Distributions #include <unistd.h>
23*d8b80295SApple OSS Distributions
24*d8b80295SApple OSS Distributions T_GLOBAL_META(
25*d8b80295SApple OSS Distributions T_META_NAMESPACE("xnu.monotonic"),
26*d8b80295SApple OSS Distributions T_META_CHECK_LEAKS(false),
27*d8b80295SApple OSS Distributions XNU_T_META_REQUIRES_DEVELOPMENT_KERNEL
28*d8b80295SApple OSS Distributions );
29*d8b80295SApple OSS Distributions
30*d8b80295SApple OSS Distributions static bool
device_supports_uncore(void)31*d8b80295SApple OSS Distributions device_supports_uncore(void)
32*d8b80295SApple OSS Distributions {
33*d8b80295SApple OSS Distributions int r;
34*d8b80295SApple OSS Distributions int type, subtype;
35*d8b80295SApple OSS Distributions unsigned int family;
36*d8b80295SApple OSS Distributions size_t size = sizeof(type);
37*d8b80295SApple OSS Distributions
38*d8b80295SApple OSS Distributions /*
39*d8b80295SApple OSS Distributions * Only arm64 Monsoon devices support uncore counters.
40*d8b80295SApple OSS Distributions */
41*d8b80295SApple OSS Distributions
42*d8b80295SApple OSS Distributions r = sysctlbyname("hw.cputype", &type, &size, NULL, 0);
43*d8b80295SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "sysctlbyname(\"hw.cputype\")");
44*d8b80295SApple OSS Distributions r = sysctlbyname("hw.cpusubtype", &subtype, &size, NULL, 0);
45*d8b80295SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "sysctlbyname(\"hw.cpusubtype\")");
46*d8b80295SApple OSS Distributions r = sysctlbyname("hw.cpufamily", &family, &size, NULL, 0);
47*d8b80295SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "sysctlbyname(\"hw.cpufamily\")");
48*d8b80295SApple OSS Distributions
49*d8b80295SApple OSS Distributions if (type == CPU_TYPE_ARM64 &&
50*d8b80295SApple OSS Distributions subtype == CPU_SUBTYPE_ARM64_V8 &&
51*d8b80295SApple OSS Distributions (family == CPUFAMILY_ARM_MONSOON_MISTRAL ||
52*d8b80295SApple OSS Distributions family == CPUFAMILY_ARM_VORTEX_TEMPEST)) {
53*d8b80295SApple OSS Distributions return true;
54*d8b80295SApple OSS Distributions }
55*d8b80295SApple OSS Distributions
56*d8b80295SApple OSS Distributions return false;
57*d8b80295SApple OSS Distributions }
58*d8b80295SApple OSS Distributions
59*d8b80295SApple OSS Distributions #define UNCORE_DEV_PATH "/dev/monotonic/uncore"
60*d8b80295SApple OSS Distributions
61*d8b80295SApple OSS Distributions static int
open_uncore_error(int * error)62*d8b80295SApple OSS Distributions open_uncore_error(int *error)
63*d8b80295SApple OSS Distributions {
64*d8b80295SApple OSS Distributions guardid_t guard;
65*d8b80295SApple OSS Distributions int fd;
66*d8b80295SApple OSS Distributions
67*d8b80295SApple OSS Distributions guard = 0xa5adcafe;
68*d8b80295SApple OSS Distributions
69*d8b80295SApple OSS Distributions T_SETUPBEGIN;
70*d8b80295SApple OSS Distributions
71*d8b80295SApple OSS Distributions fd = guarded_open_np(UNCORE_DEV_PATH, &guard,
72*d8b80295SApple OSS Distributions GUARD_CLOSE | GUARD_DUP | GUARD_WRITE, O_CLOEXEC | O_EXCL);
73*d8b80295SApple OSS Distributions if (fd < 0 && errno == ENOENT) {
74*d8b80295SApple OSS Distributions T_ASSERT_FALSE(device_supports_uncore(),
75*d8b80295SApple OSS Distributions "lack of dev node implies no uncore support");
76*d8b80295SApple OSS Distributions T_SKIP("uncore counters are unsupported");
77*d8b80295SApple OSS Distributions __builtin_unreachable();
78*d8b80295SApple OSS Distributions }
79*d8b80295SApple OSS Distributions
80*d8b80295SApple OSS Distributions if (error == NULL) {
81*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd, "open '%s'", UNCORE_DEV_PATH);
82*d8b80295SApple OSS Distributions } else {
83*d8b80295SApple OSS Distributions *error = errno;
84*d8b80295SApple OSS Distributions }
85*d8b80295SApple OSS Distributions
86*d8b80295SApple OSS Distributions T_SETUPEND;
87*d8b80295SApple OSS Distributions
88*d8b80295SApple OSS Distributions return fd;
89*d8b80295SApple OSS Distributions }
90*d8b80295SApple OSS Distributions
91*d8b80295SApple OSS Distributions static void
uncore_counts(int fd,uint64_t ctr_mask,uint64_t * counts)92*d8b80295SApple OSS Distributions uncore_counts(int fd, uint64_t ctr_mask, uint64_t *counts)
93*d8b80295SApple OSS Distributions {
94*d8b80295SApple OSS Distributions int r;
95*d8b80295SApple OSS Distributions union monotonic_ctl_counts *cts_ctl;
96*d8b80295SApple OSS Distributions
97*d8b80295SApple OSS Distributions cts_ctl = (union monotonic_ctl_counts *)counts;
98*d8b80295SApple OSS Distributions cts_ctl->in.ctr_mask = ctr_mask;
99*d8b80295SApple OSS Distributions
100*d8b80295SApple OSS Distributions r = ioctl(fd, MT_IOC_COUNTS, cts_ctl);
101*d8b80295SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "MT_IOC_COUNTS got counter values");
102*d8b80295SApple OSS Distributions }
103*d8b80295SApple OSS Distributions
104*d8b80295SApple OSS Distributions #define REF_TIMEBASE_EVENT 0x3
105*d8b80295SApple OSS Distributions #define CTRS_MAX 128
106*d8b80295SApple OSS Distributions
107*d8b80295SApple OSS Distributions T_DECL(uncore_max_counters,
108*d8b80295SApple OSS Distributions "ensure that the maximum number of uncore countes is sane",
109*d8b80295SApple OSS Distributions XNU_T_META_SOC_SPECIFIC,
110*d8b80295SApple OSS Distributions T_META_ASROOT(true))
111*d8b80295SApple OSS Distributions {
112*d8b80295SApple OSS Distributions int nctrs = 0;
113*d8b80295SApple OSS Distributions int fd;
114*d8b80295SApple OSS Distributions
115*d8b80295SApple OSS Distributions fd = open_uncore_error(NULL);
116*d8b80295SApple OSS Distributions
117*d8b80295SApple OSS Distributions do {
118*d8b80295SApple OSS Distributions union monotonic_ctl_add add_ctl;
119*d8b80295SApple OSS Distributions int r;
120*d8b80295SApple OSS Distributions
121*d8b80295SApple OSS Distributions add_ctl.in.config.event = REF_TIMEBASE_EVENT;
122*d8b80295SApple OSS Distributions add_ctl.in.config.allowed_ctr_mask = UINT64_MAX;
123*d8b80295SApple OSS Distributions
124*d8b80295SApple OSS Distributions r = ioctl(fd, MT_IOC_ADD, &add_ctl);
125*d8b80295SApple OSS Distributions if (r < 0 && errno == E2BIG) {
126*d8b80295SApple OSS Distributions break;
127*d8b80295SApple OSS Distributions }
128*d8b80295SApple OSS Distributions
129*d8b80295SApple OSS Distributions T_QUIET;
130*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(r, "added reference timebase event to counters");
131*d8b80295SApple OSS Distributions nctrs++;
132*d8b80295SApple OSS Distributions } while (nctrs < CTRS_MAX);
133*d8b80295SApple OSS Distributions
134*d8b80295SApple OSS Distributions T_EXPECT_LT(nctrs, CTRS_MAX, "able to allocate %d uncore PMCs", nctrs);
135*d8b80295SApple OSS Distributions }
136*d8b80295SApple OSS Distributions
137*d8b80295SApple OSS Distributions static uint32_t
uncore_add(int fd,uint64_t event,uint64_t allowed_ctrs,int error)138*d8b80295SApple OSS Distributions uncore_add(int fd, uint64_t event, uint64_t allowed_ctrs, int error)
139*d8b80295SApple OSS Distributions {
140*d8b80295SApple OSS Distributions int save_errno;
141*d8b80295SApple OSS Distributions int r;
142*d8b80295SApple OSS Distributions uint32_t ctr;
143*d8b80295SApple OSS Distributions union monotonic_ctl_add add_ctl;
144*d8b80295SApple OSS Distributions
145*d8b80295SApple OSS Distributions add_ctl.in.config.event = event;
146*d8b80295SApple OSS Distributions add_ctl.in.config.allowed_ctr_mask = allowed_ctrs;
147*d8b80295SApple OSS Distributions r = ioctl(fd, MT_IOC_ADD, &add_ctl);
148*d8b80295SApple OSS Distributions if (error) {
149*d8b80295SApple OSS Distributions save_errno = errno;
150*d8b80295SApple OSS Distributions T_EXPECT_LT(r, 0, "adding event to counter should fail");
151*d8b80295SApple OSS Distributions T_EXPECT_EQ(save_errno, error,
152*d8b80295SApple OSS Distributions "adding event to counter should fail with %d: %s",
153*d8b80295SApple OSS Distributions error, strerror(error));
154*d8b80295SApple OSS Distributions return UINT32_MAX;
155*d8b80295SApple OSS Distributions } else {
156*d8b80295SApple OSS Distributions T_QUIET;
157*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(r,
158*d8b80295SApple OSS Distributions "added event %#" PRIx64 " to counters", event);
159*d8b80295SApple OSS Distributions }
160*d8b80295SApple OSS Distributions
161*d8b80295SApple OSS Distributions ctr = add_ctl.out.ctr;
162*d8b80295SApple OSS Distributions T_QUIET; T_ASSERT_LT(ctr, (uint32_t)CTRS_MAX, "counter returned should be sane");
163*d8b80295SApple OSS Distributions return ctr;
164*d8b80295SApple OSS Distributions }
165*d8b80295SApple OSS Distributions
166*d8b80295SApple OSS Distributions T_DECL(uncore_collision,
167*d8b80295SApple OSS Distributions "ensure that trying to add an event on the same counter fails",
168*d8b80295SApple OSS Distributions T_META_ASROOT(true))
169*d8b80295SApple OSS Distributions {
170*d8b80295SApple OSS Distributions int fd;
171*d8b80295SApple OSS Distributions uint32_t ctr;
172*d8b80295SApple OSS Distributions
173*d8b80295SApple OSS Distributions fd = open_uncore_error(NULL);
174*d8b80295SApple OSS Distributions
175*d8b80295SApple OSS Distributions ctr = uncore_add(fd, REF_TIMEBASE_EVENT, UINT64_MAX, 0);
176*d8b80295SApple OSS Distributions T_LOG("added event to uncore counter %d\n", ctr);
177*d8b80295SApple OSS Distributions
178*d8b80295SApple OSS Distributions (void)uncore_add(fd, REF_TIMEBASE_EVENT, UINT64_C(1) << ctr, ENOSPC);
179*d8b80295SApple OSS Distributions }
180*d8b80295SApple OSS Distributions
181*d8b80295SApple OSS Distributions static void
uncore_enable(int fd)182*d8b80295SApple OSS Distributions uncore_enable(int fd)
183*d8b80295SApple OSS Distributions {
184*d8b80295SApple OSS Distributions union monotonic_ctl_enable en_ctl = {
185*d8b80295SApple OSS Distributions .in = { .enable = true }
186*d8b80295SApple OSS Distributions };
187*d8b80295SApple OSS Distributions
188*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(fd, MT_IOC_ENABLE, &en_ctl),
189*d8b80295SApple OSS Distributions "enabling counters");
190*d8b80295SApple OSS Distributions }
191*d8b80295SApple OSS Distributions
192*d8b80295SApple OSS Distributions T_DECL(uncore_enabled_busy,
193*d8b80295SApple OSS Distributions "ensure that trying to add an event while enabled fails",
194*d8b80295SApple OSS Distributions T_META_ASROOT(true))
195*d8b80295SApple OSS Distributions {
196*d8b80295SApple OSS Distributions int fd;
197*d8b80295SApple OSS Distributions
198*d8b80295SApple OSS Distributions fd = open_uncore_error(NULL);
199*d8b80295SApple OSS Distributions
200*d8b80295SApple OSS Distributions (void)uncore_add(fd, REF_TIMEBASE_EVENT, UINT64_MAX, 0);
201*d8b80295SApple OSS Distributions
202*d8b80295SApple OSS Distributions uncore_enable(fd);
203*d8b80295SApple OSS Distributions (void)uncore_add(fd, REF_TIMEBASE_EVENT, UINT64_MAX, EBUSY);
204*d8b80295SApple OSS Distributions }
205*d8b80295SApple OSS Distributions
206*d8b80295SApple OSS Distributions T_DECL(uncore_reset,
207*d8b80295SApple OSS Distributions "ensure that resetting the counters works")
208*d8b80295SApple OSS Distributions {
209*d8b80295SApple OSS Distributions int fd;
210*d8b80295SApple OSS Distributions int r;
211*d8b80295SApple OSS Distributions
212*d8b80295SApple OSS Distributions fd = open_uncore_error(NULL);
213*d8b80295SApple OSS Distributions
214*d8b80295SApple OSS Distributions (void)uncore_add(fd, REF_TIMEBASE_EVENT, UINT64_C(1), 0);
215*d8b80295SApple OSS Distributions (void)uncore_add(fd, REF_TIMEBASE_EVENT, UINT64_C(1), ENOSPC);
216*d8b80295SApple OSS Distributions
217*d8b80295SApple OSS Distributions r = ioctl(fd, MT_IOC_RESET);
218*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(r, "resetting succeeds");
219*d8b80295SApple OSS Distributions
220*d8b80295SApple OSS Distributions T_LOG("adding event to same counter after reset");
221*d8b80295SApple OSS Distributions (void)uncore_add(fd, REF_TIMEBASE_EVENT, UINT64_C(1), 0);
222*d8b80295SApple OSS Distributions }
223*d8b80295SApple OSS Distributions
224*d8b80295SApple OSS Distributions #define SLEEP_USECS (500 * 1000)
225*d8b80295SApple OSS Distributions
226*d8b80295SApple OSS Distributions static int
uncore_add_all(int fd,uint64_t event,int * nmonitors)227*d8b80295SApple OSS Distributions uncore_add_all(int fd, uint64_t event, int *nmonitors)
228*d8b80295SApple OSS Distributions {
229*d8b80295SApple OSS Distributions int nctrs = 0;
230*d8b80295SApple OSS Distributions int r;
231*d8b80295SApple OSS Distributions
232*d8b80295SApple OSS Distributions do {
233*d8b80295SApple OSS Distributions union monotonic_ctl_add add_ctl;
234*d8b80295SApple OSS Distributions
235*d8b80295SApple OSS Distributions add_ctl.in.config.event = event;
236*d8b80295SApple OSS Distributions add_ctl.in.config.allowed_ctr_mask = UINT64_MAX;
237*d8b80295SApple OSS Distributions
238*d8b80295SApple OSS Distributions r = ioctl(fd, MT_IOC_ADD, &add_ctl);
239*d8b80295SApple OSS Distributions if (r < 0 && errno == E2BIG) {
240*d8b80295SApple OSS Distributions break;
241*d8b80295SApple OSS Distributions }
242*d8b80295SApple OSS Distributions
243*d8b80295SApple OSS Distributions T_QUIET;
244*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(r, "added event %#" PRIx64 " to counters",
245*d8b80295SApple OSS Distributions event);
246*d8b80295SApple OSS Distributions nctrs++;
247*d8b80295SApple OSS Distributions } while (nctrs < CTRS_MAX);
248*d8b80295SApple OSS Distributions
249*d8b80295SApple OSS Distributions if (nmonitors) {
250*d8b80295SApple OSS Distributions union monotonic_ctl_info info_ctl;
251*d8b80295SApple OSS Distributions r = ioctl(fd, MT_IOC_GET_INFO, &info_ctl);
252*d8b80295SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "got info about uncore counters");
253*d8b80295SApple OSS Distributions
254*d8b80295SApple OSS Distributions *nmonitors = (int)info_ctl.out.nmonitors;
255*d8b80295SApple OSS Distributions }
256*d8b80295SApple OSS Distributions
257*d8b80295SApple OSS Distributions return nctrs;
258*d8b80295SApple OSS Distributions }
259*d8b80295SApple OSS Distributions
260*d8b80295SApple OSS Distributions T_DECL(uncore_accuracy,
261*d8b80295SApple OSS Distributions "ensure that the uncore counters count accurately",
262*d8b80295SApple OSS Distributions T_META_ASROOT(true),
263*d8b80295SApple OSS Distributions XNU_T_META_SOC_SPECIFIC,
264*d8b80295SApple OSS Distributions T_META_MAYFAIL("rdar://88973518, threads need to be forced onto clusters"))
265*d8b80295SApple OSS Distributions {
266*d8b80295SApple OSS Distributions int fd;
267*d8b80295SApple OSS Distributions int nctrs = 0;
268*d8b80295SApple OSS Distributions int nmonitors = 0;
269*d8b80295SApple OSS Distributions uint64_t ctr_mask;
270*d8b80295SApple OSS Distributions uint64_t counts[2][CTRS_MAX];
271*d8b80295SApple OSS Distributions uint64_t times[2];
272*d8b80295SApple OSS Distributions
273*d8b80295SApple OSS Distributions fd = open_uncore_error(NULL);
274*d8b80295SApple OSS Distributions
275*d8b80295SApple OSS Distributions /*
276*d8b80295SApple OSS Distributions * The reference timebase event counts the same as mach_continuous_time
277*d8b80295SApple OSS Distributions * (on hardware supporting uncore counters). Make sure that the counter
278*d8b80295SApple OSS Distributions * is close to the values returned from the trap.
279*d8b80295SApple OSS Distributions *
280*d8b80295SApple OSS Distributions * Fill all the counters with this event.
281*d8b80295SApple OSS Distributions */
282*d8b80295SApple OSS Distributions nctrs = uncore_add_all(fd, REF_TIMEBASE_EVENT, &nmonitors);
283*d8b80295SApple OSS Distributions ctr_mask = (UINT64_C(1) << nctrs) - 1;
284*d8b80295SApple OSS Distributions
285*d8b80295SApple OSS Distributions T_LOG("added %d counters to check", nctrs);
286*d8b80295SApple OSS Distributions
287*d8b80295SApple OSS Distributions uncore_enable(fd);
288*d8b80295SApple OSS Distributions
289*d8b80295SApple OSS Distributions /*
290*d8b80295SApple OSS Distributions * First, make sure there's an upper bound on the counter -- take the
291*d8b80295SApple OSS Distributions * time around getting the counter values.
292*d8b80295SApple OSS Distributions */
293*d8b80295SApple OSS Distributions
294*d8b80295SApple OSS Distributions times[0] = mach_absolute_time();
295*d8b80295SApple OSS Distributions uncore_counts(fd, ctr_mask, counts[0]);
296*d8b80295SApple OSS Distributions
297*d8b80295SApple OSS Distributions usleep(SLEEP_USECS);
298*d8b80295SApple OSS Distributions
299*d8b80295SApple OSS Distributions uncore_counts(fd, ctr_mask, counts[1]);
300*d8b80295SApple OSS Distributions times[1] = mach_absolute_time();
301*d8b80295SApple OSS Distributions
302*d8b80295SApple OSS Distributions T_QUIET; T_EXPECT_GT(times[1], times[0],
303*d8b80295SApple OSS Distributions "mach_continuous_time is monotonically increasing");
304*d8b80295SApple OSS Distributions for (int i = 0; i < nctrs; i++) {
305*d8b80295SApple OSS Distributions T_EXPECT_GT(counts[1][i], counts[0][i],
306*d8b80295SApple OSS Distributions "uncore counter %d value is monotonically increasing", i);
307*d8b80295SApple OSS Distributions T_EXPECT_LT(counts[1][i] - counts[0][i], times[1] - times[0],
308*d8b80295SApple OSS Distributions "reference timebase on uncore counter %d satisfies upper bound "
309*d8b80295SApple OSS Distributions "from mach_absolute_time", i);
310*d8b80295SApple OSS Distributions }
311*d8b80295SApple OSS Distributions
312*d8b80295SApple OSS Distributions /*
313*d8b80295SApple OSS Distributions * Next, the lower bound -- put mach_absolute_time inside getting the
314*d8b80295SApple OSS Distributions * counter values.
315*d8b80295SApple OSS Distributions */
316*d8b80295SApple OSS Distributions
317*d8b80295SApple OSS Distributions uncore_counts(fd, ctr_mask, counts[0]);
318*d8b80295SApple OSS Distributions times[0] = mach_absolute_time();
319*d8b80295SApple OSS Distributions
320*d8b80295SApple OSS Distributions volatile int iterations = 100000;
321*d8b80295SApple OSS Distributions while (iterations--) {
322*d8b80295SApple OSS Distributions ;
323*d8b80295SApple OSS Distributions }
324*d8b80295SApple OSS Distributions
325*d8b80295SApple OSS Distributions times[1] = mach_absolute_time();
326*d8b80295SApple OSS Distributions uncore_counts(fd, ctr_mask, counts[1]);
327*d8b80295SApple OSS Distributions
328*d8b80295SApple OSS Distributions for (int mon = 0; mon < nmonitors; mon++) {
329*d8b80295SApple OSS Distributions for (int i = 0; i < nctrs; i++) {
330*d8b80295SApple OSS Distributions uint64_t after = counts[1][i * mon];
331*d8b80295SApple OSS Distributions uint64_t before = counts[0][i * mon];
332*d8b80295SApple OSS Distributions uint64_t delta = after - before;
333*d8b80295SApple OSS Distributions T_LOG("%d,%2d: %12" PRIu64 "%12" PRIu64 " = %10" PRIu64, mon, i,
334*d8b80295SApple OSS Distributions before, after, delta);
335*d8b80295SApple OSS Distributions T_QUIET;
336*d8b80295SApple OSS Distributions T_EXPECT_GT(after, before,
337*d8b80295SApple OSS Distributions "uncore %d counter %d value is monotonically increasing",
338*d8b80295SApple OSS Distributions mon, i);
339*d8b80295SApple OSS Distributions T_QUIET;
340*d8b80295SApple OSS Distributions T_EXPECT_GT(counts[1][i * mon] - counts[0][i * mon],
341*d8b80295SApple OSS Distributions times[1] - times[0],
342*d8b80295SApple OSS Distributions "reference timebase on uncore %d counter %d satisfies "
343*d8b80295SApple OSS Distributions "lower bound from mach_absolute_time", mon, i);
344*d8b80295SApple OSS Distributions }
345*d8b80295SApple OSS Distributions }
346*d8b80295SApple OSS Distributions }
347*d8b80295SApple OSS Distributions
348*d8b80295SApple OSS Distributions T_DECL(uncore_ownership,
349*d8b80295SApple OSS Distributions "ensure the dev node cannot be open in two places",
350*d8b80295SApple OSS Distributions T_META_ASROOT(true))
351*d8b80295SApple OSS Distributions {
352*d8b80295SApple OSS Distributions int fd;
353*d8b80295SApple OSS Distributions int other_fd;
354*d8b80295SApple OSS Distributions int error;
355*d8b80295SApple OSS Distributions
356*d8b80295SApple OSS Distributions fd = open_uncore_error(NULL);
357*d8b80295SApple OSS Distributions
358*d8b80295SApple OSS Distributions other_fd = open_uncore_error(&error);
359*d8b80295SApple OSS Distributions T_ASSERT_LT(other_fd, 0, "opening a second uncore fd should fail");
360*d8b80295SApple OSS Distributions }
361*d8b80295SApple OSS Distributions
362*d8b80295SApple OSS Distributions T_DECL(uncore_root_required,
363*d8b80295SApple OSS Distributions "ensure the dev node cannot be opened by non-root users",
364*d8b80295SApple OSS Distributions T_META_ASROOT(false))
365*d8b80295SApple OSS Distributions {
366*d8b80295SApple OSS Distributions int fd;
367*d8b80295SApple OSS Distributions int error = 0;
368*d8b80295SApple OSS Distributions
369*d8b80295SApple OSS Distributions T_SKIP("libdarwintest doesn't drop privileges properly");
370*d8b80295SApple OSS Distributions
371*d8b80295SApple OSS Distributions fd = open_uncore_error(&error);
372*d8b80295SApple OSS Distributions T_ASSERT_LT(fd, 0, "opening dev node should not return an fd");
373*d8b80295SApple OSS Distributions T_ASSERT_EQ(error, EPERM,
374*d8b80295SApple OSS Distributions "opening dev node as non-root user should fail with EPERM");
375*d8b80295SApple OSS Distributions }
376*d8b80295SApple OSS Distributions
377*d8b80295SApple OSS Distributions T_DECL(perf_uncore,
378*d8b80295SApple OSS Distributions "measure the latency of accessing the counters",
379*d8b80295SApple OSS Distributions XNU_T_META_SOC_SPECIFIC,
380*d8b80295SApple OSS Distributions T_META_TAG_PERF)
381*d8b80295SApple OSS Distributions {
382*d8b80295SApple OSS Distributions int fd;
383*d8b80295SApple OSS Distributions int nctrs;
384*d8b80295SApple OSS Distributions int nmonitors;
385*d8b80295SApple OSS Distributions int r;
386*d8b80295SApple OSS Distributions uint64_t ctr_mask;
387*d8b80295SApple OSS Distributions dt_stat_thread_instructions_t counts_instrs;
388*d8b80295SApple OSS Distributions dt_stat_t counter_deltas;
389*d8b80295SApple OSS Distributions
390*d8b80295SApple OSS Distributions counts_instrs = dt_stat_thread_instructions_create("ioctl_counts");
391*d8b80295SApple OSS Distributions counter_deltas = dt_stat_create("abs_time", "between_each_counter");
392*d8b80295SApple OSS Distributions
393*d8b80295SApple OSS Distributions fd = open_uncore_error(NULL);
394*d8b80295SApple OSS Distributions
395*d8b80295SApple OSS Distributions nctrs = uncore_add_all(fd, REF_TIMEBASE_EVENT, &nmonitors);
396*d8b80295SApple OSS Distributions ctr_mask = (UINT64_C(1) << nctrs) - 1;
397*d8b80295SApple OSS Distributions
398*d8b80295SApple OSS Distributions uncore_enable(fd);
399*d8b80295SApple OSS Distributions
400*d8b80295SApple OSS Distributions do {
401*d8b80295SApple OSS Distributions dt_stat_token token;
402*d8b80295SApple OSS Distributions uint64_t counts[nctrs * nmonitors];
403*d8b80295SApple OSS Distributions union monotonic_ctl_counts *cts_ctl;
404*d8b80295SApple OSS Distributions
405*d8b80295SApple OSS Distributions cts_ctl = (union monotonic_ctl_counts *)counts;
406*d8b80295SApple OSS Distributions cts_ctl->in.ctr_mask = ctr_mask;
407*d8b80295SApple OSS Distributions
408*d8b80295SApple OSS Distributions token = dt_stat_thread_instructions_begin(counts_instrs);
409*d8b80295SApple OSS Distributions r = ioctl(fd, MT_IOC_COUNTS, cts_ctl);
410*d8b80295SApple OSS Distributions dt_stat_thread_instructions_end(counts_instrs, token);
411*d8b80295SApple OSS Distributions T_QUIET;
412*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(r,
413*d8b80295SApple OSS Distributions "getting uncore counter values %#" PRIx64, ctr_mask);
414*d8b80295SApple OSS Distributions
415*d8b80295SApple OSS Distributions for (int i = 0; i < (nctrs - 1); i++) {
416*d8b80295SApple OSS Distributions dt_stat_add(counter_deltas, (double)(counts[i + 1] - counts[i]));
417*d8b80295SApple OSS Distributions }
418*d8b80295SApple OSS Distributions } while (!dt_stat_stable(counts_instrs) || !dt_stat_stable(counter_deltas));
419*d8b80295SApple OSS Distributions
420*d8b80295SApple OSS Distributions dt_stat_finalize(counts_instrs);
421*d8b80295SApple OSS Distributions dt_stat_finalize(counter_deltas);
422*d8b80295SApple OSS Distributions }
423