10
"github.com/containers/podman/v5/libpod/define"
11
"github.com/containers/podman/v5/pkg/rctl"
12
"github.com/containers/storage/pkg/system"
13
"github.com/sirupsen/logrus"
16
// getPlatformContainerStats gets the platform-specific running stats
17
// for a given container. The previousStats is used to correctly
18
// calculate cpu percentages. You should pass nil if there is no
19
// previous stat for this container.
20
func (c *Container) getPlatformContainerStats(stats *define.ContainerStats, previousStats *define.ContainerStats) error {
21
now := uint64(time.Now().UnixNano())
23
jailName, err := c.jailName()
25
return fmt.Errorf("getting jail name: %w", err)
28
entries, err := rctl.GetRacct("jail:" + jailName)
30
return fmt.Errorf("unable to read accounting for %s: %w", jailName, err)
33
// If the current total usage is less than what was previously
34
// recorded then it means the container was restarted and runs
36
if dur, ok := entries["wallclock"]; ok {
37
if previousStats.Duration > dur*1000000000 {
38
previousStats = &define.ContainerStats{}
42
for key, val := range entries {
44
case "cputime": // CPU time, in seconds
45
stats.CPUNano = val * 1000000000
46
stats.AvgCPU = calculateCPUPercent(stats.CPUNano, 0, now, uint64(c.state.StartedTime.UnixNano()))
47
case "datasize": // data size, in bytes
48
case "stacksize": // stack size, in bytes
49
case "coredumpsize": // core dump size, in bytes
50
case "memoryuse": // resident set size, in bytes
52
case "memorylocked": // locked memory, in bytes
53
case "maxproc": // number of processes
55
case "openfiles": // file descriptor table size
56
case "vmemoryuse": // address space limit, in bytes
57
case "pseudoterminals": // number of PTYs
58
case "swapuse": // swap space that may be reserved or used, in bytes
59
case "nthr": // number of threads
60
case "msgqqueued": // number of queued SysV messages
61
case "msgqsize": // SysV message queue size, in bytes
62
case "nmsgq": // number of SysV message queues
63
case "nsem": // number of SysV semaphores
64
case "nsemop": // number of SysV semaphores modified in a single semop(2) call
65
case "nshm": // number of SysV shared memory segments
66
case "shmsize": // SysV shared memory size, in bytes
67
case "wallclock": // wallclock time, in seconds
68
stats.Duration = val * 1000000000
69
stats.UpTime = time.Duration(stats.Duration)
70
case "pcpu": // %CPU, in percents of a single CPU core
71
stats.CPU = float64(val)
72
case "readbps": // filesystem reads, in bytes per second
73
stats.BlockInput = val
74
case "writebps": // filesystem writes, in bytes per second
75
stats.BlockOutput = val
76
case "readiops": // filesystem reads, in operations per second
77
case "writeiops": // filesystem writes, in operations per second
80
stats.MemLimit = c.getMemLimit()
81
stats.SystemNano = now
86
// getMemLimit returns the memory limit for a container
87
func (c *Container) getMemLimit() uint64 {
88
memLimit := uint64(math.MaxUint64)
90
resources := c.LinuxResources()
91
if resources != nil && resources.Memory != nil && resources.Memory.Limit != nil {
92
memLimit = uint64(*resources.Memory.Limit)
95
mi, err := system.ReadMemInfo()
97
logrus.Errorf("ReadMemInfo error: %v", err)
102
physicalLimit := uint64(mi.MemTotal)
104
if memLimit <= 0 || memLimit > physicalLimit {
111
// calculateCPUPercent calculates the cpu usage using the latest measurement in stats.
112
// previousCPU is the last value of stats.CPU.Usage.Total measured at the time previousSystem.
114
// (now - previousSystem) is the time delta in nanoseconds, between the measurement in previousCPU
116
// and the updated value in stats.
117
func calculateCPUPercent(currentCPU, previousCPU, now, previousSystem uint64) float64 {
120
cpuDelta = float64(currentCPU - previousCPU)
121
systemDelta = float64(now - previousSystem)
123
if systemDelta > 0.0 && cpuDelta > 0.0 {
124
// gets a ratio of container cpu usage total, and multiplies that by 100 to get a percentage
125
cpuPercent = (cpuDelta / systemDelta) * 100
130
func getOnlineCPUs(container *Container) (int, error) {