nezhahq-agent/pkg/gpu/gpu_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

54 lines
1.0 KiB
Go

//go:build darwin && !cgo
package gpu
import (
"os/exec"
"regexp"
"strings"
)
func extractGPUInfo(cmd *exec.Cmd) ([]string, error) {
gi, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
re := regexp.MustCompile(`"model"\s*=\s*["<]?"([^">]+)"[">]?`)
matches := re.FindAllSubmatch(gi, -1)
var modelNames []string
for _, match := range matches {
if len(match) > 1 {
modelNames = append(modelNames, string(match[1]))
}
}
return modelNames, nil
}
func GetGPUModel() ([]string, error) {
vendorNames := []string{
"AMD", "Intel", "Nvidia", "Apple",
}
ioreg := exec.Command("ioreg", "-rd1", "-c", "IOAccelerator")
gi, err := extractGPUInfo(ioreg)
if err != nil || len(gi) == 0 {
ioreg = exec.Command("ioreg", "-rd1", "-c", "IOPCIDevice")
gi, err = extractGPUInfo(ioreg)
if err != nil {
return nil, err
}
}
var gpuModel []string
for _, model := range gi {
for _, vendor := range vendorNames {
if strings.Contains(model, vendor) {
gpuModel = append(gpuModel, model)
break
}
}
}
return gpuModel, nil
}