使用libvirt配置pci bus的numa親和性

半山随笔發表於2024-05-08

前面的一篇文章在qemu中配置pci bus和numa node親和性 - 半山隨筆 - 部落格園 (cnblogs.com)中記錄瞭如何使用qemu命令列來設定pci bus與numa的親和性。本篇來記錄如何使用libvirt來做同樣的事。

libvirt相較於qemu是一個更高層的工具,在提供便捷性的同時也損失了一點靈活性。就拿設定pci bus的numa親和性而言,libvirt就很難去配置。參照libvirt的官方說明:

PCI controllers also have an optional subelement <target> with the attributes and subelements listed below. These are configurable items that 1) are visible to the guest OS so must be preserved for guest ABI compatibility, and 2) are usually left to default values or derived automatically by libvirt. In almost all cases, you should not manually add a <target> subelement to a controller, nor should you modify the values in the those that are automatically generated by libvirt. Since 1.2.19 (QEMU only).

node

Some PCI controllers (pci-expander-bus for the pc machine type, pcie-expander-bus for the q35 machine type and, since 3.6.0, pci-root for the pseries machine type) can have an optional <node> subelement within the <target> subelement, which is used to set the NUMA node reported to the guest OS for that bus - the guest OS will then know that all devices on that bus are a part of the specified NUMA node (it is up to the user of the libvirt API to attach host devices to the correct pci-expander-bus when assigning them to the domain).

也就是說在pci controller下面確實有numa相關的配置,但是在大部分情況下是不允許使用者去改動的,這部分內容由libvirt自動生成。這就不好辦了,既不能改xml配置,也不能在安裝的時候設定。我的理解按照官方的設想這個引數只會在host device passthrough的時候會自動設定而不是讓使用者自由設定。

但是對於確實有這方面需求的該怎麼辦呢?

還好virt-install提供了qemu commandline直接注入的方式。

virt-install --help
...
 --qemu-commandline QEMU_COMMANDLINE
                        Pass arguments directly to the qemu emulator. Ex:
                        --qemu-commandline='-display gtk,gl=on'
                        --qemu-commandline env=DISPLAY=:0.1

既然我們可以在qemu中設定pci bus和numa親和性,那麼也可以透過將qemu引數直接注入libvirt來實現同樣的功能。

sudo virt-install \
  --connect qemu:///system \
  --name node1 \
  --disk none \
  --memory 8192 \
  --vcpus 4,sockets=1,cores=4,threads=1 \
  --network bridge=virbr0 \
  --os-type linux \
  --virt-type kvm \
  --boot hd \
  --graphics none \
  --cpu cell0.cpus=0-1,cell0.memory=4194304,cell1.cpus=2-3,cell1.memory=4194304 \
  --qemu-commandline='-device pxb,id=pcie.1,bus=pci.0,addr=0x6,numa_node=0,bus_nr=5 -device pcie-pci-bridge,id=pcie-pci-br0,bus=pcie.1 -device virtio-blk-pci,scsi=off,bus=pcie-pci-br0,addr=0x1,drive=hd1,id=virtio-disk0,bootindex=1 -drive if=none,file=/home/jianyong/vm/AnolisOS-8.9-x86_64-RHCK.qcow2,id=hd1' \

上面的命令列就可以將virtio-blk裝置掛到由pxb擴充套件出來的pci橋上,而pxb可以設定numa親和性,這樣也就設定了virtio blk的numa親和性。進入虛擬機器檢視一下。

# lstopo-no-graphics
Machine (7685MB total)
  L3 L#0 (16MB)
    Group0 L#0
      NUMANode L#0 (P#0 3710MB)
      Package L#0 + L2 L#0 (512KB) + L1d L#0 (64KB) + L1i L#0 (64KB) + Core L#0 + PU L#0 (P#0)
      Package L#1 + L2 L#1 (512KB) + L1d L#1 (64KB) + L1i L#1 (64KB) + Core L#1 + PU L#1 (P#1)
      HostBridge
        PCIBridge
          PCIBridge
            PCI 07:01.0 (SCSI)
              Block "vda"
    Group0 L#1
      NUMANode L#1 (P#1 3975MB)
      Package L#2 + L2 L#2 (512KB) + L1d L#2 (64KB) + L1i L#2 (64KB) + Core L#2 + PU L#2 (P#2)
      Package L#3 + L2 L#3 (512KB) + L1d L#3 (64KB) + L1i L#3 (64KB) + Core L#3 + PU L#3 (P#3)
  HostBridge
    PCI 00:01.1 (IDE)
    PCI 00:02.0 (Ethernet)
      Net "eth0"
  Misc(MemoryModule)

可以看到vda裝置已經掛到numa0之下,而eth0並沒有跟任何numa繫結。

當然這種設定只是虛擬的,並沒有實質性的繫結,只是給guest os一個假象。如果要提高效能還是要根絕host 相關裝置topology來設定。

相關文章