What is CPU pinning?
Generally in virtualization, the host will spread the workload of a VM accross all of the available host cores. However, with CPU pinning, you can ‘pin’ the tasks of a VM to specific cores on the host, allowing it to fully utilize those cores and ‘blocking’ them from performing any other tasks. This can greatly increase CPU performance on a VM and provide near-native CPU performance on thoe cores.
Pinning the cores
SSH into the host (Proxmox) and run the command:
1
taskset --cpu-list --all-tasks --pid 0-7 $(< /run/qemu-server/103.pid)
In this case, replace 0-9
with the number of CPU cores you want to pin to this virtual machine. note: CPU core count starts at 0, so in this example we are pinning 8 cores to the VM with ID 103. In addition, replace 103.pid
with the ID number of the VM you’d like to pin.
Checking if the cores are pinned
You can easily check if the CPU pinning has been successful by running htop
on the host machine and stressing the VM (e.g. stress --cpu [number of cores]
)
Automating the process
We can automate this process in Proxmox by creating a ‘hookscript’. In this example, we create a hook script for VM 103, to permanently pin 8 CPU cores to it.
1
nano /etc/pve/qemu-server/103.conf
to the bottom of the file, add:
1
hookscript: local:snippets/cpu-pinning-103.sh
Navigate to the local data store
1
cd /var/lib/vz
If there isn’t a snippets
directory, go ahead and create one, then create the hookscript inside it:
1
cd snippets && nano cpu-pinning-103.sh
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
vmid="$1"
phase="$2"
cpuset="0-7"
if [[ "$phase" == "post-start" ]]; then
main_pid="$(< /run/qemu-server/$vmid.pid)"
taskset --cpu-list --all-tasks --pid "$cpuset" "$main_pid"
fi
Exit out of nano and make the script executable:
1
chmod +x cpu-pinning-103.sh
You can also ‘unhook’ this script from the VM at anytime from the web GUI, as there should now be a “Hookscript” option at the bottom of the “Options” tab in your VM’s pane.
That’s it, you’re done! For pre-built, pre-configured GPU servers for AI, be sure to visit: