Skip to content

Commit 68564ee

Browse files
authored
Merge pull request #4258 from kolyshkin/fix-4094
libct/cg/fs: fix setting rt_period vs rt_runtime
2 parents 492dc55 + f805206 commit 68564ee

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

libcontainer/cgroups/fs/cpu.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,31 @@ func (s *CpuGroup) Apply(path string, r *configs.Resources, pid int) error {
3535
}
3636

3737
func (s *CpuGroup) SetRtSched(path string, r *configs.Resources) error {
38+
var period string
3839
if r.CpuRtPeriod != 0 {
39-
if err := cgroups.WriteFile(path, "cpu.rt_period_us", strconv.FormatUint(r.CpuRtPeriod, 10)); err != nil {
40-
return err
40+
period = strconv.FormatUint(r.CpuRtPeriod, 10)
41+
if err := cgroups.WriteFile(path, "cpu.rt_period_us", period); err != nil {
42+
// The values of cpu.rt_period_us and cpu.rt_runtime_us
43+
// are inter-dependent and need to be set in a proper order.
44+
// If the kernel rejects the new period value with EINVAL
45+
// and the new runtime value is also being set, let's
46+
// ignore the error for now and retry later.
47+
if !errors.Is(err, unix.EINVAL) || r.CpuRtRuntime == 0 {
48+
return err
49+
}
50+
} else {
51+
period = ""
4152
}
4253
}
4354
if r.CpuRtRuntime != 0 {
4455
if err := cgroups.WriteFile(path, "cpu.rt_runtime_us", strconv.FormatInt(r.CpuRtRuntime, 10)); err != nil {
4556
return err
4657
}
58+
if period != "" {
59+
if err := cgroups.WriteFile(path, "cpu.rt_period_us", period); err != nil {
60+
return err
61+
}
62+
}
4763
}
4864
return nil
4965
}

tests/integration/update.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,17 @@ EOF
766766

767767
check_cgroup_value "cpu.rt_period_us" 900001
768768
check_cgroup_value "cpu.rt_runtime_us" 600001
769+
770+
# https://github.com/opencontainers/runc/issues/4094
771+
runc update test_update_rt --cpu-rt-period 10000 --cpu-rt-runtime 3000
772+
[ "$status" -eq 0 ]
773+
check_cgroup_value "cpu.rt_period_us" 10000
774+
check_cgroup_value "cpu.rt_runtime_us" 3000
775+
776+
runc update test_update_rt --cpu-rt-period 100000 --cpu-rt-runtime 20000
777+
[ "$status" -eq 0 ]
778+
check_cgroup_value "cpu.rt_period_us" 100000
779+
check_cgroup_value "cpu.rt_runtime_us" 20000
769780
}
770781

771782
@test "update devices [minimal transition rules]" {

0 commit comments

Comments
 (0)