Skip to content

Commit 9f8e5b9

Browse files
committed
cli/command/container: calculateCPUPercentUnix: simplify
Pass the whole CPUStats struct instead of deconstructing it to separate variables. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e809e65 commit 9f8e5b9

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

cli/command/container/stats_helpers.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@ func (s *stats) isKnownContainer(cid string) (int, bool) {
4949
return -1, false
5050
}
5151

52-
func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, streamStats bool, waitFirst *sync.WaitGroup) { //nolint:gocyclo
53-
var (
54-
getFirst bool
55-
previousCPU uint64
56-
previousSystem uint64
57-
u = make(chan error, 1)
58-
)
52+
func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, streamStats bool, waitFirst *sync.WaitGroup) {
53+
var getFirst bool
5954

6055
defer func() {
6156
// if error happens and we get nothing of stats, release wait group whatever
@@ -71,6 +66,7 @@ func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, strea
7166
return
7267
}
7368

69+
u := make(chan error, 1)
7470
go func() {
7571
defer response.Body.Close()
7672
dec := json.NewDecoder(response.Body)
@@ -97,9 +93,7 @@ func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, strea
9793
}
9894

9995
if daemonOSType != "windows" {
100-
previousCPU = v.PreCPUStats.CPUUsage.TotalUsage
101-
previousSystem = v.PreCPUStats.SystemUsage
102-
cpuPercent = calculateCPUPercentUnix(previousCPU, previousSystem, v)
96+
cpuPercent = calculateCPUPercentUnix(v.PreCPUStats, v.CPUStats)
10397
blkRead, blkWrite = calculateBlockIO(v.BlkioStats)
10498
mem = calculateMemUsageUnixNoCache(v.MemoryStats)
10599
memLimit = float64(v.MemoryStats.Limit)
@@ -165,18 +159,18 @@ func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, strea
165159
}
166160
}
167161

168-
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *container.StatsResponse) float64 {
162+
func calculateCPUPercentUnix(previousCPU container.CPUStats, curCPUStats container.CPUStats) float64 {
169163
var (
170164
cpuPercent = 0.0
171165
// calculate the change for the cpu usage of the container in between readings
172-
cpuDelta = float64(v.CPUStats.CPUUsage.TotalUsage) - float64(previousCPU)
166+
cpuDelta = float64(curCPUStats.CPUUsage.TotalUsage) - float64(previousCPU.CPUUsage.TotalUsage)
173167
// calculate the change for the entire system between readings
174-
systemDelta = float64(v.CPUStats.SystemUsage) - float64(previousSystem)
175-
onlineCPUs = float64(v.CPUStats.OnlineCPUs)
168+
systemDelta = float64(curCPUStats.SystemUsage) - float64(previousCPU.SystemUsage)
169+
onlineCPUs = float64(curCPUStats.OnlineCPUs)
176170
)
177171

178172
if onlineCPUs == 0.0 {
179-
onlineCPUs = float64(len(v.CPUStats.CPUUsage.PercpuUsage))
173+
onlineCPUs = float64(len(curCPUStats.CPUUsage.PercpuUsage))
180174
}
181175
if systemDelta > 0.0 && cpuDelta > 0.0 {
182176
cpuPercent = (cpuDelta / systemDelta) * onlineCPUs * 100.0

0 commit comments

Comments
 (0)