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
This commit is contained in:
Nayana Bidari
2024-03-21 11:10:56 -07:00
committed by gVisor bot
parent 53f976dede
commit 094b83e18a
2 changed files with 3 additions and 3 deletions
+1 -1
View File
@@ -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")
+2 -2
View File
@@ -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
}
}