Skip to content

Commit

Permalink
Merge pull request #94041 from andyzhangx/automated-cherry-pick-of-#8…
Browse files Browse the repository at this point in the history
…3507-upstream-release-1.17

Automated cherry pick of #83507: correctly handle resetting cpuacct in a live container
  • Loading branch information
k8s-ci-robot committed Sep 3, 2020
2 parents 6ad9af2 + a91c492 commit 600dc2e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/kubelet/stats/cri_stats_provider.go
Expand Up @@ -649,7 +649,7 @@ func (p *criStatsProvider) getAndUpdateContainerUsageNanoCores(stats *runtimeapi
defer p.mutex.Unlock()

cached, ok := p.cpuUsageCache[id]
if !ok || cached.stats.UsageCoreNanoSeconds == nil {
if !ok || cached.stats.UsageCoreNanoSeconds == nil || stats.Cpu.UsageCoreNanoSeconds.Value < cached.stats.UsageCoreNanoSeconds.Value {
// Cannot compute the usage now, but update the cached stats anyway
p.cpuUsageCache[id] = &cpuUsageRecord{stats: stats.Cpu, usageNanoCores: nil}
return nil, nil
Expand Down
26 changes: 25 additions & 1 deletion pkg/kubelet/stats/cri_stats_provider_test.go
Expand Up @@ -881,8 +881,32 @@ func TestGetContainerUsageNanoCores(t *testing.T) {
},
expected: &value2,
},
{
desc: "should return nil if cpuacct is reset to 0 in a live container",
stats: &runtimeapi.ContainerStats{
Attributes: &runtimeapi.ContainerAttributes{
Id: "1",
},
Cpu: &runtimeapi.CpuUsage{
Timestamp: 2,
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
Value: 0,
},
},
},
cpuUsageCache: map[string]*cpuUsageRecord{
"1": {
stats: &runtimeapi.CpuUsage{
Timestamp: 1,
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
Value: 10000000000,
},
},
},
},
expected: nil,
},
}

for _, test := range tests {
provider := &criStatsProvider{cpuUsageCache: test.cpuUsageCache}
// Before the update, the cached value should be nil
Expand Down

0 comments on commit 600dc2e

Please sign in to comment.