1*5e3eaea3SApple OSS Distributions // Copyright (c) 2020 Apple Computer, Inc. All rights reserved.
2*5e3eaea3SApple OSS Distributions
3*5e3eaea3SApple OSS Distributions #include <darwintest.h>
4*5e3eaea3SApple OSS Distributions #include <dirent.h>
5*5e3eaea3SApple OSS Distributions #include <inttypes.h>
6*5e3eaea3SApple OSS Distributions #include <mach/machine.h>
7*5e3eaea3SApple OSS Distributions #include <stddef.h>
8*5e3eaea3SApple OSS Distributions #include <stdlib.h>
9*5e3eaea3SApple OSS Distributions #include <stdint.h>
10*5e3eaea3SApple OSS Distributions #include <sys/fcntl.h>
11*5e3eaea3SApple OSS Distributions #include <sys/ioctl.h>
12*5e3eaea3SApple OSS Distributions #include <sys/perfmon_private.h>
13*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
14*5e3eaea3SApple OSS Distributions #include <sys/syslimits.h>
15*5e3eaea3SApple OSS Distributions #include <unistd.h>
16*5e3eaea3SApple OSS Distributions
17*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
18*5e3eaea3SApple OSS Distributions T_META_NAMESPACE("xnu.perfmon"),
19*5e3eaea3SApple OSS Distributions T_META_CHECK_LEAKS(false),
20*5e3eaea3SApple OSS Distributions T_META_RUN_CONCURRENTLY(false),
21*5e3eaea3SApple OSS Distributions T_META_ASROOT(true));
22*5e3eaea3SApple OSS Distributions
23*5e3eaea3SApple OSS Distributions #define MAX_MONITORS (8)
24*5e3eaea3SApple OSS Distributions
25*5e3eaea3SApple OSS Distributions struct monitor_list {
26*5e3eaea3SApple OSS Distributions int fds[MAX_MONITORS];
27*5e3eaea3SApple OSS Distributions char *names[MAX_MONITORS];
28*5e3eaea3SApple OSS Distributions size_t length;
29*5e3eaea3SApple OSS Distributions };
30*5e3eaea3SApple OSS Distributions
31*5e3eaea3SApple OSS Distributions static struct monitor_list
open_monitors(void)32*5e3eaea3SApple OSS Distributions open_monitors(void)
33*5e3eaea3SApple OSS Distributions {
34*5e3eaea3SApple OSS Distributions struct monitor_list monitors = { 0 };
35*5e3eaea3SApple OSS Distributions
36*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
37*5e3eaea3SApple OSS Distributions
38*5e3eaea3SApple OSS Distributions struct dirent *entry;
39*5e3eaea3SApple OSS Distributions DIR *dir = opendir("/dev");
40*5e3eaea3SApple OSS Distributions T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(dir, "opendir /dev");
41*5e3eaea3SApple OSS Distributions
42*5e3eaea3SApple OSS Distributions while ((entry = readdir(dir)) != NULL) {
43*5e3eaea3SApple OSS Distributions if (strncmp(entry->d_name, "perfmon", strlen("perfmon")) == 0) {
44*5e3eaea3SApple OSS Distributions char path[PATH_MAX] = { 0 };
45*5e3eaea3SApple OSS Distributions snprintf(path, sizeof(path), "/dev/%s", entry->d_name);
46*5e3eaea3SApple OSS Distributions
47*5e3eaea3SApple OSS Distributions T_SETUPEND;
48*5e3eaea3SApple OSS Distributions
49*5e3eaea3SApple OSS Distributions monitors.fds[monitors.length] = open(path, O_RDONLY);
50*5e3eaea3SApple OSS Distributions T_QUIET;
51*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(monitors.fds[monitors.length],
52*5e3eaea3SApple OSS Distributions "open %s", entry->d_name);
53*5e3eaea3SApple OSS Distributions
54*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
55*5e3eaea3SApple OSS Distributions
56*5e3eaea3SApple OSS Distributions monitors.names[monitors.length] = strdup(entry->d_name);
57*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(monitors.names[monitors.length], "strdup");
58*5e3eaea3SApple OSS Distributions monitors.length += 1;
59*5e3eaea3SApple OSS Distributions if (monitors.length > MAX_MONITORS) {
60*5e3eaea3SApple OSS Distributions T_ASSERT_FAIL("exceeded maximum number of monitors");
61*5e3eaea3SApple OSS Distributions }
62*5e3eaea3SApple OSS Distributions }
63*5e3eaea3SApple OSS Distributions }
64*5e3eaea3SApple OSS Distributions
65*5e3eaea3SApple OSS Distributions T_SETUPEND;
66*5e3eaea3SApple OSS Distributions
67*5e3eaea3SApple OSS Distributions return monitors;
68*5e3eaea3SApple OSS Distributions }
69*5e3eaea3SApple OSS Distributions
70*5e3eaea3SApple OSS Distributions static void
close_monitors(struct monitor_list * monitors)71*5e3eaea3SApple OSS Distributions close_monitors(struct monitor_list *monitors)
72*5e3eaea3SApple OSS Distributions {
73*5e3eaea3SApple OSS Distributions for (size_t i = 0; i < monitors->length; i++) {
74*5e3eaea3SApple OSS Distributions close(monitors->fds[i]);
75*5e3eaea3SApple OSS Distributions free(monitors->names[i]);
76*5e3eaea3SApple OSS Distributions }
77*5e3eaea3SApple OSS Distributions }
78*5e3eaea3SApple OSS Distributions
79*5e3eaea3SApple OSS Distributions T_DECL(layout, "ensure layout can be read from available monitors")
80*5e3eaea3SApple OSS Distributions {
81*5e3eaea3SApple OSS Distributions struct monitor_list monitors = open_monitors();
82*5e3eaea3SApple OSS Distributions if (monitors.length == 0) {
83*5e3eaea3SApple OSS Distributions T_SKIP("no monitors present");
84*5e3eaea3SApple OSS Distributions }
85*5e3eaea3SApple OSS Distributions
86*5e3eaea3SApple OSS Distributions for (size_t i = 0; i < monitors.length; i++) {
87*5e3eaea3SApple OSS Distributions struct perfmon_layout layout = { 0 };
88*5e3eaea3SApple OSS Distributions const char *name = monitors.names[i];
89*5e3eaea3SApple OSS Distributions int ret = ioctl(monitors.fds[i], PERFMON_CTL_GET_LAYOUT, &layout);
90*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "ioctl %s PERFMON_CTL_GET_LAYOUT",
91*5e3eaea3SApple OSS Distributions monitors.names[i]);
92*5e3eaea3SApple OSS Distributions
93*5e3eaea3SApple OSS Distributions T_QUIET;
94*5e3eaea3SApple OSS Distributions T_EXPECT_GT(layout.pl_counter_count, (unsigned short)0,
95*5e3eaea3SApple OSS Distributions "%s: non-zero counters", name);
96*5e3eaea3SApple OSS Distributions T_QUIET;
97*5e3eaea3SApple OSS Distributions T_EXPECT_GT(layout.pl_unit_count, (unsigned short)0,
98*5e3eaea3SApple OSS Distributions "%s: non-zero monitors", name);
99*5e3eaea3SApple OSS Distributions T_QUIET;
100*5e3eaea3SApple OSS Distributions T_EXPECT_GT(layout.pl_reg_count, (unsigned short)0,
101*5e3eaea3SApple OSS Distributions "%s: non-zero registers", name);
102*5e3eaea3SApple OSS Distributions }
103*5e3eaea3SApple OSS Distributions }
104*5e3eaea3SApple OSS Distributions
105*5e3eaea3SApple OSS Distributions T_DECL(registers, "ensure registers can be read from available monitors")
106*5e3eaea3SApple OSS Distributions {
107*5e3eaea3SApple OSS Distributions struct monitor_list monitors = open_monitors();
108*5e3eaea3SApple OSS Distributions if (monitors.length == 0) {
109*5e3eaea3SApple OSS Distributions T_SKIP("no monitors present");
110*5e3eaea3SApple OSS Distributions }
111*5e3eaea3SApple OSS Distributions
112*5e3eaea3SApple OSS Distributions for (size_t i = 0; i < monitors.length; i++) {
113*5e3eaea3SApple OSS Distributions const char *name = monitors.names[i];
114*5e3eaea3SApple OSS Distributions
115*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
116*5e3eaea3SApple OSS Distributions
117*5e3eaea3SApple OSS Distributions struct perfmon_layout layout = { 0 };
118*5e3eaea3SApple OSS Distributions int ret = ioctl(monitors.fds[i], PERFMON_CTL_GET_LAYOUT, &layout);
119*5e3eaea3SApple OSS Distributions T_QUIET;
120*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "ioctl %s PERFMON_CTL_GET_LAYOUT",
121*5e3eaea3SApple OSS Distributions monitors.names[i]);
122*5e3eaea3SApple OSS Distributions
123*5e3eaea3SApple OSS Distributions if (layout.pl_reg_count == 0) {
124*5e3eaea3SApple OSS Distributions T_LOG("skipping %s: no registers", name);
125*5e3eaea3SApple OSS Distributions continue;
126*5e3eaea3SApple OSS Distributions }
127*5e3eaea3SApple OSS Distributions
128*5e3eaea3SApple OSS Distributions perfmon_name_t *names = calloc(layout.pl_reg_count,
129*5e3eaea3SApple OSS Distributions sizeof(names[0]));
130*5e3eaea3SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(names, "calloc");
131*5e3eaea3SApple OSS Distributions
132*5e3eaea3SApple OSS Distributions uint64_t *values = calloc(
133*5e3eaea3SApple OSS Distributions layout.pl_reg_count * layout.pl_unit_count,
134*5e3eaea3SApple OSS Distributions sizeof(values[0]));
135*5e3eaea3SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(names, "calloc");
136*5e3eaea3SApple OSS Distributions
137*5e3eaea3SApple OSS Distributions T_SETUPEND;
138*5e3eaea3SApple OSS Distributions
139*5e3eaea3SApple OSS Distributions ret = ioctl(monitors.fds[i], PERFMON_CTL_LIST_REGS, names);
140*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "ioctl %s PERFMON_CTL_LIST_REGS", name);
141*5e3eaea3SApple OSS Distributions printf("%s registers:", name);
142*5e3eaea3SApple OSS Distributions for (unsigned short j = 0; j < layout.pl_reg_count; j++) {
143*5e3eaea3SApple OSS Distributions if (j != 0) {
144*5e3eaea3SApple OSS Distributions printf(", ");
145*5e3eaea3SApple OSS Distributions }
146*5e3eaea3SApple OSS Distributions if (j % 4 == 0) {
147*5e3eaea3SApple OSS Distributions printf("\n%4s", "");
148*5e3eaea3SApple OSS Distributions }
149*5e3eaea3SApple OSS Distributions printf("%18s", names[j]);
150*5e3eaea3SApple OSS Distributions }
151*5e3eaea3SApple OSS Distributions printf("\n");
152*5e3eaea3SApple OSS Distributions
153*5e3eaea3SApple OSS Distributions ret = ioctl(monitors.fds[i], PERFMON_CTL_SAMPLE_REGS, values);
154*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "ioctl %s PERFMON_CTL_SAMPLE_REGS", name);
155*5e3eaea3SApple OSS Distributions for (unsigned short j = 0; j < layout.pl_unit_count; j++) {
156*5e3eaea3SApple OSS Distributions printf("%2d: ", j);
157*5e3eaea3SApple OSS Distributions for (unsigned short k = 0; k < layout.pl_reg_count;
158*5e3eaea3SApple OSS Distributions k++) {
159*5e3eaea3SApple OSS Distributions if (k != 0) {
160*5e3eaea3SApple OSS Distributions printf(", ");
161*5e3eaea3SApple OSS Distributions if (k % 4 == 0) {
162*5e3eaea3SApple OSS Distributions printf("\n%4s", "");
163*5e3eaea3SApple OSS Distributions }
164*5e3eaea3SApple OSS Distributions }
165*5e3eaea3SApple OSS Distributions
166*5e3eaea3SApple OSS Distributions uint64_t value = values[j * layout.pl_reg_count + k];
167*5e3eaea3SApple OSS Distributions printf("0x%016" PRIx64, value);
168*5e3eaea3SApple OSS Distributions }
169*5e3eaea3SApple OSS Distributions printf("\n");
170*5e3eaea3SApple OSS Distributions }
171*5e3eaea3SApple OSS Distributions }
172*5e3eaea3SApple OSS Distributions }
173*5e3eaea3SApple OSS Distributions
174*5e3eaea3SApple OSS Distributions T_DECL(presence, "ensure perfmon is available on supported hardware")
175*5e3eaea3SApple OSS Distributions {
176*5e3eaea3SApple OSS Distributions struct monitor_list monitors = open_monitors();
177*5e3eaea3SApple OSS Distributions
178*5e3eaea3SApple OSS Distributions #if defined(__arm64__)
179*5e3eaea3SApple OSS Distributions T_ASSERT_GT(monitors.length, (size_t)0,
180*5e3eaea3SApple OSS Distributions "ARM64 devices should have monitors");
181*5e3eaea3SApple OSS Distributions
182*5e3eaea3SApple OSS Distributions bool found_core = false;
183*5e3eaea3SApple OSS Distributions bool found_uncore = false;
184*5e3eaea3SApple OSS Distributions for (size_t i = 0; i < monitors.length; i++) {
185*5e3eaea3SApple OSS Distributions if (strcmp(monitors.names[i], "perfmon_core") == 0) {
186*5e3eaea3SApple OSS Distributions found_core = true;
187*5e3eaea3SApple OSS Distributions } else if (strcmp(monitors.names[i], "perfmon_uncore") == 0) {
188*5e3eaea3SApple OSS Distributions found_uncore = true;
189*5e3eaea3SApple OSS Distributions }
190*5e3eaea3SApple OSS Distributions }
191*5e3eaea3SApple OSS Distributions T_EXPECT_TRUE(found_core, "all ARM64 devices should expose core PMU");
192*5e3eaea3SApple OSS Distributions
193*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
194*5e3eaea3SApple OSS Distributions
195*5e3eaea3SApple OSS Distributions int subtype = 0;
196*5e3eaea3SApple OSS Distributions size_t subtype_size = sizeof(subtype);
197*5e3eaea3SApple OSS Distributions int ret = sysctlbyname("hw.cpusubtype", &subtype, &subtype_size, NULL, 0);
198*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname hw.cpusubtype");
199*5e3eaea3SApple OSS Distributions
200*5e3eaea3SApple OSS Distributions T_SETUPEND;
201*5e3eaea3SApple OSS Distributions
202*5e3eaea3SApple OSS Distributions if (access("/dev/monotonic/uncore", O_RDONLY) == 0) {
203*5e3eaea3SApple OSS Distributions T_EXPECT_TRUE(found_uncore,
204*5e3eaea3SApple OSS Distributions "any device supported by Monotonic devices should have uncore PMU");
205*5e3eaea3SApple OSS Distributions }
206*5e3eaea3SApple OSS Distributions
207*5e3eaea3SApple OSS Distributions #else // defined(__arm64__)
208*5e3eaea3SApple OSS Distributions #pragma unused(monitors)
209*5e3eaea3SApple OSS Distributions T_SKIP("non-ARM64 devices unsupported");
210*5e3eaea3SApple OSS Distributions #endif // !defined(__arm64__)
211*5e3eaea3SApple OSS Distributions }
212*5e3eaea3SApple OSS Distributions
213*5e3eaea3SApple OSS Distributions T_DECL(open_close_stress, "ensure that the files can be opened and closed")
214*5e3eaea3SApple OSS Distributions {
215*5e3eaea3SApple OSS Distributions const int n = 100;
216*5e3eaea3SApple OSS Distributions for (int i = 0; i < n; i++) {
217*5e3eaea3SApple OSS Distributions struct monitor_list monitors = open_monitors();
218*5e3eaea3SApple OSS Distributions if (monitors.length == 0) {
219*5e3eaea3SApple OSS Distributions if (i == 0) {
220*5e3eaea3SApple OSS Distributions T_SKIP("no monitors present");
221*5e3eaea3SApple OSS Distributions } else {
222*5e3eaea3SApple OSS Distributions T_ASSERT_FAIL("failed to open monitors");
223*5e3eaea3SApple OSS Distributions }
224*5e3eaea3SApple OSS Distributions }
225*5e3eaea3SApple OSS Distributions close_monitors(&monitors);
226*5e3eaea3SApple OSS Distributions }
227*5e3eaea3SApple OSS Distributions
228*5e3eaea3SApple OSS Distributions T_PASS("passed %d cycles", n);
229*5e3eaea3SApple OSS Distributions }
230