Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #232 from signalfx/docker-cpu-update
Browse files Browse the repository at this point in the history
Using newer method to calculate docker cpu percent
  • Loading branch information
keitwb authored Mar 30, 2018
2 parents 03b4d26 + 8e1f4ce commit 21dae18
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/monitors/docker/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,23 @@ func convertCPUStats(stats *dtypes.CPUStats, prior *dtypes.CPUStats) []*datapoin
return out
}

// From the old docker golang client (I can't find it in the current client
// though)
// Copied from
// https://github.com/docker/cli/blob/dbd96badb6959c2b7070664aecbcf0f7c299c538/cli/command/container/stats_helpers.go
func calculateCPUPercent(previous *dtypes.CPUStats, v *dtypes.CPUStats) float64 {
var (
cpuPercent = 0.0
// calculate the change for the cpu usage of the container in between readings
cpuDelta = float64(v.CPUUsage.TotalUsage) - float64(previous.CPUUsage.TotalUsage)
// calculate the change for the entire system between readings
systemDelta = float64(v.SystemUsage) - float64(previous.SystemUsage)
onlineCPUs = float64(v.OnlineCPUs)
)

if onlineCPUs == 0.0 {
onlineCPUs = float64(len(v.CPUUsage.PercpuUsage))
}
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CPUUsage.PercpuUsage)) * 100.0
cpuPercent = (cpuDelta / systemDelta) * onlineCPUs * 100.0
}
return cpuPercent
}
Expand Down

0 comments on commit 21dae18

Please sign in to comment.