Skip to content

Commit

Permalink
Add GetOOMScore function
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <[email protected]>
  • Loading branch information
crosbymichael committed Apr 9, 2019
1 parent 4d313c0 commit c93d645
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
12 changes: 12 additions & 0 deletions sys/oom_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ package sys

import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"

"github.com/opencontainers/runc/libcontainer/system"
)
Expand All @@ -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)))
}
20 changes: 1 addition & 19 deletions sys/oom_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ package sys

import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
7 changes: 7 additions & 0 deletions sys/oom_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c93d645

Please sign in to comment.