Skip to content

Commit

Permalink
tests/cpu-vm: Update VM CPU pinning tests for `limits.cpu.pin_strateg…
Browse files Browse the repository at this point in the history
…y` config option

Check if feature is present by checking API extension limits_cpu_pin_strategy (landing with Add limits.cpu.pin_strategy setting to disable VM CPU auto pinning by default lxd#14171); and
Test VM with auto cpu pinning and no cpu pinning.

Signed-off-by: Kadin Sayani <kadin.sayani@canonical.com>
  • Loading branch information
kadinsayani committed Oct 2, 2024
1 parent 59f2a80 commit 03982bc
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions tests/cpu-vm
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ if ! hasNeededAPIExtension cpu_hotplug; then
exit 0
fi

# required for "CPU auto pinning" feature check
# as we don't have a separate API extension for it
# and we rely on the debug output in the LXD daemon logs.
snap set lxd daemon.debug=true
systemctl restart snap.lxd.daemon.service

# Configure LXD
lxc network create lxdbr0
lxc profile device add default eth0 nic network=lxdbr0
Expand Down Expand Up @@ -79,16 +73,39 @@ for i in $(seq "${cpuCount}" -1 1); do
[ "$(lxc exec v1 -- ls /sys/devices/system/cpu | grep -xEc 'cpu[[:digit:]]+')" -eq "${i}" ]
done

# Try doing pinning while VM is running (shouldn't work)
! lxc config set v1 limits.cpu=1,2 || false
echo "==> Check that there is no CPU pinning set"
QEMU_PID=$(lxc info v1 | awk '/^PID:/ {print $2}')
! taskset --cpu-list -a -p "${QEMU_PID}" | grep -E ':\s+[0-9]+$' || false
taskset --cpu-list -a -p "${QEMU_PID}" | grep "0-$((cpuCount-1))"

# Set max CPU count
lxc config set v1 limits.cpu="${cpuCount}"
[ "$(lxc exec v1 -- ls /sys/devices/system/cpu | grep -xEc 'cpu[[:digit:]]+')" -eq "${cpuCount}" ]

# check that CPU affinity is automatically set if feature present
QEMU_PID=$(lxc info v1 | awk '/^PID:/ {print $2}')
if journalctl --quiet --no-hostname --no-pager --boot=0 --unit=snap.lxd.daemon.service | grep "Scheduler: virtual-machine"; then
# Try doing pinning while VM is running (shouldn't work)
! lxc config set v1 limits.cpu=1,2 || false

# Unset CPU limit
lxc config unset v1 limits.cpu

# Unsetting the limit should leave the VM with 1 CPU
[ "$(lxc exec v1 -- ls /sys/devices/system/cpu | grep -xEc 'cpu[[:digit:]]+')" -eq "1" ]

if hasNeededAPIExtension limits_cpu_pin_strategy; then
echo "==> Check CPU auto pinning when limits.cpu.pin_strategy=auto"

# Stop VM
lxc stop -f v1
lxc launch "${IMAGE}" v1 --vm -c limits.cpu.pin_strategy=auto -s "${poolName}" --ephemeral
waitInstanceReady v1
lxc info v1

lxc config set v1 limits.cpu="${cpuCount}"

# Try changing limits.cpu.pin_strategy while VM is running (shouldn't work)
! lxc config set v1 limits.cpu.pin_strategy=none || false

QEMU_PID=$(lxc info v1 | awk '/^PID:/ {print $2}')
# Check that there are processes with pinning set
# It will be shown like this (for limits.cpu=2):
# pid 2894's current affinity list: 6
Expand All @@ -99,20 +116,9 @@ if journalctl --quiet --no-hostname --no-pager --boot=0 --unit=snap.lxd.daemon.s
# 2894 and 2895 have affinity set, while others don't
PINNED_THREADS_NUM=$(taskset --cpu-list -a -p "${QEMU_PID}" | grep -cE ':\s+[0-9]+$')
[ "${PINNED_THREADS_NUM}" -ge "$(lxc config get v1 limits.cpu)" ]
else
# check that there is no pinning set
! taskset --cpu-list -a -p "${QEMU_PID}" | grep -E ':\s+[0-9]+$' || false
taskset --cpu-list -a -p "${QEMU_PID}" | grep "0-$((cpuCount-1))"
fi

# Unset CPU limit
lxc config unset v1 limits.cpu

# Unsetting the limit should leave the VM with 1 CPU
[ "$(lxc exec v1 -- ls /sys/devices/system/cpu | grep -xEc 'cpu[[:digit:]]+')" -eq "1" ]

echo "==> Stopping and deleting ephemeral VM"
# Stop VM and check its deleted.
lxc stop -f v1
! lxc info v1 || false

Expand Down

0 comments on commit 03982bc

Please sign in to comment.