Skip to content

Commit fef1321

Browse files
authored
feat: Enhance CPU metrics by adding CPU detailed information (#11169)
* fix: Update CPU usage retrieval to include detailed percentage information * feat: Enhance CPU metrics by adding CPU frequency and detailed usage percentages * feat: Add CPU frequency metric to dashboard base information
1 parent 5ff5b0a commit fef1321

File tree

16 files changed

+309
-135
lines changed

16 files changed

+309
-135
lines changed

agent/app/dto/dashboard.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ type DashboardBase struct {
1919
IpV4Addr string `json:"ipV4Addr"`
2020
SystemProxy string `json:"systemProxy"`
2121

22-
CPUCores int `json:"cpuCores"`
23-
CPULogicalCores int `json:"cpuLogicalCores"`
24-
CPUModelName string `json:"cpuModelName"`
22+
CPUCores int `json:"cpuCores"`
23+
CPULogicalCores int `json:"cpuLogicalCores"`
24+
CPUModelName string `json:"cpuModelName"`
25+
CPUMhz float64 `json:"cpuMhz"`
2526

2627
QuickJumps []QuickJump `json:"quickJump"`
2728
CurrentInfo DashboardCurrent `json:"currentInfo"`
@@ -58,9 +59,10 @@ type NodeCurrent struct {
5859
Load15 float64 `json:"load15"`
5960
LoadUsagePercent float64 `json:"loadUsagePercent"`
6061

61-
CPUUsedPercent float64 `json:"cpuUsedPercent"`
62-
CPUUsed float64 `json:"cpuUsed"`
63-
CPUTotal int `json:"cpuTotal"`
62+
CPUUsedPercent float64 `json:"cpuUsedPercent"`
63+
CPUUsed float64 `json:"cpuUsed"`
64+
CPUTotal int `json:"cpuTotal"`
65+
CPUDetailedPercent []float64 `json:"cpuDetailedPercent"`
6466

6567
MemoryTotal uint64 `json:"memoryTotal"`
6668
MemoryAvailable uint64 `json:"memoryAvailable"`
@@ -84,10 +86,11 @@ type DashboardCurrent struct {
8486
Load15 float64 `json:"load15"`
8587
LoadUsagePercent float64 `json:"loadUsagePercent"`
8688

87-
CPUPercent []float64 `json:"cpuPercent"`
88-
CPUUsedPercent float64 `json:"cpuUsedPercent"`
89-
CPUUsed float64 `json:"cpuUsed"`
90-
CPUTotal int `json:"cpuTotal"`
89+
CPUPercent []float64 `json:"cpuPercent"`
90+
CPUUsedPercent float64 `json:"cpuUsedPercent"`
91+
CPUUsed float64 `json:"cpuUsed"`
92+
CPUTotal int `json:"cpuTotal"`
93+
CPUDetailedPercent []float64 `json:"cpuDetailedPercent"`
9194

9295
MemoryTotal uint64 `json:"memoryTotal"`
9396
MemoryUsed uint64 `json:"memoryUsed"`

agent/app/service/dashboard.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@ func (u *DashboardService) LoadCurrentInfoForNode() *dto.NodeCurrent {
108108

109109
currentInfo.CPUTotal, _ = psutil.CPUInfo.GetLogicalCores(false)
110110

111-
cpuUsedPercent, perCore := psutil.CPU.GetCPUUsage()
111+
cpuUsedPercent, perCore, cpuDetailedPercent := psutil.CPU.GetCPUUsage()
112112
if len(perCore) == 0 {
113113
currentInfo.CPUTotal = psutil.CPU.NumCPU()
114114
} else {
115115
currentInfo.CPUTotal = len(perCore)
116116
}
117117
currentInfo.CPUUsedPercent = cpuUsedPercent
118118
currentInfo.CPUUsed = cpuUsedPercent * 0.01 * float64(currentInfo.CPUTotal)
119+
currentInfo.CPUDetailedPercent = cpuDetailedPercent
119120

120121
loadInfo, _ := load.Avg()
121122
currentInfo.Load1 = loadInfo.Load1
@@ -171,6 +172,7 @@ func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto
171172

172173
baseInfo.CPUCores, _ = psutil.CPUInfo.GetPhysicalCores(false)
173174
baseInfo.CPULogicalCores, _ = psutil.CPUInfo.GetLogicalCores(false)
175+
baseInfo.CPUMhz = cpuInfo[0].Mhz
174176

175177
baseInfo.CurrentInfo = *u.LoadCurrentInfo(ioOption, netOption)
176178
return &baseInfo, nil
@@ -184,7 +186,7 @@ func (u *DashboardService) LoadCurrentInfo(ioOption string, netOption string) *d
184186
currentInfo.Procs = hostInfo.Procs
185187
currentInfo.CPUTotal, _ = psutil.CPUInfo.GetLogicalCores(false)
186188

187-
cpuUsedPercent, perCore := psutil.CPU.GetCPUUsage()
189+
cpuUsedPercent, perCore, cpuDetailedPercent := psutil.CPU.GetCPUUsage()
188190
if len(perCore) == 0 {
189191
currentInfo.CPUTotal = psutil.CPU.NumCPU()
190192
} else {
@@ -193,6 +195,7 @@ func (u *DashboardService) LoadCurrentInfo(ioOption string, netOption string) *d
193195
currentInfo.CPUPercent = perCore
194196
currentInfo.CPUUsedPercent = cpuUsedPercent
195197
currentInfo.CPUUsed = cpuUsedPercent * 0.01 * float64(currentInfo.CPUTotal)
198+
currentInfo.CPUDetailedPercent = cpuDetailedPercent
196199

197200
loadInfo, _ := load.Avg()
198201
currentInfo.Load1 = loadInfo.Load1

0 commit comments

Comments
 (0)