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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | // Copyright (c) 2020 Apple Computer, Inc. All rights reserved. #include <darwintest.h> #include <errno.h> #include <inttypes.h> #include <stdint.h> #include "rump.h" #include <sys/perfmon_private.h> #define CONFIG_PERFMON 1 #define CPMU_PMC_COUNT 10 #define HAS_UPMU 1 #define UPMU_PMC_COUNT 16 #include "../osfmk/kern/perfmon.h" #include "../osfmk/arm64/perfmon_arm64_regs.h" #include "../osfmk/machine/machine_perfmon.h" #include "../osfmk/kern/kern_perfmon.c" #include "../osfmk/arm64/perfmon_arm64.c" T_GLOBAL_META(T_META_NAMESPACE("xnu.perfmon")); struct perfmon_event test_events[2] = { { .pe_name = "test", .pe_number = 1, .pe_counter = 3, }, { .pe_name = "second", .pe_number = 2, .pe_counter = 4, }, }; T_DECL(config_add_event_spec, "ensure events can be added to a configuration") { T_SETUPBEGIN; perfmon_machine_startup(); perfmon_config_t config = perfmon_config_create(&perfmon_sources[0]); T_QUIET; T_ASSERT_NOTNULL(config, "created config"); T_SETUPEND; struct perfmon_event event = test_events[0]; int error = perfmon_config_add_event(config, &event); T_ASSERT_POSIX_ZERO(error, "should add event to config"); const struct perfmon_spec *spec = perfmon_config_specify(config); T_ASSERT_EQ(spec->ps_event_count, (unsigned short)1, "one event added to config"); error = perfmon_config_add_event(config, &event); T_ASSERT_POSIX_ERROR(error, EALREADY, "should not add event to already-used counter"); T_QUIET; T_ASSERT_EQ(spec->ps_event_count, (unsigned short)1, "still one event added to config"); event = test_events[1]; error = perfmon_config_add_event(config, &event); T_ASSERT_POSIX_ZERO(error, "should add second event to config"); T_ASSERT_EQ(spec->ps_event_count, (unsigned short)2, "two events added to config"); event.pe_counter = 20; error = perfmon_config_add_event(config, &event); T_ASSERT_POSIX_ERROR(error, ERANGE, "should not add event to counter out of range"); T_QUIET; T_ASSERT_EQ(spec->ps_event_count, (unsigned short)2, "still two events added to config"); event.pe_counter = 0; error = perfmon_config_add_event(config, &event); T_ASSERT_POSIX_ERROR(error, ENODEV, "should not add event to fixed counter, error was %d", error); T_QUIET; T_ASSERT_EQ(spec->ps_event_count, (unsigned short)2, "still two events added to config"); error = perfmon_configure(config); T_ASSERT_POSIX_ZERO(error, "configured CPMU"); event.pe_counter = 4; error = perfmon_config_add_event(config, &event); T_ASSERT_POSIX_ERROR(error, EBUSY, "should not add event to already-configured config"); T_QUIET; T_ASSERT_EQ(spec->ps_event_count, (unsigned short)2, "still two events added to config"); const struct perfmon_event *read_events = spec->ps_events; for (unsigned short i = 0; i < spec->ps_event_count; i++) { T_QUIET; T_ASSERT_EQ_STR(read_events[i].pe_name, test_events[i].pe_name, "event %hu name matches", i); T_QUIET; T_ASSERT_EQ(read_events[i].pe_number, test_events[i].pe_number, "event %hu number matches", i); T_QUIET; T_ASSERT_EQ(read_events[i].pe_counter, test_events[i].pe_counter, "event %hu counter matches", i); T_PASS("event %hu in config matches what was set", i); } perfmon_config_destroy(config); } struct perfmon_attr test_attrs[2] = { { .pa_name = "PMCR2", .pa_value = 0x123 }, { .pa_name = "OPMAT0", .pa_value = 0x123, }, }; T_DECL(config_set_attr_spec, "ensure attributes can be set on a configuration") { T_SETUPBEGIN; perfmon_machine_startup(); perfmon_config_t config = perfmon_config_create(&perfmon_sources[0]); T_QUIET; T_ASSERT_NOTNULL(config, "created config"); T_SETUPEND; struct perfmon_attr attr = test_attrs[0]; int error = perfmon_config_set_attr(config, &attr); T_ASSERT_POSIX_ZERO(error, "should set attr in config"); struct perfmon_spec *spec = perfmon_config_specify(config); T_QUIET; T_ASSERT_EQ(spec->ps_attr_count, (unsigned short)1, "one attr set in config"); error = perfmon_config_set_attr(config, &attr); T_ASSERT_POSIX_ERROR(error, EALREADY, "should not set same attribute to config"); T_QUIET; T_ASSERT_EQ(spec->ps_attr_count, (unsigned short)1, "still one attr set in config"); attr = test_attrs[1]; error = perfmon_config_set_attr(config, &attr); T_ASSERT_POSIX_ZERO(error, "should set second attr in config"); T_QUIET; T_ASSERT_EQ(spec->ps_attr_count, (unsigned short)2, "two attrs set in config"); strlcpy(attr.pa_name, "ENOATTR", sizeof(attr.pa_name)); error = perfmon_config_set_attr(config, &attr); T_ASSERT_POSIX_ERROR(error, ENOATTR, "should not set non-existent attr in config"); T_QUIET; T_ASSERT_EQ(spec->ps_attr_count, (unsigned short)2, "still two attrs set in config"); error = perfmon_configure(config); T_ASSERT_POSIX_ZERO(error, "configured CPMU"); strlcpy(attr.pa_name, "PMCR3", sizeof(attr.pa_name)); error = perfmon_config_set_attr(config, &attr); T_ASSERT_POSIX_ERROR(error, EBUSY, "should not set attr on already-configured config"); T_QUIET; T_ASSERT_EQ(spec->ps_attr_count, (unsigned short)2, "still two attrs added to config"); const struct perfmon_attr *read_attrs = spec->ps_attrs; for (unsigned short i = 0; i < spec->ps_attr_count; i++) { T_QUIET; T_ASSERT_EQ_STR(read_attrs[i].pa_name, test_attrs[i].pa_name, "attr %hu name matches", i); T_QUIET; T_ASSERT_EQ(read_attrs[i].pa_value, test_attrs[i].pa_value, "attr %hu number matches", i); T_PASS("attr %hu in config matches what was set", i); } perfmon_config_destroy(config); } T_DECL(config_arm64_cpmu, "ensure the ARM64 configuration is correct") { T_SETUPBEGIN; perfmon_machine_startup(); struct perfmon_source *cpmu_source = &perfmon_sources[0]; perfmon_config_t config = perfmon_config_create(cpmu_source); T_QUIET; T_ASSERT_NOTNULL(config, "created config"); for (size_t i = 0; i < ARRAYLEN(test_events); i++) { int error = perfmon_config_add_event(config, &test_events[i]); T_QUIET; T_ASSERT_POSIX_ZERO(error, "add event %zu to config", i); } T_SETUPEND; int error = perfmon_configure(config); T_ASSERT_POSIX_ZERO(error, "configured CPMU"); T_LOG("PMCR0 = 0x%016" PRIx64 ", PMESR0 = 0x%016" PRIx64 ", PMESR1 = 0x%016" PRIx64, cpmu_reg_state.pcr_pmcr0, cpmu_reg_state.pcr_pmesr[0], cpmu_reg_state.pcr_pmesr[1]); unsigned short fixed_count = cpmu_source->ps_layout.pl_fixed_count; for (size_t i = 0; i < ARRAYLEN(test_events); i++) { T_EXPECT_BITS_SET(cpmu_reg_state.pcr_pmcr0, 1ULL << test_events[i].pe_counter, "PMCR0 enabled event %zu", i); T_EXPECT_BITS_SET(cpmu_reg_state.pcr_pmcr0, 1ULL << (test_events[i].pe_counter + 12), "PMCR0 enabled PMIs for event %zu", i); uint64_t event_shift = (test_events[i].pe_counter - fixed_count) * 8; T_EXPECT_EQ((cpmu_reg_state.pcr_pmesr[0] >> event_shift) & 0xff, test_events[i].pe_number, "PMESR0 has event %zu set", i); } perfmon_config_destroy(config); } T_DECL(config_lock, "ensure only one config can be active at a time") { T_SETUPBEGIN; perfmon_machine_startup(); struct perfmon_source *cpmu_source = &perfmon_sources[0]; perfmon_config_t config = perfmon_config_create(cpmu_source); T_QUIET; T_ASSERT_NOTNULL(config, "created config"); perfmon_config_t config_later = perfmon_config_create(cpmu_source); T_QUIET; T_ASSERT_NOTNULL(config_later, "created later config"); int error = perfmon_configure(config); T_ASSERT_POSIX_ZERO(error, "configured CPMU"); T_SETUPEND; error = perfmon_configure(config_later); T_ASSERT_POSIX_ERROR(error, EBUSY, "later config should be unable to configure CPMU"); perfmon_config_destroy(config); perfmon_config_destroy(config_later); } T_DECL(config_release, "ensure the active config releases control") { T_SETUPBEGIN; perfmon_machine_startup(); struct perfmon_source *cpmu_source = &perfmon_sources[0]; perfmon_config_t config = perfmon_config_create(cpmu_source); T_QUIET; T_ASSERT_NOTNULL(config, "created config"); perfmon_config_t config_later = perfmon_config_create(cpmu_source); T_QUIET; T_ASSERT_NOTNULL(config_later, "created later config"); int error = perfmon_configure(config); T_ASSERT_POSIX_ZERO(error, "configured CPMU"); perfmon_config_destroy(config); T_LOG("destroyed first config"); T_SETUPEND; error = perfmon_configure(config_later); T_ASSERT_POSIX_ZERO(error, "later config configured CPMU"); perfmon_config_destroy(config_later); } |