Skip to content

Commit

Permalink
Merge pull request #123246 from marosset/automated-cherry-pick-of-#12…
Browse files Browse the repository at this point in the history
…2999-upstream-release-1.29

Automated cherry pick of #122999: fixing issue with GetCPUUsageNanoCores on Windows
  • Loading branch information
k8s-ci-robot committed Mar 2, 2024
2 parents 95e60da + 550ae32 commit da1393a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/kubelet/winstats/perfcounter_nodestats.go
Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/kubelet/winstats/perfcounter_nodestats_test.go
Expand Up @@ -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 {
Expand Down

0 comments on commit da1393a

Please sign in to comment.