From 7304bd6808cfe76315fe288e99fb83672f685657 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 21 Jan 2015 13:42:31 -0800 Subject: [PATCH] Zero out stats values in the cli Based on some feedback, when you have a container via the cli that you are monitoring for stats, if you stop the container it will stay in the display but report the last datapoint that was received. This PR changes the display to zero out the values for containers where an update has not been received within a specified duration, i.e. 2 seconds. This signals the user that the container has stopped as it reports cpu and memory usage of 0. Signed-off-by: Michael Crosby Upstream-commit: bbc38497b6f522b0a9c4e5e976abfd79692b8b5f Component: engine --- components/engine/api/client/commands.go | 66 +++++++++++++++--------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/components/engine/api/client/commands.go b/components/engine/api/client/commands.go index 7d0023d1a1..e42c6f26ed 100644 --- a/components/engine/api/client/commands.go +++ b/components/engine/api/client/commands.go @@ -2641,34 +2641,54 @@ func (s *containerStats) Collect(stream io.ReadCloser) { previousSystem uint64 start = true dec = json.NewDecoder(stream) + u = make(chan error, 1) ) - for { - var v *stats.Stats - if err := dec.Decode(&v); err != nil { + go func() { + for { + var v *stats.Stats + if err := dec.Decode(&v); err != nil { + u <- err + return + } + var ( + memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0 + cpuPercent = 0.0 + ) + if !start { + cpuPercent = calcuateCpuPercent(previousCpu, previousSystem, v) + } + start = false s.mu.Lock() - s.err = err + s.CpuPercentage = cpuPercent + s.Memory = float64(v.MemoryStats.Usage) + s.MemoryLimit = float64(v.MemoryStats.Limit) + s.MemoryPercentage = memPercent + s.NetworkRx = float64(v.Network.RxBytes) + s.NetworkTx = float64(v.Network.TxBytes) s.mu.Unlock() - return + previousCpu = v.CpuStats.CpuUsage.TotalUsage + previousSystem = v.CpuStats.SystemUsage + u <- nil } - var ( - memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0 - cpuPercent = 0.0 - ) - if !start { - cpuPercent = calcuateCpuPercent(previousCpu, previousSystem, v) + }() + for { + select { + case <-time.After(2 * time.Second): + // zero out the values if we have not received an update within + // the specified duration. + s.mu.Lock() + s.CpuPercentage = 0 + s.Memory = 0 + s.MemoryPercentage = 0 + s.mu.Unlock() + case err := <-u: + if err != nil { + s.mu.Lock() + s.err = err + s.mu.Unlock() + return + } } - start = false - s.mu.Lock() - s.CpuPercentage = cpuPercent - s.Memory = float64(v.MemoryStats.Usage) - s.MemoryLimit = float64(v.MemoryStats.Limit) - s.MemoryPercentage = memPercent - s.NetworkRx = float64(v.Network.RxBytes) - s.NetworkTx = float64(v.Network.TxBytes) - s.mu.Unlock() - - previousCpu = v.CpuStats.CpuUsage.TotalUsage - previousSystem = v.CpuStats.SystemUsage } }