# 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:

```bash
0-1
```

```bash
0-1,6-7
```

Depending on your [CPU topology](https://wiki.archlinux.org/title/PCI_passthrough_via_OVMF#CPU_topology) assign the proper cores to your VM. You can find out the topology 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:

```shell
#!/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
```

```bash
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.

<div id="bkmrk-">  
</div>Sources:

- [https://forum.proxmox.com/threads/cpu-pinning.67805/post-304715](https://forum.proxmox.com/threads/cpu-pinning.67805/post-304715)
- [https://wiki.archlinux.org/index.php/PCI\_passthrough\_via\_OVMF#Performance\_tuning](https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF#Performance_tuning)