KVM虚拟机磁盘性能优化

MacBook Pro上运行Arch Linux 在物理主机上测试磁盘dd性能:

time (dd if=/dev/zero of=testdisk oflag=direct bs=64k count=16000;sync)

磁盘写入性能(nvme)可以达到300MB/s:

16000+0 records in
16000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 3.47852 s, 301 MB/s

real 0m4.078s
user 0m0.035s
sys  0m0.990s

但是,默认 创建KVM虚拟机 (CentOS 8),即使采用了 virtio 驱动,同样的测试命令连续写入磁盘显示性能只达到100MB/s多一点:

16000+0 records in
16000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 9.63737 s, 109 MB/s

real 0m9.667s
user 0m0.047s
sys  0m1.270s

virtio磁盘设置io=’native’

qcow2磁盘的aio支持两种模式 nativethreads :

The optional io attribute controls specific policies on I/O; qemu guests support “threads” and “native”. Since 0.8.8

  • 修改虚拟机配置:

    <disk type='file' device='disk'>
      <!-- driver name='qemu' type='qcow2' cache='none'/-->
      <driver name='qemu' type='qcow2' cache='none' io='native'/>
      <source file='/var/lib/libvirt/images/centos8.qcow2'/>
      <backingStore/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </disk>
    

其中: <driver name='qemu' type='qcow2' cache='none'/> 修改成 <driver name='qemu' type='qcow2' cache='none' io='native'/>

  • 同样磁盘测试性能,大约提高50%,吞吐量达到物理主机性能的55%,即 166MB/s

    16000+0 records in
    16000+0 records out
    1048576000 bytes (1.0 GB, 1000 MiB) copied, 6.305 s, 166 MB/s
    
    real 0m6.327s
    user 0m0.054s
    sys  0m1.118s
    

进一步优化参考

后续准备参考 KVM / Xen 做进一步实践

参考