zhangmeng
2019-05-31 6f0c11092b0a39265c6af723571906e4f834706d
gogpu.go
@@ -8,8 +8,88 @@
#include "cgpu.h"
*/
import "C"
import (
   "encoding/json"
   "errors"
   "strconv"
   "strings"
   "unsafe"
)
// IdleGPU pass needed gpu memory size test if satisfy
func IdleGPU(memSize int) int {
   return int(C.get_idle_gpu(C.int(memSize)))
}
// GpuUnitInfo gpu card info
type GpuUnitInfo struct {
   GpuUtilization int   `json:"utilization"`
   GpuTemperature int   `json:"temperature"`
   GpuDecoderUtil int   `json:"decoder"`
   GpuEncoderUtil int   `json:"encoder"`
   GpuProcesses   int   `json:"process"`
   GpuMemoryTotal int64 `json:"memory"`
   GpuMemoryFree  int64 `json:"free"`
   GpuMemoryUsed  int64 `json:"used"`
}
func (u GpuUnitInfo) String() string {
   s, _ := json.Marshal(u)
   return string(s)
}
// GpuInfo gpu info
type GpuInfo struct {
   Count int           `json:"count"`
   Info  []GpuUnitInfo `json:"info"`
}
func (g GpuInfo) String() string {
   s, _ := json.Marshal(g)
   return string(s)
}
// Info gpu infos
func Info() (*GpuInfo, error) {
   p := C.get_gpu_info()
   if p == nil {
      return nil, errors.New("can't get gpu infos")
   }
   defer C.free(unsafe.Pointer(p))
   str := C.GoString(p)
   infos := strings.Split(str, "|")
   length := len(infos)
   var units []GpuUnitInfo
   var count int
   for i := 0; i < length; i++ {
      if infos[i] == "gpu" {
         count++
         i++
         u := GpuUnitInfo{}
         u.GpuUtilization, _ = strconv.Atoi(infos[i])
         i++
         u.GpuTemperature, _ = strconv.Atoi(infos[i])
         i++
         u.GpuDecoderUtil, _ = strconv.Atoi(infos[i])
         i++
         u.GpuEncoderUtil, _ = strconv.Atoi(infos[i])
         i++
         u.GpuProcesses, _ = strconv.Atoi(infos[i])
         i++
         u.GpuMemoryTotal, _ = strconv.ParseInt(infos[i], 10, 64)
         i++
         u.GpuMemoryFree, _ = strconv.ParseInt(infos[i], 10, 64)
         i++
         u.GpuMemoryUsed, _ = strconv.ParseInt(infos[i], 10, 64)
         units = append(units, u)
      }
   }
   return &GpuInfo{count, units}, nil
}