Skip to content

Commit

Permalink
tests/cgroup: take into account LXCFS support for swap accounting
Browse files Browse the repository at this point in the history
Older LXCFS versions (4.0.x and 5.0.x branches) do not handle
swap accounting on cgroup2 properly. While we are improving cgroup2
tests for a newer LXD versions we don't want to make tests red on
the older ones. So, let's check if LXCFS declares support for
swap accounting and only in this case make any checks for it.

See also:
lxc/lxcfs#663

Signed-off-by: Alexander Mikhalitsyn <[email protected]>
  • Loading branch information
mihalicyn authored and hamistao committed Oct 10, 2024
1 parent 47582a5 commit 33fb1ea
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions tests/cgroup
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ lxd init --auto

IMAGE="${TEST_IMG:-ubuntu-minimal-daily:24.04}"

rc=0
journalctl --quiet --no-hostname --no-pager --boot=0 --unit=snap.lxd.daemon.service | grep "Kernel supports swap accounting" || rc="$?"
LXCFS_SUPPORTS_SWAP_ACCOUNTING=0
if [ "${rc}" -eq 0 ]; then
LXCFS_SUPPORTS_SWAP_ACCOUNTING=1
fi

# Test
set -x

Expand All @@ -22,7 +29,8 @@ lxc launch "${IMAGE}" c1
echo "==> Validate default values"
[ "$(lxc exec c1 -- nproc)" = "$(nproc)" ]
[ "$(lxc exec c1 -- grep ^MemTotal /proc/meminfo)" = "$(grep ^MemTotal /proc/meminfo)" ]
if [ -e "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" ] || [ -e "/sys/fs/cgroup/system.slice/memory.swap.max" ]; then
if [ -e "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" ] ||
{ [ -e "/sys/fs/cgroup/system.slice/memory.swap.max" ] && [ "${LXCFS_SUPPORTS_SWAP_ACCOUNTING}" -eq 1 ]; }; then
[ "$(lxc exec c1 -- grep ^SwapTotal /proc/meminfo)" = "$(grep ^SwapTotal /proc/meminfo)" ]
else
[ "$(lxc exec c1 -- grep ^SwapTotal /proc/meminfo)" = "SwapTotal: 0 kB" ]
Expand Down Expand Up @@ -110,7 +118,8 @@ echo "==> Testing memory limits"
MEM_LIMIT_MIB=512
lxc config set c1 limits.memory="${MEM_LIMIT_MIB}MiB"
[ "$(lxc exec c1 -- grep ^MemTotal /proc/meminfo)" = "MemTotal: $((MEM_LIMIT_MIB * 1024)) kB" ]
if [ -e "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" ] || [ -e "/sys/fs/cgroup/system.slice/memory.swap.max" ]; then
if [ -e "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" ] ||
{ [ -e "/sys/fs/cgroup/system.slice/memory.swap.max" ] && [ "${LXCFS_SUPPORTS_SWAP_ACCOUNTING}" -eq 1 ]; }; then
[ "$(lxc exec c1 -- grep ^SwapTotal /proc/meminfo)" = "SwapTotal: $((MEM_LIMIT_MIB * 1024)) kB" ]
else
[ "$(lxc exec c1 -- grep ^SwapTotal /proc/meminfo)" = "SwapTotal: 0 kB" ]
Expand All @@ -133,7 +142,8 @@ if [ -e "/sys/fs/cgroup/memory" ]; then
[ "$(lxc exec c1 -- cat /sys/fs/cgroup/memory/memory.swappiness)" = "65" ]
fi

if [ -e "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" ] || [ -e "/sys/fs/cgroup/system.slice/memory.swap.max" ]; then
if [ -e "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes" ] ||
{ [ -e "/sys/fs/cgroup/system.slice/memory.swap.max" ] && [ "${LXCFS_SUPPORTS_SWAP_ACCOUNTING}" -eq 1 ]; }; then
lxc config set c1 limits.memory 128MiB
[ "$(lxc exec c1 -- grep ^MemTotal /proc/meminfo)" = "MemTotal: 131072 kB" ]

Expand Down

0 comments on commit 33fb1ea

Please sign in to comment.