diff options
author | Doug Rabson <dfr@rabson.org> | 2022-09-13 16:05:54 +0100 |
---|---|---|
committer | Doug Rabson <dfr@rabson.org> | 2022-09-14 08:29:26 +0100 |
commit | b3e978e43b3e01a68e5b8890fe79be64eb976e65 (patch) | |
tree | 58e1509c7bd88a4cbe2e3d6d042632b4c356012e /libpod/stats_common.go | |
parent | 47bd9e8110b4e2de04fffa27a16c84e24921cd69 (diff) | |
download | podman-b3e978e43b3e01a68e5b8890fe79be64eb976e65.tar.gz podman-b3e978e43b3e01a68e5b8890fe79be64eb976e65.tar.bz2 podman-b3e978e43b3e01a68e5b8890fe79be64eb976e65.zip |
libpod: Split out the common code from GetContainerStats
This moves the cgroups code to a new method getPlatformContainerStats.
[NO NEW TESTS NEEDED]
Signed-off-by: Doug Rabson <dfr@rabson.org>
Diffstat (limited to 'libpod/stats_common.go')
-rw-r--r-- | libpod/stats_common.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/libpod/stats_common.go b/libpod/stats_common.go new file mode 100644 index 000000000..122160bda --- /dev/null +++ b/libpod/stats_common.go @@ -0,0 +1,49 @@ +//go:build linux || freebsd +// +build linux freebsd + +package libpod + +import ( + "fmt" + + "github.com/containers/podman/v4/libpod/define" +) + +// GetContainerStats gets the running stats for a given container. +// The previousStats is used to correctly calculate cpu percentages. You +// should pass nil if there is no previous stat for this container. +func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*define.ContainerStats, error) { + stats := new(define.ContainerStats) + stats.ContainerID = c.ID() + stats.Name = c.Name() + + if c.config.NoCgroups { + return nil, fmt.Errorf("cannot run top on container %s as it did not create a cgroup: %w", c.ID(), define.ErrNoCgroups) + } + + if !c.batched { + c.lock.Lock() + defer c.lock.Unlock() + if err := c.syncContainer(); err != nil { + return stats, err + } + } + + // returns stats with the fields' default values respective of their type + if c.state.State != define.ContainerStateRunning && c.state.State != define.ContainerStatePaused { + return stats, nil + } + + if previousStats == nil { + previousStats = &define.ContainerStats{ + // if we have no prev stats use the container start time as prev time + // otherwise we cannot correctly calculate the CPU percentage + SystemNano: uint64(c.state.StartedTime.UnixNano()), + } + } + + if err := c.getPlatformContainerStats(stats, previousStats); err != nil { + return nil, err + } + return stats, nil +} |