Part 2 of 6 · 2 min read
Virtual processors
Think of a shared meeting room booked by many teams. Each team believes it has the room all day, but a booking system hands it out in turns. The analogy stops in one place: a room is empty between meetings, while a virtual processor that is not running has its state saved and can be resumed at once.
In plain words: Each virtual processor (vCPU) of a guest is, on the host, a thread that the host scheduler (Level 3) runs on a real core. When the thread runs, the guest's code runs. When the host preempts it, the guest's processor has stopped, though the guest cannot see that. A host may promise more vCPUs than it has cores: this is processor overcommit. It works because guests are idle most of the time. If all guests are busy together, each one gets a share and runs slower. A guest that holds a lock when its vCPU is descheduled makes other vCPUs of the same guest spin for nothing, which is the lock-holder preemption problem (Level 4). The guest also sees time jump, because the host paused it.
A worked example. A host has 16 cores and runs 8 guests of 4 vCPUs each, so 32 vCPUs on 16 cores: an overcommit ratio of 2 to 1. If 4 guests are busy and 4 are idle, the busy ones need 16 vCPUs, which the 16 cores supply, and each busy vCPU runs at full speed. If all 8 are busy, 32 vCPUs share 16 cores and each gets 16/32 = 50 per cent of a core.
The key lines. The guest's own scheduler picks which guest thread runs on a vCPU, and the host's scheduler picks which vCPU runs on a core. There are two schedulers, and they do not know each other's plans.
Virtual processor
A virtual processor (vCPU) is the processor that a guest sees. The hypervisor implements it as a schedulable entity (on KVM, a thread of the host) that runs guest code in guest mode when it holds a real core.
Example: a guest with 4 vCPUs is 4 host threads, and each is scheduled like any other thread.
Check yourself: 1. What is a vCPU on the host? 2. What does an overcommit ratio of 2 to 1 mean? 3. Why can a descheduled vCPU slow other vCPUs of the same guest?