podman stats: improve cpu average calc

We can just calculate the cpu percent for the time the container is
running. There is no need to use datapoints.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2022-03-22 17:37:23 +01:00
parent 0edb3ddd39
commit 130bcc3a93
2 changed files with 2 additions and 9 deletions

View File

@ -138,7 +138,6 @@ type ContainerStats struct {
CPU float64 CPU float64
CPUNano uint64 CPUNano uint64
CPUSystemNano uint64 CPUSystemNano uint64
DataPoints int64
SystemNano uint64 SystemNano uint64
MemUsage uint64 MemUsage uint64
MemLimit uint64 MemLimit uint64

View File

@ -77,8 +77,8 @@ func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*de
stats.Duration = cgroupStats.CPU.Usage.Total stats.Duration = cgroupStats.CPU.Usage.Total
stats.UpTime = time.Duration(stats.Duration) stats.UpTime = time.Duration(stats.Duration)
stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, now, previousStats.SystemNano) stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, now, previousStats.SystemNano)
stats.AvgCPU = calculateAvgCPU(stats.CPU, previousStats.AvgCPU, previousStats.DataPoints) // calc the average cpu usage for the time the container is running
stats.DataPoints = previousStats.DataPoints + 1 stats.AvgCPU = calculateCPUPercent(cgroupStats, 0, now, uint64(c.state.StartedTime.UnixNano()))
stats.MemUsage = cgroupStats.Memory.Usage.Usage stats.MemUsage = cgroupStats.Memory.Usage.Usage
stats.MemLimit = c.getMemLimit() stats.MemLimit = c.getMemLimit()
stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100 stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
@ -156,9 +156,3 @@ func calculateBlockIO(stats *cgroups.Metrics) (read uint64, write uint64) {
} }
return return
} }
// calculateAvgCPU calculates the avg CPU percentage given the previous average and the number of data points.
func calculateAvgCPU(statsCPU float64, prevAvg float64, prevData int64) float64 {
avgPer := ((prevAvg * float64(prevData)) + statsCPU) / (float64(prevData) + 1)
return avgPer
}