一直没有弄明白宿主机CPU和虚拟机VCPU的对应关系是如何调度,或者说如何辨别,刚刚突然发现virsh可以直接查到这项信息
首先在一个安装有devstack的物理机上操作,当然假如有分布式openstack的节点机环境操作也行
# virsh Welcome to virsh, the virtualization interactive terminal. Type: 'help' for help with commands 'quit' to quit virsh # list Id Name State ---------------------------------------------------- 2 instance-00000001 running
这里有一项Id,要认清楚,它是与虚拟机实例一一对应的关系,没有其它的含义,但是下面会用到
查看虚拟机VCPU和物理CPU的对应关系
virsh # vcpuinfo 2 VCPU: 0 CPU: 1 State: running CPU time: 3279.4s CPU Affinity: yy
可以得到VCPU0被调度到宿主机CPU1上,目前是运行状态,运行的时间为3279.4秒,而CPU的亲和性信息显示该虚拟机实例能在物理CPU0和CPU1上调度,而且看上去宿主机就只有两个核
查一下
~$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 2 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 42 Stepping: 1 CPU MHz: 2599.998 BogoMIPS: 5199.99 Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K NUMA node0 CPU(s): 0,1
的确只有两个逻辑核,而且还发现了其实这台宿主机也是一台KVM的虚拟机而已
还可以继续查看某虚拟机可以使用哪些物理CPU
virsh # emulatorpin 2 emulator: CPU Affinity ---------------------------------- *: 0-1
如此一来,说明0和1都可用,也就是说该虚拟机VCPU可以调度到任何物理CPU上
既然如此,查一下虚拟机进程所在的物理CPU
~$ ps aux | grep qemu-system-x86_64 lihui 6564 0.0 0.0 15720 920 pts/1 S+ 00:12 0:00 grep --color=auto qemu-system-x86_64 libvirt+ 8974 9.6 4.0 1652212 249448 ? Sl Oct25 1240:25 /usr/bin/qemu-system-x86_64 -name instance-00000001 -S -machine pc-i440fx-trusty,accel=tcg,usb=off -m 512 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid ecdd5b09-39f9-4bfa-953a-4bce8e76424e -smbios type=1,manufacturer=OpenStack Foundation,product=OpenStack Nova,version=2013.2.3,serial=3237b2cc-c28f-49cb-a3c6-3f993ce98dc3,uuid=ecdd5b09-39f9-4bfa-953a-4bce8e76424e -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/instance-00000001.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -boot strict=on -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/opt/stack/data/nova/instances/ecdd5b09-39f9-4bfa-953a-4bce8e76424e/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 -netdev tap,fd=24,id=hostnet0 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=fa:16:3e:4f:60:ed,bus=pci.0,addr=0x3 -chardev file,id=charserial0,path=/opt/stack/data/nova/instances/ecdd5b09-39f9-4bfa-953a-4bce8e76424e/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 127.0.0.1:0 -k en-us -device cirrus-vga,id=video0,bus=pci.0,addr=0x2 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5
最后通过ps命令查询
~$ ps -eo pid,psr | grep 8974 8974 0
看到这个结果就比较奇怪了,按理说,假如说整个虚拟机所在进程是运行在物理CPU0上,那么它的VCPU会被调度到CPU1上么?难道不会继承么,抱着怀疑的态度,通过top查看一下cpu affinity
Tasks: 210 total, 2 running, 208 sleeping, 0 stopped, 0 zombie %Cpu0 : 34.6 us, 35.2 sy, 0.0 ni, 30.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st %Cpu1 : 25.0 us, 20.0 sy, 0.0 ni, 54.7 id, 0.0 wa, 0.3 hi, 0.0 si, 0.0 st KiB Mem: 6112692 total, 5222884 used, 889808 free, 219592 buffers KiB Swap: 0 total, 0 used, 0 free. 2280096 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND P 22023 lihui 20 0 227636 58996 4940 S 12.6 1.0 4041:10 python 1 8974 libvirt+ 20 0 1652212 249448 9832 S 6.3 4.1 1241:26 qemu-system-x86 1
猛然发现,qemu进程不停在0和1之间跳动
Tasks: 213 total, 4 running, 209 sleeping, 0 stopped, 0 zombie %Cpu0 : 33.1 us, 29.5 sy, 0.0 ni, 36.4 id, 1.0 wa, 0.0 hi, 0.0 si, 0.0 st %Cpu1 : 33.8 us, 26.8 sy, 0.0 ni, 39.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem: 6112692 total, 5229108 used, 883584 free, 219592 buffers KiB Swap: 0 total, 0 used, 0 free. 2280976 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND P 22023 lihui 20 0 227636 58996 4940 S 10.3 1.0 4041:25 python 0 8974 libvirt+ 20 0 1652212 249448 9832 S 9.3 4.1 1241:43 qemu-system-x86 0 23177 lihui 20 0 265732 79680 5800 R 5.6 1.3 228:02.12 nova-conductor 0
原来如此,多执行几遍ps查询命令,也能够得到同样的结果
lihui@devstack:~$ ps -eo pid,psr | grep 8974 8974 0 lihui@devstack:~$ ps -eo pid,psr | grep 8974 8974 0 lihui@devstack:~$ ps -eo pid,psr | grep 8974 8974 1 lihui@devstack:~$ ps -eo pid,psr | grep 8974 8974 1 lihui@devstack:~$ ps -eo pid,psr | grep 8974 8974 0
这里由于虚拟机只分配了1个vcpu,因此只能看到一个调度信息,假如是多核实例,应该可以列出所有vcpu的调度信息