Skip to content

Commit f6677a4

Browse files
committed
Cumulative stats can't decrease
During removal of the container a stat value might be reported as zero; in this case the caluclation could end up with an extremely large number. If the cumulative stat decreases report zero. Signed-off-by: James Sturtevant <[email protected]>
1 parent 283149d commit f6677a4

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

internal/cri/server/container_stats_list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ func (c *criService) getUsageNanoCores(containerID string, isSandbox bool, curre
224224
return 0, nil
225225
}
226226

227+
// can't go backwards, this value might come in as 0 if the container was just removed
228+
if currentUsageCoreNanoSeconds < oldStats.UsageCoreNanoSeconds {
229+
return 0, nil
230+
}
231+
227232
newUsageNanoCores := uint64(float64(currentUsageCoreNanoSeconds-oldStats.UsageCoreNanoSeconds) /
228233
float64(nanoSeconds) * float64(time.Second/time.Nanosecond))
229234

internal/cri/server/container_stats_list_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,57 @@ import (
3535
func TestContainerMetricsCPUNanoCoreUsage(t *testing.T) {
3636
c := newTestCRIService()
3737
timestamp := time.Now()
38-
secondAfterTimeStamp := timestamp.Add(time.Second)
39-
ID := "ID"
38+
tenSecondAftertimeStamp := timestamp.Add(time.Second * 10)
4039

4140
for _, test := range []struct {
41+
id string
4242
desc string
4343
firstCPUValue uint64
4444
secondCPUValue uint64
4545
expectedNanoCoreUsageFirst uint64
4646
expectedNanoCoreUsageSecond uint64
4747
}{
4848
{
49+
id: "id1",
4950
desc: "metrics",
5051
firstCPUValue: 50,
5152
secondCPUValue: 500,
5253
expectedNanoCoreUsageFirst: 0,
53-
expectedNanoCoreUsageSecond: 450,
54+
expectedNanoCoreUsageSecond: 45,
55+
},
56+
{
57+
id: "id2",
58+
desc: "metrics",
59+
firstCPUValue: 234235,
60+
secondCPUValue: 0,
61+
expectedNanoCoreUsageFirst: 0,
62+
expectedNanoCoreUsageSecond: 0,
5463
},
5564
} {
5665
test := test
5766
t.Run(test.desc, func(t *testing.T) {
5867
container, err := containerstore.NewContainer(
59-
containerstore.Metadata{ID: ID},
68+
containerstore.Metadata{ID: test.id},
6069
)
6170
assert.NoError(t, err)
6271
assert.Nil(t, container.Stats)
6372
err = c.containerStore.Add(container)
6473
assert.NoError(t, err)
6574

66-
cpuUsage, err := c.getUsageNanoCores(ID, false, test.firstCPUValue, timestamp)
75+
cpuUsage, err := c.getUsageNanoCores(test.id, false, test.firstCPUValue, timestamp)
6776
assert.NoError(t, err)
6877

69-
container, err = c.containerStore.Get(ID)
78+
container, err = c.containerStore.Get(test.id)
7079
assert.NoError(t, err)
7180
assert.NotNil(t, container.Stats)
7281

7382
assert.Equal(t, test.expectedNanoCoreUsageFirst, cpuUsage)
7483

75-
cpuUsage, err = c.getUsageNanoCores(ID, false, test.secondCPUValue, secondAfterTimeStamp)
84+
cpuUsage, err = c.getUsageNanoCores(test.id, false, test.secondCPUValue, tenSecondAftertimeStamp)
7685
assert.NoError(t, err)
7786
assert.Equal(t, test.expectedNanoCoreUsageSecond, cpuUsage)
7887

79-
container, err = c.containerStore.Get(ID)
88+
container, err = c.containerStore.Get(test.id)
8089
assert.NoError(t, err)
8190
assert.NotNil(t, container.Stats)
8291
})

0 commit comments

Comments
 (0)