mirror of
https://github.com/EnumeratedDev/stormfetch.git
synced 2026-09-15 23:46:12 +00:00
Improve GPU name fetching
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
distro_ascii: auto
|
distro_ascii: auto
|
||||||
|
disable_amdgpu_ids_warning: false
|
||||||
modules:
|
modules:
|
||||||
- name: distribution
|
- name: distribution
|
||||||
- name: hostname
|
- name: hostname
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
type StormfetchConfig struct {
|
type StormfetchConfig struct {
|
||||||
Ascii string `yaml:"distro_ascii"`
|
Ascii string `yaml:"distro_ascii"`
|
||||||
|
DisableAmdgpuIdsWarning bool `yaml:"disable_amdgpu_ids_warning"`
|
||||||
Modules []stormfetchModuleConfig `yaml:"modules"`
|
Modules []stormfetchModuleConfig `yaml:"modules"`
|
||||||
AnsiiColors []int `yaml:"ansii_colors"`
|
AnsiiColors []int `yaml:"ansii_colors"`
|
||||||
ForceConfigAnsii bool `yaml:"force_config_ansii"`
|
ForceConfigAnsii bool `yaml:"force_config_ansii"`
|
||||||
|
|||||||
+33
-5
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -18,6 +19,7 @@ type CPU struct {
|
|||||||
type GPU struct {
|
type GPU struct {
|
||||||
PCIAddress string
|
PCIAddress string
|
||||||
Vendor string
|
Vendor string
|
||||||
|
Name string
|
||||||
Product string
|
Product string
|
||||||
Subsystem string
|
Subsystem string
|
||||||
Driver string
|
Driver string
|
||||||
@@ -101,17 +103,43 @@ func GetGPUModels(hiddenGPUs []int) []GPU {
|
|||||||
vendor = gpu.DeviceInfo.Vendor.Name
|
vendor = gpu.DeviceInfo.Vendor.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback subsystem name to product name
|
// Set GPU name
|
||||||
subsystem := gpu.DeviceInfo.Subsystem.Name
|
name := ""
|
||||||
if subsystem == "" || subsystem == "unknown" {
|
|
||||||
subsystem = gpu.DeviceInfo.Product.Name
|
// Use GPU name from amdgpu.ids database
|
||||||
|
if vendor == "AMD" {
|
||||||
|
fetchedName, err := fetchAmdGpuName(gpu.DeviceInfo.Product.ID, gpu.DeviceInfo.Revision)
|
||||||
|
if err == nil && !config.DisableAmdgpuIdsWarning {
|
||||||
|
name = fetchedName
|
||||||
|
} else {
|
||||||
|
fmt.Println("Warning: could not fetch GPU name from amdgpu.ids database! Error: " + err.Error())
|
||||||
|
fmt.Println(" You can disable this warning in the configuration file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
if gpu.DeviceInfo.Subsystem.Name == "" || gpu.DeviceInfo.Subsystem.Name == "unknown" {
|
||||||
|
// Set GPU name to product name
|
||||||
|
name = gpu.DeviceInfo.Product.Name
|
||||||
|
} else {
|
||||||
|
// Set GPU name to subsystem name
|
||||||
|
name = gpu.DeviceInfo.Subsystem.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use GPU name in brackets
|
||||||
|
leftBracket := strings.IndexByte(name, '[')
|
||||||
|
rightBracket := strings.IndexByte(name, ']')
|
||||||
|
if leftBracket != -1 && rightBracket != -1 {
|
||||||
|
name = name[leftBracket+1 : rightBracket]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = append(ret, GPU{
|
ret = append(ret, GPU{
|
||||||
PCIAddress: gpu.Address,
|
PCIAddress: gpu.Address,
|
||||||
Vendor: vendor,
|
Vendor: vendor,
|
||||||
|
Name: name,
|
||||||
Product: gpu.DeviceInfo.Product.Name,
|
Product: gpu.DeviceInfo.Product.Name,
|
||||||
Subsystem: subsystem,
|
Subsystem: gpu.DeviceInfo.Subsystem.Name,
|
||||||
Driver: gpu.DeviceInfo.Driver,
|
Driver: gpu.DeviceInfo.Driver,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -219,7 +219,7 @@ func initializeModuleMap() {
|
|||||||
RegisterModule(cpusModule)
|
RegisterModule(cpusModule)
|
||||||
|
|
||||||
// GPUs module
|
// GPUs module
|
||||||
gpusModule := StormfetchModule{stormfetchModuleConfig: stormfetchModuleConfig{Name: "gpus", Format: "%3GPU: %4$GPU_VENDOR $GPU_SUBSYSTEM"}, Execute: func(sm StormfetchModule) string {
|
gpusModule := StormfetchModule{stormfetchModuleConfig: stormfetchModuleConfig{Name: "gpus", Format: "%3GPU: %4$GPU_VENDOR $GPU_NAME"}, Execute: func(sm StormfetchModule) string {
|
||||||
hiddenGPUsInterface, _ := sm.GetData("hidden_gpus", make([]any, 0))
|
hiddenGPUsInterface, _ := sm.GetData("hidden_gpus", make([]any, 0))
|
||||||
|
|
||||||
// Convert interface slices to string slices
|
// Convert interface slices to string slices
|
||||||
@@ -240,6 +240,8 @@ func initializeModuleMap() {
|
|||||||
return strconv.Itoa(i + 1)
|
return strconv.Itoa(i + 1)
|
||||||
case "GPU_VENDOR":
|
case "GPU_VENDOR":
|
||||||
return gpu.Vendor
|
return gpu.Vendor
|
||||||
|
case "GPU_NAME":
|
||||||
|
return gpu.Name
|
||||||
case "GPU_PRODUCT":
|
case "GPU_PRODUCT":
|
||||||
return gpu.Product
|
return gpu.Product
|
||||||
case "GPU_SUBSYSTEM":
|
case "GPU_SUBSYSTEM":
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -58,6 +59,46 @@ func ReadKeyValueFile(filepath string) (map[string]string, error) {
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fetchAmdGpuName(productId, revision string) (string, error) {
|
||||||
|
productId = strings.ToUpper(productId)
|
||||||
|
revision = strings.ToUpper(revision[2:])
|
||||||
|
|
||||||
|
// Get cache directory
|
||||||
|
cachedir, err := os.UserCacheDir()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure amdgpu.ids file exists and download it if it doesn't
|
||||||
|
if _, err := os.Stat(path.Join(cachedir, "amdgpu.ids")); err != nil {
|
||||||
|
cmd := exec.Command("curl", "-o", path.Join(cachedir, "amdgpu.ids"), "https://gitlab.freedesktop.org/mesa/libdrm/-/raw/main/data/amdgpu.ids")
|
||||||
|
err = cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Could not fetch amdgpu.ids using curl")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read amdgpu.ids file
|
||||||
|
amdgpuIds, err := os.ReadFile(path.Join(cachedir, "amdgpu.ids"))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse read data and find GPU
|
||||||
|
for _, line := range strings.Split(string(amdgpuIds), "\n") {
|
||||||
|
if len(line) < 2 || line[0] == '#' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := strings.Split(line, ",\t")
|
||||||
|
if fields[0] == productId && fields[1] == revision {
|
||||||
|
return strings.TrimPrefix(fields[2], "AMD "), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
func runCommand(command string, shell string) string {
|
func runCommand(command string, shell string) string {
|
||||||
cmd := exec.Command(shell, "-c", command)
|
cmd := exec.Command(shell, "-c", command)
|
||||||
workdir, err := os.Getwd()
|
workdir, err := os.Getwd()
|
||||||
|
|||||||
Reference in New Issue
Block a user