Skip to content

Commit

Permalink
calcurate moderate values when cpu counters are reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur1 committed Aug 22, 2023
1 parent cbd69a9 commit f6d1575
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
23 changes: 17 additions & 6 deletions mackerel-plugin-docker/lib/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,30 @@ func addCPUPercentageStats(stats *map[string]interface{}, lastStat map[string]in
currentUserUsage, ok1 := (*stats)[internalCPUStatPrefix+name+".user"]
prevUserUsage, ok2 := lastStat[internalCPUStatPrefix+name+".user"]
if ok1 && ok2 {
userUsage := float64(currentUserUsage.(uint64) - uint64(prevUserUsage.(float64)))
if userUsage >= 0 {
(*stats)["docker.cpuacct_percentage."+name+".user"] = userUsage / hostUsage * 100.0 * float64(cpuNumsInt)
currentUserUsageUInt := currentUserUsage.(uint64)
prevUserUsageUInt := uint64(prevUserUsage.(float64))
var userUsage float64
if currentUserUsageUInt >= prevUserUsageUInt {
userUsage = float64(currentUserUsage.(uint64) - uint64(prevUserUsage.(float64)))
} else {
// counter has been reset
userUsage = float64(currentUserUsageUInt)
}
(*stats)["docker.cpuacct_percentage."+name+".user"] = userUsage / hostUsage * 100.0 * float64(cpuNumsInt)
}

currentSystemUsage, ok1 := (*stats)[internalCPUStatPrefix+name+".system"]
prevSystemUsage, ok2 := lastStat[internalCPUStatPrefix+name+".system"]
if ok1 && ok2 {
systemUsage := float64(currentSystemUsage.(uint64) - uint64(prevSystemUsage.(float64)))
if systemUsage >= 0 {
(*stats)["docker.cpuacct_percentage."+name+".system"] = systemUsage / hostUsage * 100.0 * float64(cpuNumsInt)
currentSystemUsageUInt := currentSystemUsage.(uint64)
prevSystemUsageUInt := uint64(prevSystemUsage.(float64))
var systemUsage float64
if currentSystemUsageUInt >= prevSystemUsageUInt {
systemUsage = float64(currentSystemUsageUInt - prevSystemUsageUInt)
} else {
systemUsage = float64(currentSystemUsageUInt)
}
(*stats)["docker.cpuacct_percentage."+name+".system"] = systemUsage / hostUsage * 100.0 * float64(cpuNumsInt)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions mackerel-plugin-docker/lib/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func TestAddCPUPercentageStats(t *testing.T) {
"docker._internal.cpuacct.containerD.host": uint64(100000),
"docker._internal.cpuacct.containerD.user": uint64(3000),
"docker._internal.cpuacct.containerD.system": uint64(2000),
"docker._internal.cpuacct.containerF.user": uint64(3000), // it has been reset
"docker._internal.cpuacct.containerF.system": uint64(1000), // it has been reset
"docker._internal.cpuacct.containerF.host": uint64(100000100000),
"docker._internal.cpuacct.containerF.onlineCPUs": int(2),
}
oldStats := map[string]interface{}{
"docker._internal.cpuacct.containerA.host": float64(90000),
Expand All @@ -111,6 +115,9 @@ func TestAddCPUPercentageStats(t *testing.T) {
"docker._internal.cpuacct.containerE.host": float64(100000),
"docker._internal.cpuacct.containerE.user": float64(3000),
"docker._internal.cpuacct.containerE.system": float64(2000),
"docker._internal.cpuacct.containerF.user": float64(40000000000),
"docker._internal.cpuacct.containerF.system": float64(20000000000),
"docker._internal.cpuacct.containerF.host": float64(100000000000),
}
addCPUPercentageStats(&stats, oldStats)

Expand All @@ -135,4 +142,10 @@ func TestAddCPUPercentageStats(t *testing.T) {
if _, ok := stats["docker.cpuacct_percentage.containerE.user"]; ok {
t.Errorf("docker.cpuacct_percentage.containerE.user should not be calculated")
}

if stat, ok := stats["docker.cpuacct_percentage.containerF.user"]; !ok {
t.Errorf("docker.cpuacct_percentage.containerF.user should be calculated")
} else if stat != float64(6.0) {
t.Errorf("docker.cpuacct_percentage.containerF.user should be %f, but %f", float64(6.0), stat)
}
}

0 comments on commit f6d1575

Please sign in to comment.