Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <alloca.h> |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/sysctl.h> |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 10 | #include <mach/machine.h> |
| 11 | |
| 12 | #include <cpuinfo.h> |
| 13 | #include <mach/api.h> |
| 14 | #include <api.h> |
| 15 | #include <log.h> |
| 16 | |
| 17 | |
Marat Dukhan | ec86214 | 2017-10-18 17:24:46 -0700 | [diff] [blame] | 18 | struct cpuinfo_arm_isa cpuinfo_isa = { |
| 19 | #if CPUINFO_ARCH_ARM |
| 20 | .thumb = true, |
| 21 | .thumb2 = true, |
| 22 | .thumbee = false, |
| 23 | .jazelle = false, |
| 24 | .armv5e = true, |
| 25 | .armv6 = true, |
| 26 | .armv6k = true, |
| 27 | .armv7 = true, |
| 28 | .vfpv2 = false, |
| 29 | .vfpv3 = true, |
| 30 | .d32 = true, |
| 31 | .wmmx = false, |
| 32 | .wmmx2 = false, |
| 33 | .neon = true, |
| 34 | #endif |
| 35 | #if CPUINFO_ARCH_ARM64 |
| 36 | .aes = true, |
| 37 | .sha1 = true, |
| 38 | .sha2 = true, |
| 39 | .pmull = true, |
| 40 | .crc32 = true, |
| 41 | #endif |
| 42 | }; |
Marat Dukhan | 7b73888 | 2017-10-18 16:59:28 -0700 | [diff] [blame] | 43 | |
Marat Dukhan | 7d52b05 | 2018-03-18 22:57:05 -0700 | [diff] [blame] | 44 | static uint32_t get_sys_info(int type_specifier, const char* name) { |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 45 | size_t size = 0; |
| 46 | uint32_t result = 0; |
| 47 | int mib[2] = { CTL_HW, type_specifier }; |
| 48 | if (sysctl(mib, 2, NULL, &size, NULL, 0) != 0) { |
| 49 | cpuinfo_log_error("sysctl(\"%s\") failed: %s", name, strerror(errno)); |
| 50 | } else if (size == sizeof(uint32_t)) { |
| 51 | sysctl(mib, 2, &result, &size, NULL, 0); |
| 52 | cpuinfo_log_debug("%s: %"PRIu32 ", size = %lu", name, result, size); |
| 53 | } else { |
| 54 | cpuinfo_log_warning("sysctl does not support non-integer lookup for (\"%s\")", name); |
| 55 | } |
| 56 | return result; |
| 57 | } |
| 58 | |
Marat Dukhan | 7d52b05 | 2018-03-18 22:57:05 -0700 | [diff] [blame] | 59 | static uint64_t get_sys_info_by_name(const char* type_specifier) { |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 60 | size_t size = 0; |
| 61 | uint32_t result = 0; |
| 62 | if (sysctlbyname(type_specifier, NULL, &size, NULL, 0) != 0) { |
| 63 | cpuinfo_log_error("sysctlbyname(\"%s\") failed: %s", type_specifier, strerror(errno)); |
| 64 | } else if (size == sizeof(uint32_t)) { |
| 65 | sysctlbyname(type_specifier, &result, &size, NULL, 0); |
| 66 | cpuinfo_log_debug("%s: %"PRIu32 ", size = %lu", type_specifier, result, size); |
| 67 | } else { |
| 68 | cpuinfo_log_warning("sysctl does not support non-integer lookup for (\"%s\")", type_specifier); |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | static enum cpuinfo_uarch decode_uarch(uint32_t cpu_family, uint32_t cpu_subtype, uint32_t core_index) { |
| 74 | switch (cpu_family) { |
| 75 | case CPUFAMILY_ARM_SWIFT: |
| 76 | return cpuinfo_uarch_swift; |
| 77 | case CPUFAMILY_ARM_CYCLONE: |
| 78 | return cpuinfo_uarch_cyclone; |
| 79 | case CPUFAMILY_ARM_TYPHOON: |
| 80 | return cpuinfo_uarch_typhoon; |
| 81 | case CPUFAMILY_ARM_TWISTER: |
| 82 | return cpuinfo_uarch_twister; |
| 83 | case CPUFAMILY_ARM_HURRICANE: |
| 84 | return cpuinfo_uarch_hurricane; |
Marat Dukhan | 7b73888 | 2017-10-18 16:59:28 -0700 | [diff] [blame] | 85 | #ifdef CPUFAMILY_ARM_MONSOON_MISTRAL |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 86 | case CPUFAMILY_ARM_MONSOON_MISTRAL: |
Marat Dukhan | 7b73888 | 2017-10-18 16:59:28 -0700 | [diff] [blame] | 87 | #else |
| 88 | case 0xe81e7ef6: |
| 89 | /* Hard-coded value for older SDKs which do not define CPUFAMILY_ARM_MONSOON_MISTRAL */ |
| 90 | #endif |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 91 | /* 2x Monsoon + 4x Mistral cores */ |
| 92 | return core_index < 2 ? cpuinfo_uarch_monsoon : cpuinfo_uarch_mistral; |
| 93 | default: |
| 94 | /* Use hw.cpusubtype for detection */ |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | switch (cpu_subtype) { |
| 99 | case CPU_SUBTYPE_ARM_V7: |
| 100 | return cpuinfo_uarch_cortex_a8; |
| 101 | case CPU_SUBTYPE_ARM_V7F: |
| 102 | return cpuinfo_uarch_cortex_a9; |
| 103 | case CPU_SUBTYPE_ARM_V7K: |
| 104 | return cpuinfo_uarch_cortex_a7; |
| 105 | default: |
| 106 | return cpuinfo_uarch_unknown; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void decode_package_name(char* package_name) { |
| 111 | size_t size; |
| 112 | if (sysctlbyname("hw.machine", NULL, &size, NULL, 0) != 0) { |
| 113 | cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno)); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | char *machine_name = alloca(size); |
| 118 | if (sysctlbyname("hw.machine", machine_name, &size, NULL, 0) != 0) { |
| 119 | cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno)); |
| 120 | return; |
| 121 | } |
| 122 | cpuinfo_log_debug("hw.machine: %s", machine_name); |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 123 | |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 124 | char name[10]; |
| 125 | uint32_t major = 0, minor = 0; |
| 126 | if (sscanf(machine_name, "%9[^,0123456789]%"SCNu32",%"SCNu32, name, &major, &minor) != 3) { |
| 127 | cpuinfo_log_warning("parsing \"hw.machine\" failed: %s", strerror(errno)); |
| 128 | return; |
| 129 | } |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 130 | |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 131 | uint32_t chip_model = 0; |
| 132 | char suffix = '\0'; |
| 133 | if (strcmp(name, "iPhone") == 0) { |
| 134 | /* |
| 135 | * iPhone 4 and up are supported: |
| 136 | * - iPhone 4 [A4]: iPhone3,1, iPhone3,2, iPhone3,3 |
| 137 | * - iPhone 4S [A5]: iPhone4,1 |
| 138 | * - iPhone 5 [A6]: iPhone5,1, iPhone5,2 |
| 139 | * - iPhone 5c [A6]: iPhone5,3, iPhone5,4 |
| 140 | * - iPhone 5s [A7]: iPhone6,1, iPhone6,2 |
| 141 | * - iPhone 6 [A8]: iPhone7,2 |
| 142 | * - iPhone 6 Plus [A8]: iPhone7,1 |
| 143 | * - iPhone 6s [A9]: iPhone8,1 |
| 144 | * - iPhone 6s Plus [A9]: iPhone8,2 |
| 145 | * - iPhone SE [A9]: iPhone8,4 |
| 146 | * - iPhone 7 [A10]: iPhone9,1, iPhone9,3 |
| 147 | * - iPhone 7 Plus [A10]: iPhone9,2, iPhone9,4 |
| 148 | * - iPhone 8 [A11]: iPhone10,1, iPhone10,4 |
| 149 | * - iPhone 8 Plus [A11]: iPhone10,2, iPhone10,5 |
| 150 | * - iPhone X [A11]: iPhone10,3, iPhone10,6 |
Marat Dukhan | fd54c3d | 2018-09-30 22:21:20 -0700 | [diff] [blame] | 151 | * - iPhone XS [A12]: iPhone11,2, |
| 152 | * - iPhone XS Max [A12]: iPhone11,4, iPhone11,6 |
| 153 | * - iPhone XR [A12]: iPhone11,8 |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 154 | */ |
| 155 | chip_model = major + 1; |
| 156 | } else if (strcmp(name, "iPad") == 0) { |
| 157 | switch (major) { |
| 158 | /* iPad 2 and up are supported */ |
| 159 | case 2: |
| 160 | /* |
| 161 | * iPad 2 [A5]: iPad2,1, iPad2,2, iPad2,3, iPad2,4 |
| 162 | * iPad mini [A5]: iPad2,5, iPad2,6, iPad2,7 |
| 163 | */ |
| 164 | chip_model = major + 3; |
| 165 | break; |
| 166 | case 3: |
| 167 | /* |
| 168 | * iPad 3rd Gen [A5X]: iPad3,1, iPad3,2, iPad3,3 |
| 169 | * iPad 4th Gen [A6X]: iPad3,4, iPad3,5, iPad3,6 |
| 170 | */ |
| 171 | chip_model = (minor <= 3) ? 5 : 6; |
| 172 | suffix = 'X'; |
| 173 | break; |
| 174 | case 4: |
| 175 | /* |
| 176 | * iPad Air [A7]: iPad4,1, iPad4,2, iPad4,3 |
| 177 | * iPad mini Retina [A7]: iPad4,4, iPad4,5, iPad4,6 |
| 178 | * iPad mini 3 [A7]: iPad4,7, iPad4,8, iPad4,9 |
| 179 | */ |
| 180 | chip_model = major + 3; |
| 181 | break; |
| 182 | case 5: |
| 183 | /* |
| 184 | * iPad mini 4 [A8]: iPad5,1, iPad5,2 |
| 185 | * iPad Air 2 [A8X]: iPad5,3, iPad5,4 |
| 186 | */ |
| 187 | chip_model = major + 3; |
| 188 | suffix = (minor <= 2) ? '\0' : 'X'; |
| 189 | break; |
| 190 | case 6: |
| 191 | /* |
| 192 | * iPad Pro 9.7" [A9X]: iPad6,3, iPad6,4 |
| 193 | * iPad Pro [A9X]: iPad6,7, iPad6,8 |
| 194 | * iPad 5th Gen [A9]: iPad6,11, iPad6,12 |
| 195 | */ |
| 196 | chip_model = major + 3; |
| 197 | suffix = minor <= 8 ? 'X' : '\0'; |
| 198 | break; |
| 199 | case 7: |
| 200 | /* |
| 201 | * iPad Pro 12.9" [A10X]: iPad7,1, iPad7,2 |
| 202 | * iPad Pro 10.5" [A10X]: iPad7,3, iPad7,4 |
Marat Dukhan | fd54c3d | 2018-09-30 22:21:20 -0700 | [diff] [blame] | 203 | * iPad 6th Gen [A10]: iPad7,5, iPad7,6 |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 204 | */ |
| 205 | chip_model = major + 3; |
Marat Dukhan | fd54c3d | 2018-09-30 22:21:20 -0700 | [diff] [blame] | 206 | suffix = minor <= 4 ? 'X' : '\0'; |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 207 | break; |
| 208 | default: |
| 209 | cpuinfo_log_info("unknown iPad: %s", machine_name); |
| 210 | break; |
| 211 | } |
| 212 | } else if (strcmp(name, "iPod") == 0) { |
| 213 | switch (major) { |
| 214 | case 5: |
| 215 | chip_model = 5; |
| 216 | break; |
| 217 | /* iPod touch (5th Gen) [A5]: iPod5,1 */ |
| 218 | case 7: |
| 219 | /* iPod touch (6th Gen, 2015) [A8]: iPod7,1 */ |
| 220 | chip_model = 8; |
| 221 | break; |
| 222 | default: |
| 223 | cpuinfo_log_info("unknown iPod: %s", machine_name); |
| 224 | break; |
| 225 | } |
| 226 | } else { |
| 227 | cpuinfo_log_info("unknown device: %s", machine_name); |
| 228 | } |
| 229 | if (chip_model != 0) { |
| 230 | snprintf(package_name, CPUINFO_PACKAGE_NAME_MAX, "Apple A%"PRIu32"%c", chip_model, suffix); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void cpuinfo_arm_mach_init(void) { |
| 235 | struct cpuinfo_processor* processors = NULL; |
| 236 | struct cpuinfo_core* cores = NULL; |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 237 | struct cpuinfo_cluster* clusters = NULL; |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 238 | struct cpuinfo_package* packages = NULL; |
| 239 | struct cpuinfo_cache* l1i = NULL; |
| 240 | struct cpuinfo_cache* l1d = NULL; |
| 241 | struct cpuinfo_cache* l2 = NULL; |
| 242 | struct cpuinfo_cache* l3 = NULL; |
| 243 | |
| 244 | struct cpuinfo_mach_topology mach_topology = cpuinfo_mach_detect_topology(); |
| 245 | processors = calloc(mach_topology.threads, sizeof(struct cpuinfo_processor)); |
| 246 | if (processors == NULL) { |
| 247 | cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" logical processors", |
| 248 | mach_topology.threads * sizeof(struct cpuinfo_processor), mach_topology.threads); |
| 249 | goto cleanup; |
| 250 | } |
| 251 | cores = calloc(mach_topology.cores, sizeof(struct cpuinfo_core)); |
| 252 | if (cores == NULL) { |
| 253 | cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" cores", |
| 254 | mach_topology.cores * sizeof(struct cpuinfo_core), mach_topology.cores); |
| 255 | goto cleanup; |
| 256 | } |
| 257 | packages = calloc(mach_topology.packages, sizeof(struct cpuinfo_package)); |
| 258 | if (packages == NULL) { |
| 259 | cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" packages", |
| 260 | mach_topology.packages * sizeof(struct cpuinfo_package), mach_topology.packages); |
| 261 | goto cleanup; |
| 262 | } |
| 263 | |
| 264 | const uint32_t threads_per_core = mach_topology.threads / mach_topology.cores; |
| 265 | const uint32_t threads_per_package = mach_topology.threads / mach_topology.packages; |
| 266 | const uint32_t cores_per_package = mach_topology.cores / mach_topology.packages; |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 267 | |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 268 | for (uint32_t i = 0; i < mach_topology.packages; i++) { |
| 269 | packages[i] = (struct cpuinfo_package) { |
| 270 | .processor_start = i * threads_per_package, |
| 271 | .processor_count = threads_per_package, |
| 272 | .core_start = i * cores_per_package, |
| 273 | .core_count = cores_per_package, |
| 274 | }; |
| 275 | decode_package_name(packages[i].name); |
| 276 | } |
Marat Dukhan | ec86214 | 2017-10-18 17:24:46 -0700 | [diff] [blame] | 277 | |
| 278 | |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 279 | const uint32_t cpu_family = get_sys_info_by_name("hw.cpufamily"); |
Marat Dukhan | ec86214 | 2017-10-18 17:24:46 -0700 | [diff] [blame] | 280 | const uint32_t cpu_type = get_sys_info_by_name("hw.cputype"); |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 281 | const uint32_t cpu_subtype = get_sys_info_by_name("hw.cpusubtype"); |
Marat Dukhan | ec86214 | 2017-10-18 17:24:46 -0700 | [diff] [blame] | 282 | switch (cpu_type) { |
| 283 | case CPU_TYPE_ARM64: |
| 284 | cpuinfo_isa.aes = true; |
| 285 | cpuinfo_isa.sha1 = true; |
| 286 | cpuinfo_isa.sha2 = true; |
| 287 | cpuinfo_isa.pmull = true; |
| 288 | cpuinfo_isa.crc32 = true; |
| 289 | break; |
| 290 | #if CPUINFO_ARCH_ARM |
| 291 | case CPU_TYPE_ARM: |
| 292 | switch (cpu_subtype) { |
| 293 | case CPU_SUBTYPE_ARM_V8: |
| 294 | cpuinfo_isa.aes = true; |
| 295 | cpuinfo_isa.sha1 = true; |
| 296 | cpuinfo_isa.sha2 = true; |
| 297 | cpuinfo_isa.pmull = true; |
| 298 | cpuinfo_isa.crc32 = true; |
| 299 | /* Fall-through to add ARMv7S features */ |
| 300 | case CPU_SUBTYPE_ARM_V7S: |
| 301 | case CPU_SUBTYPE_ARM_V7K: |
| 302 | cpuinfo_isa.fma = true; |
| 303 | /* Fall-through to add ARMv7F features */ |
| 304 | case CPU_SUBTYPE_ARM_V7F: |
| 305 | cpuinfo_isa.armv7mp = true; |
| 306 | cpuinfo_isa.fp16 = true; |
| 307 | /* Fall-through to add ARMv7 features */ |
| 308 | case CPU_SUBTYPE_ARM_V7: |
| 309 | break; |
| 310 | default: |
| 311 | break; |
| 312 | } |
| 313 | break; |
| 314 | #endif |
| 315 | } |
| 316 | |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 317 | uint32_t num_clusters = 1; |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 318 | for (uint32_t i = 0; i < mach_topology.cores; i++) { |
| 319 | cores[i] = (struct cpuinfo_core) { |
| 320 | .processor_start = i * threads_per_core, |
| 321 | .processor_count = threads_per_core, |
| 322 | .core_id = i % cores_per_package, |
| 323 | .package = packages + i / cores_per_package, |
| 324 | .vendor = cpuinfo_vendor_apple, |
| 325 | .uarch = decode_uarch(cpu_family, cpu_subtype, i), |
| 326 | }; |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 327 | if (i != 0 && cores[i].uarch != cores[i - 1].uarch) { |
| 328 | num_clusters++; |
| 329 | } |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 330 | } |
| 331 | for (uint32_t i = 0; i < mach_topology.threads; i++) { |
| 332 | const uint32_t smt_id = i % threads_per_core; |
| 333 | const uint32_t core_id = i / threads_per_core; |
| 334 | const uint32_t package_id = i / threads_per_package; |
| 335 | |
| 336 | processors[i].smt_id = smt_id; |
Marat Dukhan | 7fcd441 | 2017-11-30 09:46:49 -0800 | [diff] [blame] | 337 | processors[i].core = &cores[core_id]; |
| 338 | processors[i].package = &packages[package_id]; |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 341 | clusters = calloc(num_clusters, sizeof(struct cpuinfo_cluster)); |
| 342 | if (clusters == NULL) { |
| 343 | cpuinfo_log_error( |
| 344 | "failed to allocate %zu bytes for descriptions of %"PRIu32" clusters", |
| 345 | num_clusters * sizeof(struct cpuinfo_cluster), num_clusters); |
| 346 | goto cleanup; |
| 347 | } |
| 348 | uint32_t cluster_idx = UINT32_MAX; |
| 349 | for (uint32_t i = 0; i < mach_topology.cores; i++) { |
| 350 | if (i == 0 || cores[i].uarch != cores[i - 1].uarch) { |
| 351 | cluster_idx++; |
| 352 | clusters[cluster_idx] = (struct cpuinfo_cluster) { |
| 353 | .processor_start = i * threads_per_core, |
| 354 | .processor_count = 1, |
| 355 | .core_start = i, |
| 356 | .core_count = 1, |
| 357 | .cluster_id = cluster_idx, |
| 358 | .package = cores[i].package, |
| 359 | .vendor = cores[i].vendor, |
| 360 | .uarch = cores[i].uarch, |
| 361 | }; |
| 362 | } else { |
| 363 | clusters[cluster_idx].processor_count++; |
| 364 | clusters[cluster_idx].core_count++; |
| 365 | } |
| 366 | cores[i].cluster = &clusters[cluster_idx]; |
| 367 | } |
| 368 | |
| 369 | for (uint32_t i = 0; i < mach_topology.threads; i++) { |
| 370 | const uint32_t core_id = i / threads_per_core; |
| 371 | processors[i].cluster = cores[core_id].cluster; |
| 372 | } |
| 373 | |
| 374 | for (uint32_t i = 0; i < mach_topology.packages; i++) { |
| 375 | packages[i].cluster_start = 0; |
| 376 | packages[i].cluster_count = num_clusters; |
| 377 | } |
| 378 | |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 379 | const uint32_t cacheline_size = get_sys_info(HW_CACHELINE, "HW_CACHELINE"); |
| 380 | const uint32_t l1d_cache_size = get_sys_info(HW_L1DCACHESIZE, "HW_L1DCACHESIZE"); |
| 381 | const uint32_t l1i_cache_size = get_sys_info(HW_L1ICACHESIZE, "HW_L1ICACHESIZE"); |
| 382 | const uint32_t l2_cache_size = get_sys_info(HW_L2CACHESIZE, "HW_L2CACHESIZE"); |
| 383 | const uint32_t l3_cache_size = get_sys_info(HW_L3CACHESIZE, "HW_L3CACHESIZE"); |
| 384 | const uint32_t l1_cache_associativity = 4; |
| 385 | const uint32_t l2_cache_associativity = 8; |
| 386 | const uint32_t l3_cache_associativity = 16; |
| 387 | const uint32_t cache_partitions = 1; |
| 388 | const uint32_t cache_flags = 0; |
| 389 | |
| 390 | uint32_t threads_per_l1 = 0, l1_count = 0; |
| 391 | if (l1i_cache_size != 0 || l1d_cache_size != 0) { |
Hao Lu | 3617d5b | 2017-10-23 15:16:50 -0700 | [diff] [blame] | 392 | /* Assume L1 caches are private to each core */ |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 393 | threads_per_l1 = 1; |
| 394 | l1_count = mach_topology.threads / threads_per_l1; |
| 395 | cpuinfo_log_debug("detected %"PRIu32" L1 caches", l1_count); |
| 396 | } |
| 397 | |
| 398 | uint32_t threads_per_l2 = 0, l2_count = 0; |
| 399 | if (l2_cache_size != 0) { |
Hao Lu | 3617d5b | 2017-10-23 15:16:50 -0700 | [diff] [blame] | 400 | /* Assume L2 cache is shared between all cores */ |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 401 | threads_per_l2 = mach_topology.cores; |
| 402 | l2_count = 1; |
| 403 | cpuinfo_log_debug("detected %"PRIu32" L2 caches", l2_count); |
| 404 | } |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 405 | |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 406 | uint32_t threads_per_l3 = 0, l3_count = 0; |
| 407 | if (l3_cache_size != 0) { |
Hao Lu | 3617d5b | 2017-10-23 15:16:50 -0700 | [diff] [blame] | 408 | /* Assume L3 cache is shared between all cores */ |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 409 | threads_per_l3 = mach_topology.cores; |
| 410 | l3_count = 1; |
| 411 | cpuinfo_log_debug("detected %"PRIu32" L3 caches", l3_count); |
| 412 | } |
| 413 | |
| 414 | if (l1i_cache_size != 0) { |
| 415 | l1i = calloc(l1_count, sizeof(struct cpuinfo_cache)); |
| 416 | if (l1i == NULL) { |
| 417 | cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L1I caches", |
| 418 | l1_count * sizeof(struct cpuinfo_cache), l1_count); |
| 419 | goto cleanup; |
| 420 | } |
| 421 | for (uint32_t c = 0; c < l1_count; c++) { |
| 422 | l1i[c] = (struct cpuinfo_cache) { |
| 423 | .size = l1i_cache_size, |
| 424 | .associativity = l1_cache_associativity, |
| 425 | .sets = l1i_cache_size / (l1_cache_associativity * cacheline_size), |
| 426 | .partitions = cache_partitions, |
| 427 | .line_size = cacheline_size, |
| 428 | .flags = cache_flags, |
| 429 | .processor_start = c * threads_per_l1, |
| 430 | .processor_count = threads_per_l1, |
| 431 | }; |
| 432 | } |
| 433 | for (uint32_t t = 0; t < mach_topology.threads; t++) { |
| 434 | processors[t].cache.l1i = &l1i[t / threads_per_l1]; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (l1d_cache_size != 0) { |
| 439 | l1d = calloc(l1_count, sizeof(struct cpuinfo_cache)); |
| 440 | if (l1d == NULL) { |
| 441 | cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L1D caches", |
| 442 | l1_count * sizeof(struct cpuinfo_cache), l1_count); |
| 443 | goto cleanup; |
| 444 | } |
| 445 | for (uint32_t c = 0; c < l1_count; c++) { |
| 446 | l1d[c] = (struct cpuinfo_cache) { |
| 447 | .size = l1d_cache_size, |
| 448 | .associativity = l1_cache_associativity, |
| 449 | .sets = l1d_cache_size / (l1_cache_associativity * cacheline_size), |
| 450 | .partitions = cache_partitions, |
| 451 | .line_size = cacheline_size, |
| 452 | .flags = cache_flags, |
| 453 | .processor_start = c * threads_per_l1, |
| 454 | .processor_count = threads_per_l1, |
| 455 | }; |
| 456 | } |
| 457 | for (uint32_t t = 0; t < mach_topology.threads; t++) { |
| 458 | processors[t].cache.l1d = &l1d[t / threads_per_l1]; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (l2_count != 0) { |
| 463 | l2 = calloc(l2_count, sizeof(struct cpuinfo_cache)); |
| 464 | if (l2 == NULL) { |
| 465 | cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L2 caches", |
| 466 | l2_count * sizeof(struct cpuinfo_cache), l2_count); |
| 467 | goto cleanup; |
| 468 | } |
| 469 | for (uint32_t c = 0; c < l2_count; c++) { |
| 470 | l2[c] = (struct cpuinfo_cache) { |
| 471 | .size = l2_cache_size, |
| 472 | .associativity = l2_cache_associativity, |
| 473 | .sets = l2_cache_size / (l2_cache_associativity * cacheline_size), |
| 474 | .partitions = cache_partitions, |
| 475 | .line_size = cacheline_size, |
| 476 | .flags = cache_flags, |
| 477 | .processor_start = c * threads_per_l2, |
| 478 | .processor_count = threads_per_l2, |
| 479 | }; |
| 480 | } |
| 481 | for (uint32_t t = 0; t < mach_topology.threads; t++) { |
| 482 | processors[t].cache.l2 = &l2[0]; |
| 483 | } |
| 484 | } |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 485 | |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 486 | if (l3_count != 0) { |
| 487 | l3 = calloc(l3_count, sizeof(struct cpuinfo_cache)); |
| 488 | if (l3 == NULL) { |
| 489 | cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L3 caches", |
| 490 | l3_count * sizeof(struct cpuinfo_cache), l3_count); |
| 491 | goto cleanup; |
| 492 | } |
| 493 | for (uint32_t c = 0; c < l3_count; c++) { |
| 494 | l3[c] = (struct cpuinfo_cache) { |
| 495 | .size = l3_cache_size, |
| 496 | .associativity = l3_cache_associativity, |
| 497 | .sets = l3_cache_size / (l3_cache_associativity * cacheline_size), |
| 498 | .partitions = cache_partitions, |
| 499 | .line_size = cacheline_size, |
| 500 | .flags = cache_flags, |
| 501 | .processor_start = c * threads_per_l3, |
| 502 | .processor_count = threads_per_l3, |
| 503 | }; |
| 504 | } |
| 505 | for (uint32_t t = 0; t < mach_topology.threads; t++) { |
| 506 | processors[t].cache.l3 = &l3[0]; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /* Commit changes */ |
| 511 | cpuinfo_cache[cpuinfo_cache_level_1i] = l1i; |
| 512 | cpuinfo_cache[cpuinfo_cache_level_1d] = l1d; |
| 513 | cpuinfo_cache[cpuinfo_cache_level_2] = l2; |
| 514 | cpuinfo_cache[cpuinfo_cache_level_3] = l3; |
| 515 | |
| 516 | cpuinfo_processors = processors; |
| 517 | cpuinfo_cores = cores; |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 518 | cpuinfo_clusters = clusters; |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 519 | cpuinfo_packages = packages; |
| 520 | |
| 521 | cpuinfo_cache_count[cpuinfo_cache_level_1i] = l1_count; |
| 522 | cpuinfo_cache_count[cpuinfo_cache_level_1d] = l1_count; |
| 523 | cpuinfo_cache_count[cpuinfo_cache_level_2] = l2_count; |
| 524 | cpuinfo_cache_count[cpuinfo_cache_level_3] = l3_count; |
| 525 | |
| 526 | cpuinfo_processors_count = mach_topology.threads; |
| 527 | cpuinfo_cores_count = mach_topology.cores; |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 528 | cpuinfo_clusters_count = num_clusters; |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 529 | cpuinfo_packages_count = mach_topology.packages; |
| 530 | |
Marat Dukhan | cf70aee | 2018-03-24 23:21:02 -0700 | [diff] [blame] | 531 | __sync_synchronize(); |
| 532 | |
| 533 | cpuinfo_is_initialized = true; |
| 534 | |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 535 | processors = NULL; |
| 536 | cores = NULL; |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 537 | clusters = NULL; |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 538 | packages = NULL; |
| 539 | l1i = l1d = l2 = l3 = NULL; |
| 540 | |
| 541 | cleanup: |
| 542 | free(processors); |
| 543 | free(cores); |
Hao Lu | 8c2a383 | 2018-07-23 23:12:11 -0700 | [diff] [blame] | 544 | free(clusters); |
Hao Lu | 922070c | 2017-10-18 16:29:02 -0700 | [diff] [blame] | 545 | free(packages); |
| 546 | free(l1i); |
| 547 | free(l1d); |
| 548 | free(l2); |
| 549 | free(l3); |
| 550 | } |