Skip to content

Commit 5e3d244

Browse files
authored
Merge pull request #246 from dlenski/main
For Apple silicon, use machdep.cpu.brand_string in preference to decoding hw.machine
2 parents 1a02246 + 2624a7e commit 5e3d244

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/arm/mach/init.c

+25-5
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,42 @@ static enum cpuinfo_uarch decode_uarch(uint32_t cpu_family, uint32_t core_index,
101101
return cpuinfo_uarch_unknown;
102102
}
103103

104-
static void decode_package_name(char* package_name) {
104+
static int read_package_name_from_brand_string(char* package_name) {
105+
size_t size;
106+
if (sysctlbyname("machdep.cpu.brand_string", NULL, &size, NULL, 0) != 0) {
107+
sysctlfail:
108+
cpuinfo_log_warning("sysctlbyname(\"machdep.cpu.brand_string\") failed: %s", strerror(errno));
109+
return false;
110+
}
111+
112+
char* brand_string = alloca(size);
113+
if (sysctlbyname("machdep.cpu.brand_string", brand_string, &size, NULL, 0) != 0)
114+
goto sysctlfail;
115+
cpuinfo_log_debug("machdep.cpu.brand_string: %s", brand_string);
116+
117+
strlcpy(package_name, brand_string, CPUINFO_PACKAGE_NAME_MAX);
118+
return true;
119+
}
120+
121+
static int decode_package_name_from_hw_machine(char* package_name) {
105122
size_t size;
106123
if (sysctlbyname("hw.machine", NULL, &size, NULL, 0) != 0) {
107124
cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno));
108-
return;
125+
return false;
109126
}
110127

111128
char* machine_name = alloca(size);
112129
if (sysctlbyname("hw.machine", machine_name, &size, NULL, 0) != 0) {
113130
cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno));
114-
return;
131+
return false;
115132
}
116133
cpuinfo_log_debug("hw.machine: %s", machine_name);
117134

118135
char name[10];
119136
uint32_t major = 0, minor = 0;
120137
if (sscanf(machine_name, "%9[^,0123456789]%" SCNu32 ",%" SCNu32, name, &major, &minor) != 3) {
121138
cpuinfo_log_warning("parsing \"hw.machine\" failed: %s", strerror(errno));
122-
return;
139+
return false;
123140
}
124141

125142
uint32_t chip_model = 0;
@@ -224,7 +241,9 @@ static void decode_package_name(char* package_name) {
224241
}
225242
if (chip_model != 0) {
226243
snprintf(package_name, CPUINFO_PACKAGE_NAME_MAX, "Apple A%" PRIu32 "%c", chip_model, suffix);
244+
return true;
227245
}
246+
return false;
228247
}
229248

230249
void cpuinfo_arm_mach_init(void) {
@@ -275,7 +294,8 @@ void cpuinfo_arm_mach_init(void) {
275294
.core_start = i * cores_per_package,
276295
.core_count = cores_per_package,
277296
};
278-
decode_package_name(packages[i].name);
297+
if (!read_package_name_from_brand_string(packages[i].name))
298+
decode_package_name_from_hw_machine(packages[i].name);
279299
}
280300

281301
const uint32_t cpu_family = get_sys_info_by_name("hw.cpufamily");

0 commit comments

Comments
 (0)