From 0a324c77e33d8c0b61fd6d166cc8d3cec7d78139 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期三, 10 七月 2019 18:29:23 +0800
Subject: [PATCH] update
---
gogpu.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 105 insertions(+), 2 deletions(-)
diff --git a/gogpu.go b/gogpu.go
index a4eb39f..86a7605 100644
--- a/gogpu.go
+++ b/gogpu.go
@@ -8,8 +8,111 @@
#include "cgpu.h"
*/
import "C"
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "strconv"
+ "strings"
+ "unsafe"
+)
-// IdleGPU pass needed gpu memory size test if satisfy
-func IdleGPU(memSize int) int {
+// ValidGPU pass needed gpu memory size test if satisfy
+func ValidGPU(memSize int) int {
return int(C.get_idle_gpu(C.int(memSize)))
}
+
+// IdleGPU idle most
+func IdleGPU() int {
+ info, err := Info()
+ if err != nil {
+ fmt.Println("no gpu")
+ return -1
+ }
+ if info.Count == 1 {
+ return 0
+ }
+
+ var free int64
+ gpu := 0
+ for k, v := range info.Info {
+ if v.GpuMemoryFree > free {
+ free = v.GpuMemoryFree
+ gpu = k
+ }
+ }
+ return gpu
+}
+
+// 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
+}
--
Gitblit v1.8.0