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
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 21 additions & 1 deletion cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {

// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
cpuFrequency, err := getFrequency()
Lomanic marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return ret, err
}
Expand All @@ -102,6 +102,26 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
return append(ret, c), nil
}

func getFrequency() (uint64, error) {
// This works for Intel macs.
if cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency"); err == nil {
return cpuFrequency, nil
}

// 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 CountsWithContext(ctx context.Context, logical bool) (int, error) {
var cpuArgument string
if logical {
Expand Down
22 changes: 21 additions & 1 deletion v3/cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {

// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
cpuFrequency, err := getFrequency()
if err != nil {
return ret, err
}
Expand All @@ -102,6 +102,26 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
return append(ret, c), nil
}

func getFrequency() (uint64, error) {
// This works for Intel macs.
if cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency"); err == nil {
return cpuFrequency, nil
}

// 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 CountsWithContext(ctx context.Context, logical bool) (int, error) {
var cpuArgument string
if logical {
Expand Down