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