1
//go:build !remote && (linux || freebsd)
8
"github.com/containers/podman/v5/libpod/define"
11
// GetContainerStats gets the running stats for a given container.
12
// The previousStats is used to correctly calculate cpu percentages. You
13
// should pass nil if there is no previous stat for this container.
14
func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*define.ContainerStats, error) {
15
stats := new(define.ContainerStats)
16
stats.ContainerID = c.ID()
19
if c.config.NoCgroups {
20
return nil, fmt.Errorf("cannot run top on container %s as it did not create a cgroup: %w", c.ID(), define.ErrNoCgroups)
26
if err := c.syncContainer(); err != nil {
31
// returns stats with the fields' default values respective of their type
32
if c.state.State != define.ContainerStateRunning && c.state.State != define.ContainerStatePaused {
36
if previousStats == nil {
37
previousStats = &define.ContainerStats{
38
// if we have no prev stats use the container start time as prev time
39
// otherwise we cannot correctly calculate the CPU percentage
40
SystemNano: uint64(c.state.StartedTime.UnixNano()),
44
netStats, err := getContainerNetIO(c)
48
stats.Network = netStats
50
if err := c.getPlatformContainerStats(stats, previousStats); err != nil {
56
// GetOnlineCPUs returns the number of online CPUs as set in the container cpu-set using sched_getaffinity
57
func GetOnlineCPUs(container *Container) (int, error) {
58
return getOnlineCPUs(container)