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 <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano
2021-09-09 17:16:24 +02:00
parent 2b85382ca1
commit 50688da29b

View File

@ -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