nezhahq-agent/pkg/gpu/stat/stat_darwin.go
UUBulb 65bf012579
feat: add GPU information retrieval for darwin using cgo (#40)
* feat: add GPU information retrieval for darwin using cgo

* fix kIOMasterPortDefault thing

* return 0
2024-07-19 19:00:59 +08:00

37 lines
692 B
Go

//go:build darwin && !cgo
package stat
import (
"os/exec"
"regexp"
"strconv"
)
func extractGPUStat(cmd *exec.Cmd) ([]float64, error) {
gs, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
re := regexp.MustCompile(`"Device Utilization %"\s*=\s*(\d+)`)
matches := re.FindAllSubmatch(gs, -1)
var u []float64
for _, match := range matches {
if len(match) > 1 {
p, _ := strconv.ParseFloat(string(match[1]), 64)
u = append(u, p)
}
}
return u, nil
}
func GetGPUStat() (float64, error) {
ioreg := exec.Command("ioreg", "-rd1", "-c", "IOAccelerator")
gs, err := extractGPUStat(ioreg)
if err != nil || len(gs) == 0 {
return 0, err
}
return gs[0], nil
}