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