Skip to content

Commit e18a918

Browse files
committed
Add support for updating oom_score_adj
Signed-off-by: karthik-k-n <karthikkn1997@gmail.com> rebaes
1 parent 2f93d66 commit e18a918

7 files changed

Lines changed: 34 additions & 5 deletions

File tree

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ require (
3939
github.com/russross/blackfriday/v2 v2.1.0 // indirect
4040
github.com/vishvananda/netns v0.0.4 // indirect
4141
)
42+
43+
replace github.com/opencontainers/runtime-spec v1.2.1 => github.com/Karthik-K-N/runtime-spec v0.0.0-20250310091823-a5fba8a48c25

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
2+
github.com/Karthik-K-N/runtime-spec v0.0.0-20250310091823-a5fba8a48c25 h1:f0CPE05qGTPCd7U44s0+bhr/tbGs1FYz/rtELxvjxjw=
3+
github.com/Karthik-K-N/runtime-spec v0.0.0-20250310091823-a5fba8a48c25/go.mod h1:9tp5Ly3OaIUitPDTBTBrOFWw19vXQHlQd5iMm0wPjic=
24
github.com/checkpoint-restore/go-criu/v6 v6.3.0 h1:mIdrSO2cPNWQY1truPg6uHLXyKHk3Z5Odx4wjKOASzA=
35
github.com/checkpoint-restore/go-criu/v6 v6.3.0/go.mod h1:rrRTN/uSwY2X+BPRl/gkulo9gsKOSAeVp9/K2tv7xZI=
46
github.com/cilium/ebpf v0.17.3 h1:FnP4r16PWYSE4ux6zN+//jMcW4nMVRvuTLVTvCjyyjg=
@@ -51,8 +53,6 @@ github.com/mrunalp/fileutils v0.5.1 h1:F+S7ZlNKnrwHfSwdlgNSkKo67ReVf8o9fel6C3dkm
5153
github.com/mrunalp/fileutils v0.5.1/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
5254
github.com/opencontainers/cgroups v0.0.1 h1:MXjMkkFpKv6kpuirUa4USFBas573sSAY082B4CiHEVA=
5355
github.com/opencontainers/cgroups v0.0.1/go.mod h1:s8lktyhlGUqM7OSRL5P7eAW6Wb+kWPNvt4qvVfzA5vs=
54-
github.com/opencontainers/runtime-spec v1.2.1 h1:S4k4ryNgEpxW1dzyqffOmhI1BHYcjzU8lpJfSlR0xww=
55-
github.com/opencontainers/runtime-spec v1.2.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
5656
github.com/opencontainers/selinux v1.11.1 h1:nHFvthhM0qY8/m+vfhJylliSshm8G1jJ2jDMcgULaH8=
5757
github.com/opencontainers/selinux v1.11.1/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
5858
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

libcontainer/container_linux.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ func (c *Container) Set(config configs.Config) error {
171171
if status == Stopped {
172172
return ErrNotRunning
173173
}
174+
175+
// Set the oom_score_adj value
176+
if config.OomScoreAdj != nil && *config.OomScoreAdj != 0 {
177+
if c.hasInit() {
178+
if err := os.WriteFile(fmt.Sprintf("/proc/%d/oom_score_adj", c.initProcess.pid()), []byte(strconv.Itoa(*config.OomScoreAdj)), unix.O_WRONLY); err != nil {
179+
return err
180+
}
181+
}
182+
}
183+
174184
if err := c.cgroupManager.Set(config.Cgroups.Resources); err != nil {
175185
// Set configs back
176186
if err2 := c.cgroupManager.Set(c.config.Cgroups.Resources); err2 != nil {

update.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ The accepted format is as follow (unchanged values can be omitted):
5454
},
5555
"blockIO": {
5656
"weight": 0
57-
}
57+
},
58+
"oomScoreAdj": 0
5859
}
5960
6061
Note: if data is to be read from a file or the standard input, all
@@ -136,6 +137,10 @@ other options are ignored.
136137
Name: "mem-bw-schema",
137138
Usage: "The string of Intel RDT/MBA memory bandwidth schema",
138139
},
140+
cli.StringFlag{
141+
Name: "oom-score-adj",
142+
Usage: "oom score adj value",
143+
},
139144
},
140145
Action: func(context *cli.Context) error {
141146
if err := checkArgs(context, 1, exactArgs); err != nil {
@@ -177,6 +182,8 @@ other options are ignored.
177182
if err != nil {
178183
return err
179184
}
185+
aJSON, _ := json.Marshal(r)
186+
fmt.Println("Passed resources are", string(aJSON))
180187
} else {
181188
if val := context.Int("blkio-weight"); val != 0 {
182189
r.BlockIO.Weight = u16Ptr(uint16(val))
@@ -253,6 +260,10 @@ other options are ignored.
253260
}
254261

255262
r.Pids.Limit = int64(context.Int("pids-limit"))
263+
264+
if oomScoreAdj := context.Int("oom-score-adj"); oomScoreAdj != 0 {
265+
r.OOMScoreAdj = &oomScoreAdj
266+
}
256267
}
257268

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

392+
if r.OOMScoreAdj != nil {
393+
config.OomScoreAdj = r.OOMScoreAdj
394+
}
395+
381396
return container.Set(config)
382397
},
383398
}

vendor/github.com/opencontainers/runtime-spec/specs-go/config.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/opencontainers/runtime-spec/specs-go/version.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ github.com/opencontainers/cgroups/fscommon
6262
github.com/opencontainers/cgroups/internal/path
6363
github.com/opencontainers/cgroups/manager
6464
github.com/opencontainers/cgroups/systemd
65-
# github.com/opencontainers/runtime-spec v1.2.1
65+
# github.com/opencontainers/runtime-spec v1.2.1 => github.com/Karthik-K-N/runtime-spec v0.0.0-20250310091823-a5fba8a48c25
6666
## explicit
6767
github.com/opencontainers/runtime-spec/specs-go
6868
github.com/opencontainers/runtime-spec/specs-go/features

0 commit comments

Comments
 (0)