revert to the old(not vibecoded and copied from melvi) getGpu function
This commit is contained in:
parent
ab0a982f13
commit
c291f35c95
104
main.go
104
main.go
@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -18,8 +18,6 @@ const (
|
|||||||
|
|
||||||
var dist string
|
var dist string
|
||||||
|
|
||||||
type LinuxProvider struct{}
|
|
||||||
|
|
||||||
func getDist() string {
|
func getDist() string {
|
||||||
f, _ := os.Open("/etc/os-release")
|
f, _ := os.Open("/etc/os-release")
|
||||||
defer func(f *os.File) {
|
defer func(f *os.File) {
|
||||||
@ -98,83 +96,26 @@ func getCpu() string {
|
|||||||
return cpu
|
return cpu
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lp *LinuxProvider) getNvidiaGpuInfoPath() string {
|
func getGpu() string {
|
||||||
entries, err := os.ReadDir("/proc/driver/nvidia/gpus/")
|
out, err := exec.Command("sh", "-c", "lspci | grep -E 'VGA|3D'").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return "Unknown GPU"
|
||||||
}
|
}
|
||||||
|
line := string(out)
|
||||||
for _, entry := range entries {
|
parts := strings.Split(line, ": ")
|
||||||
return filepath.Join("/proc/driver/nvidia/gpus/", entry.Name(), "information")
|
if len(parts) > 1 {
|
||||||
|
return cleanGpuString(parts[1])
|
||||||
}
|
}
|
||||||
|
return "GPU not found"
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lp *LinuxProvider) findAmdGpuBase() string {
|
func cleanGpuString(raw string) string {
|
||||||
for i := 0; i < 10; i++ {
|
re := regexp.MustCompile(`\(.*\)`)
|
||||||
base := fmt.Sprintf("/sys/class/drm/card%d/device/", i)
|
res := re.ReplaceAllString(raw, "")
|
||||||
vendorPath := filepath.Join(base, "vendor")
|
res = strings.ReplaceAll(res, "Corporation", "")
|
||||||
|
res = strings.ReplaceAll(res, "[", "")
|
||||||
data, err := os.ReadFile(vendorPath)
|
res = strings.ReplaceAll(res, "]", "")
|
||||||
if err == nil {
|
return strings.Join(strings.Fields(res), " ")
|
||||||
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"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getKernel() string {
|
func getKernel() string {
|
||||||
@ -245,16 +186,15 @@ func main() {
|
|||||||
Label string
|
Label string
|
||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
lp := &LinuxProvider{}
|
|
||||||
|
|
||||||
stats := []Info{
|
stats := []Info{
|
||||||
{"distro", getDist()},
|
{"dist", getDist()},
|
||||||
{"packages", getPkgs()},
|
{"host", getHostname()},
|
||||||
{"kernel", getKernel()},
|
|
||||||
{"hostname", getHostname()},
|
|
||||||
{"memory", getRam()},
|
|
||||||
{"cpu", getCpu()},
|
{"cpu", getCpu()},
|
||||||
{"gpu", lp.getGpu()},
|
{"krnl", getKernel()},
|
||||||
|
{"ram", getRam()},
|
||||||
|
{"gpu", getGpu()},
|
||||||
|
{"pkgs", getPkgs()},
|
||||||
}
|
}
|
||||||
fmt.Print(BlueBright)
|
fmt.Print(BlueBright)
|
||||||
fmt.Printf(getAscii())
|
fmt.Printf(getAscii())
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user