Skip to content

Commit c93d645

Browse files
committed
Add GetOOMScore function
Signed-off-by: Michael Crosby <[email protected]>
1 parent 4d313c0 commit c93d645

3 files changed

Lines changed: 20 additions & 19 deletions

File tree

sys/oom_unix.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ package sys
2020

2121
import (
2222
"fmt"
23+
"io/ioutil"
2324
"os"
2425
"strconv"
26+
"strings"
2527

2628
"github.com/opencontainers/runc/libcontainer/system"
2729
)
@@ -45,3 +47,13 @@ func SetOOMScore(pid, score int) error {
4547
}
4648
return nil
4749
}
50+
51+
// GetOOMScoreAdj gets the oom score for a process
52+
func GetOOMScoreAdj(pid int) (int, error) {
53+
path := fmt.Sprintf("/proc/%d/oom_score_adj", pid)
54+
data, err := ioutil.ReadFile(path)
55+
if err != nil {
56+
return 0, err
57+
}
58+
return strconv.Atoi(strings.TrimSpace(string(data)))
59+
}

sys/oom_unix_test.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@ package sys
2020

2121
import (
2222
"errors"
23-
"fmt"
24-
"io/ioutil"
2523
"os"
2624
"os/exec"
27-
"strconv"
28-
"strings"
2925
"testing"
3026
"time"
3127

@@ -85,7 +81,7 @@ func adjustOom(adjustment int) (int, error) {
8581
return 0, err
8682
}
8783

88-
return readOomScoreAdj(pid)
84+
return GetOOMScoreAdj(pid)
8985
}
9086

9187
func waitForPid(process *os.Process) (int, error) {
@@ -106,17 +102,3 @@ func waitForPid(process *os.Process) (int, error) {
106102
return 0, errors.New("process did not start in 10 seconds")
107103
}
108104
}
109-
110-
func readOomScoreAdj(pid int) (int, error) {
111-
oomScore, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid))
112-
if err != nil {
113-
return 0, err
114-
}
115-
116-
scoreAsInt, err := strconv.Atoi(strings.TrimSpace(string(oomScore)))
117-
if err != nil {
118-
return 0, err
119-
}
120-
121-
return scoreAsInt, nil
122-
}

sys/oom_windows.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ package sys
2222
func SetOOMScore(pid, score int) error {
2323
return nil
2424
}
25+
26+
// GetOOMScoreAdj gets the oom score for a process
27+
//
28+
// Not implemented on Windows
29+
func GetOOMScoreAdj(pid int) (int, error) {
30+
return 0, nil
31+
}

0 commit comments

Comments
 (0)