Configure virtual CPU pinning

To improve the CPU performance of a virtual machine (VM), you can pin a virtual CPU (vCPU) to a specific physical CPU thread on the host. This ensures that the vCPU has its own dedicated physical CPU thread, which can significantly improve the vCPU performance.

About this task

In addition to pinning vCPUs, you can also pin QEMU process threads associated with a specified VM to a specific host CPU to optimize CPU performance.

Procedure

  1. Check the CPU topology on the host:
    # lscpu -p=node,cpu
    Node,CPU
    0,0
    0,1
    0,2
    0,3
    0,4
    0,5
    0,6
    0,7
    1,0
    1,1
    1,2
    1,3
    1,4
    1,5
    1,6
    1,7

    In this example, the output contains NUMA nodes and the available physical CPU threads on the host.

  2. Check the number of vCPU threads inside the VM:
    # lscpu -p=node,cpu
    Node,CPU
    0,0
    0,1
    0,2
    0,3

    In this example, the output contains NUMA nodes and the available vCPU threads inside the VM.

  3. Pin specific vCPU threads from a VM to a specific host CPU or range of CPUs. This is a generally safe method of vCPU performance improvement.

    For example, the following commands pin vCPU threads 0 to 3 of the testguest6 VM to host CPUs 1, 3, 5, 7, respectively:

    # virsh vcpupin testguest6 0 1
    # virsh vcpupin testguest6 1 3
    # virsh vcpupin testguest6 2 5
    # virsh vcpupin testguest6 3 7
  4. Optional: Verify whether the vCPU threads are successfully pinned to CPUs.
    # virsh vcpupin testguest6
    VCPU   CPU Affinity
    ----------------------
    0      1
    1      3
    2      5
    3      7
  5. Optional: After pinning vCPU threads, you can also pin QEMU process threads associated with a specified VM to a specific host CPU or range of CPUs. This can further help the QEMU process to run more efficiently on the physical CPU.

    For example, the following commands pin the QEMU process thread of testguest6 to CPUs 2 and 4, and verify this was successful:

    # virsh emulatorpin testguest6 2,4
    # virsh emulatorpin testguest6
    emulator: CPU Affinity
    ----------------------------------
           *: 2,4