From 6f0c11092b0a39265c6af723571906e4f834706d Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 31 五月 2019 16:50:20 +0800
Subject: [PATCH] add info
---
cgpu.cpp | 6 +
gogpu.go | 80 ++++++++++++++++++++++++++
gpu/info.cpp | 58 ++++++++++++++++++
cgpu.h | 2
gpu/info.h | 3
5 files changed, 145 insertions(+), 4 deletions(-)
diff --git a/cgpu.cpp b/cgpu.cpp
index cc9c2ef..80421f4 100644
--- a/cgpu.cpp
+++ b/cgpu.cpp
@@ -12,5 +12,9 @@
#include "gpu/info.cpp"
int get_idle_gpu(const int mem_size){
- return gpu::getGPU(mem_size);
+ return gpu::getIdleGPU(mem_size);
+}
+
+char* get_gpu_info(){
+ return gpu::getGpuInfo();
}
\ No newline at end of file
diff --git a/cgpu.h b/cgpu.h
index 4d26109..bd268ea 100644
--- a/cgpu.h
+++ b/cgpu.h
@@ -7,6 +7,8 @@
int get_idle_gpu(const int mem_size);
+char* get_gpu_info();
+
#ifdef __cplusplus
}
#endif
diff --git a/gogpu.go b/gogpu.go
index a4eb39f..25f7c71 100644
--- a/gogpu.go
+++ b/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
+}
diff --git a/gpu/info.cpp b/gpu/info.cpp
index d38a8ae..f387ef2 100644
--- a/gpu/info.cpp
+++ b/gpu/info.cpp
@@ -301,7 +301,7 @@
goto gpu_fail;
}
- if(process_buf_size >= 0){
+ if(process_buf_size > 0){
infos->devices[i].running_processes = process_buf_size;
}
@@ -369,7 +369,7 @@
return suitable_gpu;
}
-int getGPU(const int need){
+int getIdleGPU(const int need){
nvGpuInfo_t gpu_buf;
int ret = get_gpu_info(&gpu_buf);
@@ -381,6 +381,60 @@
return -1;
}
+
+ // unsigned int decoder_utilization;
+ // unsigned int encoder_utilization;
+ // unsigned int gpu_utilization;
+ // unsigned int memory_utilization;
+ // unsigned int temperature;
+ // unsigned int running_processes;
+
+ // unsigned long long memory_total;
+ // unsigned long long memory_free;
+ // unsigned long long memory_used;
+
+char *getGpuInfo(){
+
+ nvGpuInfo_t gpu_info;
+ int flag = get_gpu_info(&gpu_info);
+
+ char * ret= NULL;
+
+ if(!flag){
+
+ char f[] = "gpu";
+ char s[] = "|";
+
+ ret = (char*)malloc(1024 * gpu_info.device_count);
+ int len = 0;
+
+ char tmp[1024];
+
+ for(int i = 0; i < gpu_info.device_count; i++){
+ memset(tmp, 0, 1024);
+
+ nvGpuUnitInfo_t g = gpu_info.devices[i];
+ sprintf(tmp, "%s%s%d%s%d%s%d%s%d%s%d%s%llu%s%llu%s%llu%s",
+ f,s,
+ g.gpu_utilization,s,
+ g.temperature,s,
+ g.decoder_utilization,s,
+ g.encoder_utilization,s,
+ g.running_processes,s,
+ g.memory_total,s,
+ g.memory_free,s,
+ g.memory_used,s);
+
+ int l = strlen(tmp);
+ memcpy(ret+len, tmp, l);
+ len += l;
+ }
+ ret[len] = '\0';
+ }
+
+ return ret;
+}
+
int test(void)
{
nvGpuInfo_t gpu_buf;
diff --git a/gpu/info.h b/gpu/info.h
index 73da8ab..2595f9f 100644
--- a/gpu/info.h
+++ b/gpu/info.h
@@ -2,7 +2,8 @@
#define _nvidia_gpu_info_h_
namespace gpu{
- int getGPU(const int need);
+ int getIdleGPU(const int need);
+ char *getGpuInfo();
}
#endif
\ No newline at end of file
--
Gitblit v1.8.0