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

hw.cpufrequency does not exist on Apple Silicon (M1/M2) #872

Closed
michaelsproul opened this issue Oct 27, 2022 · 2 comments · Fixed by #1023
Closed

hw.cpufrequency does not exist on Apple Silicon (M1/M2) #872

michaelsproul opened this issue Oct 27, 2022 · 2 comments · Fixed by #1023

Comments

@michaelsproul
Copy link

michaelsproul commented Oct 27, 2022

sysinfo currently uses the sysctl call hw.cpufrequency to get the CPU frequency on macOS.

sysinfo/src/apple/cpu.rs

Lines 128 to 134 in 463a88c

libc::sysctlbyname(
b"hw.cpufrequency\0".as_ptr() as *const c_char,
&mut speed as *mut _ as _,
&mut len,
std::ptr::null_mut(),
0,
);

Due to trickery on Apple's part, this call returns nothing on new Apple Silicon parts like the M1.

This can be confirmed by trying to call sysinfo::Cpu::frequency on an Apple Silicon device, or by running sysctl in a terminal:

$ sysctl hw.cpufrequency
# no output
$ sudo sysctl hw.cpufrequency
# no output

It seems that Apple Silicon broke a lot of sysinfo-like libraries in other languages, and there are long threads discussing potential solutions, e.g. here: giampaolo/psutil#1892

The upshot of that thread seems to be that parsing the voltage-states1-sram value from ioreg -l is the most promising way of determining something vaguely like the CPU's nominal frequency. I haven't dug into how to call ioreg via a syscall, or how to parse its output.

@GuillaumeGomez
Copy link
Owner

Interesting, thanks for the information! I'd prefer to not run external binaries (because it's very slow) so I'll wait for a solution to appear before checking how this solution actually gets the information.

@GuillaumeGomez
Copy link
Owner

Seems like there is a PR open which gets this information: giampaolo/psutil#2222.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants