psutil: [MacOS] psutil.cpu_freq() broken on Apple M1
Summary
- OS: MacOS
- Architecture: Apple M1
- Psutil version: N/A
- Python version: N/A
- Type: core
Description
Sorry that I couldn’t provide some of the info in the template as I don’t own an Apple M1 device, but it is highly suspected that calling psutil.cpu_freq() on Apple M1 is failing as the hw.cpufrequency sysctl call (used here) was removed on this arch (can be checked with sysctl hw.cpufrequency on any Apple M1 device, while it is working on amd64).
See https://github.com/shirou/gopsutil/pull/999 and https://github.com/shirou/gopsutil/issues/1000
In https://github.com/shirou/gopsutil/pull/999 a fix was suggested but I couldn’t confirm it.
psutil user-base is probably bigger than gopsutil’s so I hope this will help both projects find a solution or confirm @shoenig fix in his PR.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 30 (15 by maintainers)
Commits related to this issue
- #1892 fix cpu_freq() broken on Apple M1 (#1895) fix #1456: [macOS] psutil.cpu_freq()'s min and max are set to 0 if can't be determined (instead of crashing). fix #1892: [macOS] psutil.cpu_freq() bro... — committed to giampaolo/psutil by giampaolo 4 years ago
- Workaround for Apple M1 https://github.com/giampaolo/psutil/issues/1892 — committed to Torom/BotLi by Torom 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: mark test_cpu_freq as expected failure on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: skip test_cpu_freq on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: skip test_cpu_freq on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: skip test_cpu_freq on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- chore: skip test_cpu_freq on macOS arm64 macOS arm64 does not support cpu_freq: issue #1892 Signed-off-by: mayeut <mayeut@users.noreply.github.com> — committed to mayeut/psutil by mayeut 2 years ago
- Fix cpu_freq for Apple silicon Apple SoC returns empty string after querying the cpu frequency using sysctl, this information however, can still be extracted from the IOKit registry. This PR adds a n... — committed to snOm3ad/psutil by snOm3ad a year ago
- Fix cpu_freq for Apple silicon Apple SoC returns empty string after querying the cpu frequency using sysctl, this information however, can still be extracted from the IOKit registry. This PR adds a n... — committed to snOm3ad/psutil by snOm3ad a year ago
- Fix cpu_freq for Apple silicon Apple SoC returns empty string after querying the cpu frequency using sysctl, this information however, can still be extracted from the IOKit registry. This PR adds a n... — committed to snOm3ad/psutil by snOm3ad a year ago
I see this wasn’t really fixed in #1895 – I’m still getting:
on M1 (MBP 2021, OS 12.2.1). Let me know how I could help with testing!
I’m also interested by thix fix, as on M1, without that, no way it seems to use BabyAGI 😃 (https://github.com/oliveirabruno01/babyagi-asi)
However this works: in the requirements.txt file, replace the line with that dependency, e.g
with
then
pip install ...Same on 5.9.4 on an M1 Pro with Ventura 13.2.1
I just took a grand tour of almost every GitHub issue regarding this, and think I’ve cracked this nut.
This comment pointing to the clockrate.hz field is relevant, but the problem is what to multiply it by. The answer appears to be the time base frequency.
On M1s, sysctl hw.tbfrequency returns 24,000,000 which when multiplied by 100 Hz gives the expected 2.4 GHz. However, that sysctl is not reliable on Intel hardware: on my Intel chip I get 1,000,000,000.
This would obviously need testing by anyone with an M1 (or M2?) but I think the general logic should be:
PR is up, could someone please review it? Thank you @shoenig and @dbwiddis both your examples where useful.
And here’s a Java/JNA based solution based on this Issue/thread: https://github.com/oshi/oshi/blob/e78a0ffb32d5bc2fe6943b903ed5eeb97f949fc5/oshi-core/src/main/java/oshi/hardware/platform/mac/MacCentralProcessor.java#L322-L357
For reference here’s a simple C version we use for Go https://github.com/shoenig/go-m1cpu/blob/main/cpu.go
Still facing this issue with M2 Max and psutil 5.9.4
@dbwiddis no problem! 👍
@dbwiddis 2.4GHz is the timebase frequency, which is not the same as CPU frequency. Apple Silicon has different speeds per cluster. I.e an M1 truly has a PCPU Frequency of 3204 MHz, and a ECPU frequency of 2064 MHz.
M2 chips can have a single core in the PCPU boost to ~3500MHz and the entire ECPU is upped to 2424Mhz.
So if timebase freq is the true goal, then 2.4Ghz may be logical to hardcode.
Otherwise, if you want the real CPU freq, you’ll need to pull data from the
voltage-statesproperties in the IORegistry PMGR. Specificallyvoltage-states1-sramand ``voltage-states5-sram`. (The data there is 32bit little endian and should result in an array. The biggest value is the CPU freq.)Hopefully that’s helpful 👍
The problem is that on my virtualized macOS I get this:
HW_CPU_FREQ: 2590000000 KERN_CLOCKRATE -> clockinfo.hz: 100
…so it appears they are 2 different things.
Fixed by https://github.com/giampaolo/psutil/pull/1895.