Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor gpu info list #43

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions external/gpu.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package external

import (
"fmt"
"os/exec"
"strconv"
"strings"

"github.com/samber/lo"
)

type GPUInfo struct {
MemoryTotal int
MemoryUsed int
MemoryFree int
Name string
TemperatureGPU int
}

type NvidiaGPUInfo struct {
Index int
UUID string
UtilizationGPU int
Expand All @@ -22,17 +31,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 {
Expand All @@ -42,7 +48,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,
Expand All @@ -60,6 +66,25 @@ 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{
MemoryTotal: gpuInfo.MemoryTotal,
MemoryUsed: gpuInfo.MemoryUsed,
MemoryFree: gpuInfo.MemoryFree,
Name: gpuInfo.Name,
TemperatureGPU: gpuInfo.TemperatureGPU,
}
}),
)
return GPUInfos, nil
}
Loading