summaryrefslogtreecommitdiff
path: root/libpod/stats.go
diff options
context:
space:
mode:
authorcdoern <cbdoer23@g.holycross.edu>2021-06-11 08:35:01 -0400
committercdoern <cbdoer23@g.holycross.edu>2021-06-23 09:23:40 -0400
commitf26fa5392110c2b59cb17d4f1013cf6bb4ecd873 (patch)
tree2288c6e882a5a68b1746f543a1c367d6a2bddff2 /libpod/stats.go
parent2970e3518cc95910444903f572418f5887316e47 (diff)
downloadpodman-f26fa5392110c2b59cb17d4f1013cf6bb4ecd873.tar.gz
podman-f26fa5392110c2b59cb17d4f1013cf6bb4ecd873.tar.bz2
podman-f26fa5392110c2b59cb17d4f1013cf6bb4ecd873.zip
Podman Stats additional features
added Avg Cpu calculation and CPU up time to podman stats. Adding different feature sets in different PRs, CPU first. resolves #9258 Signed-off-by: cdoern <cbdoer23@g.holycross.edu>
Diffstat (limited to 'libpod/stats.go')
-rw-r--r--libpod/stats.go10
1 files changed, 10 insertions, 0 deletions
diff --git a/libpod/stats.go b/libpod/stats.go
index f4732b4fc..6f0360ef1 100644
--- a/libpod/stats.go
+++ b/libpod/stats.go
@@ -56,7 +56,11 @@ func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*de
previousCPU := previousStats.CPUNano
now := uint64(time.Now().UnixNano())
+ stats.Duration = cgroupStats.CPU.Usage.Total
+ stats.UpTime = time.Duration(stats.Duration)
stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, now, previousStats.SystemNano)
+ stats.AvgCPU = calculateAvgCPU(stats.CPU, previousStats.AvgCPU, previousStats.DataPoints)
+ stats.DataPoints = previousStats.DataPoints + 1
stats.MemUsage = cgroupStats.Memory.Usage.Usage
stats.MemLimit = getMemLimit(cgroupStats.Memory.Usage.Limit)
stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
@@ -127,3 +131,9 @@ func calculateBlockIO(stats *cgroups.Metrics) (read uint64, write uint64) {
}
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
+}