KVM虚拟机CPU绑定性能调优

时间:2022-12-25 08:04:20

关于linux 进程的处理器亲和性的vCPU 隔离和绑定之前已经做过测试,查考 ubuntu中测试进程的处理器亲和性和vCPU的绑定

查考连接   Setting KVM processor affinities  https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/ch33s08.html

那么KVM 虚拟化下如何直接为虚拟机绑定CPU 呢,KVM 提供了很方便的方法

查看说有虚拟机

root@network:~# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     instance-000000ef              running
 4     instance-000000f3              running
 7     instance-00000120              running
 10    instance-00000146              running
 11    instance-00000147              running
 12    instance-00000148              running
 26    instance-00000157              running
 35    instance-0000015c              running
 38    instance-00000158              running
 39    instance-0000015b              running
 -     instance-00000122              shut off

查看虚拟机信息

root@network:~# virsh dumpxml instance-00000146
<domain type='kvm' id='10'>
  <name>instance-00000146</name>
  <uuid>45d77d2b-723f-40c8-a953-13f886a317f8</uuid>
  <memory unit='KiB'>4194304</memory>
  <currentMemory unit='KiB'>4194304</currentMemory>
  <vcpu placement='static'>4</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <sysinfo type='smbios'>
    <system>
      <entry name='manufacturer'>OpenStack Foundation</entry>
      <entry name='product'>OpenStack Nova</entry>
      <entry name='version'>2014.1.3</entry>
      <entry name='serial'>202b2f9b-3fb8-dc11-8e76-e03f490e5d2c</entry>
      <entry name='uuid'>45d77d2b-723f-40c8-a953-13f886a317f8</entry>
    </system>
  </sysinfo>
  <os>
查看cvpu

root@network:~# ps -ef|grep instance-00000146
root     21561 13180  0 14:31 pts/12   00:00:00 grep --color=auto instance-00000146
libvirt+ 31858     1  5 Jan08 ?        17:09:02 /usr/bin/qemu-system-x86_64 -name instance-00000146 -S -machine pc-i440fx-trusty,accel=kvm,usb=off -cpu Opteron_G5,+bmi1,+perfctr_nb,+perfctr_core,+topoext,+nodeid_msr,+tce,+lwp,+wdt,+skinit,+ibs,+osvw,+cr8legacy,+extapic,+cmp_legacy,+fxsr_opt,+mmxext,+osxsave,+monitor,+ht,+vme -m 4096 -realtime mlock=off -smp 4,sockets=4,cores=1,threads=1 -uuid 45d77d2b-723f-40c8-a953-13f886a317f8 -smbios type=1,manufacturer=OpenStack Foundation,product=OpenStack Nova,version=2014.1.3,serial=202b2f9b-3fb8-dc11-8e76-e03f490e5d2c,uuid=45d77d2b-723f-40c8-a953-13f886a317f8 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/instance-00000146.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -boot strict=on -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/var/lib/nova/instances/45d77d2b-723f-40c8-a953-13f886a317f8/disk,if=none,id=drive-virtio-disk0,format=qcow2,cache=none -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x4,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive file=/var/lib/nova/instances/45d77d2b-723f-40c8-a953-13f886a317f8/disk.swap,if=none,id=drive-virtio-disk1,format=qcow2,cache=none -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk1,id=virtio-disk1 -netdev tap,fd=25,id=hostnet0,vhost=on,vhostfd=34 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=fa:16:3e:83:8e:cf,bus=pci.0,addr=0x3 -chardev file,id=charserial0,path=/var/lib/nova/instances/45d77d2b-723f-40c8-a953-13f886a317f8/console.log -device isa-serial,chardev=charserial0,id=serial0 -chardev pty,id=charserial1 -device isa-serial,chardev=charserial1,id=serial1 -device usb-tablet,id=input0 -vnc 0.0.0.0:8 -k en-us -device cirrus-vga,id=video0,bus=pci.0,addr=0x2 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x6
root@network:~# ps -eLo ruser,pid,ppid,lwp,psr|grep 31858
libvirt+ 31858     1 31858   0
libvirt+ 31858     1 31867   6
libvirt+ 31858     1 31869   4
libvirt+ 31858     1 31871   7
libvirt+ 31858     1 31873   0
libvirt+ 31858     1 31882   7
libvirt+ 31858     1 19834   6
libvirt+ 31858     1 20272   6
libvirt+ 31858     1 20679   6
libvirt+ 31858     1 20925   6
libvirt+ 31858     1 20926   6
libvirt+ 31858     1 20927   6
libvirt+ 31858     1 20928   7
libvirt+ 31858     1 20930   6

最后一列是cpu 的编号,重复执行 ps -eLo ruser,pid,ppid,lwp,psr|grep 31858  可以看到 线程是不停在不通cpu 上漂移的,绑定这么多线程是很麻烦的

利用 virsh vcpuinfo instance-00000146  查看每个vCPU 对应物理核心(也是浮动的)

root@network:~# virsh vcpuinfo instance-00000146
VCPU:           0
CPU:            4
State:          running
CPU time:       16025.9s
CPU Affinity:   yyyyyyyy            <span style="color:#ff0000;">这里看到是宿主机所有物理CPU 核心,Y代表有可以使用,基于CPU时间片来回切换</span>


VCPU:           1
CPU:            6
State:          running
CPU time:       20221.3s
CPU Affinity:   yyyyyyyy

VCPU:           2
CPU:            6
State:          running
CPU time:       12179.5s
CPU Affinity:   yyyyyyyy

VCPU:           3
CPU:            5
State:          running
CPU time:       12411.4s
CPU Affinity:   yyyyyyyy

绑定instance-00000146 的vcpu0 到物理cpu3

root@network:~# virsh vcpupin instance-00000146 0 3

root@network:~# virsh vcpuinfo instance-00000146
VCPU:           0
CPU:            3
State:          running
CPU time:       16033.7s
CPU Affinity:   ---y----                   <span style="color:#ff0000;"> 这里看到只有第三个cpu 为Y
</span>
VCPU:           1
CPU:            1
State:          running
CPU time:       20234.7s
CPU Affinity:   yyyyyyyy

VCPU:           2
CPU:            0
State:          running
CPU time:       12188.5s
CPU Affinity:   yyyyyyyy

VCPU:           3
CPU:            0
State:          running
CPU time:       12420.2s
CPU Affinity:   yyyyyyyy
依次绑定

root@network:~# virsh vcpupin instance-00000146 1 4

root@network:~# virsh vcpupin instance-00000146 2 5

root@network:~# virsh vcpupin instance-00000146 3 6

root@network:~# virsh vcpuinfo instance-00000146
VCPU:           0
CPU:            3
State:          running
CPU time:       16050.3s
CPU Affinity:   ---y----

VCPU:           1
CPU:            4
State:          running
CPU time:       20255.6s
CPU Affinity:   ----y---

VCPU:           2
CPU:            5
State:          running
CPU time:       12203.2s
CPU Affinity:   -----y--

VCPU:           3
CPU:            6
State:          running
CPU time:       12438.0s
CPU Affinity:   ------y-