Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For Apple silicon, use machdep.cpu.brand_string in preference to decoding hw.machine #246

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/arm/mach/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,42 @@ static enum cpuinfo_uarch decode_uarch(uint32_t cpu_family, uint32_t core_index,
return cpuinfo_uarch_unknown;
}

static void decode_package_name(char* package_name) {
static int read_package_name_from_brand_string(char* package_name) {
size_t size;
if (sysctlbyname("machdep.cpu.brand_string", NULL, &size, NULL, 0) != 0) {
sysctlfail:
cpuinfo_log_warning("sysctlbyname(\"machdep.cpu.brand_string\") failed: %s", strerror(errno));
return false;
}

char *brand_string = alloca(size);
if (sysctlbyname("machdep.cpu.brand_string", brand_string, &size, NULL, 0) != 0)
goto sysctlfail;
cpuinfo_log_debug("machdep.cpu.brand_string: %s", brand_string);

strlcpy(package_name, brand_string, CPUINFO_PACKAGE_NAME_MAX);
return true;
}

static int decode_package_name_from_hw_machine(char* package_name) {
size_t size;
if (sysctlbyname("hw.machine", NULL, &size, NULL, 0) != 0) {
cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno));
return;
return false;
}

char* machine_name = alloca(size);
if (sysctlbyname("hw.machine", machine_name, &size, NULL, 0) != 0) {
cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno));
return;
return false;
}
cpuinfo_log_debug("hw.machine: %s", machine_name);

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

uint32_t chip_model = 0;
Expand Down Expand Up @@ -224,7 +241,9 @@ static void decode_package_name(char* package_name) {
}
if (chip_model != 0) {
snprintf(package_name, CPUINFO_PACKAGE_NAME_MAX, "Apple A%" PRIu32 "%c", chip_model, suffix);
return true;
}
return false;
}

void cpuinfo_arm_mach_init(void) {
Expand Down Expand Up @@ -275,7 +294,8 @@ void cpuinfo_arm_mach_init(void) {
.core_start = i * cores_per_package,
.core_count = cores_per_package,
};
decode_package_name(packages[i].name);
if (!read_package_name_from_brand_string(packages[i].name))
decode_package_name_from_hw_machine(packages[i].name);
Copy link
Author

@dlenski dlenski May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic in decode_package_name_from_hw_machine has already become somewhat obsolete, which is why it makes sense to prefer read_package_name_from_brand_string wherever it succeeds on newer devices/OSes.

For example, the package in iPhone 15 Pro should be described as Apple A17 Pro, rather than simply A17 (which is what the current logic would show, according to the hw.machine values shown at: https://gist.github.com/adamawolf/3048717#file-apple_mobile_device_types-txt-L55-L56).

}

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