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

cpufreq for apple silicon macs #999

Closed
wants to merge 2 commits into from
Closed

Conversation

shoenig
Copy link
Contributor

@shoenig shoenig commented Nov 21, 2020

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.

Before:

 m1  cpu  master % go test
--- FAIL: TestCpuInfo (0.00s)
    cpu_test.go:117: error no such file or directory
    cpu_test.go:120: could not get CPU Info
FAIL
exit status 1
FAIL	github.com/shirou/gopsutil/v3/cpu	0.371s

After:

 m1  cpu  m1freq % go test
PASS
ok  	github.com/shirou/gopsutil/v3/cpu	0.248s

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.
@shoenig shoenig changed the title support for apple silicon macs cpufreq for apple silicon macs Nov 21, 2020
Copy link
Collaborator

@Lomanic Lomanic left a 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).

cpu/cpu_darwin.go Outdated Show resolved Hide resolved
@tmm1
Copy link
Contributor

tmm1 commented Nov 25, 2020

hoping hw.tbfrequency and kern.clockrate are also available on amd64

Yes they appear to be.

@Lomanic
Copy link
Collaborator

Lomanic commented Nov 27, 2020

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

2500000000 <nil>                                                                                                                                                                                                                                                               
100000000000 <nil>

Also, is python psutil also failing with psutil.cpu_freq() on Apple M1?

@shoenig
Copy link
Contributor Author

shoenig commented Nov 27, 2020

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

@shoenig
Copy link
Contributor Author

shoenig commented Nov 28, 2020

Also, is python psutil also failing with psutil.cpu_freq() on Apple M1?

I don't have Python setup, but I would suspect it fails since they use the absent sysctl values.
https://github.com/giampaolo/psutil/blob/master/psutil/_psutil_osx.c#L693

@Lomanic
Copy link
Collaborator

Lomanic commented Dec 17, 2020

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.

@giampaolo
Copy link

Also see giampaolo/psutil#1892 (comment).

@shirou
Copy link
Owner

shirou commented Dec 20, 2020

FYI: psutil fixed by using sysctl. I don't have an Apple M1. So I hope some other kind person make a PR.

@Lomanic
Copy link
Collaborator

Lomanic commented Jan 2, 2021

FYI: psutil fixed by using sysctl.

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.

@shoenig
Copy link
Contributor Author

shoenig commented Jan 2, 2021

Indeed, I'll go ahead and close this PR

@shoenig shoenig closed this Jan 2, 2021
ryicoh added a commit to ryicoh/pd that referenced this pull request Mar 6, 2021
gopsutil v2 does not work on Apple Silicon M1 (darwin/arm64)
shirou/gopsutil#999
ryicoh added a commit to ryicoh/pd that referenced this pull request Mar 6, 2021
gopsutil v2 does not work on Apple Silicon M1 (darwin/arm64)
shirou/gopsutil#999

Signed-off-by: ryicoh <ryicoh@gmail.com>
muaazsaleem pushed a commit to zalando/skipper that referenced this pull request Mar 30, 2021
See (github.com/shirou/gopsutil#999). Also ran go mod tidy.

Signed-off-by: Muaaz Saleem <muaaz.saleem@zalando.de>
aryszka pushed a commit to zalando/skipper that referenced this pull request Mar 31, 2021
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants