mirror of
https://github.com/containers/podman.git
synced 2025-07-15 03:02:52 +08:00
podman stats add networking
Add networking information to podman stats output. Also correct an issue filed where memory constraints of the cgroup were not reflected in the stats output. And finally, fix issue with PID count. Resolves issue #364 Signed-off-by: baude <bbaude@redhat.com> Closes: #417 Approved by: mheon
This commit is contained in:
@ -632,7 +632,7 @@ func (c *Container) NamespacePath(ns LinuxNS) (string, error) {
|
|||||||
|
|
||||||
// CGroupPath returns a cgroups "path" for a given container.
|
// CGroupPath returns a cgroups "path" for a given container.
|
||||||
func (c *Container) CGroupPath() cgroups.Path {
|
func (c *Container) CGroupPath() cgroups.Path {
|
||||||
return cgroups.StaticPath(filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID())))
|
return cgroups.StaticPath(filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s/%s", c.ID(), c.ID())))
|
||||||
}
|
}
|
||||||
|
|
||||||
// RootFsSize returns the root FS size of the container
|
// RootFsSize returns the root FS size of the container
|
||||||
|
@ -6,8 +6,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containerd/cgroups"
|
"github.com/containerd/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer"
|
"github.com/containernetworking/plugins/pkg/ns"
|
||||||
|
"github.com/cri-o/ocicni/pkg/ocicni"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/vishvananda/netlink"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ContainerStats contains the statistics information for a running container
|
// ContainerStats contains the statistics information for a running container
|
||||||
@ -55,6 +57,11 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container
|
|||||||
return stats, errors.Wrapf(err, "unable to determine container state")
|
return stats, errors.Wrapf(err, "unable to determine container state")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
netStats, err := getContainerNetIO(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
previousCPU := previousStats.CPUNano
|
previousCPU := previousStats.CPUNano
|
||||||
previousSystem := previousStats.SystemNano
|
previousSystem := previousStats.SystemNano
|
||||||
stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, previousSystem)
|
stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, previousSystem)
|
||||||
@ -63,13 +70,14 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container
|
|||||||
stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
|
stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
|
||||||
stats.PIDs = 0
|
stats.PIDs = 0
|
||||||
if conState == ContainerStateRunning {
|
if conState == ContainerStateRunning {
|
||||||
stats.PIDs = cgroupStats.Pids.Current - 1
|
stats.PIDs = cgroupStats.Pids.Current
|
||||||
}
|
}
|
||||||
stats.BlockInput, stats.BlockOutput = calculateBlockIO(cgroupStats)
|
stats.BlockInput, stats.BlockOutput = calculateBlockIO(cgroupStats)
|
||||||
stats.CPUNano = cgroupStats.CPU.Usage.Total
|
stats.CPUNano = cgroupStats.CPU.Usage.Total
|
||||||
stats.SystemNano = cgroupStats.CPU.Usage.Kernel
|
stats.SystemNano = cgroupStats.CPU.Usage.Kernel
|
||||||
// TODO Figure out where to get the Netout stuff.
|
stats.NetInput = netStats.TxBytes
|
||||||
//stats.NetInput, stats.NetOutput = getContainerNetIO(cgroupStats)
|
stats.NetOutput = netStats.RxBytes
|
||||||
|
|
||||||
return stats, nil
|
return stats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,13 +98,17 @@ func getMemLimit(cgroupLimit uint64) uint64 {
|
|||||||
return cgroupLimit
|
return cgroupLimit
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the total number of bytes transmitted and received for the given container stats
|
func getContainerNetIO(ctr *Container) (*netlink.LinkStatistics, error) {
|
||||||
func getContainerNetIO(stats *libcontainer.Stats) (received uint64, transmitted uint64) { //nolint
|
var netStats *netlink.LinkStatistics
|
||||||
for _, iface := range stats.Interfaces {
|
err := ns.WithNetNSPath(ctr.state.NetNS.Path(), func(_ ns.NetNS) error {
|
||||||
received += iface.RxBytes
|
link, err := netlink.LinkByName(ocicni.DefaultInterfaceName)
|
||||||
transmitted += iface.TxBytes
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return
|
netStats = link.Attrs().Statistics
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return netStats, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateCPUPercent(stats *cgroups.Metrics, previousCPU, previousSystem uint64) float64 {
|
func calculateCPUPercent(stats *cgroups.Metrics, previousCPU, previousSystem uint64) float64 {
|
||||||
|
Reference in New Issue
Block a user