mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
bf15cc99b1
Tested with both Podman (master) and Moby (master), on Ubuntu 19.10 . $ podman --cgroup-manager=systemd run -it --rm --runtime=runc \ --cgroupns=host --memory 42m --cpus 0.42 --pids-limit 42 alpine / # cat /proc/self/cgroup 0::/user.slice/user-1001.slice/user@1001.service/user.slice/libpod-132ff0d72245e6f13a3bbc6cdc5376886897b60ac59eaa8dea1df7ab959cbf1c.scope / # cat /sys/fs/cgroup/user.slice/user-1001.slice/user@1001.service/user.slice/libpod-132ff0d72245e6f13a3bbc6cdc5376886897b60ac59eaa8dea1df7ab959cbf1c.scope/memory.max 44040192 / # cat /sys/fs/cgroup/user.slice/user-1001.slice/user@1001.service/user.slice/libpod-132ff0d72245e6f13a3bbc6cdc5376886897b60ac59eaa8dea1df7ab959cbf1c.scope/cpu.max 42000 100000 / # cat /sys/fs/cgroup/user.slice/user-1001.slice/user@1001.service/user.slice/libpod-132ff0d72245e6f13a3bbc6cdc5376886897b60ac59eaa8dea1df7ab959cbf1c.scope/pids.max 42 Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
315 lines
9.1 KiB
Bash
315 lines
9.1 KiB
Bash
#!/usr/bin/env bats
|
|
|
|
load helpers
|
|
|
|
function teardown() {
|
|
rm -f $BATS_TMPDIR/runc-cgroups-integration-test.json
|
|
teardown_running_container test_update
|
|
teardown_running_container test_update_rt
|
|
teardown_busybox
|
|
}
|
|
|
|
function setup() {
|
|
teardown
|
|
setup_busybox
|
|
|
|
set_cgroups_path "$BUSYBOX_BUNDLE"
|
|
|
|
# Set some initial known values
|
|
DATA=$(cat <<EOF
|
|
"memory": {
|
|
"limit": 33554432,
|
|
"reservation": 25165824
|
|
},
|
|
"cpu": {
|
|
"shares": 100,
|
|
"quota": 500000,
|
|
"period": 1000000,
|
|
"cpus": "0"
|
|
},
|
|
"pids": {
|
|
"limit": 20
|
|
}
|
|
EOF
|
|
)
|
|
DATA=$(echo ${DATA} | sed 's/\n/\\n/g')
|
|
if grep -qw \"resources\" ${BUSYBOX_BUNDLE}/config.json; then
|
|
sed -i "s/\(\"resources\": {\)/\1\n${DATA},/" ${BUSYBOX_BUNDLE}/config.json
|
|
else
|
|
sed -i "s/\(\"linux\": {\)/\1\n\"resources\": {${DATA}},/" ${BUSYBOX_BUNDLE}/config.json
|
|
fi
|
|
}
|
|
|
|
# Tests whatever limits are (more or less) common between cgroup
|
|
# v1 and v2: memory/swap, pids, and cpuset.
|
|
@test "update cgroup v1/v2 common limits" {
|
|
[[ "$ROOTLESS" -ne 0 && -z "$RUNC_USE_SYSTEMD" ]] && requires rootless_cgroup
|
|
if [[ "$ROOTLESS" -ne 0 && -n "$RUNC_USE_SYSTEMD" ]]; then
|
|
file="/sys/fs/cgroup/user.slice/user-$(id -u).slice/user@$(id -u).service/cgroup.controllers"
|
|
# NOTE: delegation of cpuset requires systemd >= 244 (Fedora >= 32, Ubuntu >= 20.04).
|
|
for f in memory pids cpuset; do
|
|
if grep -qwv $f $file; then
|
|
skip "$f is not enabled in $file"
|
|
fi
|
|
done
|
|
fi
|
|
init_cgroup_paths
|
|
|
|
# run a few busyboxes detached
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update
|
|
[ "$status" -eq 0 ]
|
|
|
|
# Set a few variables to make the code below work for both v1 and v2
|
|
case $CGROUP_UNIFIED in
|
|
no)
|
|
MEM_LIMIT="memory.limit_in_bytes"
|
|
MEM_RESERVE="memory.soft_limit_in_bytes"
|
|
MEM_SWAP="memory.memsw.limit_in_bytes"
|
|
SYSTEM_MEM=$(cat "${CGROUP_MEMORY_BASE_PATH}/${MEM_LIMIT}")
|
|
SYSTEM_MEM_SWAP=$(cat "${CGROUP_MEMORY_BASE_PATH}/$MEM_SWAP")
|
|
;;
|
|
yes)
|
|
MEM_LIMIT="memory.max"
|
|
MEM_RESERVE="memory.low"
|
|
MEM_SWAP="memory.swap.max"
|
|
SYSTEM_MEM="max"
|
|
SYSTEM_MEM_SWAP="max"
|
|
# checking swap is currently disabled for v2
|
|
#CGROUP_MEMORY=$CGROUP_PATH
|
|
;;
|
|
esac
|
|
|
|
# check that initial values were properly set
|
|
check_cgroup_value "cpuset.cpus" 0
|
|
if [[ "$CGROUP_UNIFIED" = "yes" ]] && ! grep -qw memory "$CGROUP_PATH/cgroup.controllers"; then
|
|
# This happen on containerized environment because "echo +memory > /sys/fs/cgroup/cgroup.subtree_control" fails with EINVAL
|
|
skip "memory controller not available"
|
|
fi
|
|
check_cgroup_value $MEM_LIMIT 33554432
|
|
check_cgroup_value $MEM_RESERVE 25165824
|
|
check_cgroup_value "pids.max" 20
|
|
|
|
# update cpuset if supported (i.e. we're running on a multicore cpu)
|
|
cpu_count=$(grep -c '^processor' /proc/cpuinfo)
|
|
if [ $cpu_count -gt 1 ]; then
|
|
runc update test_update --cpuset-cpus "1"
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpuset.cpus" 1
|
|
fi
|
|
|
|
# update memory limit
|
|
runc update test_update --memory 67108864
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value $MEM_LIMIT 67108864
|
|
if [[ -n "${RUNC_USE_SYSTEMD}" ]] ; then
|
|
if [ "$CGROUP_UNIFIED" != "yes" ]; then
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "MemoryLimit=" "MemoryLimit=67108864"
|
|
else
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "MemoryMax=" "MemoryMax=67108864"
|
|
fi
|
|
fi
|
|
|
|
runc update test_update --memory 50M
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value $MEM_LIMIT 52428800
|
|
if [[ -n "${RUNC_USE_SYSTEMD}" ]] ; then
|
|
if [ "$CGROUP_UNIFIED" != "yes" ]; then
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "MemoryLimit=" "MemoryLimit=52428800"
|
|
else
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "MemoryMax=" "MemoryMax=52428800"
|
|
fi
|
|
fi
|
|
|
|
# update memory soft limit
|
|
runc update test_update --memory-reservation 33554432
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "$MEM_RESERVE" 33554432
|
|
|
|
# Run swap memory tests if swap is available
|
|
if [ -f "$CGROUP_MEMORY/$MEM_SWAP" ]; then
|
|
# try to remove memory swap limit
|
|
runc update test_update --memory-swap -1
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "$MEM_SWAP" $SYSTEM_MEM_SWAP
|
|
|
|
# update memory swap
|
|
runc update test_update --memory-swap 96468992
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "$MEM_SWAP" 96468992
|
|
fi
|
|
|
|
# try to remove memory limit
|
|
runc update test_update --memory -1
|
|
[ "$status" -eq 0 ]
|
|
|
|
# check memory limit is gone
|
|
check_cgroup_value $MEM_LIMIT $SYSTEM_MEM
|
|
|
|
# check swap memory limited is gone
|
|
if [ -f "$CGROUP_MEMORY/$MEM_SWAP" ]; then
|
|
check_cgroup_value $MEM_SWAP $SYSTEM_MEM
|
|
fi
|
|
|
|
# update pids limit
|
|
runc update test_update --pids-limit 10
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "pids.max" 10
|
|
if [[ -n "${RUNC_USE_SYSTEMD}" ]] ; then
|
|
check_systemd_value "runc-cgroups-integration-test.scope" "TasksMax=" "TasksMax=10"
|
|
fi
|
|
|
|
# Revert to the test initial value via json on stdin
|
|
runc update -r - test_update <<EOF
|
|
{
|
|
"memory": {
|
|
"limit": 33554432,
|
|
"reservation": 25165824
|
|
},
|
|
"cpu": {
|
|
"shares": 100,
|
|
"quota": 500000,
|
|
"period": 1000000,
|
|
"cpus": "0"
|
|
},
|
|
"pids": {
|
|
"limit": 20
|
|
}
|
|
}
|
|
EOF
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpuset.cpus" 0
|
|
check_cgroup_value $MEM_LIMIT 33554432
|
|
check_cgroup_value $MEM_RESERVE 25165824
|
|
check_cgroup_value "pids.max" 20
|
|
|
|
# redo all the changes at once
|
|
runc update test_update \
|
|
--cpu-period 900000 --cpu-quota 600000 --cpu-share 200 \
|
|
--memory 67108864 --memory-reservation 33554432 \
|
|
--pids-limit 10
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value $MEM_LIMIT 67108864
|
|
check_cgroup_value $MEM_RESERVE 33554432
|
|
check_cgroup_value "pids.max" 10
|
|
|
|
# reset to initial test value via json file
|
|
cat << EOF > $BATS_TMPDIR/runc-cgroups-integration-test.json
|
|
{
|
|
"memory": {
|
|
"limit": 33554432,
|
|
"reservation": 25165824
|
|
},
|
|
"cpu": {
|
|
"shares": 100,
|
|
"quota": 500000,
|
|
"period": 1000000,
|
|
"cpus": "0"
|
|
},
|
|
"pids": {
|
|
"limit": 20
|
|
}
|
|
}
|
|
EOF
|
|
|
|
runc update -r $BATS_TMPDIR/runc-cgroups-integration-test.json test_update
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpuset.cpus" 0
|
|
check_cgroup_value $MEM_LIMIT 33554432
|
|
check_cgroup_value $MEM_RESERVE 25165824
|
|
check_cgroup_value "pids.max" 20
|
|
}
|
|
|
|
@test "update cgroup v1 cpu limits" {
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
requires cgroups_v1
|
|
|
|
# run a few busyboxes detached
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update
|
|
[ "$status" -eq 0 ]
|
|
|
|
# check that initial values were properly set
|
|
check_cgroup_value "cpu.cfs_period_us" 1000000
|
|
check_cgroup_value "cpu.cfs_quota_us" 500000
|
|
check_cgroup_value "cpu.shares" 100
|
|
|
|
# update cpu-period
|
|
runc update test_update --cpu-period 900000
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpu.cfs_period_us" 900000
|
|
|
|
# update cpu-quota
|
|
runc update test_update --cpu-quota 600000
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpu.cfs_quota_us" 600000
|
|
|
|
# update cpu-shares
|
|
runc update test_update --cpu-share 200
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpu.shares" 200
|
|
|
|
# Revert to the test initial value via json on stding
|
|
runc update -r - test_update <<EOF
|
|
{
|
|
"cpu": {
|
|
"shares": 100,
|
|
"quota": 500000,
|
|
"period": 1000000
|
|
}
|
|
}
|
|
EOF
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpu.cfs_period_us" 1000000
|
|
check_cgroup_value "cpu.cfs_quota_us" 500000
|
|
check_cgroup_value "cpu.shares" 100
|
|
|
|
# redo all the changes at once
|
|
runc update test_update \
|
|
--cpu-period 900000 --cpu-quota 600000 --cpu-share 200
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpu.cfs_period_us" 900000
|
|
check_cgroup_value "cpu.cfs_quota_us" 600000
|
|
check_cgroup_value "cpu.shares" 200
|
|
|
|
# reset to initial test value via json file
|
|
cat << EOF > $BATS_TMPDIR/runc-cgroups-integration-test.json
|
|
{
|
|
"cpu": {
|
|
"shares": 100,
|
|
"quota": 500000,
|
|
"period": 1000000
|
|
}
|
|
}
|
|
EOF
|
|
|
|
runc update -r $BATS_TMPDIR/runc-cgroups-integration-test.json test_update
|
|
[ "$status" -eq 0 ]
|
|
check_cgroup_value "cpu.cfs_period_us" 1000000
|
|
check_cgroup_value "cpu.cfs_quota_us" 500000
|
|
check_cgroup_value "cpu.shares" 100
|
|
}
|
|
|
|
@test "update rt period and runtime" {
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
requires cgroups_rt
|
|
|
|
# run a detached busybox
|
|
runc run -d --console-socket $CONSOLE_SOCKET test_update_rt
|
|
[ "$status" -eq 0 ]
|
|
|
|
runc update -r - test_update_rt <<EOF
|
|
{
|
|
"cpu": {
|
|
"realtimePeriod": 800001,
|
|
"realtimeRuntime": 500001
|
|
}
|
|
}
|
|
EOF
|
|
check_cgroup_value "cpu.rt_period_us" 800001
|
|
check_cgroup_value "cpu.rt_runtime_us" 500001
|
|
|
|
runc update test_update_rt --cpu-rt-period 900001 --cpu-rt-runtime 600001
|
|
|
|
check_cgroup_value "cpu.rt_period_us" 900001
|
|
check_cgroup_value "cpu.rt_runtime_us" 600001
|
|
}
|