Skip to content

Commit

Permalink
Merge pull request #3115 from kolyshkin/1.0-cpu-quota-period
Browse files Browse the repository at this point in the history
[1.0] libct/cg/v1: work around CPU quota period set failure
  • Loading branch information
Mrunal Patel authored Aug 11, 2021
2 parents 0b77b2d + 4c70105 commit 4f08893
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
24 changes: 22 additions & 2 deletions libcontainer/cgroups/fs/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ package fs

import (
"bufio"
"errors"
"fmt"
"os"
"strconv"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
"golang.org/x/sys/unix"
)

type CpuGroup struct{}
Expand Down Expand Up @@ -71,15 +73,33 @@ func (s *CpuGroup) Set(path string, r *configs.Resources) error {
return fmt.Errorf("the minimum allowed cpu-shares is %d", sharesRead)
}
}

var period string
if r.CpuPeriod != 0 {
if err := cgroups.WriteFile(path, "cpu.cfs_period_us", strconv.FormatUint(r.CpuPeriod, 10)); err != nil {
return err
period = strconv.FormatUint(r.CpuPeriod, 10)
if err := cgroups.WriteFile(path, "cpu.cfs_period_us", period); err != nil {
// Sometimes when the period to be set is smaller
// than the current one, it is rejected by the kernel
// (EINVAL) as old_quota/new_period exceeds the parent
// cgroup quota limit. If this happens and the quota is
// going to be set, ignore the error for now and retry
// after setting the quota.
if !errors.Is(err, unix.EINVAL) || r.CpuQuota == 0 {
return err
}
} else {
period = ""
}
}
if r.CpuQuota != 0 {
if err := cgroups.WriteFile(path, "cpu.cfs_quota_us", strconv.FormatInt(r.CpuQuota, 10)); err != nil {
return err
}
if period != "" {
if err := cgroups.WriteFile(path, "cpu.cfs_period_us", period); err != nil {
return err
}
}
}
return s.SetRtSched(path, r)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/update.bats
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,15 @@ EOF
check_cpu_quota -1 1000000 "infinity"
}

@test "set cpu period with no quota (invalid period)" {
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup

update_config '.linux.resources.cpu |= { "period": 100 }'

runc run -d --console-socket "$CONSOLE_SOCKET" test_update
[ "$status" -eq 1 ]
}

@test "set cpu quota with no period" {
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup

Expand Down

0 comments on commit 4f08893

Please sign in to comment.