2018-06-20 18:23:24 +00:00
|
|
|
// +build linux
|
|
|
|
|
2017-12-04 21:06:06 +00:00
|
|
|
package libpod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2020-07-28 12:23:45 +00:00
|
|
|
"github.com/containers/podman/v2/libpod/define"
|
|
|
|
"github.com/containers/podman/v2/pkg/cgroups"
|
2017-12-04 21:06:06 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetContainerStats gets the running stats for a given container
|
2020-05-04 19:10:30 +00:00
|
|
|
func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*define.ContainerStats, error) {
|
|
|
|
stats := new(define.ContainerStats)
|
2018-01-18 20:54:16 +00:00
|
|
|
stats.ContainerID = c.ID()
|
2018-01-23 21:50:20 +00:00
|
|
|
stats.Name = c.Name()
|
2018-08-09 21:36:10 +00:00
|
|
|
|
2019-07-15 20:44:56 +00:00
|
|
|
if c.config.NoCgroups {
|
|
|
|
return nil, errors.Wrapf(define.ErrNoCgroups, "cannot run top on container %s as it did not create a cgroup", c.ID())
|
|
|
|
}
|
|
|
|
|
2018-08-09 21:36:10 +00:00
|
|
|
if !c.batched {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
if err := c.syncContainer(); err != nil {
|
|
|
|
return stats, err
|
|
|
|
}
|
2017-12-04 21:06:06 +00:00
|
|
|
}
|
2018-08-09 21:36:10 +00:00
|
|
|
|
2019-06-25 13:40:19 +00:00
|
|
|
if c.state.State != define.ContainerStateRunning {
|
2019-06-24 20:48:34 +00:00
|
|
|
return stats, define.ErrCtrStateInvalid
|
2018-01-18 20:54:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:46:11 +00:00
|
|
|
cgroupPath, err := c.cGroupPath()
|
2017-12-04 21:06:06 +00:00
|
|
|
if err != nil {
|
2018-03-21 15:28:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-19 09:13:18 +00:00
|
|
|
cgroup, err := cgroups.Load(cgroupPath)
|
2018-03-21 15:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return stats, errors.Wrapf(err, "unable to load cgroup at %s", cgroupPath)
|
2017-12-04 21:06:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:10:46 +00:00
|
|
|
// Ubuntu does not have swap memory in cgroups because swap is often not enabled.
|
2019-06-19 09:13:18 +00:00
|
|
|
cgroupStats, err := cgroup.Stat()
|
2017-12-04 21:06:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return stats, errors.Wrapf(err, "unable to obtain cgroup stats")
|
|
|
|
}
|
|
|
|
conState := c.state.State
|
2018-02-28 16:02:54 +00:00
|
|
|
netStats, err := getContainerNetIO(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:06:06 +00:00
|
|
|
previousCPU := previousStats.CPUNano
|
2019-11-01 19:52:08 +00:00
|
|
|
now := uint64(time.Now().UnixNano())
|
|
|
|
stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, now, previousStats.SystemNano)
|
2017-12-04 21:06:06 +00:00
|
|
|
stats.MemUsage = cgroupStats.Memory.Usage.Usage
|
|
|
|
stats.MemLimit = getMemLimit(cgroupStats.Memory.Usage.Limit)
|
|
|
|
stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
|
|
|
|
stats.PIDs = 0
|
2019-06-25 13:40:19 +00:00
|
|
|
if conState == define.ContainerStateRunning {
|
2018-02-28 16:02:54 +00:00
|
|
|
stats.PIDs = cgroupStats.Pids.Current
|
2017-12-04 21:06:06 +00:00
|
|
|
}
|
|
|
|
stats.BlockInput, stats.BlockOutput = calculateBlockIO(cgroupStats)
|
2018-02-01 18:30:55 +00:00
|
|
|
stats.CPUNano = cgroupStats.CPU.Usage.Total
|
2020-02-11 18:48:23 +00:00
|
|
|
stats.CPUSystemNano = cgroupStats.CPU.Usage.Kernel
|
2019-11-01 19:52:08 +00:00
|
|
|
stats.SystemNano = now
|
2020-02-11 18:48:23 +00:00
|
|
|
stats.PerCPU = cgroupStats.CPU.Usage.PerCPU
|
2018-08-20 22:24:35 +00:00
|
|
|
// Handle case where the container is not in a network namespace
|
|
|
|
if netStats != nil {
|
|
|
|
stats.NetInput = netStats.TxBytes
|
|
|
|
stats.NetOutput = netStats.RxBytes
|
|
|
|
} else {
|
|
|
|
stats.NetInput = 0
|
|
|
|
stats.NetOutput = 0
|
|
|
|
}
|
2018-02-28 16:02:54 +00:00
|
|
|
|
2017-12-04 21:06:06 +00:00
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMemory limit returns the memory limit for a given cgroup
|
|
|
|
// If the configured memory limit is larger than the total memory on the sys, the
|
|
|
|
// physical system memory size is returned
|
|
|
|
func getMemLimit(cgroupLimit uint64) uint64 {
|
|
|
|
si := &syscall.Sysinfo_t{}
|
|
|
|
err := syscall.Sysinfo(si)
|
|
|
|
if err != nil {
|
|
|
|
return cgroupLimit
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:42:52 +00:00
|
|
|
//nolint:unconvert
|
|
|
|
physicalLimit := uint64(si.Totalram)
|
2017-12-04 21:06:06 +00:00
|
|
|
if cgroupLimit > physicalLimit {
|
|
|
|
return physicalLimit
|
|
|
|
}
|
|
|
|
return cgroupLimit
|
|
|
|
}
|
|
|
|
|
2019-11-01 19:52:08 +00:00
|
|
|
// calculateCPUPercent calculates the cpu usage using the latest measurement in stats.
|
|
|
|
// previousCPU is the last value of stats.CPU.Usage.Total measured at the time previousSystem.
|
|
|
|
// (now - previousSystem) is the time delta in nanoseconds, between the measurement in previousCPU
|
|
|
|
// and the updated value in stats.
|
|
|
|
func calculateCPUPercent(stats *cgroups.Metrics, previousCPU, now, previousSystem uint64) float64 {
|
2017-12-04 21:06:06 +00:00
|
|
|
var (
|
|
|
|
cpuPercent = 0.0
|
2018-02-01 18:30:55 +00:00
|
|
|
cpuDelta = float64(stats.CPU.Usage.Total - previousCPU)
|
2019-11-01 19:52:08 +00:00
|
|
|
systemDelta = float64(now - previousSystem)
|
2017-12-04 21:06:06 +00:00
|
|
|
)
|
|
|
|
if systemDelta > 0.0 && cpuDelta > 0.0 {
|
2019-11-01 19:52:08 +00:00
|
|
|
// gets a ratio of container cpu usage total, and multiplies that by 100 to get a percentage
|
|
|
|
cpuPercent = (cpuDelta / systemDelta) * 100
|
2017-12-04 21:06:06 +00:00
|
|
|
}
|
|
|
|
return cpuPercent
|
|
|
|
}
|
|
|
|
|
2018-02-01 18:30:55 +00:00
|
|
|
func calculateBlockIO(stats *cgroups.Metrics) (read uint64, write uint64) {
|
2017-12-04 21:06:06 +00:00
|
|
|
for _, blkIOEntry := range stats.Blkio.IoServiceBytesRecursive {
|
|
|
|
switch strings.ToLower(blkIOEntry.Op) {
|
|
|
|
case "read":
|
|
|
|
read += blkIOEntry.Value
|
|
|
|
case "write":
|
|
|
|
write += blkIOEntry.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|