From 8998f59f2234fc02fbf93266235f4d0f09edb11d Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Thu, 25 Jan 2024 16:45:52 +0800 Subject: [PATCH 1/2] feat: refactor gpu info list --- external/gpu.go | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/external/gpu.go b/external/gpu.go index 4d2f5ac..b0f4053 100644 --- a/external/gpu.go +++ b/external/gpu.go @@ -1,13 +1,25 @@ package external import ( - "fmt" "os/exec" "strconv" "strings" + + "github.com/samber/lo" ) type GPUInfo struct { + Index int + UUID string + MemoryTotal int + MemoryUsed int + MemoryFree int + Name string + GPUSerial string + TemperatureGPU int +} + +type NvidiaGPUInfo struct { Index int UUID string UtilizationGPU int @@ -22,17 +34,14 @@ type GPUInfo struct { TemperatureGPU int } -func GPUInfoList() ([]GPUInfo, error) { - // execute shell - // parse result - GPUInfos := []GPUInfo{} +func NvidiaGPUInfoList() ([]NvidiaGPUInfo, error) { + GPUInfos := []NvidiaGPUInfo{} output, err := exec.Command("nvidia-smi", "--query-gpu=index,uuid,utilization.gpu,memory.total,memory.used,memory.free,driver_version,name,gpu_serial,display_active,display_mode,temperature.gpu", "--format=csv,noheader,nounits").Output() if err != nil { return nil, err } lines := strings.Split(string(output), "\n") - fmt.Println(lines) for _, line := range lines { value := strings.Split(line, ", ") if len(value) == 12 { @@ -42,7 +51,7 @@ func GPUInfoList() ([]GPUInfo, error) { memoryUsed, _ := strconv.Atoi(value[4]) memoryFree, _ := strconv.Atoi(value[5]) temperatureGPU, _ := strconv.Atoi(value[11]) - GPUInfos = append(GPUInfos, GPUInfo{ + GPUInfos = append(GPUInfos, NvidiaGPUInfo{ Index: index, UUID: value[1], UtilizationGPU: utilizationGPU, @@ -60,6 +69,28 @@ func GPUInfoList() ([]GPUInfo, error) { continue } } + return GPUInfos, nil +} +func GPUInfoList() ([]interface{}, error) { + GPUInfos := []interface{}{} + nvidiaGPUInfoList, err := NvidiaGPUInfoList() + if err != nil { + return nil, err + } + GPUInfos = append(GPUInfos, lo.Map( + nvidiaGPUInfoList, func(gpuInfo NvidiaGPUInfo, index int) GPUInfo { + return GPUInfo{ + Index: gpuInfo.Index, + UUID: gpuInfo.UUID, + MemoryTotal: gpuInfo.MemoryTotal, + MemoryUsed: gpuInfo.MemoryUsed, + MemoryFree: gpuInfo.MemoryFree, + Name: gpuInfo.Name, + GPUSerial: gpuInfo.GPUSerial, + TemperatureGPU: gpuInfo.TemperatureGPU, + } + }), + ) return GPUInfos, nil } From 94cbb117d55020e26d7ab2d8b5c382d46c17c853 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Thu, 25 Jan 2024 16:49:54 +0800 Subject: [PATCH 2/2] feat: reduce info --- external/gpu.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/external/gpu.go b/external/gpu.go index b0f4053..9dbfde1 100644 --- a/external/gpu.go +++ b/external/gpu.go @@ -9,13 +9,10 @@ import ( ) type GPUInfo struct { - Index int - UUID string MemoryTotal int MemoryUsed int MemoryFree int Name string - GPUSerial string TemperatureGPU int } @@ -81,13 +78,10 @@ func GPUInfoList() ([]interface{}, error) { GPUInfos = append(GPUInfos, lo.Map( nvidiaGPUInfoList, func(gpuInfo NvidiaGPUInfo, index int) GPUInfo { return GPUInfo{ - Index: gpuInfo.Index, - UUID: gpuInfo.UUID, MemoryTotal: gpuInfo.MemoryTotal, MemoryUsed: gpuInfo.MemoryUsed, MemoryFree: gpuInfo.MemoryFree, Name: gpuInfo.Name, - GPUSerial: gpuInfo.GPUSerial, TemperatureGPU: gpuInfo.TemperatureGPU, } }),