blob: edadb7a8768d605299d5108e547deb7701ed58b1 [file] [log] [blame]
Hao Lu922070c2017-10-18 16:29:02 -07001#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>
10#include <mach/host_info.h>
11#include <mach/mach_host.h>
12#include <mach/machine.h>
13
14#include <cpuinfo.h>
15#include <mach/api.h>
16#include <api.h>
17#include <log.h>
18
19
20static uint32_t get_sys_info(int type_specifier, char* name) {
21 size_t size = 0;
22 uint32_t result = 0;
23 int mib[2] = { CTL_HW, type_specifier };
24 if (sysctl(mib, 2, NULL, &size, NULL, 0) != 0) {
25 cpuinfo_log_error("sysctl(\"%s\") failed: %s", name, strerror(errno));
26 } else if (size == sizeof(uint32_t)) {
27 sysctl(mib, 2, &result, &size, NULL, 0);
28 cpuinfo_log_debug("%s: %"PRIu32 ", size = %lu", name, result, size);
29 } else {
30 cpuinfo_log_warning("sysctl does not support non-integer lookup for (\"%s\")", name);
31 }
32 return result;
33}
34
35static uint64_t get_sys_info_by_name(char* type_specifier) {
36 size_t size = 0;
37 uint32_t result = 0;
38 if (sysctlbyname(type_specifier, NULL, &size, NULL, 0) != 0) {
39 cpuinfo_log_error("sysctlbyname(\"%s\") failed: %s", type_specifier, strerror(errno));
40 } else if (size == sizeof(uint32_t)) {
41 sysctlbyname(type_specifier, &result, &size, NULL, 0);
42 cpuinfo_log_debug("%s: %"PRIu32 ", size = %lu", type_specifier, result, size);
43 } else {
44 cpuinfo_log_warning("sysctl does not support non-integer lookup for (\"%s\")", type_specifier);
45 }
46 return result;
47}
48
49static enum cpuinfo_uarch decode_uarch(uint32_t cpu_family, uint32_t cpu_subtype, uint32_t core_index) {
50 switch (cpu_family) {
51 case CPUFAMILY_ARM_SWIFT:
52 return cpuinfo_uarch_swift;
53 case CPUFAMILY_ARM_CYCLONE:
54 return cpuinfo_uarch_cyclone;
55 case CPUFAMILY_ARM_TYPHOON:
56 return cpuinfo_uarch_typhoon;
57 case CPUFAMILY_ARM_TWISTER:
58 return cpuinfo_uarch_twister;
59 case CPUFAMILY_ARM_HURRICANE:
60 return cpuinfo_uarch_hurricane;
61 case CPUFAMILY_ARM_MONSOON_MISTRAL:
62 /* 2x Monsoon + 4x Mistral cores */
63 return core_index < 2 ? cpuinfo_uarch_monsoon : cpuinfo_uarch_mistral;
64 default:
65 /* Use hw.cpusubtype for detection */
66 break;
67 }
68
69 switch (cpu_subtype) {
70 case CPU_SUBTYPE_ARM_V7:
71 return cpuinfo_uarch_cortex_a8;
72 case CPU_SUBTYPE_ARM_V7F:
73 return cpuinfo_uarch_cortex_a9;
74 case CPU_SUBTYPE_ARM_V7K:
75 return cpuinfo_uarch_cortex_a7;
76 default:
77 return cpuinfo_uarch_unknown;
78 }
79}
80
81static void decode_package_name(char* package_name) {
82 size_t size;
83 if (sysctlbyname("hw.machine", NULL, &size, NULL, 0) != 0) {
84 cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno));
85 return;
86 }
87
88 char *machine_name = alloca(size);
89 if (sysctlbyname("hw.machine", machine_name, &size, NULL, 0) != 0) {
90 cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno));
91 return;
92 }
93 cpuinfo_log_debug("hw.machine: %s", machine_name);
94
95 char name[10];
96 uint32_t major = 0, minor = 0;
97 if (sscanf(machine_name, "%9[^,0123456789]%"SCNu32",%"SCNu32, name, &major, &minor) != 3) {
98 cpuinfo_log_warning("parsing \"hw.machine\" failed: %s", strerror(errno));
99 return;
100 }
101
102 uint32_t chip_model = 0;
103 char suffix = '\0';
104 if (strcmp(name, "iPhone") == 0) {
105 /*
106 * iPhone 4 and up are supported:
107 * - iPhone 4 [A4]: iPhone3,1, iPhone3,2, iPhone3,3
108 * - iPhone 4S [A5]: iPhone4,1
109 * - iPhone 5 [A6]: iPhone5,1, iPhone5,2
110 * - iPhone 5c [A6]: iPhone5,3, iPhone5,4
111 * - iPhone 5s [A7]: iPhone6,1, iPhone6,2
112 * - iPhone 6 [A8]: iPhone7,2
113 * - iPhone 6 Plus [A8]: iPhone7,1
114 * - iPhone 6s [A9]: iPhone8,1
115 * - iPhone 6s Plus [A9]: iPhone8,2
116 * - iPhone SE [A9]: iPhone8,4
117 * - iPhone 7 [A10]: iPhone9,1, iPhone9,3
118 * - iPhone 7 Plus [A10]: iPhone9,2, iPhone9,4
119 * - iPhone 8 [A11]: iPhone10,1, iPhone10,4
120 * - iPhone 8 Plus [A11]: iPhone10,2, iPhone10,5
121 * - iPhone X [A11]: iPhone10,3, iPhone10,6
122 */
123 chip_model = major + 1;
124 } else if (strcmp(name, "iPad") == 0) {
125 switch (major) {
126 /* iPad 2 and up are supported */
127 case 2:
128 /*
129 * iPad 2 [A5]: iPad2,1, iPad2,2, iPad2,3, iPad2,4
130 * iPad mini [A5]: iPad2,5, iPad2,6, iPad2,7
131 */
132 chip_model = major + 3;
133 break;
134 case 3:
135 /*
136 * iPad 3rd Gen [A5X]: iPad3,1, iPad3,2, iPad3,3
137 * iPad 4th Gen [A6X]: iPad3,4, iPad3,5, iPad3,6
138 */
139 chip_model = (minor <= 3) ? 5 : 6;
140 suffix = 'X';
141 break;
142 case 4:
143 /*
144 * iPad Air [A7]: iPad4,1, iPad4,2, iPad4,3
145 * iPad mini Retina [A7]: iPad4,4, iPad4,5, iPad4,6
146 * iPad mini 3 [A7]: iPad4,7, iPad4,8, iPad4,9
147 */
148 chip_model = major + 3;
149 break;
150 case 5:
151 /*
152 * iPad mini 4 [A8]: iPad5,1, iPad5,2
153 * iPad Air 2 [A8X]: iPad5,3, iPad5,4
154 */
155 chip_model = major + 3;
156 suffix = (minor <= 2) ? '\0' : 'X';
157 break;
158 case 6:
159 /*
160 * iPad Pro 9.7" [A9X]: iPad6,3, iPad6,4
161 * iPad Pro [A9X]: iPad6,7, iPad6,8
162 * iPad 5th Gen [A9]: iPad6,11, iPad6,12
163 */
164 chip_model = major + 3;
165 suffix = minor <= 8 ? 'X' : '\0';
166 break;
167 case 7:
168 /*
169 * iPad Pro 12.9" [A10X]: iPad7,1, iPad7,2
170 * iPad Pro 10.5" [A10X]: iPad7,3, iPad7,4
171 */
172 chip_model = major + 3;
173 suffix = 'X';
174 break;
175 default:
176 cpuinfo_log_info("unknown iPad: %s", machine_name);
177 break;
178 }
179 } else if (strcmp(name, "iPod") == 0) {
180 switch (major) {
181 case 5:
182 chip_model = 5;
183 break;
184 /* iPod touch (5th Gen) [A5]: iPod5,1 */
185 case 7:
186 /* iPod touch (6th Gen, 2015) [A8]: iPod7,1 */
187 chip_model = 8;
188 break;
189 default:
190 cpuinfo_log_info("unknown iPod: %s", machine_name);
191 break;
192 }
193 } else {
194 cpuinfo_log_info("unknown device: %s", machine_name);
195 }
196 if (chip_model != 0) {
197 snprintf(package_name, CPUINFO_PACKAGE_NAME_MAX, "Apple A%"PRIu32"%c", chip_model, suffix);
198 }
199}
200
201void cpuinfo_arm_mach_init(void) {
202 struct cpuinfo_processor* processors = NULL;
203 struct cpuinfo_core* cores = NULL;
204 struct cpuinfo_package* packages = NULL;
205 struct cpuinfo_cache* l1i = NULL;
206 struct cpuinfo_cache* l1d = NULL;
207 struct cpuinfo_cache* l2 = NULL;
208 struct cpuinfo_cache* l3 = NULL;
209
210 struct cpuinfo_mach_topology mach_topology = cpuinfo_mach_detect_topology();
211 processors = calloc(mach_topology.threads, sizeof(struct cpuinfo_processor));
212 if (processors == NULL) {
213 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" logical processors",
214 mach_topology.threads * sizeof(struct cpuinfo_processor), mach_topology.threads);
215 goto cleanup;
216 }
217 cores = calloc(mach_topology.cores, sizeof(struct cpuinfo_core));
218 if (cores == NULL) {
219 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" cores",
220 mach_topology.cores * sizeof(struct cpuinfo_core), mach_topology.cores);
221 goto cleanup;
222 }
223 packages = calloc(mach_topology.packages, sizeof(struct cpuinfo_package));
224 if (packages == NULL) {
225 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" packages",
226 mach_topology.packages * sizeof(struct cpuinfo_package), mach_topology.packages);
227 goto cleanup;
228 }
229
230 const uint32_t threads_per_core = mach_topology.threads / mach_topology.cores;
231 const uint32_t threads_per_package = mach_topology.threads / mach_topology.packages;
232 const uint32_t cores_per_package = mach_topology.cores / mach_topology.packages;
233
234 for (uint32_t i = 0; i < mach_topology.packages; i++) {
235 packages[i] = (struct cpuinfo_package) {
236 .processor_start = i * threads_per_package,
237 .processor_count = threads_per_package,
238 .core_start = i * cores_per_package,
239 .core_count = cores_per_package,
240 };
241 decode_package_name(packages[i].name);
242 }
243
244 const uint32_t cpu_family = get_sys_info_by_name("hw.cpufamily");
245 const uint32_t cpu_subtype = get_sys_info_by_name("hw.cpusubtype");
246 for (uint32_t i = 0; i < mach_topology.cores; i++) {
247 cores[i] = (struct cpuinfo_core) {
248 .processor_start = i * threads_per_core,
249 .processor_count = threads_per_core,
250 .core_id = i % cores_per_package,
251 .package = packages + i / cores_per_package,
252 .vendor = cpuinfo_vendor_apple,
253 .uarch = decode_uarch(cpu_family, cpu_subtype, i),
254 };
255 }
256 for (uint32_t i = 0; i < mach_topology.threads; i++) {
257 const uint32_t smt_id = i % threads_per_core;
258 const uint32_t core_id = i / threads_per_core;
259 const uint32_t package_id = i / threads_per_package;
260
261 processors[i].smt_id = smt_id;
262 processors[i].core = cores + i / threads_per_core;
263 processors[i].package = packages + i / threads_per_package;
264 }
265
266 const uint32_t cacheline_size = get_sys_info(HW_CACHELINE, "HW_CACHELINE");
267 const uint32_t l1d_cache_size = get_sys_info(HW_L1DCACHESIZE, "HW_L1DCACHESIZE");
268 const uint32_t l1i_cache_size = get_sys_info(HW_L1ICACHESIZE, "HW_L1ICACHESIZE");
269 const uint32_t l2_cache_size = get_sys_info(HW_L2CACHESIZE, "HW_L2CACHESIZE");
270 const uint32_t l3_cache_size = get_sys_info(HW_L3CACHESIZE, "HW_L3CACHESIZE");
271 const uint32_t l1_cache_associativity = 4;
272 const uint32_t l2_cache_associativity = 8;
273 const uint32_t l3_cache_associativity = 16;
274 const uint32_t cache_partitions = 1;
275 const uint32_t cache_flags = 0;
276
277 uint32_t threads_per_l1 = 0, l1_count = 0;
278 if (l1i_cache_size != 0 || l1d_cache_size != 0) {
279 /* Assume that L1 caches are private to each core */
280 threads_per_l1 = 1;
281 l1_count = mach_topology.threads / threads_per_l1;
282 cpuinfo_log_debug("detected %"PRIu32" L1 caches", l1_count);
283 }
284
285 uint32_t threads_per_l2 = 0, l2_count = 0;
286 if (l2_cache_size != 0) {
287 /* L2 cache is shared between all cores */
288 threads_per_l2 = mach_topology.cores;
289 l2_count = 1;
290 cpuinfo_log_debug("detected %"PRIu32" L2 caches", l2_count);
291 }
292
293 uint32_t threads_per_l3 = 0, l3_count = 0;
294 if (l3_cache_size != 0) {
295 /* L3 cache is shared between all cores */
296 threads_per_l3 = mach_topology.cores;
297 l3_count = 1;
298 cpuinfo_log_debug("detected %"PRIu32" L3 caches", l3_count);
299 }
300
301 if (l1i_cache_size != 0) {
302 l1i = calloc(l1_count, sizeof(struct cpuinfo_cache));
303 if (l1i == NULL) {
304 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L1I caches",
305 l1_count * sizeof(struct cpuinfo_cache), l1_count);
306 goto cleanup;
307 }
308 for (uint32_t c = 0; c < l1_count; c++) {
309 l1i[c] = (struct cpuinfo_cache) {
310 .size = l1i_cache_size,
311 .associativity = l1_cache_associativity,
312 .sets = l1i_cache_size / (l1_cache_associativity * cacheline_size),
313 .partitions = cache_partitions,
314 .line_size = cacheline_size,
315 .flags = cache_flags,
316 .processor_start = c * threads_per_l1,
317 .processor_count = threads_per_l1,
318 };
319 }
320 for (uint32_t t = 0; t < mach_topology.threads; t++) {
321 processors[t].cache.l1i = &l1i[t / threads_per_l1];
322 }
323 }
324
325 if (l1d_cache_size != 0) {
326 l1d = calloc(l1_count, sizeof(struct cpuinfo_cache));
327 if (l1d == NULL) {
328 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L1D caches",
329 l1_count * sizeof(struct cpuinfo_cache), l1_count);
330 goto cleanup;
331 }
332 for (uint32_t c = 0; c < l1_count; c++) {
333 l1d[c] = (struct cpuinfo_cache) {
334 .size = l1d_cache_size,
335 .associativity = l1_cache_associativity,
336 .sets = l1d_cache_size / (l1_cache_associativity * cacheline_size),
337 .partitions = cache_partitions,
338 .line_size = cacheline_size,
339 .flags = cache_flags,
340 .processor_start = c * threads_per_l1,
341 .processor_count = threads_per_l1,
342 };
343 }
344 for (uint32_t t = 0; t < mach_topology.threads; t++) {
345 processors[t].cache.l1d = &l1d[t / threads_per_l1];
346 }
347 }
348
349 if (l2_count != 0) {
350 l2 = calloc(l2_count, sizeof(struct cpuinfo_cache));
351 if (l2 == NULL) {
352 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L2 caches",
353 l2_count * sizeof(struct cpuinfo_cache), l2_count);
354 goto cleanup;
355 }
356 for (uint32_t c = 0; c < l2_count; c++) {
357 l2[c] = (struct cpuinfo_cache) {
358 .size = l2_cache_size,
359 .associativity = l2_cache_associativity,
360 .sets = l2_cache_size / (l2_cache_associativity * cacheline_size),
361 .partitions = cache_partitions,
362 .line_size = cacheline_size,
363 .flags = cache_flags,
364 .processor_start = c * threads_per_l2,
365 .processor_count = threads_per_l2,
366 };
367 }
368 for (uint32_t t = 0; t < mach_topology.threads; t++) {
369 processors[t].cache.l2 = &l2[0];
370 }
371 }
372
373 if (l3_count != 0) {
374 l3 = calloc(l3_count, sizeof(struct cpuinfo_cache));
375 if (l3 == NULL) {
376 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L3 caches",
377 l3_count * sizeof(struct cpuinfo_cache), l3_count);
378 goto cleanup;
379 }
380 for (uint32_t c = 0; c < l3_count; c++) {
381 l3[c] = (struct cpuinfo_cache) {
382 .size = l3_cache_size,
383 .associativity = l3_cache_associativity,
384 .sets = l3_cache_size / (l3_cache_associativity * cacheline_size),
385 .partitions = cache_partitions,
386 .line_size = cacheline_size,
387 .flags = cache_flags,
388 .processor_start = c * threads_per_l3,
389 .processor_count = threads_per_l3,
390 };
391 }
392 for (uint32_t t = 0; t < mach_topology.threads; t++) {
393 processors[t].cache.l3 = &l3[0];
394 }
395 }
396
397 /* Commit changes */
398 cpuinfo_cache[cpuinfo_cache_level_1i] = l1i;
399 cpuinfo_cache[cpuinfo_cache_level_1d] = l1d;
400 cpuinfo_cache[cpuinfo_cache_level_2] = l2;
401 cpuinfo_cache[cpuinfo_cache_level_3] = l3;
402
403 cpuinfo_processors = processors;
404 cpuinfo_cores = cores;
405 cpuinfo_packages = packages;
406
407 cpuinfo_cache_count[cpuinfo_cache_level_1i] = l1_count;
408 cpuinfo_cache_count[cpuinfo_cache_level_1d] = l1_count;
409 cpuinfo_cache_count[cpuinfo_cache_level_2] = l2_count;
410 cpuinfo_cache_count[cpuinfo_cache_level_3] = l3_count;
411
412 cpuinfo_processors_count = mach_topology.threads;
413 cpuinfo_cores_count = mach_topology.cores;
414 cpuinfo_packages_count = mach_topology.packages;
415
416 processors = NULL;
417 cores = NULL;
418 packages = NULL;
419 l1i = l1d = l2 = l3 = NULL;
420
421cleanup:
422 free(processors);
423 free(cores);
424 free(packages);
425 free(l1i);
426 free(l1d);
427 free(l2);
428 free(l3);
429}