Commit eea4decc authored by Ubuntu's avatar Ubuntu

add gpu usage

parent 9c74210a
......@@ -2,7 +2,6 @@ package main
import (
"fmt"
"log"
"github.com/NVIDIA/go-nvml/pkg/nvml"
)
......@@ -12,18 +11,18 @@ import (
func getGPU() ([]DeviceInfo, error) {
ret := nvml.Init()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to initialize NVML: %v", nvml.ErrorString(ret))
return nil, fmt.Errorf(ret)
}
defer func() {
ret := nvml.Shutdown()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to shutdown NVML: %v", nvml.ErrorString(ret))
return nil, fmt.Errorf(err)
}
}()
count, ret := nvml.DeviceGetCount()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to get device count: %v", nvml.ErrorString(ret))
return nil, fmt.Errorf(err)
}
res := make([]DeviceInfo, 0, count)
......@@ -32,7 +31,7 @@ func getGPU() ([]DeviceInfo, error) {
device, ret := nvml.DeviceGetHandleByIndex(i)
if ret != nvml.SUCCESS {
log.Fatalf("Unable to get device at index %d: %v", i, nvml.ErrorString(ret))
return nil, fmt.Errorf(err)
}
// memory, err := device.GetMemoryInfo()
......@@ -47,12 +46,13 @@ func getGPU() ([]DeviceInfo, error) {
name, err := device.GetName()
if err != nvml.SUCCESS {
panic(err)
return nil, fmt.Errorf(err)
}
memory, err := device.GetMemoryInfo()
if err != nvml.SUCCESS {
fmt.Println(err)
return nil, fmt.Errorf(err)
//fmt.Println(err)
}
//fmt.Printf(" %v\n", proc)
......@@ -70,14 +70,16 @@ func getGPU() ([]DeviceInfo, error) {
if v, ok := comput[name]; ok {
c = v
} else {
fmt.Println("can not find the model '" + name + "'")
for k, v := range comput {
fmt.Println(k, v)
}
}
// } else {
// fmt.Println("can not find the model '" + name + "'")
// for k, v := range comput {
// fmt.Println(k, v)
// }
// }
e = DeviceInfo{
Type: fmt.Sprintf("GPU-%d", i),
Model: name,
......@@ -93,6 +95,74 @@ func getGPU() ([]DeviceInfo, error) {
}
func getGpuUsage() ([]DeviceInfo, error) {
ret := nvml.Init()
if ret != nvml.SUCCESS {
return nil, fmt.Errorf(ret)
}
defer func() {
ret := nvml.Shutdown()
if ret != nvml.SUCCESS {
return nil, fmt.Errorf(ret)
//log.Fatalf("Unable to shutdown NVML: %v", nvml.ErrorString(ret))
}
}()
count, ret := nvml.DeviceGetCount()
if ret != nvml.SUCCESS {
return nil, fmt.Errorf(ret)
//log.Fatalf("Unable to get device count: %v", nvml.ErrorString(ret))
}
res := make([]DeviceInfo, 0, count)
for i := 0; i < count; i++ {
device, ret := nvml.DeviceGetHandleByIndex(i)
if ret != nvml.SUCCESS {
return nil, fmt.Errorf(ret)
//log.Fatalf("Unable to get device at index %d: %v", i, nvml.ErrorString(ret))
}
name, ret := device.GetName()
if ret != nvml.SUCCESS {
return nil, fmt.Errorf(ret)
//panic(err)
}
utilization, ret := device.GetUtilizationRates()
if ret != nvml.SUCCESS {
return nil, fmt.Errorf(ret)
}
e := DeviceInfo{
Type: fmt.Sprintf("GPU-%d", i),
Model: name,
Param: "gpu usage",
Power: utilization.Gpu,
}
res = append(res, e)
e = DeviceInfo{
Type: fmt.Sprintf("GPU-%d", i),
Model: name,
Param: "gpu mem usage",
Power: utilization.Memory,
}
res = append(res, e)
//fmt.Println("utilization", utilization)
}
return res, nil
}
var comput map[string]uint64
//NVIDIA GeForce RTX 3080,
......
......@@ -52,6 +52,18 @@ func main() {
app.Get("/hw/used", func(c fiber.Ctx) error {
res := make([]DeviceInfo, 0)
gpu, err := getGpuUsage()
if err != nil {
c.SendString("getGpuUsage err: " + err.Error())
}
res = append(res, gpu...)
return c.JSON(res)
res := make([]DeviceInfo, 0)
return c.JSON(res)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment