鸟在笼中,恨关羽不能张飞;Survival of the fittest
分类: 虚拟化
2013-01-24 11:49:47
在最新Xen4.2中,对CPU的调度默认是基于Credit的调度算法,其效率还是不错的;不过,为了获得更好的CPU性能,我们有时还是需要将guest的vCPU绑定到固定的pCPU上去。在本文最后第2个链接中,IBM的文档提到在以前的Xen,是否绑定vCPU可能会导致2-3倍的性能差异;本文主要说明,在Xen4.2中如何绑定dom0和guest的CPU。思路是:先限制dom0的vCPU数为2,然后绑定dom0的vCPU到固定的2个pCPU上;启动设置有4个vCPU的guest,将guest的vCPU绑定到固定的4个pCPU上。当然不要绑定geust的vCPU到和dom0同样的pCPU上,另外,为了获得更好的性能,最好将guest的多个vCPU都尽可能地绑定到同一个CPU的Node(Socket)上。
1. Dom0的vCPU个数设置和绑定:
对与dom0的vCPU个数,可以有两种方法来设置:
1.1 在grub中Xen启动行上添加dom0_max_vcpus=N参数,gurb.conf中的配置dom0仅使用2个vCPU的实例如下:
1 2 3 4 5 |
title xen-test root (hd0,0) kernel (hd0,0)/boot/xen.gz dom0_mem=2048M dom0_max_vcpus=2 iommu=1 module (hd0,0)/boot/vmlinuz-xen o root=/dev/sda1 console=hvc0 module (hd0,0)/boot/initrd-xen.img |
1.2 在dom0启动后,使用”xl vcpu-set $dom-ID N”命令来动态地设置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
[root@xen ~]# xl list Name ID Mem VCPUs State Time(s) Domain-0 0 2047 24 r----- 821.1 [root@xen ~]# xl vcpu-list Name ID VCPU CPU State Time(s) CPU Affinity Domain-0 0 0 4 -b- 68.3 any cpu Domain-0 0 1 15 -b- 39.3 any cpu Domain-0 0 2 13 -b- 36.2 any cpu Domain-0 0 3 18 -b- 43.5 any cpu Domain-0 0 4 7 -b- 32.4 any cpu Domain-0 0 5 5 -b- 35.7 any cpu Domain-0 0 6 3 -b- 32.6 any cpu Domain-0 0 7 14 -b- 36.4 any cpu Domain-0 0 8 1 -b- 37.0 any cpu Domain-0 0 9 8 -b- 34.3 any cpu Domain-0 0 10 20 -b- 34.1 any cpu Domain-0 0 11 11 -b- 33.8 any cpu Domain-0 0 12 10 -b- 29.0 any cpu Domain-0 0 13 6 -b- 30.7 any cpu Domain-0 0 14 17 -b- 30.6 any cpu Domain-0 0 15 2 -b- 33.1 any cpu Domain-0 0 16 0 -b- 28.0 any cpu Domain-0 0 17 1 -b- 30.9 any cpu Domain-0 0 18 19 -b- 28.1 any cpu Domain-0 0 19 23 r-- 29.4 any cpu Domain-0 0 20 21 -b- 28.4 any cpu Domain-0 0 21 22 -b- 30.5 any cpu Domain-0 0 22 9 -b- 28.8 any cpu Domain-0 0 23 16 -b- 30.1 any cpu [root@xen ~]# xl vcpu-set 0 2 [root@xen ~]# xl list Name ID Mem VCPUs State Time(s) Domain-0 0 2047 2 r----- 824.1 [root@xen ~]# xl vcpu-list Name ID VCPU CPU State Time(s) CPU Affinity Domain-0 0 0 4 -b- 68.5 any cpu Domain-0 0 1 15 r-- 39.6 any cpu Domain-0 0 2 - --p 36.3 any cpu Domain-0 0 3 - --p 43.6 any cpu Domain-0 0 4 - --p 32.5 any cpu Domain-0 0 5 - --p 35.8 any cpu Domain-0 0 6 - --p 32.7 any cpu Domain-0 0 7 - --p 36.6 any cpu Domain-0 0 8 - --p 37.1 any cpu Domain-0 0 9 - --p 34.4 any cpu Domain-0 0 10 - --p 34.2 any cpu Domain-0 0 11 - --p 33.9 any cpu Domain-0 0 12 - --p 29.1 any cpu Domain-0 0 13 - --p 30.8 any cpu Domain-0 0 14 - --p 30.7 any cpu Domain-0 0 15 - --p 33.2 any cpu Domain-0 0 16 - --p 28.1 any cpu Domain-0 0 17 - --p 31.0 any cpu Domain-0 0 18 - --p 28.2 any cpu Domain-0 0 19 - --p 29.5 any cpu Domain-0 0 20 - --p 28.5 any cpu Domain-0 0 21 - --p 30.5 any cpu Domain-0 0 22 - --p 29.0 any cpu Domain-0 0 23 - --p 30.3 any cpu |
可以看出来,Dom0中的vCPU数量已经被设置为2了(这vcpu-list里面,”Domain-0 0 2 – –p 36.3 any cpu”这样的行没有调度到任何pCPU是没有意义的)。
对于dom0的vCPU绑定,使用”xl vcpu-set $dom-ID $Nvcpu $Npcpu”命令来实现虚拟CPU到物理CPU的绑定。
1 2 3 4 5 6 |
[root@xen ~]# xl vcpu-pin 0 0 0 [root@xen ~]# xl vcpu-pin 0 1 1 [root@xen ~]# xl vcpu-list Name ID VCPU CPU State Time(s) CPU Affinity Domain-0 0 0 0 -b- 72.3 0 Domain-0 0 1 1 r-- 43.2 1 |
可以看到,经过绑定之后,dom0的0/1vCPU分别被绑定到,物理CPU的0/1之上(如:CPU Affinity一列所示)。
2. 设置和绑定guest的vCPU:
2.1 创建Guest时绑定:在guest的config文件中,写上如下的配置,则启动包含4个vCPU的guest,且其中四个CPU分别绑定到2/3/4/5号pCPU上。
BASH
1 2 3 |
# Number of VCPUS vcpus = 4 cpus = ["2", "3", "4", "5"] |
guest启动后,查看vCPU的情况,其操作过程如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@xen jay]# xl list Name ID Mem VCPUs State Time(s) Domain-0 0 2047 2 r----- 859.0 Jay hvm 3 1019 4 -b---- 9.6 [root@xen jay]# xl vcpu-list 0 Name ID VCPU CPU State Time(s) CPU Affinity Domain-0 0 0 0 -b- 94.8 0 Domain-0 0 1 1 r-- 55.0 1 [root@xen jay]# xl vcpu-list 3 Name ID VCPU CPU State Time(s) CPU Affinity Jay hvm 3 0 2 -b- 12.5 2 Jay hvm 3 1 3 -b- 2.3 3 Jay hvm 3 2 4 -b- 3.8 4 Jay hvm 3 3 5 -b- 1.8 5 |
2.2 guest创建后动态地用’xl vcpu-pin’命令来绑定vCPU到pCPU上。
在创建guest时,只是启动4个vCPU而不绑定(即没有“cpus = ["2", "3", "4", "5"]”这个配置)。
具体绑定vCPU的过程如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[root@xen jay]# xl list Name ID Mem VCPUs State Time(s) Domain-0 0 2047 2 r----- 916.7 Jay hvm 4 1023 4 -b---- 23.2 [root@xen jay]# xl vcpu-list 4 Name ID VCPU CPU State Time(s) CPU Affinity Jay hvm 4 0 6 -b- 13.7 any cpu Jay hvm 4 1 4 -b- 2.8 any cpu Jay hvm 4 2 23 -b- 4.8 any cpu Jay hvm 4 3 2 -b- 2.6 any cpu [root@xen jay]# xl vcpu-pin 4 0 2 [root@xen jay]# xl vcpu-pin 4 1 3 [root@xen jay]# xl vcpu-pin 4 2 4 [root@xen jay]# xl vcpu-pin 4 3 5 [root@xen jay]# xl vcpu-list 4 Name ID VCPU CPU State Time(s) CPU Affinity Jay hvm 4 0 2 -b- 13.8 2 Jay hvm 4 1 3 -b- 2.8 3 Jay hvm 4 2 4 -b- 4.9 4 Jay hvm 4 3 5 -b- 2.6 5 |
最后我们可以看到dom 4上的4个vCPU分别绑定到了2~5号pCPU上了。