| | |
| | | #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(); |
| | | } |
| | |
| | | |
| | | int get_idle_gpu(const int mem_size); |
| | | |
| | | char* get_gpu_info(); |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |
| | |
| | | #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 |
| | | } |
| | |
| | | goto gpu_fail; |
| | | } |
| | | |
| | | if(process_buf_size >= 0){ |
| | | if(process_buf_size > 0){ |
| | | infos->devices[i].running_processes = process_buf_size; |
| | | } |
| | | |
| | |
| | | return suitable_gpu; |
| | | } |
| | | |
| | | int getGPU(const int need){ |
| | | int getIdleGPU(const int need){ |
| | | nvGpuInfo_t gpu_buf; |
| | | |
| | | int ret = get_gpu_info(&gpu_buf); |
| | |
| | | |
| | | 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; |
| | |
| | | #define _nvidia_gpu_info_h_ |
| | | |
| | | namespace gpu{ |
| | | int getGPU(const int need); |
| | | int getIdleGPU(const int need); |
| | | char *getGpuInfo(); |
| | | } |
| | | |
| | | #endif |