@@ -39,6 +39,7 @@ var monitorCancel context.CancelFunc
3939type IMonitorService interface {
4040 Run ()
4141 LoadMonitorData (req dto.MonitorSearch ) ([]dto.MonitorData , error )
42+ LoadGPUOptions () []string
4243 LoadGPUMonitorData (req dto.MonitorGPUSearch ) (dto.MonitorGPUData , error )
4344 LoadSetting () (* dto.MonitorSetting , error )
4445 UpdateSetting (key , value string ) error
@@ -118,6 +119,43 @@ func (m *MonitorService) LoadMonitorData(req dto.MonitorSearch) ([]dto.MonitorDa
118119 return data , nil
119120}
120121
122+ func (m * MonitorService ) LoadGPUOptions () []string {
123+ gpuExist , gpuClient := gpu .New ()
124+ xpuExist , xpuClient := xpu .New ()
125+ if ! gpuExist && ! xpuExist {
126+ return nil
127+ }
128+ if gpuExist {
129+ gpuInfo , err := gpuClient .LoadGpuInfo ()
130+ if err != nil || len (gpuInfo .GPUs ) == 0 {
131+ global .LOG .Error ("Load GPU info failed or no GPU found, err: " , err )
132+ return nil
133+ }
134+ sort .Slice (gpuInfo .GPUs , func (i , j int ) bool {
135+ return gpuInfo .GPUs [i ].Index < gpuInfo .GPUs [j ].Index
136+ })
137+ var products []string
138+ for _ , item := range gpuInfo .GPUs {
139+ products = append (products , fmt .Sprintf ("%d - %s" , item .Index , item .ProductName ))
140+ }
141+ return products
142+ } else {
143+ xpuInfo , err := xpuClient .LoadGpuInfo ()
144+ if err != nil || len (xpuInfo .Xpu ) == 0 {
145+ global .LOG .Error ("Load XPU info failed or no XPU found, err: " , err )
146+ return nil
147+ }
148+ sort .Slice (xpuInfo .Xpu , func (i , j int ) bool {
149+ return xpuInfo .Xpu [i ].Basic .DeviceID < xpuInfo .Xpu [j ].Basic .DeviceID
150+ })
151+ var products []string
152+ for _ , item := range xpuInfo .Xpu {
153+ products = append (products , fmt .Sprintf ("%d - %s" , item .Basic .DeviceID , item .Basic .DeviceName ))
154+ }
155+ return products
156+ }
157+ }
158+
121159func (m * MonitorService ) LoadGPUMonitorData (req dto.MonitorGPUSearch ) (dto.MonitorGPUData , error ) {
122160 loc , _ := time .LoadLocation (common .LoadTimeZoneByCmd ())
123161 req .StartTime = req .StartTime .In (loc )
@@ -137,9 +175,14 @@ func (m *MonitorService) LoadGPUMonitorData(req dto.MonitorGPUSearch) (dto.Monit
137175 global .LOG .Error ("Load GPU info failed or no GPU found, err: " , err )
138176 return data , buserr .New ("ErrRecordNotFound" )
139177 }
140- req .ProductName = gpuInfo .GPUs [0 ].ProductName
178+ sort .Slice (gpuInfo .GPUs , func (i , j int ) bool {
179+ return gpuInfo .GPUs [i ].Index < gpuInfo .GPUs [j ].Index
180+ })
141181 for _ , item := range gpuInfo .GPUs {
142- data .ProductNames = append (data .ProductNames , item .ProductName )
182+ data .ProductNames = append (data .ProductNames , fmt .Sprintf ("%d - %s" , item .Index , item .ProductName ))
183+ }
184+ if len (data .ProductNames ) > 0 {
185+ req .ProductName = data .ProductNames [0 ]
143186 }
144187 } else {
145188 data .GPUType = "xpu"
@@ -148,9 +191,14 @@ func (m *MonitorService) LoadGPUMonitorData(req dto.MonitorGPUSearch) (dto.Monit
148191 global .LOG .Error ("Load XPU info failed or no XPU found, err: " , err )
149192 return data , buserr .New ("ErrRecordNotFound" )
150193 }
151- req .ProductName = xpuInfo .Xpu [0 ].Basic .DeviceName
194+ sort .Slice (xpuInfo .Xpu , func (i , j int ) bool {
195+ return xpuInfo .Xpu [i ].Basic .DeviceID < xpuInfo .Xpu [j ].Basic .DeviceID
196+ })
152197 for _ , item := range xpuInfo .Xpu {
153- data .ProductNames = append (data .ProductNames , item .Basic .DeviceName )
198+ data .ProductNames = append (data .ProductNames , fmt .Sprintf ("%d - %s" , item .Basic .DeviceID , item .Basic .DeviceName ))
199+ }
200+ if len (data .ProductNames ) > 0 {
201+ req .ProductName = data .ProductNames [0 ]
154202 }
155203 }
156204 }
@@ -571,9 +619,9 @@ func saveGPUDataToDB() {
571619 var list []model.MonitorGPU
572620 for _ , gpuItem := range gpuInfo .GPUs {
573621 item := model.MonitorGPU {
574- ProductName : gpuItem .ProductName ,
622+ ProductName : fmt . Sprintf ( "%d - %s" , gpuItem .Index , gpuItem . ProductName ) ,
575623 GPUUtil : loadGPUInfoFloat (gpuItem .GPUUtil ),
576- Temperature : loadGPUInfoInt (gpuItem .Temperature ),
624+ Temperature : loadGPUInfoFloat (gpuItem .Temperature ),
577625 PowerDraw : loadGPUInfoFloat (gpuItem .PowerDraw ),
578626 MemUsed : loadGPUInfoFloat (gpuItem .MemUsed ),
579627 MemTotal : loadGPUInfoFloat (gpuItem .MemTotal ),
@@ -602,13 +650,12 @@ func saveXPUDataToDB() {
602650 var list []model.MonitorGPU
603651 for _ , xpuItem := range xpuInfo .Xpu {
604652 item := model.MonitorGPU {
605- ProductName : xpuItem .Basic .DeviceName ,
606- GPUUtil : loadGPUInfoFloat (xpuItem .Stats .MemoryUtil ),
607- Temperature : loadGPUInfoInt (xpuItem .Stats .Temperature ),
608- PowerDraw : loadGPUInfoFloat (xpuItem .Stats .Power ),
609- MaxPowerLimit : float64 (xpuItem .Config .PowerLimit ),
610- MemUsed : loadGPUInfoFloat (xpuItem .Stats .MemoryUsed ),
611- MemTotal : loadGPUInfoFloat (xpuItem .Basic .Memory ),
653+ ProductName : fmt .Sprintf ("%d - %s" , xpuItem .Basic .DeviceID , xpuItem .Basic .DeviceName ),
654+ GPUUtil : loadGPUInfoFloat (xpuItem .Stats .MemoryUtil ),
655+ Temperature : loadGPUInfoFloat (xpuItem .Stats .Temperature ),
656+ PowerDraw : loadGPUInfoFloat (xpuItem .Stats .Power ),
657+ MemUsed : loadGPUInfoFloat (xpuItem .Stats .MemoryUsed ),
658+ MemTotal : loadGPUInfoFloat (xpuItem .Basic .Memory ),
612659 }
613660 if len (xpuItem .Processes ) != 0 {
614661 var processItem []dto.GPUProcess
@@ -643,6 +690,7 @@ func loadGPUInfoInt(val string) int {
643690func loadGPUInfoFloat (val string ) float64 {
644691 valItem := strings .ReplaceAll (val , "W" , "" )
645692 valItem = strings .ReplaceAll (valItem , "MB" , "" )
693+ valItem = strings .ReplaceAll (valItem , "°C" , "" )
646694 valItem = strings .ReplaceAll (valItem , "%" , "" )
647695 valItem = strings .TrimSpace (valItem )
648696 data , _ := strconv .ParseFloat (valItem , 64 )
0 commit comments