Skip to content

Commit 462204d

Browse files
committed
Omit Failcnt in json
I don't see why this should be special, it feels wired in some stat sections such as kernel memory and swap memory which have only Failcnt with 0 value. And this also added unit test cases for failcnt and oom control. Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
1 parent eb4da49 commit 462204d

2 files changed

Lines changed: 93 additions & 5 deletions

File tree

libcontainer/cgroups/fs/memory_test.go

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212
const (
1313
memoryStatContents = `cache 512
1414
rss 1024`
15-
memoryUsageContents = "2048\n"
16-
memoryMaxUsageContents = "4096\n"
17-
memoryFailcnt = "100\n"
15+
memoryUsageContents = "2048\n"
16+
memoryMaxUsageContents = "4096\n"
17+
memoryFailcnt = "100\n"
18+
memoryOomControlContents = `oom_kill_disable 1
19+
under_oom 0`
1820
)
1921

2022
func TestMemorySetMemory(t *testing.T) {
@@ -173,6 +175,8 @@ func TestMemoryStatsNoStatFile(t *testing.T) {
173175
helper.writeFileContents(map[string]string{
174176
"memory.usage_in_bytes": memoryUsageContents,
175177
"memory.max_usage_in_bytes": memoryMaxUsageContents,
178+
"memory.failcnt": memoryFailcnt,
179+
"memory.oom_control": memoryOomControlContents,
176180
})
177181

178182
memory := &MemoryGroup{}
@@ -189,6 +193,8 @@ func TestMemoryStatsNoUsageFile(t *testing.T) {
189193
helper.writeFileContents(map[string]string{
190194
"memory.stat": memoryStatContents,
191195
"memory.max_usage_in_bytes": memoryMaxUsageContents,
196+
"memory.failcnt": memoryFailcnt,
197+
"memory.oom_control": memoryOomControlContents,
192198
})
193199

194200
memory := &MemoryGroup{}
@@ -205,6 +211,8 @@ func TestMemoryStatsNoMaxUsageFile(t *testing.T) {
205211
helper.writeFileContents(map[string]string{
206212
"memory.stat": memoryStatContents,
207213
"memory.usage_in_bytes": memoryUsageContents,
214+
"memory.failcnt": memoryFailcnt,
215+
"memory.oom_control": memoryOomControlContents,
208216
})
209217

210218
memory := &MemoryGroup{}
@@ -215,13 +223,51 @@ func TestMemoryStatsNoMaxUsageFile(t *testing.T) {
215223
}
216224
}
217225

226+
func TestMemoryStatsNoFailcntFile(t *testing.T) {
227+
helper := NewCgroupTestUtil("memory", t)
228+
defer helper.cleanup()
229+
helper.writeFileContents(map[string]string{
230+
"memory.stat": memoryStatContents,
231+
"memory.usage_in_bytes": memoryUsageContents,
232+
"memory.max_usage_in_bytes": memoryMaxUsageContents,
233+
"memory.oom_control": memoryOomControlContents,
234+
})
235+
236+
memory := &MemoryGroup{}
237+
actualStats := *cgroups.NewStats()
238+
err := memory.GetStats(helper.CgroupPath, &actualStats)
239+
if err == nil {
240+
t.Fatal("Expected failure")
241+
}
242+
}
243+
244+
func TestMemoryStatsNoOomControlFile(t *testing.T) {
245+
helper := NewCgroupTestUtil("memory", t)
246+
defer helper.cleanup()
247+
helper.writeFileContents(map[string]string{
248+
"memory.stat": memoryStatContents,
249+
"memory.usage_in_bytes": memoryUsageContents,
250+
"memory.max_usage_in_bytes": memoryMaxUsageContents,
251+
"memory.failcnt": memoryFailcnt,
252+
})
253+
254+
memory := &MemoryGroup{}
255+
actualStats := *cgroups.NewStats()
256+
err := memory.GetStats(helper.CgroupPath, &actualStats)
257+
if err != nil {
258+
t.Fatal(err)
259+
}
260+
}
261+
218262
func TestMemoryStatsBadStatFile(t *testing.T) {
219263
helper := NewCgroupTestUtil("memory", t)
220264
defer helper.cleanup()
221265
helper.writeFileContents(map[string]string{
222266
"memory.stat": "rss rss",
223267
"memory.usage_in_bytes": memoryUsageContents,
224268
"memory.max_usage_in_bytes": memoryMaxUsageContents,
269+
"memory.failcnt": memoryFailcnt,
270+
"memory.oom_control": memoryOomControlContents,
225271
})
226272

227273
memory := &MemoryGroup{}
@@ -239,6 +285,8 @@ func TestMemoryStatsBadUsageFile(t *testing.T) {
239285
"memory.stat": memoryStatContents,
240286
"memory.usage_in_bytes": "bad",
241287
"memory.max_usage_in_bytes": memoryMaxUsageContents,
288+
"memory.failcnt": memoryFailcnt,
289+
"memory.oom_control": memoryOomControlContents,
242290
})
243291

244292
memory := &MemoryGroup{}
@@ -256,6 +304,46 @@ func TestMemoryStatsBadMaxUsageFile(t *testing.T) {
256304
"memory.stat": memoryStatContents,
257305
"memory.usage_in_bytes": memoryUsageContents,
258306
"memory.max_usage_in_bytes": "bad",
307+
"memory.failcnt": memoryFailcnt,
308+
"memory.oom_control": memoryOomControlContents,
309+
})
310+
311+
memory := &MemoryGroup{}
312+
actualStats := *cgroups.NewStats()
313+
err := memory.GetStats(helper.CgroupPath, &actualStats)
314+
if err == nil {
315+
t.Fatal("Expected failure")
316+
}
317+
}
318+
319+
func TestMemoryStatsBadFailcntFile(t *testing.T) {
320+
helper := NewCgroupTestUtil("memory", t)
321+
defer helper.cleanup()
322+
helper.writeFileContents(map[string]string{
323+
"memory.stat": memoryStatContents,
324+
"memory.usage_in_bytes": memoryUsageContents,
325+
"memory.max_usage_in_bytes": memoryMaxUsageContents,
326+
"memory.failcnt": "bad",
327+
"memory.oom_control": memoryOomControlContents,
328+
})
329+
330+
memory := &MemoryGroup{}
331+
actualStats := *cgroups.NewStats()
332+
err := memory.GetStats(helper.CgroupPath, &actualStats)
333+
if err == nil {
334+
t.Fatal("Expected failure")
335+
}
336+
}
337+
338+
func TestMemoryStatsBadOomControlFile(t *testing.T) {
339+
helper := NewCgroupTestUtil("memory", t)
340+
defer helper.cleanup()
341+
helper.writeFileContents(map[string]string{
342+
"memory.stat": memoryStatContents,
343+
"memory.usage_in_bytes": memoryUsageContents,
344+
"memory.max_usage_in_bytes": memoryMaxUsageContents,
345+
"memory.failcnt": memoryFailcnt,
346+
"memory.oom_control": "bad bad",
259347
})
260348

261349
memory := &MemoryGroup{}

libcontainer/cgroups/stats.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type CpuStats struct {
3535
type MemoryData struct {
3636
Usage uint64 `json:"usage,omitempty"`
3737
MaxUsage uint64 `json:"max_usage,omitempty"`
38-
Failcnt uint64 `json:"failcnt"`
38+
Failcnt uint64 `json:"failcnt,omitempty"`
3939
}
4040
type MemoryStats struct {
4141
// memory used for cache
@@ -77,7 +77,7 @@ type HugetlbStats struct {
7777
// maximum usage ever recorded.
7878
MaxUsage uint64 `json:"max_usage,omitempty"`
7979
// number of times htgetlb usage allocation failure.
80-
Failcnt uint64 `json:"failcnt"`
80+
Failcnt uint64 `json:"failcnt,omitempty"`
8181
}
8282

8383
type Stats struct {

0 commit comments

Comments
 (0)