Skip to content

Commit b39f21b

Browse files
committed
runc exec: fix setting process.Scheduler
Commit 770728e added Scheduler field into both Config and Process, but forgot to add a mechanism to actually use Process.Scheduler. As a result, runc exec does not set Process.Scheduler ever. Fix it, and a test case (which fails before the fix). Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent 92ae668 commit b39f21b

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

libcontainer/container_linux.go

+4
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ func (c *Container) newInitConfig(process *Process) *initConfig {
699699
ProcessLabel: c.config.ProcessLabel,
700700
Rlimits: c.config.Rlimits,
701701
IOPriority: c.config.IOPriority,
702+
Scheduler: c.config.Scheduler,
702703
CreateConsole: process.ConsoleSocket != nil,
703704
ConsoleWidth: process.ConsoleWidth,
704705
ConsoleHeight: process.ConsoleHeight,
@@ -724,6 +725,9 @@ func (c *Container) newInitConfig(process *Process) *initConfig {
724725
if process.IOPriority != nil {
725726
cfg.IOPriority = process.IOPriority
726727
}
728+
if process.Scheduler != nil {
729+
cfg.Scheduler = process.Scheduler
730+
}
727731

728732
// Set misc properties.
729733

libcontainer/init_linux.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type initConfig struct {
8282
ProcessLabel string `json:"process_label"`
8383
Rlimits []configs.Rlimit `json:"rlimits"`
8484
IOPriority *configs.IOPriority `json:"io_priority,omitempty"`
85+
Scheduler *configs.Scheduler `json:"scheduler,omitempty"`
8586

8687
// Miscellaneous properties, filled in by [Container.newInitConfig]
8788
// unless documented otherwise.
@@ -656,7 +657,7 @@ func setupRlimits(limits []configs.Rlimit, pid int) error {
656657
return nil
657658
}
658659

659-
func setupScheduler(config *configs.Config) error {
660+
func setupScheduler(config *initConfig) error {
660661
if config.Scheduler == nil {
661662
return nil
662663
}
@@ -665,7 +666,7 @@ func setupScheduler(config *configs.Config) error {
665666
return err
666667
}
667668
if err := unix.SchedSetAttr(0, attr, 0); err != nil {
668-
if errors.Is(err, unix.EPERM) && config.Cgroups.CpusetCpus != "" {
669+
if errors.Is(err, unix.EPERM) && config.Config.Cgroups.CpusetCpus != "" {
669670
return errors.New("process scheduler can't be used together with AllowedCPUs")
670671
}
671672
return fmt.Errorf("error setting scheduler: %w", err)

libcontainer/setns_init_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (l *linuxSetnsInit) Init() error {
7272
unix.Umask(int(*l.config.Config.Umask))
7373
}
7474

75-
if err := setupScheduler(l.config.Config); err != nil {
75+
if err := setupScheduler(l.config); err != nil {
7676
return err
7777
}
7878

libcontainer/standard_init_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (l *linuxStandardInit) Init() error {
156156
}
157157
}
158158

159-
if err := setupScheduler(l.config.Config); err != nil {
159+
if err := setupScheduler(l.config); err != nil {
160160
return err
161161
}
162162

tests/integration/scheduler.bats

+37-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,51 @@ function teardown() {
1212
}
1313

1414
@test "scheduler is applied" {
15-
update_config ' .process.scheduler = {"policy": "SCHED_DEADLINE", "nice": 19, "priority": 0, "runtime": 42000, "deadline": 1000000, "period": 1000000, }'
15+
update_config ' .process.scheduler = {
16+
"policy": "SCHED_BATCH",
17+
"priority": 0,
18+
"nice": 19
19+
}'
1620

1721
runc run -d --console-socket "$CONSOLE_SOCKET" test_scheduler
1822
[ "$status" -eq 0 ]
1923

24+
# Check init settings.
2025
runc exec test_scheduler chrt -p 1
2126
[ "$status" -eq 0 ]
27+
[[ "${lines[0]}" == *"scheduling policy: SCHED_BATCH" ]]
28+
[[ "${lines[1]}" == *"priority: 0" ]]
29+
30+
# Check exec settings derived from config.json.
31+
runc exec test_scheduler sh -c 'chrt -p $$'
32+
[ "$status" -eq 0 ]
33+
[[ "${lines[0]}" == *"scheduling policy: SCHED_BATCH" ]]
34+
[[ "${lines[1]}" == *"priority: 0" ]]
35+
36+
# Another exec, with different scheduler settings.
37+
proc='
38+
{
39+
"terminal": false,
40+
"args": [ "/bin/sleep", "600" ],
41+
"cwd": "/",
42+
"scheduler": {
43+
"policy": "SCHED_DEADLINE",
44+
"flags": [ "SCHED_FLAG_RESET_ON_FORK" ],
45+
"nice": 19,
46+
"priority": 0,
47+
"runtime": 42000,
48+
"deadline": 100000,
49+
"period": 1000000
50+
}
51+
}'
52+
__runc exec -d --pid-file pid.txt --process <(echo "$proc") test_scheduler
53+
[ "$status" -eq 0 ]
2254

23-
[[ "${lines[0]}" == *"scheduling policy: SCHED_DEADLINE" ]]
55+
# shellcheck disable=SC2016
56+
run chrt -p "$(cat pid.txt)"
57+
[[ "${lines[0]}" == *"scheduling policy: SCHED_DEADLINE|SCHED_RESET_ON_FORK" ]]
2458
[[ "${lines[1]}" == *"priority: 0" ]]
25-
[[ "${lines[2]}" == *"runtime/deadline/period parameters: 42000/1000000/1000000" ]]
59+
[[ "${lines[2]}" == *"runtime/deadline/period parameters: 42000/100000/1000000" ]]
2660
}
2761

2862
# Checks that runc emits a specific error when scheduling policy is used

0 commit comments

Comments
 (0)