Why a virtual CPU is just a thread

Video 076 of 140 in Virtualization Foundations, in the unit “Processes, threads and scheduling”. 4:00, with English and Hindi audio.

A virtual machine says it has four CPUs. Where are those CPUs really? On a KVM host, each one is an ordinary thread, the kind Linux schedules every day. Knowing this explains why a guest can run slowly even when nothing is wrong inside it. This article shows what a virtual CPU is, how it maps to a thread, what steal time means, and how to find the threads behind a virtual machine.

What you will learn

What a virtual CPU is

A virtual CPU, or vCPU, is the CPU that a virtual machine sees and uses. It is backed by time on a real CPU of the host. The guest does not know that it shares the real CPU with other work.

On KVM, the virtualization system built into Linux, each vCPU is a thread inside a QEMU process. QEMU is the program that runs one virtual machine on the host. VMware's hypervisor, called ESXi, has its own scheduler. It is not Linux, but it places vCPUs on physical CPUs for the same purpose.

The word is used in public clouds too. On most x86 machine types, one vCPU is one hardware thread on a real CPU.

What happens when a vCPU runs

Inside the virtual machine, its own operating system runs code on what it believes is a CPU. In reality, the host runs a vCPU thread. When that thread gets a real CPU, the guest runs. Linux schedules the thread like any other thread, so it can wait in line like one.

Sometimes the guest needs the host for a device request. Then the thread steps back to the host for a moment. If no real CPU is free at all, the vCPU thread waits, and the guest sees time pass without running.

Steal time: the guest's report of a wait

Take a host with two real CPUs and three vCPU threads. Two vCPU threads run, and the third waits in line. The guest on the waiting vCPU wants to run, but its thread has no free CPU. Its steal time climbs, which is the guest's record of time it wanted to run and could not run.

When a real CPU frees up, the scheduler moves the waiting vCPU onto it. Steal time is the trace of that wait, and it shows that the host is short of real CPUs.

Find the vCPU threads on a KVM host

Each virtual machine is a QEMU process, and its vCPUs appear as threads in that process. The ps command lists the threads that match qemu, so you can see them.

List the threads of the QEMU processes
ps -eLf | grep qemu | head -n 6

The virsh tool asks libvirt, the tool that manages virtual machines on Linux, about the vCPUs of one machine. It shows the host CPU each vCPU last ran on, its state and its affinity.

Show the vCPUs of one virtual machine
virsh vcpuinfo guest1

Where it goes wrong, and what to check first

In Harbor Point's development cluster, the hosts are promised more vCPUs than real cores. Then the vCPU threads queue for their turn, and each guest waits longer.

Note: Check steal time in the guest, and busy CPUs on the host, before you add more virtual CPUs to any machine.

Find the threads behind a virtual machine

You need a KVM host with a running virtual machine. Replace guest1 in the second step with the name of your own virtual machine.

  1. List the threads of the QEMU processes. Each virtual CPU appears as a thread in this list.

    ps -eLf | grep qemu | head -n 6

    What you should see: Each virtual machine is a QEMU process, and its vCPUs appear as threads in this list.

  2. Ask libvirt about the vCPUs of one virtual machine.

    virsh vcpuinfo guest1

    What you should see: For each vCPU you see the host CPU it last ran on, its state and its affinity.

Terms

Virtual CPU (vCPU)
The CPU that a virtual machine sees and uses. On the host, it is backed by time on a real CPU.
QEMU
The program that runs one virtual machine on a host. On KVM, each virtual machine is one QEMU process.
Steal time
The time a guest wanted to run but could not, because no real CPU on the host was free for its vCPU thread.

Check yourself

On a KVM host, what is a virtual CPU?

A thread inside the QEMU process that runs the virtual machine. Linux schedules it like any other thread.

What does steal time show inside a guest?

The time the guest wanted to run but could not, because no real CPU on the host was free for its vCPU thread.

Where do you first check for steal time?

Inside the guest, where top shows it as steal time. Then check for busy CPUs on the host.

Previous: CPU affinity: pinning a process to chosen CPUs. Next: Server racks and rack units.