The idea: move a running VM and pause it only briefly

A cloud provider sometimes has to empty a host: to patch it, to replace a part, or to spread the load. Live migration moves a running virtual machine to another host while it keeps working. Its users notice at most a short pause, the downtime. The hard part is the memory: gigabytes that the guest keeps changing while they are copied.

Two time lines for the database VM. Pre-copy: round 0 sends 4 GB in 4 s while the VM runs on the source, rounds 1 and 2 are shorter, then a 200 ms pause. Post-copy: an 8 ms pause first, then the VM runs on the target while pages are pushed until 4.008 s, with page faults over the network along the way.
Pre-copy copies first and pauses at the end; post-copy pauses first and copies while the guest runs on the target.

The page follows the pre-copy design of Clark et al. (NSDI 2005), which QEMU/KVM, Xen and VMware all use, and shows post-copy as the alternative. The canvas draws two KVM hosts joined by a 10 Gbit/s link. Each host shows the guest's 4 GB of memory as 250 squares of 4,000 pages (16 MB each).

What has to move

  • Memory: 4 GB here, 1,000,000 pages of 4 KB. This is almost all of the work.
  • CPU and device state: registers of each vCPU, timers, the state of the virtual disk and network cards. A few megabytes; the page uses 8 MB.
  • The disk: in a cloud it usually sits on shared network storage that both hosts can reach, so it does not move.
  • The network address: the VM keeps its MAC and IP address. After the move the target sends a gratuitous ARP (QEMU sends RARP announce packets) so that the switch sends the VM's traffic to the new port. Open TCP connections survive.

Pre-copy rounds and the dirty bitmap

The target starts QEMU with -incoming and waits. The source turns on dirty logging: KVM write-protects the guest's memory, and every write the guest makes sets one bit in a dirty bitmap, one bit per page. Then the rounds begin:

  1. Round 0 sends every page. The guest keeps running and writing.
  2. Each later round reads and clears the bitmap and sends only the pages marked in it: the pages written since they were last sent. On the target, the old copies of those pages are stale (yellow on the canvas).
  3. When the pages left are few enough to send within the downtime budget, the source pauses the guest (stop-and-copy), sends them with the CPU state, and the guest resumes on the target.

Demo: pre-copy converges (database VM). The database writes 400 MB/s (100,000 pages per second) to a working set of 1.2 GB. The link carries 1,000 MB/s, so each round lasts 0.4 times as long as the one before:

RoundPages sentTimeDirtied meanwhileTime to send those
01,000,0004 s300,0001.2 s > 300 ms
1300,0001.2 s120,000480 ms > 300 ms
2120,000480 ms48,000192 ms ≤ 300 ms ✓
stop48,000 + 8 MB state200 msguest pauseddowntime 200 ms

The whole move takes 5.88 s and sends 5,880 MB for a 4 GB VM. The guest is paused for only 200 ms of it. In round 0 the dirtied pages stop at 300,000 although the guest writes for 4 s: it keeps rewriting the same 1.2 GB, its writable working set. Demo: idle web server: one round is enough writes only 20 MB/s, so after round 0 just 20,000 pages are dirty: 88 ms of downtime, 4.088 s in all.

When pre-copy converges, and when it does not

Each round sends what the guest dirtied during the round before. With dirty rate D and link speed B, a round lasts about D / B times as long as the previous one, until the dirtied pages reach the whole working set. So the rounds shrink only while the guest writes slower than the link carries.

Bar chart of pages left to send. Database: 1,000,000, then 300,000, 120,000 and 48,000, under the 75,000-page line after round 2. Write-heavy: 1,000,000, then 600,000 after every round, always above the line.
The database's rounds shrink by 0.4 each time and drop under the budget line; the write-heavy VM stays at 600,000 pages for ever.

Demo: write-heavy VM never converges runs a memory benchmark that writes 1,200 MB/s over a 2.4 GB working set. That is faster than the 1,000 MB/s link, so every round ends with the whole working set dirty again: 600,000 pages, 2.4 s to send. QEMU would keep trying for ever. The page gives up after 6 rounds (Clark et al.'s Xen stopped after 30) and forces the switch: 2.408 s of downtime, 8 times the limit, after 18.408 s and 18,408 MB sent, 4.6 times the VM's memory.

The QEMU/KVM knobs

ParameterWhat it doesOn the page
downtime-limitThe longest pause QEMU accepts. It switches when the rest can be sent within it. Default 300 ms.300, 100 or 30 ms
max-bandwidthCaps the migration stream, so the VM's own traffic still gets through.1,000 MB/s on a 10 Gbit/s link
auto-convergeSlows the guest's vCPUs down step by step (cpu-throttle-initial, cpu-throttle-increment) until it dirties less than the link carries.not drawn
postcopy-ramAllows switching to post-copy (migrate-start-postcopy).method: post-copy

A smaller downtime-limit means more rounds. Demo: tighter downtime-limit (30 ms) moves the database VM in 6 rounds instead of 3: downtime 20.3 ms instead of 200 ms, but 6 s and 6,000 MB in all. On the QEMU monitor a migration looks like this:

(target)  qemu-system-x86_64 ... -incoming tcp:0:4444
(source)  migrate_set_parameter downtime-limit 300
          migrate_set_parameter max-bandwidth 1000M
          migrate -d tcp:target-host:4444
          info migrate

Tools such as libvirt (virsh migrate --live) and OpenStack drive the same commands.

Post-copy: switch first, fetch pages on fault

Post-copy turns the order around. It pauses the guest, sends only the CPU and device state (8 ms), and resumes the guest on the target with none of its memory there. The source then pushes the pages in the background. When the guest touches a page that has not arrived, it takes a page fault over the network: the target (through Linux's userfaultfd) asks the source for that page first, and the vCPU waits for the answer.

Demo: post-copy for the write-heavy VM moves the VM that pre-copy could not:

Write-heavy VMPre-copyPost-copy
downtime2.408 s ✗8 ms ✓
total time18.408 s4.008 s
data sent18,408 MB4,008 MB: each page once
cost while movingnone: the guest runs at full speed on the source101 network faults; the guest waits 2.013 s in all

Post-copy always finishes, in one pass over memory. Its price is a slow guest right after the switch: every first touch of a missing page costs a network round trip instead of a memory access. In the database VM the guest waits 1.024 s over 51 faults. Many tools start with pre-copy and switch to post-copy only if it does not converge.

When the link fails

The two methods fail very differently. In Demo: link fails during pre-copy the link breaks halfway through round 1. Nothing is lost: the source still holds the whole, current VM, which never stopped. The target throws its copy away, and the downtime is 0. In Demo: link fails during post-copy the link breaks after 1.6 s. The guest already runs on the target, but 150 of its 250 squares are still on the source. Its next fault cannot be served, so QEMU stops it (postcopy-paused). It can resume with migrate-recover when the link comes back; if the source host has died, the VM is lost.

How the guest runs on the CPU, and how KVM and QEMU split the work, is in hypervisor VM exits; dirty logging works by write-protecting guest pages in the EPT shown there. Containers vs VMs compares a VM with a container, and cloud service models shows the IaaS rentals that providers move around like this. Pages and page faults inside one machine are in virtual memory, and the gratuitous ARP in ARP.

What the page leaves out

Real dirty rates change from second to second; the page keeps them fixed and uses decimal toy units (4 GB = 1,000,000 pages of 4 KB). A grid square stands for 4,000 pages; in post-copy a fault fetches the whole square in one 16 ms slot, while QEMU fetches a 4 KB page (or a 2 MB huge page) and prefetches its neighbours. Real QEMU also skips zero pages, can compress or send only changed bytes (xbzrle), uses several streams (multifd), and estimates the bandwidth instead of knowing it. Moving the disk as well (block migration), auto-converge in action, the cost of dirty logging on the guest, and migration between hosts with different CPUs are not shown.

On the mind map

Open the Operating Systems mind mapOperatingSystemsBoot and program start: from power on to main()Boot and program startProcesses and the kernel: how user code talks to the kernelProcesses and the kernelInterrupts and devices: hardware interrupts the CPUInterrupts and devicesScheduling and deadlock: who runs next, who gets whatScheduling and deadlockVirtual memory: every address is translatedVirtual memoryPhysical memory: who gets which bytes of RAMPhysical memoryFiles and storage: from open() down to the diskFiles and storageDisks and RAID: blocks down to platter or flashDisks and RAIDIsolation and security: limit what a process can doIsolation and securityPermissions and capabilitiesPermissions and capabilitieseuid against owner, group, other rwxContainer internalsContainer internalsnamespaces hide, cgroups limitDocker image layersDocker image layersread-only layers, cached by contentseccomp and eBPFseccomp and eBPFa BPF filter judges every syscallContainers vs VMsContainers vs VMsVM: own guest kernel; container: sharedCloud service modelsCloud service modelsIaaS/PaaS/SaaS: who manages which layerHow a hypervisor runs a VMHow a hypervisor runs a VMGuest traps, VMM emulates, resumesVM live migrationCopy dirty pages until the rest fitsOpen the Operating Systems mind mapOperating Systemslimit what a process can doIsolation and securityPermissions and capabilitiesPermissions and capabilitieseuid against owner, group, other rwxContainer internalsContainer internalsnamespaces hide, cgroups limitDocker image layersDocker image layersread-only layers, cached by contentseccomp and eBPFseccomp and eBPFa BPF filter judges every syscallContainers vs VMsContainers vs VMsVM: own guest kernel; container: sharedCloud service modelsCloud service modelsIaaS/PaaS/SaaS: who manages which layerHow a hypervisor runs a VMHow a hypervisor runs a VMGuest traps, VMM emulates, resumesVM live migrationCopy dirty pages until the rest fits
This page on the Operating Systems mind map · 78 pages in 9 branches · open it to see the whole map and test yourself in recall mode.