Skip to content

Commit

Permalink
fix: 修复上报cpu和内存资源时计算错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambition9186 committed May 22, 2024
1 parent b254852 commit edbadcc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
10 changes: 2 additions & 8 deletions internal/util/process_collect/process_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,11 @@ func NewProcessCollector(ctx context.Context) {

// setCpuUsage set current, max, and min CPU usage
func setCpuUsage(usage float64) {
if usage == 0 {
return
}
cpuUsage = usage
if cpuUsage > cpuMaxUsage {
cpuMaxUsage = cpuUsage
}
if cpuUsage < cpuMinUsage || cpuMinUsage == 0 {
if cpuUsage > 0 && cpuUsage < cpuMinUsage {
cpuMinUsage = cpuUsage
}
cpuTotalUsage += cpuUsage
Expand All @@ -86,14 +83,11 @@ func setCpuUsage(usage float64) {

// setMemUsage set current, max, and min memory usage
func setMemUsage(usage uint64) {
if usage == 0 {
return
}
memoryUsage = usage
if memoryUsage > memoryMaxUsage {
memoryMaxUsage = memoryUsage
}
if memoryUsage < memoryMinUsage || memoryMinUsage == 0 {
if memoryUsage > 0 && memoryUsage < memoryMinUsage {
memoryMinUsage = memoryUsage
}
memoryTotalUsage += memoryUsage
Expand Down
12 changes: 11 additions & 1 deletion internal/util/process_collect/process_collector_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (c *processCollector) processCollect() {
return
}

// 定义上一次上报的cpu时间
var prevCPUTime, cpuUsage float64

for {
select {
case <-time.After(time.Second):
Expand All @@ -49,8 +52,15 @@ func (c *processCollector) processCollect() {
logger.Error("returns the current status information of the process", logger.ErrAttr(err))
return
}
// 计算 CPU 每秒的使用量
diffTime := stat.CPUTime() - prevCPUTime
// 如果差值不等于0更新cpu使用量
if diffTime != 0 {
cpuUsage = diffTime
}
prevCPUTime = stat.CPUTime()
// set cpu usage
setCpuUsage(stat.CPUTime())
setCpuUsage(cpuUsage)
// set memory usage
setMemUsage(uint64(stat.ResidentMemory()))
case <-c.ctx.Done():
Expand Down
12 changes: 10 additions & 2 deletions internal/util/process_collect/process_collector_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,24 @@ func (c *processCollector) processCollect() {
h := windows.CurrentProcess()
var startTime, exitTime, kernelTime, userTime windows.Filetime

// 定义上一次上报的cpu时间
var prevCPUTime, cpuUsage float64

for {
select {
case <-time.After(time.Second):
if err := windows.GetProcessTimes(h, &startTime, &exitTime, &kernelTime, &userTime); err != nil {
logger.Error("get process times", logger.ErrAttr(err))
return
}

// 计算 CPU 每秒的使用量
diffTime := fileTimeToSeconds(kernelTime) + fileTimeToSeconds(userTime) - prevCPUTime
if diffTime != 0 {
cpuUsage = diffTime
}
prevCPUTime = fileTimeToSeconds(kernelTime) + fileTimeToSeconds(userTime)
// set cpu usage
setCpuUsage(fileTimeToSeconds(kernelTime) + fileTimeToSeconds(userTime))
setCpuUsage(cpuUsage)

mem, err := getProcessMemoryInfo(h)
if err != nil {
Expand Down

0 comments on commit edbadcc

Please sign in to comment.