From c93d6454358ee764bdcee9f14c37a71deb92e9d5 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 8 Apr 2019 16:26:05 -0400 Subject: [PATCH] Add GetOOMScore function Signed-off-by: Michael Crosby --- sys/oom_unix.go | 12 ++++++++++++ sys/oom_unix_test.go | 20 +------------------- sys/oom_windows.go | 7 +++++++ 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/sys/oom_unix.go b/sys/oom_unix.go index 7192efec1..54412e9c3 100644 --- a/sys/oom_unix.go +++ b/sys/oom_unix.go @@ -20,8 +20,10 @@ package sys import ( "fmt" + "io/ioutil" "os" "strconv" + "strings" "github.com/opencontainers/runc/libcontainer/system" ) @@ -45,3 +47,13 @@ func SetOOMScore(pid, score int) error { } return nil } + +// GetOOMScoreAdj gets the oom score for a process +func GetOOMScoreAdj(pid int) (int, error) { + path := fmt.Sprintf("/proc/%d/oom_score_adj", pid) + data, err := ioutil.ReadFile(path) + if err != nil { + return 0, err + } + return strconv.Atoi(strings.TrimSpace(string(data))) +} diff --git a/sys/oom_unix_test.go b/sys/oom_unix_test.go index 0e97b7b15..2e399b52f 100644 --- a/sys/oom_unix_test.go +++ b/sys/oom_unix_test.go @@ -20,12 +20,8 @@ package sys import ( "errors" - "fmt" - "io/ioutil" "os" "os/exec" - "strconv" - "strings" "testing" "time" @@ -85,7 +81,7 @@ func adjustOom(adjustment int) (int, error) { return 0, err } - return readOomScoreAdj(pid) + return GetOOMScoreAdj(pid) } func waitForPid(process *os.Process) (int, error) { @@ -106,17 +102,3 @@ func waitForPid(process *os.Process) (int, error) { return 0, errors.New("process did not start in 10 seconds") } } - -func readOomScoreAdj(pid int) (int, error) { - oomScore, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid)) - if err != nil { - return 0, err - } - - scoreAsInt, err := strconv.Atoi(strings.TrimSpace(string(oomScore))) - if err != nil { - return 0, err - } - - return scoreAsInt, nil -} diff --git a/sys/oom_windows.go b/sys/oom_windows.go index f44bcebd1..a917ba635 100644 --- a/sys/oom_windows.go +++ b/sys/oom_windows.go @@ -22,3 +22,10 @@ package sys func SetOOMScore(pid, score int) error { return nil } + +// GetOOMScoreAdj gets the oom score for a process +// +// Not implemented on Windows +func GetOOMScoreAdj(pid int) (int, error) { + return 0, nil +}