Binding VMs to Cores
Purpose
Binding VMs to cores can effectively reduce the overhead of context switching and load balancing on the OS, thereby improving the execution efficiency of applications.
Procedure
- Open the XML configuration file of the VM. In the following command, vm1 indicates the VM name. Replace it with the actual one.
1virsh edit vm1
- Modify the content in the configuration file as follows:
1 2 3 4 5 6 7 8 9 10 11 12
<domain type = 'KVM'> ... <vcpu placement = 'static' cpuset='4-7'>4</vcpu> <cputune> <vcpupin vcpu='0' cpuset='4'/> <vcpupin vcpu='1' cpuset='5'/> <vcpupin vcpu='2' cpuset='6'/> <vcpupin vcpu='3' cpuset='7'/> <emulatorpin cpuset='4-7'/> </cputune> ... <domain>
- emulatorpin cpuset='4-7' indicates that the QEMU main thread is bound to physical CPUs 4 to 7.
- vcpu placement = 'static' cpuset='4-7' indicates that only cores 4 to 7 (four cores) can be used when this parameter is used for I/O threads or worker threads. If this parameter is not set, the VM task threads float on random CPU cores, causing more losses due to cross-NUMA and cross-die overhead.
- vcpupin is used to restrict one-to-one binding between VM CPU threads and physical cores. If vcpupin is not used to bind CPU threads to cores, the threads will switch between cores 4 to 7, causing extra overhead.
- Press Esc, type :wq!, and press Enter to save the file and exit.
Parent topic: VM Tuning