mirror of
https://github.com/containers/podman.git
synced 2025-10-20 20:54:45 +08:00
[v1.6.4-rhel] stats: report correctly CPU usage
[NO NEW TESTS NEEDED} Backport of #4423 to the v1.6.4-rhel branch so that it can be included into RHEL 8.2 This fixes the was that CPU is reported from the `podman stats` command. This fix was first put into podman v1.7.0 Addresses https://bugzilla.redhat.com/show_bug.cgi?id=2060095 Signed-off-by: tomsweeneyredhat <tsweeney@redhat.com>
This commit is contained in:
@ -3,7 +3,6 @@
|
|||||||
package libpod
|
package libpod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@ -56,8 +55,8 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container
|
|||||||
}
|
}
|
||||||
|
|
||||||
previousCPU := previousStats.CPUNano
|
previousCPU := previousStats.CPUNano
|
||||||
previousSystem := previousStats.SystemNano
|
now := uint64(time.Now().UnixNano())
|
||||||
stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, previousSystem)
|
stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, now, previousStats.SystemNano)
|
||||||
stats.MemUsage = cgroupStats.Memory.Usage.Usage
|
stats.MemUsage = cgroupStats.Memory.Usage.Usage
|
||||||
stats.MemLimit = getMemLimit(cgroupStats.Memory.Usage.Limit)
|
stats.MemLimit = getMemLimit(cgroupStats.Memory.Usage.Limit)
|
||||||
stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
|
stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
|
||||||
@ -67,7 +66,7 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container
|
|||||||
}
|
}
|
||||||
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 = now
|
||||||
// Handle case where the container is not in a network namespace
|
// Handle case where the container is not in a network namespace
|
||||||
if netStats != nil {
|
if netStats != nil {
|
||||||
stats.NetInput = netStats.TxBytes
|
stats.NetInput = netStats.TxBytes
|
||||||
@ -98,20 +97,19 @@ func getMemLimit(cgroupLimit uint64) uint64 {
|
|||||||
return cgroupLimit
|
return cgroupLimit
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateCPUPercent(stats *cgroups.Metrics, previousCPU, previousSystem uint64) float64 {
|
// 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 {
|
||||||
var (
|
var (
|
||||||
cpuPercent = 0.0
|
cpuPercent = 0.0
|
||||||
cpuDelta = float64(stats.CPU.Usage.Total - previousCPU)
|
cpuDelta = float64(stats.CPU.Usage.Total - previousCPU)
|
||||||
systemDelta = float64(uint64(time.Now().UnixNano()) - previousSystem)
|
systemDelta = float64(now - previousSystem)
|
||||||
)
|
)
|
||||||
if systemDelta > 0.0 && cpuDelta > 0.0 {
|
if systemDelta > 0.0 && cpuDelta > 0.0 {
|
||||||
// gets a ratio of container cpu usage total, multiplies it by the number of cores (4 cores running
|
// gets a ratio of container cpu usage total, and multiplies that by 100 to get a percentage
|
||||||
// at 100% utilization should be 400% utilization), and multiplies that by 100 to get a percentage
|
cpuPercent = (cpuDelta / systemDelta) * 100
|
||||||
nCPUS := len(stats.CPU.Usage.PerCPU)
|
|
||||||
if nCPUS == 0 {
|
|
||||||
nCPUS = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
cpuPercent = (cpuDelta / systemDelta) * float64(nCPUS) * 100
|
|
||||||
}
|
}
|
||||||
return cpuPercent
|
return cpuPercent
|
||||||
}
|
}
|
||||||
|
@ -81,14 +81,14 @@ func (c *cpuHandler) Stat(ctr *CgroupControl, m *Metrics) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if val, found := values["usage_usec"]; found {
|
if val, found := values["usage_usec"]; found {
|
||||||
usage.Kernel, err = strconv.ParseUint(cleanString(val[0]), 10, 0)
|
usage.Total, err = strconv.ParseUint(cleanString(val[0]), 10, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
usage.Kernel *= 1000
|
usage.Kernel *= 1000
|
||||||
}
|
}
|
||||||
if val, found := values["system_usec"]; found {
|
if val, found := values["system_usec"]; found {
|
||||||
usage.Total, err = strconv.ParseUint(cleanString(val[0]), 10, 0)
|
usage.Kernel, err = strconv.ParseUint(cleanString(val[0]), 10, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user