Skip to content

Commit

Permalink
libc/cg: convert r.CPU.Cpus/Mems to systemd props
Browse files Browse the repository at this point in the history
Support for systemd properties AllowedCPUs and AllowedMemoryNodes
was added by commit 13afa58, but only for unified resources
of systemd v2 driver.

This adds support for Cpu.Cpus and Cpu.Mems resources to
both systemd v1 and v2 cgroup drivers.

An integration test is added to check that the settings work.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Jan 14, 2021
1 parent dbbe7e6 commit 9c49ded
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
22 changes: 22 additions & 0 deletions libcontainer/cgroups/systemd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,25 @@ func addCpuQuota(conn *systemdDbus.Conn, properties *[]systemdDbus.Property, quo
newProp("CPUQuotaPerSecUSec", cpuQuotaPerSecUSec))
}
}

func addCpuset(props *[]systemdDbus.Property, cpus, mems string) error {
if cpus != "" {
bits, err := rangeToBits(cpus)
if err != nil {
return fmt.Errorf("resources.CPU.Cpus=%q conversion error: %w",
cpus, err)
}
*props = append(*props,
newProp("AllowedCPUs", bits))
}
if mems != "" {
bits, err := rangeToBits(mems)
if err != nil {
return fmt.Errorf("resources.CPU.Mems=%q conversion error: %w",
mems, err)
}
*props = append(*props,
newProp("AllowedMemoryNodes", bits))
}
return nil
}
5 changes: 5 additions & 0 deletions libcontainer/cgroups/systemd/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func genV1ResourcesProperties(c *configs.Cgroup, conn *systemdDbus.Conn) ([]syst
newProp("TasksMax", uint64(r.PidsLimit)))
}

err = addCpuset(&properties, r.CpusetCpus, r.CpusetMems)
if err != nil {
return nil, err
}

return properties, nil
}

Expand Down
5 changes: 5 additions & 0 deletions libcontainer/cgroups/systemd/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ func genV2ResourcesProperties(c *configs.Cgroup, conn *systemdDbus.Conn) ([]syst
newProp("TasksMax", uint64(r.PidsLimit)))
}

err = addCpuset(&properties, r.CpusetCpus, r.CpusetMems)
if err != nil {
return nil, err
}

// ignore r.KernelMemory

// convert Resources.Unified map to systemd properties
Expand Down
47 changes: 47 additions & 0 deletions tests/integration/update.bats
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,53 @@ EOF
check_systemd_value "TasksMax" 10
}

@test "update cpuset parameters via resources.CPU" {
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
requires smp

update_config ' .linux.resources.CPU |= {
"Cpus": "0",
"Mems": "0"
}' "${BUSYBOX_BUNDLE}"
runc run -d --console-socket "$CONSOLE_SOCKET" test_update
[ "$status" -eq 0 ]

# check that initial values were properly set
check_systemd_value "AllowedCPUs" 0
check_systemd_value "AllowedMemoryNodes" 0

runc update -r - test_update <<EOF
{
"CPU": {
"Cpus": "1"
}
}
EOF
[ "$status" -eq 0 ]

# check the updated systemd unit properties
check_systemd_value "AllowedCPUs" 1

# More than 1 numa memory node is required to test this
file="/sys/fs/cgroup/cpuset.mems.effective"
if ! test -r $file || grep -q '^0$' $file; then
# skip the rest of it
return 0
fi

runc update -r - test_update <<EOF
{
"CPU": {
"Mems": "1"
}
}
EOF
[ "$status" -eq 0 ]

# check the updated systemd unit properties
check_systemd_value "AllowedMemoryNodes" 1
}

@test "update cpuset parameters via v2 unified map" {
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
requires cgroups_v2 smp
Expand Down

0 comments on commit 9c49ded

Please sign in to comment.