nezhahq-agent/pkg/gpu/stat/stat_darwin.go
UUBulb fb7b45527b
service: logging to syslog (#33)
* service: logging to syslog

* chore: update gopsutil to v4

* chore: move println() to the util package

* gpu: only return 0 on failure
2024-07-05 22:31:56 +08:00

37 lines
684 B
Go

//go:build darwin
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
}