Skip to content

Commit 91e7d21

Browse files
committed
sys: add AdjustOOMScore() utility
Handle the limits in this function so that consumers don't have to perform the boundary checks. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 4424011 commit 91e7d21

4 files changed

Lines changed: 21 additions & 8 deletions

File tree

runtime/v1/shim/client/client.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,7 @@ func setupOOMScore(shimPid int) error {
182182
return errors.Wrap(err, "get daemon OOM score")
183183
}
184184
shimScore := score + 1
185-
if shimScore > sys.OOMScoreAdjMax {
186-
shimScore = sys.OOMScoreAdjMax
187-
}
188-
if err := sys.SetOOMScore(shimPid, shimScore); err != nil {
185+
if err := sys.AdjustOOMScore(shimPid, shimScore); err != nil {
189186
return errors.Wrap(err, "set shim OOM score")
190187
}
191188
return nil

runtime/v2/shim/util_unix.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ func AdjustOOMScore(pid int) error {
6161
return errors.Wrap(err, "get parent OOM score")
6262
}
6363
shimScore := score + 1
64-
if shimScore > sys.OOMScoreAdjMax {
65-
shimScore = sys.OOMScoreAdjMax
66-
}
67-
if err := sys.SetOOMScore(pid, shimScore); err != nil {
64+
if err := sys.AdjustOOMScore(pid, shimScore); err != nil {
6865
return errors.Wrap(err, "set shim OOM score")
6966
}
7067
return nil

sys/oom_linux.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ const (
3636
OOMScoreAdjMax = 1000
3737
)
3838

39+
// AdjustOOMScore sets the oom score for the provided pid. If the provided score
40+
// is out of range (-1000 - 1000), it is clipped to the min/max value.
41+
func AdjustOOMScore(pid, score int) error {
42+
if score > OOMScoreAdjMax {
43+
score = OOMScoreAdjMax
44+
} else if score < OOMScoreAdjMin {
45+
score = OOMScoreAdjMin
46+
}
47+
return SetOOMScore(pid, score)
48+
}
49+
3950
// SetOOMScore sets the oom score for the provided pid
4051
func SetOOMScore(pid, score int) error {
4152
if score > OOMScoreAdjMax || score < OOMScoreAdjMin {

sys/oom_unsupported.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ const (
2525
OOMScoreAdjMax = 0
2626
)
2727

28+
// AdjustOOMScore sets the oom score for the provided pid. If the provided score
29+
// is out of range (-1000 - 1000), it is clipped to the min/max value.
30+
//
31+
// Not implemented on Windows
32+
func AdjustOOMScore(pid, score int) error {
33+
return nil
34+
}
35+
2836
// SetOOMScore sets the oom score for the process
2937
//
3038
// Not implemented on Windows

0 commit comments

Comments
 (0)