qemu 中文文档 下载 App

QEMU 性能慢 CPU feature 警告与调优清单

常见性能警告

启动 QEMU 时看到类似输出:

qemu-system-x86_64: warning: host doesn't support requested feature:
CPUID.01H:ECX.vmx [bit 5]
qemu-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.hle [bit 4]

这些只是警告,不是错误——Guest 请求的某些 CPU 特性在宿主上不存在,QEMU 跳过了它们。虚拟机仍然能跑,只是这些特性用不了。

根治方法

方法一:用 -cpu host(推荐)

qemu-system-x86_64 -cpu host ...

把宿主 CPU 的所有特性完整暴露给 Guest。没有警告,性能最优。但迁移到不同 CPU 的宿主时可能有问题。

方法二:用 -cpu max(兼容优先)

qemu-system-x86_64 -cpu max ...

暴露 QEMU 知道的最强 CPU 特性集,跨宿主机兼容性更好。

方法三:指定具体型号

qemu-system-x86_64 -cpu qemu64 ...
qemu-system-x86_64 -cpu IvyBridge ...
qemu-system-x86_64 -cpu EPYC ...

老型号不会有太多宿主不支持的特性警告。但性能不如 host/max。

性能调优清单

1. 确认用了硬件加速

# Linux
qemu-system-x86_64 -accel kvm ...

# macOS
qemu-system-aarch64 -accel hvf ...

# Windows
qemu-system-x86_64 -accel whpx ...

没有硬件加速 = TCG 翻译 = 慢 10~30 倍。这是最优先级。

2. CPU 型号用 host 或 max

-cpu host       # 最佳性能
# 或
-cpu max

3. CPU 拓扑合理

-smp 4,cores=4,sockets=1,threads=1
# 或
-smp 8,cores=4,sockets=2,threads=1

给 Guest 分配的总线程数不要超过宿主物理核数太多。过度分配反而慢。

4. 磁盘用 virtio

-drive file=xxx.qcow2,if=virtio,format=qcow2
# 或
-device virtio-blk-pci,drive=my-drive

virtio-blk 比 IDE/AHCI 快一个数量级。

5. 网卡用 virtio

-nic user,model=virtio-net-pci,netdev=n0
# 或
-device virtio-net-pci,netdev=n0

6. 显卡用 virtio-gpu

-device virtio-gpu-pci
-vga none

比 cirrus/qxl 性能好得多。

7. 内存分配

8. qcow2 集群大小

qemu-img create -f qcow2 -o cluster_size=2M disk.qcow2 50G

2M 集群比默认 64K 性能更好,特别是大 I/O 场景。

9. 压缩清理

定期压缩镜像(Guest 里先 fstrim):

qemu-img convert -c -O qcow2 old.qcow2 new.qcow2

10. TCG 多线程

跨架构模拟时(只能用 TCG),开多线程翻译:

-accel tcg,thread=multi

宿主机调优

Linux 内核参数

# /etc/sysctl.conf
vm.swappiness = 10              # 减少 swap
vm.min_free_kbytes = 65536      # 保留足够内存
net.core.netdev_max_backlog = 5000

CPU 调度策略

Linux 上把 QEMU 进程绑定到特定核:

taskset -c 0,1,2,3 qemu-system-x86_64 ...

NUMA 感知

多 socket 的服务器:

qemu-system-x86_64 \
  -smp 8,sockets=2,cores=2,threads=2 \
  -numa node,cpus=0-1,mem=4G \
  -numa node,cpus=2-3,mem=4G \
  ...

让 Guest 的 NUMA 拓扑和宿主对齐,减少跨 NUMA 访问。

文件系统

qcow2 镜像放在:

Guest 侧调优

Linux Guest

# 确认 virtio 驱动加载
lsmod | grep virtio

# 磁盘调度器改成 deadline 或 none
echo deadline | sudo tee /sys/block/vda/queue/scheduler

# 文件系统 mount 加 noatime
# /etc/fstab:
/dev/vda1  /  ext4  defaults,noatime  0  1

Windows Guest

安装 virtio-win 驱动(GitHub 上找 .iso 下载挂载安装)。这能把磁盘、网卡、显卡都换成 VirtIO 驱动。

性能测试

CPU 测试

# Guest 里
sysbench cpu run --cpu-max-prime=20000

磁盘测试

# Guest 里
fio --name=random-write --ioengine=libaio --rw=randwrite --bs=4k --numjobs=1 --size=4G --runtime=60 --group_reporting

网络测试

# Host 上跑 iperf3 server
iperf3 -s

# Guest 上连
iperf3 -c host-ip

一个优化后的完整示例

qemu-system-x86_64 \
  -accel kvm \
  -cpu host \
  -smp 4,cores=4,sockets=1,threads=1 \
  -m 8G \
  -mem-prealloc \
  -drive file=disk.qcow2,if=virtio,format=qcow2,throttling.write_iops_sec=100000 \
  -device virtio-scsi-pci \
  -device virtio-net-pci,netdev=n0 \
  -netdev user,id=n0,hostfwd=tcp::2222-:22 \
  -device virtio-gpu-pci,max_outputs=2 \
  -device virtio-tablet-pci \
  -device virtio-rng-pci \
  -device virtio-balloon-pci \
  -device virtio-iommu-pci \
  -vga none \
  -display default,show-cursor=on

下一步

性能优化到位了?去 QEMU 历史与版本 了解它的发展脉络,或直接完成任务。