Skip to content

Commit

Permalink
libct/cg/sd/v2: support cpu.max unified resource
Browse files Browse the repository at this point in the history
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Nov 4, 2020
1 parent 1c4690d commit 0eb3112
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
32 changes: 29 additions & 3 deletions libcontainer/cgroups/systemd/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func NewUnifiedManager(config *configs.Cgroup, path string, rootless bool) cgrou
// For the list of keys, see https://www.kernel.org/doc/Documentation/cgroup-v2.txt
//
// For the list of systemd unit properties, see systemd.resource-control(5).
func unifiedResToSystemdProps(res map[string]string) (props []systemdDbus.Property, _ error) {
func unifiedResToSystemdProps(conn *systemdDbus.Conn, res map[string]string) (props []systemdDbus.Property, _ error) {
var err error

for k, v := range res {
if strings.Contains(k, "/") {
return nil, fmt.Errorf("unified resource %q must be a file name (no slashes)", k)
Expand All @@ -54,11 +56,35 @@ func unifiedResToSystemdProps(res map[string]string) (props []systemdDbus.Proper
if len(sk) != 2 {
return nil, fmt.Errorf("unified resource %q must be in the form CONTROLLER.PARAMETER", k)
}
// Please keep cases in alphabetical order.
switch k {
case "cpu.max":
// value: quota [period]
quota := int64(0) // 0 means "unlimited" if period is set
period := defCPUQuotaPeriod
sv := strings.SplitN(v, " ", 3)
if len(sv) > 2 {
return nil, fmt.Errorf("unified resource %q value invalid: %q", k, v)
}
// quota
if sv[0] != "max" {
quota, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, fmt.Errorf("unified resource %q value conversion error: %w", k, err)
}
}
// period
if len(sv) == 2 {
period, err = strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, fmt.Errorf("unified resource %q value conversion error: %w", k, err)
}
}
addCpuQuota(conn, &props, quota, period)

case "pids.max":
num := uint64(math.MaxUint64)
if v != "max" {
var err error
num, err = strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, fmt.Errorf("unified resource %q value conversion error: %w", k, err)
Expand Down Expand Up @@ -127,7 +153,7 @@ func genV2ResourcesProperties(c *configs.Cgroup, conn *systemdDbus.Conn) ([]syst

// convert Resources.Unified map to systemd properties
if r.Unified != nil {
unifiedProps, err := unifiedResToSystemdProps(r.Unified)
unifiedProps, err := unifiedResToSystemdProps(conn, r.Unified)
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion tests/integration/cgroups.bats
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ function setup() {
echo "$output" | grep -q '^cpu.max:10000 100000$'

check_systemd_value "TasksMax" 99
check_cpu_quota 10000 100000 "100ms"
}

@test "runc run (cgroup v2 resources.unified override)" {
Expand All @@ -227,10 +228,15 @@ function setup() {
update_config ' .linux.resources.memory |= { "limit": 33554432 }
| .linux.resources.memorySwap |= { "limit": 33554432 }
| .linux.resources.pids |= { "limit": 20 }
| .linux.resources.cpu |= {
"quota": 40000,
"period": 100000
}
| .linux.resources.unified |= {
"memory.min": "131072",
"memory.max": "10485760",
"pids.max": "42"
"pids.max": "42",
"cpu.max": "5000 50000"
}' "$BUSYBOX_BUNDLE"

runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified
Expand All @@ -247,5 +253,7 @@ function setup() {
runc exec test_cgroups_unified cat /sys/fs/cgroup/pids.max
[ "$status" -eq 0 ]
[ "$output" = '42' ]

check_systemd_value "TasksMax" 42
check_cpu_quota 5000 50000 "100ms"
}

0 comments on commit 0eb3112

Please sign in to comment.