From 53dc99fa609b5a18458e27b99df546df9095b5d0 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Thu, 9 Sep 2021 16:56:33 +0200 Subject: stats: allow to read stats for paused containers paused containers still a cgroup we can use to grab the stats. Signed-off-by: Giuseppe Scrivano --- libpod/stats.go | 4 ++-- test/e2e/pause_test.go | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/libpod/stats.go b/libpod/stats.go index 6f0360ef1..2532b35c2 100644 --- a/libpod/stats.go +++ b/libpod/stats.go @@ -30,7 +30,7 @@ func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*de } } - if c.state.State != define.ContainerStateRunning { + if c.state.State != define.ContainerStateRunning && c.state.State != define.ContainerStatePaused { return stats, define.ErrCtrStateInvalid } @@ -65,7 +65,7 @@ func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*de stats.MemLimit = getMemLimit(cgroupStats.Memory.Usage.Limit) stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100 stats.PIDs = 0 - if conState == define.ContainerStateRunning { + if conState == define.ContainerStateRunning || conState == define.ContainerStatePaused { stats.PIDs = cgroupStats.Pids.Current } stats.BlockInput, stats.BlockOutput = calculateBlockIO(cgroupStats) diff --git a/test/e2e/pause_test.go b/test/e2e/pause_test.go index ea7a96428..2e5e07de9 100644 --- a/test/e2e/pause_test.go +++ b/test/e2e/pause_test.go @@ -79,6 +79,11 @@ var _ = Describe("Podman pause", func() { Expect(result).To(ExitWithError()) Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0)) Expect(strings.ToLower(podmanTest.GetContainerStatus())).To(ContainSubstring(createdState)) + + // check we can read stats for a paused container + result = podmanTest.Podman([]string{"stats", "--no-stream", cid}) + result.WaitWithDefaultTimeout() + Expect(result).To(ExitWithError()) }) It("podman pause a running container by id", func() { -- cgit v1.2.3-54-g00ecf From 2b85382ca187fdcc000803b1d26d8b46e8cb755f Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Thu, 9 Sep 2021 17:00:27 +0200 Subject: api: correctly set the container stats override the outer scope variable instead of creating a local one. Otherwise the wrong variable would be used for the next iterations. Signed-off-by: Giuseppe Scrivano --- pkg/api/handlers/compat/containers_stats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/api/handlers/compat/containers_stats.go b/pkg/api/handlers/compat/containers_stats.go index 851955207..b95fbfdd8 100644 --- a/pkg/api/handlers/compat/containers_stats.go +++ b/pkg/api/handlers/compat/containers_stats.go @@ -97,7 +97,7 @@ streamLabel: // A label to flatten the scope default: // Container stats - stats, err := ctnr.GetContainerStats(stats) + stats, err = ctnr.GetContainerStats(stats) if err != nil { logrus.Errorf("Unable to get container stats: %v", err) return -- cgit v1.2.3-54-g00ecf From 50688da29b4a37006d0a7fafc25aca1a6447c7d9 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Thu, 9 Sep 2021 17:16:24 +0200 Subject: stats: detect containers restart if the current cpu usage time is lower than what previously recorded, then it means the container was restarted and now it runs in a new cgroup. When this happens, reset the prevStats. Closes: https://github.com/containers/podman/issues/11469 Signed-off-by: Giuseppe Scrivano --- libpod/stats.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libpod/stats.go b/libpod/stats.go index 2532b35c2..975152535 100644 --- a/libpod/stats.go +++ b/libpod/stats.go @@ -54,6 +54,12 @@ func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*de return nil, err } + // If the current total usage in the cgroup is less than what was previously + // recorded then it means the container was restarted and runs in a new cgroup + if previousStats.Duration > cgroupStats.CPU.Usage.Total { + previousStats = &define.ContainerStats{} + } + previousCPU := previousStats.CPUNano now := uint64(time.Now().UnixNano()) stats.Duration = cgroupStats.CPU.Usage.Total -- cgit v1.2.3-54-g00ecf