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