diff --git a/cpu/cpu_darwin.go b/cpu/cpu_darwin.go index 7acb258d9..41f395e5e 100644 --- a/cpu/cpu_darwin.go +++ b/cpu/cpu_darwin.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + "github.com/shoenig/go-m1cpu" "github.com/tklauser/go-sysconf" "golang.org/x/sys/unix" ) @@ -85,11 +86,15 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) { c.CacheSize = int32(cacheSize) c.VendorID, _ = unix.Sysctl("machdep.cpu.vendor") - // 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") - if err == nil { - c.Mhz = float64(cpuFrequency) / 1000000.0 + if m1cpu.IsAppleSilicon() { + c.Mhz = float64(m1cpu.PCoreHz() / 1_000_000) + } else { + // 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") + if err == nil { + c.Mhz = float64(cpuFrequency) / 1000000.0 + } } return append(ret, c), nil diff --git a/cpu/cpu_darwin_test.go b/cpu/cpu_darwin_test.go new file mode 100644 index 000000000..57b3d66ee --- /dev/null +++ b/cpu/cpu_darwin_test.go @@ -0,0 +1,33 @@ +//go:build darwin +// +build darwin + +package cpu + +import ( + "testing" + + "github.com/shoenig/go-m1cpu" +) + +func Test_CpuInfo_AppleSilicon(t *testing.T) { + if !m1cpu.IsAppleSilicon() { + t.Skip("wrong cpu type") + } + + v, err := Info() + if err != nil { + t.Errorf("cpu info should be implemented on darwin systems") + } + + for _, vv := range v { + if vv.ModelName == "" { + t.Errorf("could not get CPU info: %v", vv) + } + if vv.Mhz <= 0 { + t.Errorf("could not get frequency of: %s", vv.ModelName) + } + if vv.Mhz > 6000 { + t.Errorf("cpu frequency is absurdly high value: %f MHz", vv.Mhz) + } + } +} diff --git a/go.mod b/go.mod index 208cdfbf4..1bd6cf698 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/google/go-cmp v0.5.9 github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c + github.com/shoenig/go-m1cpu v0.1.4 github.com/stretchr/testify v1.8.2 github.com/tklauser/go-sysconf v0.3.11 github.com/yusufpapurcu/wmi v1.2.2 diff --git a/go.sum b/go.sum index a1d5b790e..9a14075dd 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= +github.com/shoenig/go-m1cpu v0.1.4 h1:SZPIgRM2sEF9NJy50mRHu9PKGwxyyTTJIWvCtgVbozs= +github.com/shoenig/go-m1cpu v0.1.4/go.mod h1:Wwvst4LR89UxjeFtLRMrpgRiyY4xPsejnVZym39dbAQ= +github.com/shoenig/test v0.6.3 h1:GVXWJFk9PiOjN0KoJ7VrJGH6uLPnqxR7/fe3HUPfE0c= +github.com/shoenig/test v0.6.3/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=