VCPU强制绑定

作者: LiHui 分类: OpenStack,Virtualization 发布时间: 2015-11-04 00:45

创建一个多VCPU的虚拟机,flavor如下

+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
| ID | Name      | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
| 3  | m1.medium | 4096      | 40   | 0         |      | 2     | 1.0         | True      |

VCPU调度情况

virsh # list
 Id    Name                           State
----------------------------------------------------
 8     instance-00000007              running

virsh #
virsh # vcpuinfo 8
VCPU:           0
CPU:            3
State:          running
CPU time:       21.6s
CPU Affinity:   yyyy

VCPU:           1
CPU:            3
State:          running
CPU time:       42.9s
CPU Affinity:   yyyy

再运行一次

virsh # vcpuinfo 8
VCPU:           0
CPU:            3
State:          running
CPU time:       21.8s
CPU Affinity:   yyyy

VCPU:           1
CPU:            2
State:          running
CPU time:       48.1s
CPU Affinity:   yyyy

可见VCPU0~1对应着物理CPU0~3随意调度

下面想要强行将VCPU和CPU的对应关系绑定

首先一种方法,想到的是通过Linux的taskset命令,直接将qemu进程给设置affinity

查看进程号

lihui@devstack:~$ ps aux | grep qemu-system-x86_64
lihui     3088  0.0  0.0  15720   920 pts/1    S+   00:23   0:00 grep --color=auto qemu-system-x86_64
libvirt+ 11196  9.0  2.4 7821508 400204 ?      Sl   00:02   1:53 /usr/bin/qemu-system-x86_64 -name instance-00000007

绑定

lihui@devstack:~$ sudo taskset -cp 3 11196
pid 11196's current affinity list: 0-3
pid 11196's new affinity list: 3

验证,多运行几次,永不会变,不信用top再观察一下,进程确实是稳定在CPU3上

lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   3
lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   3
lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   3
lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   3

这时候看一下virsh的对应关系,出现了神奇的一幕

virsh # vcpuinfo 8
VCPU:           0
CPU:            3
State:          running
CPU time:       24.0s
CPU Affinity:   yyyy

VCPU:           1
CPU:            3
State:          running
CPU time:       118.9s
CPU Affinity:   ---y

virsh # vcpuinfo 8
VCPU:           0
CPU:            1
State:          running
CPU time:       24.0s
CPU Affinity:   yyyy

VCPU:           1
CPU:            3
State:          running
CPU time:       119.0s
CPU Affinity:   ---y

从最后一项CPU Affinity可以看出来,VCPU1确实被绑定了和CPU3对应,但是VCPU0却依旧在0~3上随机调度

而且就算通过taskset多绑几次,得到的结果都一样,VCPU0依旧在CPU0~3上调度

这种结果的原因未知,可能跟taskset原理只负责绑定进程到指定CPU,还有虚拟机VCPU原理有关

 

下面再通过virsh强制绑定

virsh # vcpupin 8 0 2

virsh # vcpupin 8 1 1

将VCPU0绑定CPU2,VCPU1绑定CPU1,看下结果

virsh # vcpuinfo 8
VCPU:           0
CPU:            2
State:          running
CPU time:       26.1s
CPU Affinity:   --y-

VCPU:           1
CPU:            1
State:          running
CPU time:       185.1s
CPU Affinity:   -y--

 显然这是对的,因为CPU Affinity已经固定死了

查看此时进程

lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   1
lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   1
lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   1
lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   1
lihui@devstack:~$ ps -eo pid,psr | grep 11196
11196   1

同时在虚拟机的xml配置文件中能看到对应信息

vcpupin vcpu='0' cpuset='2'
vcpupin vcpu='1' cpuset='1'

既然如此,重启一下虚拟机看看

nova reboot --hard e77bb302-15b3-4e91-836c-3bdd78c8044a

最终查看一下VCPU的绑定情况

virsh # list
 Id    Name                           State
----------------------------------------------------
 9     instance-00000007              running

virsh # vcpuinfo 9
VCPU:           0
CPU:            0
State:          running
CPU time:       18.1s
CPU Affinity:   yyyy

VCPU:           1
CPU:            1
State:          running
CPU time:       7.4s
CPU Affinity:   yyyy

 首先Id变了,然后CPU Affinity又恢复到初始状态了,跟想象中不太一样哇,通过dumpxml得到虚拟机的配置信息,果然上面cpuset信息都没了

看来想知道这些绑定的流程,需要了解下原理,通过什么实现的才行 

纯属恶搞

浙ICP备16024533号

浙公网安备 33010802007459号