Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Docker CPU Percentage's unusual spike when restarting containers #1100

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
15 changes: 14 additions & 1 deletion 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,13 +115,16 @@ 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)

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

if _, ok := stats["docker.cpuacct_percentage.containerC.user"]; ok {
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)
}
}