@@ -39,6 +39,7 @@ var monitorCancel context.CancelFunc
3939type IMonitorService interface {
4040 Run ()
4141 LoadMonitorData (req dto.MonitorSearch ) ([]dto.MonitorData , error )
42+ LoadGPUOptions () dto.MonitorGPUOptions
4243 LoadGPUMonitorData (req dto.MonitorGPUSearch ) (dto.MonitorGPUData , error )
4344 LoadSetting () (* dto.MonitorSetting , error )
4445 UpdateSetting (key , value string ) error
@@ -118,42 +119,49 @@ func (m *MonitorService) LoadMonitorData(req dto.MonitorSearch) ([]dto.MonitorDa
118119 return data , nil
119120}
120121
122+ func (m * MonitorService ) LoadGPUOptions () dto.MonitorGPUOptions {
123+ var data dto.MonitorGPUOptions
124+ gpuExist , gpuClient := gpu .New ()
125+ xpuExist , xpuClient := xpu .New ()
126+ if ! gpuExist && ! xpuExist {
127+ return data
128+ }
129+ if gpuExist {
130+ data .GPUType = "gpu"
131+ gpuInfo , err := gpuClient .LoadGpuInfo ()
132+ if err != nil || len (gpuInfo .GPUs ) == 0 {
133+ global .LOG .Error ("Load GPU info failed or no GPU found, err: " , err )
134+ return data
135+ }
136+ sort .Slice (gpuInfo .GPUs , func (i , j int ) bool {
137+ return gpuInfo .GPUs [i ].Index < gpuInfo .GPUs [j ].Index
138+ })
139+ for _ , item := range gpuInfo .GPUs {
140+ data .Options = append (data .Options , fmt .Sprintf ("%d - %s" , item .Index , item .ProductName ))
141+ }
142+ return data
143+ } else {
144+ data .GPUType = "xpu"
145+ xpuInfo , err := xpuClient .LoadGpuInfo ()
146+ if err != nil || len (xpuInfo .Xpu ) == 0 {
147+ global .LOG .Error ("Load XPU info failed or no XPU found, err: " , err )
148+ return data
149+ }
150+ sort .Slice (xpuInfo .Xpu , func (i , j int ) bool {
151+ return xpuInfo .Xpu [i ].Basic .DeviceID < xpuInfo .Xpu [j ].Basic .DeviceID
152+ })
153+ for _ , item := range xpuInfo .Xpu {
154+ data .Options = append (data .Options , fmt .Sprintf ("%d - %s" , item .Basic .DeviceID , item .Basic .DeviceName ))
155+ }
156+ return data
157+ }
158+ }
159+
121160func (m * MonitorService ) LoadGPUMonitorData (req dto.MonitorGPUSearch ) (dto.MonitorGPUData , error ) {
122161 loc , _ := time .LoadLocation (common .LoadTimeZoneByCmd ())
123162 req .StartTime = req .StartTime .In (loc )
124163 req .EndTime = req .EndTime .In (loc )
125-
126164 var data dto.MonitorGPUData
127- gpuExist , gpuclient := gpu .New ()
128- xpuExist , xpuClient := xpu .New ()
129- if ! gpuExist && ! xpuExist {
130- return data , nil
131- }
132- if len (req .ProductName ) == 0 {
133- if gpuExist {
134- data .GPUType = "gpu"
135- gpuInfo , err := gpuclient .LoadGpuInfo ()
136- if err != nil || len (gpuInfo .GPUs ) == 0 {
137- global .LOG .Error ("Load GPU info failed or no GPU found, err: " , err )
138- return data , buserr .New ("ErrRecordNotFound" )
139- }
140- req .ProductName = gpuInfo .GPUs [0 ].ProductName
141- for _ , item := range gpuInfo .GPUs {
142- data .ProductNames = append (data .ProductNames , item .ProductName )
143- }
144- } else {
145- data .GPUType = "xpu"
146- xpuInfo , err := xpuClient .LoadGpuInfo ()
147- if err != nil || len (xpuInfo .Xpu ) == 0 {
148- global .LOG .Error ("Load XPU info failed or no XPU found, err: " , err )
149- return data , buserr .New ("ErrRecordNotFound" )
150- }
151- req .ProductName = xpuInfo .Xpu [0 ].Basic .DeviceName
152- for _ , item := range xpuInfo .Xpu {
153- data .ProductNames = append (data .ProductNames , item .Basic .DeviceName )
154- }
155- }
156- }
157165 gpuList , err := monitorRepo .GetGPU (repo .WithByCreatedAt (req .StartTime , req .EndTime ), monitorRepo .WithByProductName (req .ProductName ))
158166 if err != nil {
159167 return data , err
@@ -571,9 +579,9 @@ func saveGPUDataToDB() {
571579 var list []model.MonitorGPU
572580 for _ , gpuItem := range gpuInfo .GPUs {
573581 item := model.MonitorGPU {
574- ProductName : gpuItem .ProductName ,
582+ ProductName : fmt . Sprintf ( "%d - %s" , gpuItem .Index , gpuItem . ProductName ) ,
575583 GPUUtil : loadGPUInfoFloat (gpuItem .GPUUtil ),
576- Temperature : loadGPUInfoInt (gpuItem .Temperature ),
584+ Temperature : loadGPUInfoFloat (gpuItem .Temperature ),
577585 PowerDraw : loadGPUInfoFloat (gpuItem .PowerDraw ),
578586 MemUsed : loadGPUInfoFloat (gpuItem .MemUsed ),
579587 MemTotal : loadGPUInfoFloat (gpuItem .MemTotal ),
@@ -602,13 +610,12 @@ func saveXPUDataToDB() {
602610 var list []model.MonitorGPU
603611 for _ , xpuItem := range xpuInfo .Xpu {
604612 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 ),
613+ ProductName : fmt .Sprintf ("%d - %s" , xpuItem .Basic .DeviceID , xpuItem .Basic .DeviceName ),
614+ GPUUtil : loadGPUInfoFloat (xpuItem .Stats .MemoryUtil ),
615+ Temperature : loadGPUInfoFloat (xpuItem .Stats .Temperature ),
616+ PowerDraw : loadGPUInfoFloat (xpuItem .Stats .Power ),
617+ MemUsed : loadGPUInfoFloat (xpuItem .Stats .MemoryUsed ),
618+ MemTotal : loadGPUInfoFloat (xpuItem .Basic .Memory ),
612619 }
613620 if len (xpuItem .Processes ) != 0 {
614621 var processItem []dto.GPUProcess
@@ -643,6 +650,7 @@ func loadGPUInfoInt(val string) int {
643650func loadGPUInfoFloat (val string ) float64 {
644651 valItem := strings .ReplaceAll (val , "W" , "" )
645652 valItem = strings .ReplaceAll (valItem , "MB" , "" )
653+ valItem = strings .ReplaceAll (valItem , "°C" , "" )
646654 valItem = strings .ReplaceAll (valItem , "%" , "" )
647655 valItem = strings .TrimSpace (valItem )
648656 data , _ := strconv .ParseFloat (valItem , 64 )
0 commit comments