Merge pull request #122999 from marosset/fix-windows-stats-cpu-nano-seconds-usage

fixing issue with GetCPUUsageNanoCores on Windows
This commit is contained in:
Kubernetes Prow Robot 2024-02-05 11:42:47 -08:00 committed by GitHub
commit 980033ee81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 3 deletions

View File

@ -250,7 +250,8 @@ func (p *perfCounterNodeStatsClient) convertCPUValue(cpuCores int, cpuValue uint
func (p *perfCounterNodeStatsClient) getCPUUsageNanoCores() uint64 {
cachePeriodSeconds := uint64(defaultCachePeriod / time.Second)
cpuUsageNanoCores := (p.cpuUsageCoreNanoSecondsCache.latestValue - p.cpuUsageCoreNanoSecondsCache.previousValue) / cachePeriodSeconds
perfCounterUpdatePeriodSeconds := uint64(perfCounterUpdatePeriod / time.Second)
cpuUsageNanoCores := ((p.cpuUsageCoreNanoSecondsCache.latestValue - p.cpuUsageCoreNanoSecondsCache.previousValue) * perfCounterUpdatePeriodSeconds) / cachePeriodSeconds
return cpuUsageNanoCores
}

View File

@ -160,14 +160,16 @@ func TestConvertCPUValue(t *testing.T) {
}
func TestGetCPUUsageNanoCores(t *testing.T) {
// Scaled expected unit test values by the frequency the CPU perf counters are polled.
perfCounterUpdatePeriodSeconds := uint64(perfCounterUpdatePeriod / time.Second)
testCases := []struct {
latestValue uint64
previousValue uint64
expected uint64
}{
{latestValue: uint64(0), previousValue: uint64(0), expected: uint64(0)},
{latestValue: uint64(2000000000), previousValue: uint64(0), expected: uint64(200000000)},
{latestValue: uint64(5000000000), previousValue: uint64(2000000000), expected: uint64(300000000)},
{latestValue: uint64(2000000000), previousValue: uint64(0), expected: uint64(200000000 * perfCounterUpdatePeriodSeconds)},
{latestValue: uint64(5000000000), previousValue: uint64(2000000000), expected: uint64(300000000 * perfCounterUpdatePeriodSeconds)},
}
for _, tc := range testCases {