-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_ubuntu.go
51 lines (41 loc) · 1.2 KB
/
sensor_ubuntu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package sensor
import (
"encoding/json"
"github.com/LaynePeng/cfd/pkg/spec"
"github.com/LaynePeng/cfd/pkg/utils"
"os"
"strings"
)
type GpuSensorUbuntu struct {
}
func (gs *GpuSensorUbuntu) IsSupported() (bool, error) {
if _, err := os.Stat("/usr/bin/nvidia-smi"); err == nil {
return true, nil
}
return false, nil
}
func (gs *GpuSensorUbuntu) Desc() (string, error) {
return "", nil
}
func (gs *GpuSensorUbuntu) Detail() (string, error) {
ret := utils.RunCmd("nvidia-smi --query-gpu=uuid,gpu_name,memory.free,memory.used,utilization.gpu,driver_version --format=csv,noheader,nounits")
gpuInfosRet := utils.ParseCSV(ret)
var gpuInfos []*spec.GPU
var gpu *spec.GPU
for _, oneGpuInfo := range gpuInfosRet {
if len(oneGpuInfo) == 6 {
gpu = &spec.GPU{
UUID: strings.TrimSpace(oneGpuInfo[0]),
Name: strings.TrimSpace(oneGpuInfo[1]),
MemFree: strings.TrimSpace(oneGpuInfo[2]),
MemUsed: strings.TrimSpace(oneGpuInfo[3]),
GPUUtil: strings.TrimSpace(oneGpuInfo[4]),
DriverVersion: strings.TrimSpace(oneGpuInfo[5]),
Platform: "Nvidia",
}
gpuInfos = append(gpuInfos, gpu)
}
}
b, _ := json.Marshal(gpuInfos)
return string(b), nil
}