Skip to content

Commit cd8bc4c

Browse files
committed
server: implement swap support
Signed-off-by: Peter Hunt <[email protected]>
1 parent 9ab385d commit cd8bc4c

4 files changed

Lines changed: 56 additions & 9 deletions

File tree

server/container_create_linux.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,18 @@ func (s *Server) createSandboxContainer(ctx context.Context, ctr ctrIface.Contai
367367
return nil, err
368368
}
369369
specgen.SetLinuxResourcesMemoryLimit(memoryLimit)
370-
if node.CgroupHasMemorySwap() {
371-
specgen.SetLinuxResourcesMemorySwap(memoryLimit)
370+
if resources.MemorySwapLimitInBytes != 0 {
371+
if resources.MemorySwapLimitInBytes < resources.MemoryLimitInBytes {
372+
return nil, errors.Errorf(
373+
"container %s create failed because memory swap limit (%d) cannot be lower than memory limit (%d)",
374+
ctr.ID(),
375+
resources.MemorySwapLimitInBytes,
376+
resources.MemoryLimitInBytes,
377+
)
378+
}
379+
memoryLimit = resources.MemorySwapLimitInBytes
372380
}
381+
specgen.SetLinuxResourcesMemorySwap(memoryLimit)
373382
}
374383

375384
specgen.SetProcessOOMScoreAdj(int(resources.OomScoreAdj))

test/cgroups.bats

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,37 @@ function teardown() {
3636
output=$(systemctl status "crio-conmon-$pod_id.scope")
3737
[[ "$output" == *"customcrioconmon.slice"* ]]
3838
}
39+
40+
@test "ctr with swap should be configured" {
41+
if ! grep -v Filename < /proc/swaps; then
42+
skip "swap not enabled"
43+
fi
44+
start_crio
45+
# memsw should be greater than or equal to memory limit
46+
# 210763776 = 1024*1024*200
47+
jq ' .linux.resources.memory_swap_limit_in_bytes = 210763776
48+
| .linux.resources.memory_limit_in_bytes = 209715200' \
49+
"$TESTDATA"/container_sleep.json > "$newconfig"
50+
51+
ctr_id=$(crictl run "$newconfig" "$TESTDATA"/sandbox_config.json)
52+
set_swap_fields_given_cgroup_version
53+
54+
if test -r "$CGROUP_MEM_SWAP_FILE"; then
55+
output=$(crictl exec --sync "$ctr_id" sh -c "cat $CGROUP_MEM_SWAP_FILE")
56+
[[ "$output" == "210763776" ]]
57+
fi
58+
}
59+
60+
@test "ctr with swap should fail when swap is lower" {
61+
if ! grep -v Filename < /proc/swaps; then
62+
skip "swap not enabled"
63+
fi
64+
start_crio
65+
# memsw should be greater than or equal to memory limit
66+
# 210763776 = 1024*1024*200
67+
jq ' .linux.resources.memory_swap_limit_in_bytes = 209715200
68+
| .linux.resources.memory_limit_in_bytes = 210763776' \
69+
"$TESTDATA"/container_sleep.json > "$newconfig"
70+
71+
! crictl run "$newconfig" "$TESTDATA"/sandbox_config.json
72+
}

test/ctr.bats

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,7 @@ function check_oci_annotation() {
731731
ctr_id=$(crictl create "$pod_id" "$TESTDATA"/container_redis.json "$TESTDATA"/sandbox_config.json)
732732
crictl start "$ctr_id"
733733

734-
# set memory {,swap} max file for cgroupv1 or v2
735-
CGROUP_MEM_SWAP_FILE="/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
736-
CGROUP_MEM_FILE="/sys/fs/cgroup/memory/memory.limit_in_bytes"
737-
if is_cgroup_v2; then
738-
CGROUP_MEM_SWAP_FILE="/sys/fs/cgroup/memory.swap.max"
739-
CGROUP_MEM_FILE="/sys/fs/cgroup/memory.max"
740-
fi
734+
set_swap_fields_given_cgroup_version
741735

742736
output=$(crictl exec --sync "$ctr_id" sh -c "cat $CGROUP_MEM_FILE")
743737
[[ "$output" == *"209715200"* ]]

test/helpers.bash

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,13 @@ runtime_type = "$RUNTIME_TYPE"
566566
allowed_annotations = ["$ANNOTATION"]
567567
EOF
568568
}
569+
570+
function set_swap_fields_given_cgroup_version() {
571+
# set memory {,swap} max file for cgroupv1 or v2
572+
export CGROUP_MEM_SWAP_FILE="/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
573+
export CGROUP_MEM_FILE="/sys/fs/cgroup/memory/memory.limit_in_bytes"
574+
if is_cgroup_v2; then
575+
export CGROUP_MEM_SWAP_FILE="/sys/fs/cgroup/memory.swap.max"
576+
export CGROUP_MEM_FILE="/sys/fs/cgroup/memory.max"
577+
fi
578+
}

0 commit comments

Comments
 (0)