Fix CPU accounting
Signed-off-by: Yann Ramin <atrus@stackworks.net>
This commit is contained in:
parent
b4a469edfe
commit
10c5dbf061
2 changed files with 6 additions and 4 deletions
|
@ -16,7 +16,7 @@ type ContainerStats struct {
|
||||||
Container string
|
Container string
|
||||||
CPU float64
|
CPU float64
|
||||||
CPUNano uint64
|
CPUNano uint64
|
||||||
SystemNano uint64
|
SystemNano int64
|
||||||
MemUsage uint64
|
MemUsage uint64
|
||||||
MemLimit uint64
|
MemLimit uint64
|
||||||
MemPerc float64
|
MemPerc float64
|
||||||
|
@ -38,6 +38,8 @@ func (c *ContainerServer) GetContainerStats(ctr *oci.Container, previousStats *C
|
||||||
cgroupStats := libcontainerStats.CgroupStats
|
cgroupStats := libcontainerStats.CgroupStats
|
||||||
stats := new(ContainerStats)
|
stats := new(ContainerStats)
|
||||||
stats.Container = ctr.ID()
|
stats.Container = ctr.ID()
|
||||||
|
stats.CPUNano = cgroupStats.CpuStats.CpuUsage.TotalUsage
|
||||||
|
stats.SystemNano = time.Now().UnixNano()
|
||||||
stats.CPU = calculateCPUPercent(libcontainerStats, previousCPU, previousSystem)
|
stats.CPU = calculateCPUPercent(libcontainerStats, previousCPU, previousSystem)
|
||||||
stats.MemUsage = cgroupStats.MemoryStats.Usage.Usage
|
stats.MemUsage = cgroupStats.MemoryStats.Usage.Usage
|
||||||
stats.MemLimit = getMemLimit(cgroupStats.MemoryStats.Usage.Limit)
|
stats.MemLimit = getMemLimit(cgroupStats.MemoryStats.Usage.Limit)
|
||||||
|
@ -84,11 +86,11 @@ func getContainerNetIO(stats *libcontainer.Stats) (received uint64, transmitted
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateCPUPercent(stats *libcontainer.Stats, previousCPU, previousSystem uint64) float64 {
|
func calculateCPUPercent(stats *libcontainer.Stats, previousCPU uint64, previousSystem int64) float64 {
|
||||||
var (
|
var (
|
||||||
cpuPercent = 0.0
|
cpuPercent = 0.0
|
||||||
cpuDelta = float64(stats.CgroupStats.CpuStats.CpuUsage.TotalUsage - previousCPU)
|
cpuDelta = float64(stats.CgroupStats.CpuStats.CpuUsage.TotalUsage - previousCPU)
|
||||||
systemDelta = float64(uint64(time.Now().UnixNano()) - previousSystem)
|
systemDelta = float64(uint64(time.Now().UnixNano()) - uint64(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, multiplies it by the number of cores (4 cores running
|
||||||
|
|
|
@ -39,7 +39,7 @@ func (s *Server) ContainerStats(ctx context.Context, req *pb.ContainerStatsReque
|
||||||
},
|
},
|
||||||
Cpu: &pb.CpuUsage{
|
Cpu: &pb.CpuUsage{
|
||||||
Timestamp: now,
|
Timestamp: now,
|
||||||
UsageCoreNanoSeconds: &pb.UInt64Value{stats.CPUNano + stats.SystemNano},
|
UsageCoreNanoSeconds: &pb.UInt64Value{stats.CPUNano},
|
||||||
},
|
},
|
||||||
Memory: &pb.MemoryUsage{
|
Memory: &pb.MemoryUsage{
|
||||||
Timestamp: now,
|
Timestamp: now,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue