From 094b83e18a5657f4a86e89c731d74a90cd2be037 Mon Sep 17 00:00:00 2001 From: Nayana Bidari Date: Thu, 21 Mar 2024 11:07:51 -0700 Subject: [PATCH] Fix CPUUsage() method in cgroup v2 version. CPUUsage() returns the CPU usage used in calculating the pod CPU utilization. The cgroup v1 version returns this value in nanoseconds,but the v2 version was returning in microseconds which resulted in incorrect CPU usage values when cgroupv2 was used as default. Fix this by changing the return value of CPUUsage() in cgroupv2 to nanoseconds PiperOrigin-RevId: 617903260 --- runsc/cgroup/cgroup.go | 2 +- runsc/cgroup/cgroup_v2.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/runsc/cgroup/cgroup.go b/runsc/cgroup/cgroup.go index f9bea2f86..bf43266e4 100644 --- a/runsc/cgroup/cgroup.go +++ b/runsc/cgroup/cgroup.go @@ -643,7 +643,7 @@ func (c *cgroupV1) CPUQuota() (float64, error) { return float64(quota) / float64(period), nil } -// CPUUsage returns the total CPU usage of the cgroup. +// CPUUsage returns the total CPU usage of the cgroup in nanoseconds. func (c *cgroupV1) CPUUsage() (uint64, error) { path := c.MakePath("cpuacct") usage, err := getValue(path, "cpuacct.usage") diff --git a/runsc/cgroup/cgroup_v2.go b/runsc/cgroup/cgroup_v2.go index bfc20e468..f956165c8 100644 --- a/runsc/cgroup/cgroup_v2.go +++ b/runsc/cgroup/cgroup_v2.go @@ -286,7 +286,7 @@ func parseCPUQuota(cpuMax string) (float64, error) { } -// CPUUsage returns the total CPU usage of the cgroup. +// CPUUsage returns the total CPU usage of the cgroup in nanoseconds. func (c *cgroupV2) CPUUsage() (uint64, error) { cpuStat, err := getValue(c.MakePath(""), "cpu.stat") if err != nil { @@ -300,7 +300,7 @@ func (c *cgroupV2) CPUUsage() (uint64, error) { return 0, err } if key == "usage_usec" { - return value, nil + return value * 1000, nil } }