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