Skip to content

Commit 10281ff

Browse files
authored
Merge pull request #6583 from thaJeztah/stats_cleanups
cli/command/container: cleanup stats code
2 parents e809e65 + 5007c96 commit 10281ff

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

cli/command/container/stats_helpers.go

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,10 @@ func (s *stats) isKnownContainer(cid string) (int, bool) {
5050
}
5151

5252
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-
)
53+
var getFirst bool
5954

6055
defer func() {
61-
// if error happens and we get nothing of stats, release wait group whatever
56+
// if error happens, and we get nothing of stats, release wait group whatever
6257
if !getFirst {
6358
getFirst = true
6459
waitFirst.Done()
@@ -71,21 +66,15 @@ 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)
7773
for {
7874
if ctx.Err() != nil {
7975
return
8076
}
81-
var (
82-
v *container.StatsResponse
83-
memPercent, cpuPercent float64
84-
blkRead, blkWrite uint64 // Only used on Linux
85-
mem, memLimit float64
86-
pidsStatsCurrent uint64
87-
)
88-
77+
var v container.StatsResponse
8978
if err := dec.Decode(&v); err != nil {
9079
dec = json.NewDecoder(io.MultiReader(dec.Buffered(), response.Body))
9180
u <- err
@@ -96,35 +85,36 @@ func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, strea
9685
continue
9786
}
9887

99-
if daemonOSType != "windows" {
100-
previousCPU = v.PreCPUStats.CPUUsage.TotalUsage
101-
previousSystem = v.PreCPUStats.SystemUsage
102-
cpuPercent = calculateCPUPercentUnix(previousCPU, previousSystem, v)
103-
blkRead, blkWrite = calculateBlockIO(v.BlkioStats)
104-
mem = calculateMemUsageUnixNoCache(v.MemoryStats)
105-
memLimit = float64(v.MemoryStats.Limit)
106-
memPercent = calculateMemPercentUnixNoCache(memLimit, mem)
107-
pidsStatsCurrent = v.PidsStats.Current
88+
if daemonOSType == "windows" {
89+
netRx, netTx := calculateNetwork(v.Networks)
90+
s.SetStatistics(StatsEntry{
91+
Name: v.Name,
92+
ID: v.ID,
93+
CPUPercentage: calculateCPUPercentWindows(&v),
94+
Memory: float64(v.MemoryStats.PrivateWorkingSet),
95+
NetworkRx: netRx,
96+
NetworkTx: netTx,
97+
BlockRead: float64(v.StorageStats.ReadSizeBytes),
98+
BlockWrite: float64(v.StorageStats.WriteSizeBytes),
99+
})
108100
} else {
109-
cpuPercent = calculateCPUPercentWindows(v)
110-
blkRead = v.StorageStats.ReadSizeBytes
111-
blkWrite = v.StorageStats.WriteSizeBytes
112-
mem = float64(v.MemoryStats.PrivateWorkingSet)
101+
memUsage := calculateMemUsageUnixNoCache(v.MemoryStats)
102+
netRx, netTx := calculateNetwork(v.Networks)
103+
blkRead, blkWrite := calculateBlockIO(v.BlkioStats)
104+
s.SetStatistics(StatsEntry{
105+
Name: v.Name,
106+
ID: v.ID,
107+
CPUPercentage: calculateCPUPercentUnix(v.PreCPUStats, v.CPUStats),
108+
Memory: memUsage,
109+
MemoryPercentage: calculateMemPercentUnixNoCache(float64(v.MemoryStats.Limit), memUsage),
110+
MemoryLimit: float64(v.MemoryStats.Limit),
111+
NetworkRx: netRx,
112+
NetworkTx: netTx,
113+
BlockRead: float64(blkRead),
114+
BlockWrite: float64(blkWrite),
115+
PidsCurrent: v.PidsStats.Current,
116+
})
113117
}
114-
netRx, netTx := calculateNetwork(v.Networks)
115-
s.SetStatistics(StatsEntry{
116-
Name: v.Name,
117-
ID: v.ID,
118-
CPUPercentage: cpuPercent,
119-
Memory: mem,
120-
MemoryPercentage: memPercent,
121-
MemoryLimit: memLimit,
122-
NetworkRx: netRx,
123-
NetworkTx: netTx,
124-
BlockRead: float64(blkRead),
125-
BlockWrite: float64(blkWrite),
126-
PidsCurrent: pidsStatsCurrent,
127-
})
128118
u <- nil
129119
if !streamStats {
130120
return
@@ -165,18 +155,18 @@ func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, strea
165155
}
166156
}
167157

168-
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *container.StatsResponse) float64 {
158+
func calculateCPUPercentUnix(previousCPU container.CPUStats, curCPUStats container.CPUStats) float64 {
169159
var (
170160
cpuPercent = 0.0
171161
// calculate the change for the cpu usage of the container in between readings
172-
cpuDelta = float64(v.CPUStats.CPUUsage.TotalUsage) - float64(previousCPU)
162+
cpuDelta = float64(curCPUStats.CPUUsage.TotalUsage) - float64(previousCPU.CPUUsage.TotalUsage)
173163
// calculate the change for the entire system between readings
174-
systemDelta = float64(v.CPUStats.SystemUsage) - float64(previousSystem)
175-
onlineCPUs = float64(v.CPUStats.OnlineCPUs)
164+
systemDelta = float64(curCPUStats.SystemUsage) - float64(previousCPU.SystemUsage)
165+
onlineCPUs = float64(curCPUStats.OnlineCPUs)
176166
)
177167

178168
if onlineCPUs == 0.0 {
179-
onlineCPUs = float64(len(v.CPUStats.CPUUsage.PercpuUsage))
169+
onlineCPUs = float64(len(curCPUStats.CPUUsage.PercpuUsage))
180170
}
181171
if systemDelta > 0.0 && cpuDelta > 0.0 {
182172
cpuPercent = (cpuDelta / systemDelta) * onlineCPUs * 100.0
@@ -188,13 +178,11 @@ func calculateCPUPercentWindows(v *container.StatsResponse) float64 {
188178
// Max number of 100ns intervals between the previous time read and now
189179
possIntervals := uint64(v.Read.Sub(v.PreRead).Nanoseconds()) // Start with number of ns intervals
190180
possIntervals /= 100 // Convert to number of 100ns intervals
191-
possIntervals *= uint64(v.NumProcs) // Multiple by the number of processors
192-
193-
// Intervals used
194-
intervalsUsed := v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage
181+
possIntervals *= uint64(v.NumProcs) // Multiply by the number of processors
195182

196183
// Percentage avoiding divide-by-zero
197184
if possIntervals > 0 {
185+
intervalsUsed := v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage
198186
return float64(intervalsUsed) / float64(possIntervals) * 100.0
199187
}
200188
return 0.00

0 commit comments

Comments
 (0)