Skip to main content

CPU Pinning

How to do CPU pinning (work in progress)

Create a file under /etc/pve/qemu-server/$vmid.cpuset for the VM which you want to set the CPU pinning for.

Examples:

0-1
0-1,6-7

Depending on your CPU topology assign the proper cores to your VM. You can find out the topology out by running lscpu -e

Next create a file called taskset-hook.sh and save it under /var/lib/vz/snippets/, create the snippets folder if it's not there already and make the script executable.

Content:

#!/bin/bash

vmid="$1"
phase="$2"

if [[ "$phase" == "post-start" ]]; then
    main_pid="$(< /run/qemu-server/$vmid.pid)"

    #cpuset="0-11"
    cpuset="$(< /etc/pve/qemu-server/$vmid.cpuset)"

    taskset --cpu-list  --all-tasks --pid "$cpuset" "$main_pid"
fi
mkdir /var/lib/vz/snippets
cp taskset-hook.sh /var/lib/vz/snippets
chmod +x  /var/lib/vz/snippets/taskset-hook.sh

Finally set the script to the VM:
qm set VMID --hookscript local:snippets/taskset-hook.sh

If for some reason the script is not working for you or you just want it to be removed, simply open /etc/pve/qemu-server/VMID.conf and remove the script.


Sources: