Skip to content

[WIP][DNM] Add support for updating oom_score_adj #4669

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
)

replace github.com/opencontainers/runtime-spec v1.2.1 => github.com/Karthik-K-N/runtime-spec v0.0.0-20250310091823-a5fba8a48c25
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/Karthik-K-N/runtime-spec v0.0.0-20250310091823-a5fba8a48c25 h1:f0CPE05qGTPCd7U44s0+bhr/tbGs1FYz/rtELxvjxjw=
github.com/Karthik-K-N/runtime-spec v0.0.0-20250310091823-a5fba8a48c25/go.mod h1:9tp5Ly3OaIUitPDTBTBrOFWw19vXQHlQd5iMm0wPjic=
github.com/checkpoint-restore/go-criu/v6 v6.3.0 h1:mIdrSO2cPNWQY1truPg6uHLXyKHk3Z5Odx4wjKOASzA=
github.com/checkpoint-restore/go-criu/v6 v6.3.0/go.mod h1:rrRTN/uSwY2X+BPRl/gkulo9gsKOSAeVp9/K2tv7xZI=
github.com/cilium/ebpf v0.17.3 h1:FnP4r16PWYSE4ux6zN+//jMcW4nMVRvuTLVTvCjyyjg=
Expand Down Expand Up @@ -51,8 +53,6 @@ github.com/mrunalp/fileutils v0.5.1 h1:F+S7ZlNKnrwHfSwdlgNSkKo67ReVf8o9fel6C3dkm
github.com/mrunalp/fileutils v0.5.1/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
github.com/opencontainers/cgroups v0.0.1 h1:MXjMkkFpKv6kpuirUa4USFBas573sSAY082B4CiHEVA=
github.com/opencontainers/cgroups v0.0.1/go.mod h1:s8lktyhlGUqM7OSRL5P7eAW6Wb+kWPNvt4qvVfzA5vs=
github.com/opencontainers/runtime-spec v1.2.1 h1:S4k4ryNgEpxW1dzyqffOmhI1BHYcjzU8lpJfSlR0xww=
github.com/opencontainers/runtime-spec v1.2.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/selinux v1.11.1 h1:nHFvthhM0qY8/m+vfhJylliSshm8G1jJ2jDMcgULaH8=
github.com/opencontainers/selinux v1.11.1/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
10 changes: 10 additions & 0 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ func (c *Container) Set(config configs.Config) error {
if status == Stopped {
return ErrNotRunning
}

// Set the oom_score_adj value
if config.OomScoreAdj != nil && *config.OomScoreAdj != 0 {
if c.hasInit() {
if err := os.WriteFile(fmt.Sprintf("/proc/%d/oom_score_adj", c.initProcess.pid()), []byte(strconv.Itoa(*config.OomScoreAdj)), unix.O_WRONLY); err != nil {
return err
}
}
}

if err := c.cgroupManager.Set(config.Cgroups.Resources); err != nil {
// Set configs back
if err2 := c.cgroupManager.Set(c.config.Cgroups.Resources); err2 != nil {
Expand Down
17 changes: 16 additions & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ The accepted format is as follow (unchanged values can be omitted):
},
"blockIO": {
"weight": 0
}
},
"oomScoreAdj": 0
}

Note: if data is to be read from a file or the standard input, all
Expand Down Expand Up @@ -136,6 +137,10 @@ other options are ignored.
Name: "mem-bw-schema",
Usage: "The string of Intel RDT/MBA memory bandwidth schema",
},
cli.StringFlag{
Name: "oom-score-adj",
Usage: "oom score adj value",
},
},
Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, exactArgs); err != nil {
Expand Down Expand Up @@ -177,6 +182,8 @@ other options are ignored.
if err != nil {
return err
}
aJSON, _ := json.Marshal(r)
fmt.Println("Passed resources are", string(aJSON))
} else {
if val := context.Int("blkio-weight"); val != 0 {
r.BlockIO.Weight = u16Ptr(uint16(val))
Expand Down Expand Up @@ -253,6 +260,10 @@ other options are ignored.
}

r.Pids.Limit = int64(context.Int("pids-limit"))

if oomScoreAdj := context.Int("oom-score-adj"); oomScoreAdj != 0 {
r.OOMScoreAdj = &oomScoreAdj
}
}

// Fix up values
Expand Down Expand Up @@ -378,6 +389,10 @@ other options are ignored.
// Note this field is not saved into container's state.json.
config.Cgroups.SkipDevices = true

if r.OOMScoreAdj != nil {
config.OomScoreAdj = r.OOMScoreAdj
}

return container.Set(config)
},
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ github.com/opencontainers/cgroups/fscommon
github.com/opencontainers/cgroups/internal/path
github.com/opencontainers/cgroups/manager
github.com/opencontainers/cgroups/systemd
# github.com/opencontainers/runtime-spec v1.2.1
# github.com/opencontainers/runtime-spec v1.2.1 => github.com/Karthik-K-N/runtime-spec v0.0.0-20250310091823-a5fba8a48c25
## explicit
github.com/opencontainers/runtime-spec/specs-go
github.com/opencontainers/runtime-spec/specs-go/features
Expand Down