2024-02-17 03:48:29 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
cpu: add frequency support for apple silicon m1/m2 cpus
This PR adds support for reading the frequency of Apple Silicon
M1/M2 CPUs. We do so by reading the values out of the IOKit
framework, as a few other projects have now demonstrated to be
possible. This requires the use of CGO. The library provides a
convenience IsAppleSilicon() guard to detect whether the values
can be read.
Currently gopsutil does not support the big.LITTLE CPU architectures
(i think?) - in fact the P and E cores have different max frequencies.
For now, just read the P core frequency. The E core data is readily
available if we want to read it in the future.
Closes #1000
Small example program
```go
package main
import (
"fmt"
"github.com/shoenig/go-m1cpu"
"github.com/shirou/gopsutil/v3/cpu"
)
func main() {
fmt.Println("is Apple Silicon:", m1cpu.IsAppleSilicon())
fmt.Println("model name", m1cpu.ModelName())
fmt.Println("pCore GHz", m1cpu.PCoreGHz())
fmt.Println("eCore GHz", m1cpu.ECoreGHz())
fmt.Println("pCore Hz", m1cpu.PCoreHz())
fmt.Println("eCore Hz", m1cpu.ECoreHz())
fmt.Println("----- gopsutil ----")
infos, err := cpu.Info()
if err != nil {
panic(err)
}
for _, info := range infos {
fmt.Println("info.Mhz", info.Mhz)
}
}
```
```shell
go run main.go
is Apple Silicon: true
model name Apple M2 Pro
pCore GHz 3.504
eCore GHz 2.424
pCore Hz 3504000000
eCore Hz 2424000000
----- gopsutil ----
info.Mhz 3.504e+09
```
2023-03-23 14:38:53 -05:00
|
|
|
//go:build darwin
|
|
|
|
|
|
|
|
package cpu
|
|
|
|
|
|
|
|
import (
|
2024-02-24 01:25:24 +00:00
|
|
|
"os"
|
2024-09-17 17:04:23 +08:00
|
|
|
"runtime"
|
cpu: add frequency support for apple silicon m1/m2 cpus
This PR adds support for reading the frequency of Apple Silicon
M1/M2 CPUs. We do so by reading the values out of the IOKit
framework, as a few other projects have now demonstrated to be
possible. This requires the use of CGO. The library provides a
convenience IsAppleSilicon() guard to detect whether the values
can be read.
Currently gopsutil does not support the big.LITTLE CPU architectures
(i think?) - in fact the P and E cores have different max frequencies.
For now, just read the P core frequency. The E core data is readily
available if we want to read it in the future.
Closes #1000
Small example program
```go
package main
import (
"fmt"
"github.com/shoenig/go-m1cpu"
"github.com/shirou/gopsutil/v3/cpu"
)
func main() {
fmt.Println("is Apple Silicon:", m1cpu.IsAppleSilicon())
fmt.Println("model name", m1cpu.ModelName())
fmt.Println("pCore GHz", m1cpu.PCoreGHz())
fmt.Println("eCore GHz", m1cpu.ECoreGHz())
fmt.Println("pCore Hz", m1cpu.PCoreHz())
fmt.Println("eCore Hz", m1cpu.ECoreHz())
fmt.Println("----- gopsutil ----")
infos, err := cpu.Info()
if err != nil {
panic(err)
}
for _, info := range infos {
fmt.Println("info.Mhz", info.Mhz)
}
}
```
```shell
go run main.go
is Apple Silicon: true
model name Apple M2 Pro
pCore GHz 3.504
eCore GHz 2.424
pCore Hz 3504000000
eCore Hz 2424000000
----- gopsutil ----
info.Mhz 3.504e+09
```
2023-03-23 14:38:53 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-02-24 01:58:24 +00:00
|
|
|
func TestInfo_AppleSilicon(t *testing.T) {
|
2024-09-17 17:04:23 +08:00
|
|
|
if runtime.GOARCH != "arm64" {
|
cpu: add frequency support for apple silicon m1/m2 cpus
This PR adds support for reading the frequency of Apple Silicon
M1/M2 CPUs. We do so by reading the values out of the IOKit
framework, as a few other projects have now demonstrated to be
possible. This requires the use of CGO. The library provides a
convenience IsAppleSilicon() guard to detect whether the values
can be read.
Currently gopsutil does not support the big.LITTLE CPU architectures
(i think?) - in fact the P and E cores have different max frequencies.
For now, just read the P core frequency. The E core data is readily
available if we want to read it in the future.
Closes #1000
Small example program
```go
package main
import (
"fmt"
"github.com/shoenig/go-m1cpu"
"github.com/shirou/gopsutil/v3/cpu"
)
func main() {
fmt.Println("is Apple Silicon:", m1cpu.IsAppleSilicon())
fmt.Println("model name", m1cpu.ModelName())
fmt.Println("pCore GHz", m1cpu.PCoreGHz())
fmt.Println("eCore GHz", m1cpu.ECoreGHz())
fmt.Println("pCore Hz", m1cpu.PCoreHz())
fmt.Println("eCore Hz", m1cpu.ECoreHz())
fmt.Println("----- gopsutil ----")
infos, err := cpu.Info()
if err != nil {
panic(err)
}
for _, info := range infos {
fmt.Println("info.Mhz", info.Mhz)
}
}
```
```shell
go run main.go
is Apple Silicon: true
model name Apple M2 Pro
pCore GHz 3.504
eCore GHz 2.424
pCore Hz 3504000000
eCore Hz 2424000000
----- gopsutil ----
info.Mhz 3.504e+09
```
2023-03-23 14:38:53 -05:00
|
|
|
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)
|
|
|
|
}
|
2024-02-24 01:25:24 +00:00
|
|
|
if vv.Mhz <= 0 && os.Getenv("CI") != "true" {
|
cpu: add frequency support for apple silicon m1/m2 cpus
This PR adds support for reading the frequency of Apple Silicon
M1/M2 CPUs. We do so by reading the values out of the IOKit
framework, as a few other projects have now demonstrated to be
possible. This requires the use of CGO. The library provides a
convenience IsAppleSilicon() guard to detect whether the values
can be read.
Currently gopsutil does not support the big.LITTLE CPU architectures
(i think?) - in fact the P and E cores have different max frequencies.
For now, just read the P core frequency. The E core data is readily
available if we want to read it in the future.
Closes #1000
Small example program
```go
package main
import (
"fmt"
"github.com/shoenig/go-m1cpu"
"github.com/shirou/gopsutil/v3/cpu"
)
func main() {
fmt.Println("is Apple Silicon:", m1cpu.IsAppleSilicon())
fmt.Println("model name", m1cpu.ModelName())
fmt.Println("pCore GHz", m1cpu.PCoreGHz())
fmt.Println("eCore GHz", m1cpu.ECoreGHz())
fmt.Println("pCore Hz", m1cpu.PCoreHz())
fmt.Println("eCore Hz", m1cpu.ECoreHz())
fmt.Println("----- gopsutil ----")
infos, err := cpu.Info()
if err != nil {
panic(err)
}
for _, info := range infos {
fmt.Println("info.Mhz", info.Mhz)
}
}
```
```shell
go run main.go
is Apple Silicon: true
model name Apple M2 Pro
pCore GHz 3.504
eCore GHz 2.424
pCore Hz 3504000000
eCore Hz 2424000000
----- gopsutil ----
info.Mhz 3.504e+09
```
2023-03-23 14:38:53 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|