revert to the old(not vibecoded and copied from melvi) getGpu function
This commit is contained in:
parent
ab0a982f13
commit
c291f35c95
110
main.go
110
main.go
@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -18,8 +18,6 @@ const (
|
||||
|
||||
var dist string
|
||||
|
||||
type LinuxProvider struct{}
|
||||
|
||||
func getDist() string {
|
||||
f, _ := os.Open("/etc/os-release")
|
||||
defer func(f *os.File) {
|
||||
@ -98,84 +96,27 @@ func getCpu() string {
|
||||
return cpu
|
||||
}
|
||||
|
||||
func (lp *LinuxProvider) getNvidiaGpuInfoPath() string {
|
||||
entries, err := os.ReadDir("/proc/driver/nvidia/gpus/")
|
||||
func getGpu() string {
|
||||
out, err := exec.Command("sh", "-c", "lspci | grep -E 'VGA|3D'").Output()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
return filepath.Join("/proc/driver/nvidia/gpus/", entry.Name(), "information")
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (lp *LinuxProvider) findAmdGpuBase() string {
|
||||
for i := 0; i < 10; i++ {
|
||||
base := fmt.Sprintf("/sys/class/drm/card%d/device/", i)
|
||||
vendorPath := filepath.Join(base, "vendor")
|
||||
|
||||
data, err := os.ReadFile(vendorPath)
|
||||
if err == nil {
|
||||
vendorID := strings.TrimSpace(string(data))
|
||||
// 0x1002 — это PCI ID компании AMD
|
||||
if vendorID == "0x1002" {
|
||||
return base
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (lp *LinuxProvider) findStringInStreamWithDelimiter(filePath string, targetKey string, delimiter string) string {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.Contains(line, targetKey) {
|
||||
parts := strings.SplitN(line, delimiter, 2)
|
||||
if len(parts) > 1 {
|
||||
return strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (lp *LinuxProvider) getGpu() string {
|
||||
if _, err := os.Stat("/proc/driver/nvidia"); err == nil {
|
||||
path := lp.getNvidiaGpuInfoPath()
|
||||
if path != "" {
|
||||
name := lp.findStringInStreamWithDelimiter(path, "Model", ":")
|
||||
if name != "" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
} else {
|
||||
base := lp.findAmdGpuBase()
|
||||
if base != "" {
|
||||
if data, err := os.ReadFile(filepath.Join(base, "product_name")); err == nil {
|
||||
name := strings.TrimSpace(string(data))
|
||||
if name != "" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
||||
pci := lp.findStringInStreamWithDelimiter(filepath.Join(base, "uevent"), "PCI_ID", "=")
|
||||
if pci != "" && pci != "?" {
|
||||
return "AMD GPU (" + pci + ")"
|
||||
}
|
||||
return "AMD GPU"
|
||||
}
|
||||
}
|
||||
|
||||
return "Unknown GPU"
|
||||
}
|
||||
line := string(out)
|
||||
parts := strings.Split(line, ": ")
|
||||
if len(parts) > 1 {
|
||||
return cleanGpuString(parts[1])
|
||||
}
|
||||
return "GPU not found"
|
||||
}
|
||||
|
||||
func cleanGpuString(raw string) string {
|
||||
re := regexp.MustCompile(`\(.*\)`)
|
||||
res := re.ReplaceAllString(raw, "")
|
||||
res = strings.ReplaceAll(res, "Corporation", "")
|
||||
res = strings.ReplaceAll(res, "[", "")
|
||||
res = strings.ReplaceAll(res, "]", "")
|
||||
return strings.Join(strings.Fields(res), " ")
|
||||
}
|
||||
|
||||
func getKernel() string {
|
||||
out, err := exec.Command("sh", "-c", "uname -r").Output()
|
||||
@ -245,16 +186,15 @@ func main() {
|
||||
Label string
|
||||
Value string
|
||||
}
|
||||
lp := &LinuxProvider{}
|
||||
|
||||
stats := []Info{
|
||||
{"distro", getDist()},
|
||||
{"packages", getPkgs()},
|
||||
{"kernel", getKernel()},
|
||||
{"hostname", getHostname()},
|
||||
{"memory", getRam()},
|
||||
{"dist", getDist()},
|
||||
{"host", getHostname()},
|
||||
{"cpu", getCpu()},
|
||||
{"gpu", lp.getGpu()},
|
||||
{"krnl", getKernel()},
|
||||
{"ram", getRam()},
|
||||
{"gpu", getGpu()},
|
||||
{"pkgs", getPkgs()},
|
||||
}
|
||||
fmt.Print(BlueBright)
|
||||
fmt.Printf(getAscii())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user