1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package controllers
import (
"encoding/json"
"example.com/m/conf"
"example.com/m/models"
"example.com/m/nm"
"example.com/m/utils"
"io"
)
type StateController struct {
BaseController
}
var circularBuffer *models.CircularBuffer
func init() {
circularBuffer = &models.CircularBuffer{}
}
func (c *StateController) GetRunningState() {
res := nm.RunningState
c.ResponseInfo(200, "get running state successful", res)
}
func (c *StateController) GetRunningTp() {
info := utils.GetApiHardwareInfo(conf.GetConfig().HardwareUrl)
if info == nil {
c.ResponseInfo(500, "get running tp failed", 0)
return
}
if len(info.Data.Gpus) > 0 {
c.ResponseInfo(200, "get running state successful", 82.58)
}
c.ResponseInfo(500, "get running tp failed", 0)
}
func (c *StateController) GetRunningLineChart() {
info := utils.GetApiHardwareInfo(conf.GetConfig().HardwareUrl)
if info == nil {
c.ResponseInfo(500, "get running tp failed", "")
return
}
if len(info.Data.Gpus) > 0 {
var totalUsage int64
for _, gpu := range info.Data.Gpus {
totalUsage += int64(gpu.Usage)
}
avgTemp := totalUsage / int64(len(info.Data.Gpus))
circularBuffer.Add(avgTemp)
c.ResponseInfo(200, "get running state successful", circularBuffer.GetDataTail())
}
c.ResponseInfo(500, "get running tp failed", "")
}
func (c *StateController) GetWorkerInfo() {
res := models.WorkerAccount{
WorkerAcc: conf.GetConfig().SignPublicAddress.Hex(),
ChainID: conf.GetConfig().ChainID,
}
c.ResponseInfo(200, "get worker info successful", res)
}
func (c *StateController) GetListGpuInfo() {
info := utils.GetApiHardwareInfo(conf.GetConfig().HardwareUrl)
if info != nil && info.Data != nil {
c.ResponseInfo(200, "get list gpu info successful", info.Data.Gpus)
return
}
c.ResponseInfo(500, "get list gpu info failed", nil)
}
func (c *StateController) GetGpuUsageInfo() {
bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil || bodyReq == nil {
c.ResponseInfo(500, "param error", "")
return
}
req := &models.GpuUsageReq{}
err = json.Unmarshal(bodyReq, req)
if err != nil {
c.ResponseInfo(500, "param error", "")
return
}
info := utils.GetApiHardwareInfo(conf.GetConfig().HardwareUrl)
if info != nil {
for _, gpu := range info.Data.Gpus {
if gpu.Seq == req.Seq {
c.ResponseInfo(200, "set seed successful", gpu)
return
}
}
}
c.ResponseInfo(500, "get gpu usage info failed", nil)
}
func (c *StateController) GetOtherHardwareInfo() {
info := utils.GetApiHardwareInfo(conf.GetConfig().HardwareUrl)
var diskTotal, diskFree int64
for _, disk := range info.Data.Disk {
for _, point := range disk.MountPoints {
if point == "/" {
diskTotal += disk.SizeBytes
diskFree += disk.FreeBytes
break
}
}
}
diskUsage := int32((1 - float64(diskFree)/float64(diskTotal)) * 100)
res := &models.OtherHardwareInfoResp{
NodeID: conf.GetConfig().SignPublicAddress.Hex(),
CpuName: info.Data.Cpus.Model,
CpuUsage: info.Data.Cpus.Usage,
CpuFrequency: info.Data.Cpus.Frequency,
RamSize: info.Data.Mem.Total,
RamUsage: info.Data.Mem.MemUtil,
DiskSize: diskTotal,
DiskUsage: diskUsage,
}
c.ResponseInfo(200, "get hardware info successful", res)
}