nezhahq-agent/pkg/gpu/gpu_darwin.go
UUBulb 09c4ef1764
refactor(pty): switch to using interface (#34)
* refactor(pty): switch to using interface

for better flexibility (although may not be useful)

* chore: reduce error messages

* delete redundant messages
2024-07-07 10:18:59 +08:00

54 lines
1.0 KiB
Go

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