Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mem][linux] Expose oom_kill as part of SwapMemory #1782

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mem/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type SwapMemoryStat struct {
// Linux specific numbers
// https://www.kernel.org/doc/Documentation/cgroup-v2.txt
PgMajFault uint64 `json:"pgMajFault"`
OomKill uint64 `json:"oomKill"`
}

func (m VirtualMemoryStat) String() string {
Expand Down
6 changes: 6 additions & 0 deletions mem/mem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
continue
}
ret.PgMajFault = value * 4 * 1024
case "oom_kill":
value, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
continue
}
ret.OomKill = value
}
}
return ret, nil
Expand Down
45 changes: 45 additions & 0 deletions mem/mem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,48 @@ func TestParseSwapsFile_EmptyFile(t *testing.T) {
_, err := parseSwapsFile(context.Background(), strings.NewReader(""))
assert.Error(t, err)
}

var swapMemoryVmstatTests = []struct {
mockedRootFS string
swap *SwapMemoryStat
}{
{
"oomkill", &SwapMemoryStat{
// not checked
Total: 0,
Used: 0,
Free: 0,
UsedPercent: 0,

// checked
PgIn: 1 * 4 * 1024,
PgOut: 2 * 4 * 1024,
PgFault: 3 * 4 * 1024,
PgMajFault: 4 * 4 * 1024,
Sin: 3 * 4 * 1024,
Sout: 4 * 4 * 1024,
OomKill: 5,
},
},
}

func TestSwapVmstatMemoryLinux(t *testing.T) {
for _, tt := range swapMemoryVmstatTests {
t.Run(tt.mockedRootFS, func(t *testing.T) {
t.Setenv("HOST_PROC", filepath.Join("testdata/linux/swapmemory/", tt.mockedRootFS, "proc"))

stat, err := SwapMemory()
stat.Total = 0
stat.Used = 0
stat.Free = 0
stat.UsedPercent = 0
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if !reflect.DeepEqual(stat, tt.swap) {
t.Errorf("got: %+v\nwant: %+v", stat, tt.swap)
}
})
}
}
3 changes: 2 additions & 1 deletion mem/mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ func TestSwapMemoryStat_String(t *testing.T) {
PgOut: 4,
PgFault: 5,
PgMajFault: 6,
OomKill: 7,
}
e := `{"total":10,"used":30,"free":40,"usedPercent":30.1,"sin":1,"sout":2,"pgIn":3,"pgOut":4,"pgFault":5,"pgMajFault":6}`
e := `{"total":10,"used":30,"free":40,"usedPercent":30.1,"sin":1,"sout":2,"pgIn":3,"pgOut":4,"pgFault":5,"pgMajFault":6,"oomKill":7}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("SwapMemoryStat string is invalid: %v", v)
}
Expand Down
15 changes: 15 additions & 0 deletions mem/testdata/linux/swapmemory/oomkill/proc/vmstat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pgpgin 1
pgpgout 2
pswpin 3
pswpout 4
pgalloc_dma 5
pgalloc_dma32 6
pgalloc_normal 7
pgalloc_movable 8
pgfree 9
pgactivate 10
pgdeactivate 1
pglazyfree 2
pgfault 3
pgmajfault 4
oom_kill 5