-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
cpufreq for apple silicon macs #999
Conversation
Previously on darwin, it was safe to assume that `hw.cpufrequency` would be defined in sysctl. On the new M1 macs instead use the base frequency `hw.tbfrequency` and `kern.clockrate[hz]`. The computed value appears to be 2.4 GHz, which is I believe is the correct base clock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR, I didn't test it yet (hoping hw.tbfrequency
and kern.clockrate
are also available on amd64 as I don't have access to any Apple ARM machine).
Yes they appear to be. |
Having tested with the following small script on an amd64 hackintosh VM, I'm just confused by the result package main
import (
"fmt"
"golang.org/x/sys/unix"
)
func getFrequencyIntel() (uint64, error) {
// This works for Intel macs.
return unix.SysctlUint64("hw.cpufrequency")
}
func getFrequencyArm() (uint64, error) {
// On Apple Silicon fallback to hw.tbfrequency and kern.clockrate[hz]
tbFreq, err := unix.SysctlUint64("hw.tbfrequency")
if err != nil {
return 0, err
}
kernClockrate, err := unix.SysctlClockinfo("kern.clockrate")
if err != nil {
return 0, err
}
return uint64(kernClockrate.Hz) * tbFreq, nil
}
func main() {
fmt.Println(getFrequencyIntel())
fmt.Println(getFrequencyArm())
} As it returns
Also, is python psutil also failing with |
Yeah @Lomanic, I'm still trying to dig through and make sense of what Apple is doing here. There is probably a more correct way to get this value that is consistent across Intel/ARM macs but maybe only with cgo. If anyone else wants to play along, https://github.com/knightsc/darwin-xnu/blob/master/pexpert/arm/pe_identify_machine.c#L45 |
I don't have Python setup, but I would suspect it fails since they use the absent sysctl values. |
Darwin kernel is under the Apple Public Source License which I understand is not compatible with ours so linking against is not an option. Porting its code (in Go?) looks heavily involved. Reported the issue for psutil in giampaolo/psutil#1892. |
Also see giampaolo/psutil#1892 (comment). |
FYI: psutil fixed by using sysctl. I don't have an Apple M1. So I hope some other kind person make a PR. |
For the record, it is not fixed in psutil, see giampaolo/psutil#1892 (comment). And @giampaolo concluded the same thing as #999 (comment) in giampaolo/psutil#1892 (comment): these values are not linked and don't seem to represent the same thing. For the moment, it seems impossible to get the nominal CPU frequency on Apple M1 devices. |
Indeed, I'll go ahead and close this PR |
gopsutil v2 does not work on Apple Silicon M1 (darwin/arm64) shirou/gopsutil#999
gopsutil v2 does not work on Apple Silicon M1 (darwin/arm64) shirou/gopsutil#999 Signed-off-by: ryicoh <ryicoh@gmail.com>
See (github.com/shirou/gopsutil#999). Also ran go mod tidy. Signed-off-by: Muaaz Saleem <muaaz.saleem@zalando.de>
See (github.com/shirou/gopsutil#999). Also ran go mod tidy. Signed-off-by: Muaaz Saleem <muaaz.saleem@zalando.de> Co-authored-by: Muaaz Saleem <muaaz.saleem@zalando.de>
Previously on darwin, it was safe to assume that
hw.cpufrequency
would be defined in sysctl. On the new M1 macs instead use the base
frequency
hw.tbfrequency
andkern.clockrate[hz]
. The computedvalue appears to be 2.4 GHz, which is I believe is the correct base
clock.
Before:
After: