Debug KVM Guests With kvm Tracepoints and perf kvm
Find why a KVM guest is slow or stuck by recording kvm tracepoints with trace-cmd and summarizing VM exits by reason with perf kvm stat, with sample output.

When a guest is slow, the question is rarely "what is the guest doing" and nearly always "what is it asking the host for". Every request is a VM exit, and the host kernel can report each one: which reason, from which instruction, how long it took. KVM ships a set of tracepoints for exactly this, and perf kvm adds a summarizing front end. Together they answer the first question about any performance or hang problem, namely which exit dominates, without touching the guest at all. This post shows how to record the kvm tracepoints with trace-cmd, read a trace line, summarize exits with perf kvm stat, find a guest that is spinning or stuck and turn the findings into a fix. It uses Linux 6.12 on an Intel host with QEMU 9.2, and it builds on the host setup in the post on a KVM workstation and the model in the complete guide to the Linux KVM API.
Why exits are the right place to look
Guest code that runs without exiting runs at native speed. Time is lost when the guest exits, because each exit stops the guest, runs hypervisor code and resumes. A guest that spends its time doing arithmetic generates few exits. A guest slowed by emulated devices generates a flood of I/O and MMIO exits, a guest in a lock-contention loop generates PAUSE exits and a guest idling generates HLT exits. The reason code on the exit tells you which situation you are in.
The kernel records exits through tracepoints in arch/x86/kvm/trace.h. The ones used most:
| Tracepoint | Fires when |
|---|---|
kvm_entry | The host enters the guest |
kvm_exit | The guest exits, with the reason code and the guest rip |
kvm_pio | The guest did port I/O handled outside the kernel |
kvm_mmio | The guest accessed MMIO handled outside the kernel |
kvm_inj_virq | An interrupt was injected into the guest |
kvm_apic, kvm_apic_ipi | Local APIC register accesses and inter-processor interrupts |
kvm_page_fault | An EPT violation on guest memory |
kvm_msr | An MSR read or write that exited |
kvm_halt_poll_ns | A halted vCPU's polling window changed |
kvm_cpuid | The guest executed CPUID |
List what your kernel offers:
sudo ls /sys/kernel/tracing/events/kvm/ | head -n 12enable
filter
kvm_age_hva
kvm_apic
kvm_apic_accept_irq
kvm_apic_ipi
kvm_async_pf_completed
kvm_cpuid
kvm_entry
kvm_exit
kvm_fpu
kvm_halt_poll_nsRecord with trace-cmd
trace-cmd is the friendliest front end to the kernel's tracing infrastructure. Record every kvm event for ten seconds while the guest runs its workload:
sudo trace-cmd record -e kvm -o kvm.dat sleep 10
sudo trace-cmd report kvm.dat | head -n 8CPU 0 is empty
cpus=8
CPU 1/KVM-2206 [003] 4981.120311: kvm_exit: vcpu 1 reason EXTERNAL_INTERRUPT rip 0xffffffff81a5c7f6 info1 0x0000000000000000 info2 0x0000000000000000 intr_info 0x800000ec error_code 0x00000000
CPU 1/KVM-2206 [003] 4981.120318: kvm_entry: vcpu 1, rip 0xffffffff81a5c7f6
CPU 1/KVM-2206 [003] 4981.120401: kvm_exit: vcpu 1 reason IO_INSTRUCTION rip 0xffffffff81a2b3d4 info1 0x00000000003f8008 info2 0x0000000000000000 intr_info 0x00000000 error_code 0x00000000
CPU 1/KVM-2206 [003] 4981.120403: kvm_pio: pio_write at 0x3f8 size 1 count 1 val 0x48
CPU 1/KVM-2206 [003] 4981.120420: kvm_entry: vcpu 1, rip 0xffffffff81a2b3d6The fields vary by kernel version, and the values here are an illustration rather than a real capture. Read one line: the task name and thread ID (CPU 1/KVM-2206), the host CPU in brackets, a timestamp in seconds, the event name and its fields. kvm_exit carries the exit reason by name, the guest rip where it happened, and architecture-specific detail words. Here the guest took an external interrupt, then performed port I/O: kvm_pio shows a one-byte write of 0x48 (the letter H) to the serial port at 0x3f8.
Two things become clear at once. The pairs of kvm_exit and kvm_entry bracket the time the host spent handling each exit: the timestamps 4981.120401 and 4981.120420 show about 19 microseconds between the exit and the next entry in this illustration, which includes the user-space emulation of the serial port. And the rip values locate the guest code: resolve them against the guest kernel's System.map or vmlinux to see which function was running.
For a quick, filter-first approach, enable just the events you care about with -e kvm:kvm_exit -e kvm:kvm_pio, or filter inside an event: -f 'reason == 30' on kvm_exit selects one exit reason (30 is EXIT_REASON_IO_INSTRUCTION on Intel). The names in the output are easier to read than the numbers, but the filter takes the number.
Summarize with perf kvm stat
A raw trace is detailed and overwhelming. perf kvm stat aggregates it. Record a session while the workload runs, then report:
sudo perf kvm stat record -a sleep 10
sudo perf kvm stat report --event=vmexitAnalyze events for all VMs, all VCPUs:
VM-EXIT Samples Samples% Time% Min Time Max Time Avg time
EXTERNAL_INTERRUPT 18412 52.31% 7.11% 0.62us 54.20us 1.88us ( +- 1.41% )
HLT 6120 17.39% 81.55% 4.10us 10124.33us 64.80us ( +- 3.20% )
IO_INSTRUCTION 5231 14.86% 5.20% 3.55us 210.07us 4.84us ( +- 2.02% )
MSR_WRITE 2910 8.27% 1.88% 0.55us 22.31us 3.15us ( +- 2.50% )
EPT_VIOLATION 1802 5.12% 3.31% 2.10us 802.51us 8.94us ( +- 4.15% )
Total Samples:35195, Total events handled time:61250.40us.This table is a plausible example, not a measurement, and your numbers will differ with the workload. Read the columns this way. Samples% is the share of exits by count, and Time% is the share of the time spent handling them. They answer different questions. EXTERNAL_INTERRUPT is the most frequent exit and costs little each, so it is mostly noise from host timers. HLT is a small share of the count and most of the time, because a halted vCPU is idle by definition and its "handling time" is the idle time, so a high HLT time share means the guest is mostly idle, which is healthy. IO_INSTRUCTION at 15% of the count suggests emulated port I/O, and a guest doing that much of it is a candidate for a virtio device instead of an emulated one.
Use --event=ioport to break down port I/O by port, and --event=mmio for MMIO by address:
sudo perf kvm stat report --event=ioportAnalyze events for all VMs, all VCPUs:
IO Port Access Samples Samples% Time% Min Time Max Time Avg time
0x3f8:POUT 4810 91.95% 93.20% 3.51us 210.07us 4.91us ( +- 2.10% )
0x60:PIN 201 3.84% 2.51% 3.02us 12.00us 3.40us ( +- 5.00% )
0xcf8:POUT 120 2.29% 3.10% 4.11us 30.33us 6.50us ( +- 6.10% )Here almost all port I/O is output to the serial port at 0x3f8. A guest printing a lot of text to an emulated UART pays an exit per byte, and the fix is a quieter console, a lower log level or a virtio console. That is the type of finding this tool delivers in minutes.
There is also a live mode, perf kvm stat live, which updates the table every second while the guest runs, useful for watching a stuck guest.
Pattern guide: what the exits say
| Observation | Likely cause | Next step |
|---|---|---|
Very high IO_INSTRUCTION or EPT_MISCONFIG counts | Emulated devices on a hot path | Switch to virtio, check ioport and mmio reports |
High PAUSE_INSTRUCTION | Guest spinlock contention, lock-holder preemption | Fewer vCPUs than host cores, pin vCPUs, check guest locks |
Mostly HLT with long times | Guest is idle | Nothing to fix, check that the workload is running |
High EPT_VIOLATION after boot | Guest touching new memory, or host swapping | Pre-fault memory, use huge pages, check host memory pressure |
High MSR_WRITE of a timer MSR | Guest reprogramming timers often | Check guest tickless settings, TSC deadline timer support |
APIC_ACCESS or EOI_INDUCED bursts | Interrupt-heavy guest without APIC virtualization | Check APICv support, enable_apicv in kvm_intel |
EXTERNAL_INTERRUPT dominating | Host interrupts landing on vCPU cores | Move host IRQs off those cores with affinity |
The first column ties directly to counts from perf kvm stat, and the third to the tuning knobs in the KVM and QEMU posts.
A guest that is stuck
A hung guest has one of three signatures in the trace. If the trace is empty, the vCPUs are blocked outside the guest, not entering at all: look at the QEMU main loop or at a wedged device backend, since the guest is waiting for the host. If kvm_entry and kvm_exit alternate rapidly with the same rip and reason, the guest is spinning on an exit such as polling a device register that never changes. The rip and the I/O address tell you which. If the trace shows HLT exits and the interrupt count stops, the guest has halted with interrupts disabled or is waiting for an interrupt that never arrives, so check kvm_inj_virq and kvm_apic_accept_irq for the missing device:
sudo trace-cmd record -e kvm_exit -e kvm_inj_virq -e kvm_mmio -o stuck.dat sleep 3
sudo trace-cmd report stuck.dat | awk '{print $5, $6, $7}' | sort | uniq -c | sort -rn | head -n 5 9120 kvm_exit: vcpu 0
3 kvm_inj_virq: irq 236
1 kvm_mmio: mmio writeNine thousand exits and almost no interrupt injections is a spinning guest. The same reason repeating, with an MMIO address that does not change, would be a driver polling a status bit your emulated device never sets. This is how many "QEMU hangs at boot" reports are resolved: the trace names the register.
Cost and etiquette
Tracing is not free, and kvm_exit can fire hundreds of thousands of times a second on a busy host. Record for seconds, not minutes, filter to what you need and write the trace file to fast storage. On a production host, prefer perf kvm stat, which aggregates in memory, over full event recording. Also remember that tracing is host-wide: a recording with -a or -e kvm captures every guest on the machine, so on a shared host filter by process ID or vcpu, and handle the resulting file with care, as it contains guest instruction addresses.
Other host views
Several other interfaces complement the tracepoints. The debugfs directory /sys/kernel/debug/kvm/ has per-VM counters such as exits, halt_exits and mmu_cache_miss in older layouts, and recent kernels also expose a binary statistics interface that QEMU and other VMMs can read. perf stat -e 'kvm:*' -a sleep 5 counts events without recording them. And perf record -e kvm:kvm_exit -a -g with call graphs shows the host kernel path to each exit, useful for kernel developers rather than guest debugging.


