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