Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2998386
  • 博文数量: 535
  • 博客积分: 15788
  • 博客等级: 上将
  • 技术积分: 6507
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-07 09:11
文章分类

全部博文(535)

文章存档

2016年(1)

2015年(1)

2014年(10)

2013年(26)

2012年(43)

2011年(86)

2010年(76)

2009年(136)

2008年(97)

2007年(59)

分类: 虚拟化

2011-07-26 10:04:46

参考:

默认情况下,libvirt为guests提供默认的策略。对于大多数的虚拟机管理程序,默认策略是guests运行在任何可用的处理器core或cpu。对于NUMA(Non-Uniform Memory Access,非一致性内存访问),有一个明确的使用cpu的策略会更好。
关于NUMA:
NUMA模式是一种分布式存储器访问方式,处理器可以同时访问不同的存储器地址(内存),大幅度提高并行性。 NUMA模式下,处理器被划分成多个"节点"(node), 每个节点被分配有的本地存储器空间。 所有节点中的处理器都可以访问全部的系统物理存储器,但是访问本节点内的存储器所需要的时间,比访问某些远程节点内的存储器所花的时间要少得多。
一个guest应该使用一个固定的处理器内核,这样,guest所使用的内存也固定在所运行的node上。这样可以避免跨节点的内存拷贝造成性能下降。

1、确定CPU和NUMA拓扑
virsh nodeinfo 命令提供有关host上sockets,cores和hyperthreads的信息。
  1. # virsh nodeinfo
  2. CPU model:           x86_64
  3. CPU(s):              8
  4. CPU frequency:       2394 MHz
  5. CPU socket(s):       1
  6. Core(s) per socket:  4
  7. Thread(s) per core:  1
  8. NUMA cell(s):        2
  9. Memory size:         8242076 kB 
This system has eight CPUs in one socket.
上面的输出显示出系统是一个NUMA的架构。
使用 virsh capabilities 获取更多cpu的信息。
  1. [root@srv ~]# virsh capabilities
  2. <capabilities>

  3.   <host>
  4.     <uuid>b7fe937d-3644-ec7c-feff-d196936a35b1</uuid>
  5.     <cpu>
  6.       <arch>x86_64</arch>
  7.       <model>phenom</model>
  8.       <topology sockets='1' cores='4' threads='1'/>
  9.       <feature name='wdt'/>
  10.       <feature name='skinit'/>
  11.       <feature name='osvw'/>
  12.       <feature name='3dnowprefetch'/>
  13.       <feature name='misalignsse'/>
  14.       <feature name='sse4a'/>
  15.       <feature name='abm'/>
  16.       <feature name='cr8legacy'/>
  17.       <feature name='extapic'/>
  18.       <feature name='cmp_legacy'/>
  19.       <feature name='lahf_lm'/>
  20.       <feature name='rdtscp'/>
  21.       <feature name='pdpe1gb'/>
  22.       <feature name='popcnt'/>
  23.       <feature name='cx16'/>
  24.       <feature name='ht'/>
  25.       <feature name='vme'/>
  26.     </cpu>
  27.     <migration_features>
  28.       <live/>
  29.       <uri_transports>
  30.         <uri_transport>tcp</uri_transport>
  31.       </uri_transports>
  32.     </migration_features>
  33.     <topology>
  34.       <cells num='2'>
  35.         <cell id='0'>
  36.           <cpus num='4'>
  37.             <cpu id='0'/>
  38.             <cpu id='2'/>
  39.             <cpu id='4'/>
  40.             <cpu id='6'/>
  41.           </cpus>
  42.         </cell>
  43.         <cell id='1'>
  44.           <cpus num='4'>
  45.             <cpu id='1'/>
  46.             <cpu id='3'/>
  47.             <cpu id='5'/>
  48.             <cpu id='7'/>
  49.           </cpus>
  50.         </cell>
  51.       </cells>
  52.     </topology>
  53.     <secmodel>
  54.       <model>selinux</model>
  55.       <doi>0</doi>
  56.     </secmodel>
  57.   </host>

  58.  [ Additional XML removed ]

  59. </capabilities>
上面的输出显示有两个NUMA nodes(也叫做NUMA cells),每个node包含4个逻辑cpu(4 processing cores)。对于一个分配4个cpu的guest,最佳的选择是为guest锁定cpu为0,2,4,6或1,3,5,7以避免访问非本地内存的情况。

2、确定使用哪个NUMA node来运行guest
将guest锁定在一个不能为guest提供足够内存的NUMA node上不能获得好处。使用virsh freecell显示所有NUMA nodes的空闲内存# virsh freecell
0: 2203620 kB 1: 3354784 kB
如果一个guest需要分配3GB的内存。guest应该运行在NUMA node1,node0只有2.2GB的空闲内存,不能为guset提供足够的内存。

3、将guset锁定在一个NUMA node或物理CPU集合。
通过virsh capabilities的输出确定使用哪个cell。
修改guest的xml文件:
4
4
注:libvirt 0.9.0后的写法
  1. <cputune>
  2.     <vcpupin vcpu="0" cpuset="1-4,^2"/>
  3.     <vcpupin vcpu="1" cpuset="0,1"/>
  4.     <vcpupin vcpu="2" cpuset="2,3"/>
  5.     <vcpupin vcpu="3" cpuset="0,4"/>
  6.     <shares>2048</shares>
  7.     <period>1000000</period>
  8.     <quota>-1</quota>
  9.   </cputune

4、使用virt-install自动锁定guset使用的cpu。
virt-insstall提供一个简单的方法,用于在创建guest时自动提供一个最合适的NUMA策略。
对于NUMA系统,使用--cpuset=auto参数和virt-install命令创建新的guest。

5、使用virsh vcpuinfo 和 virsh vcpupin调整运行中guest的CPU affinity
  1. [root@srv ~]# virsh vcpuinfo vm1
  2. VCPU: 0
  3. CPU: 4
  4. State: running
  5. CPU time: 10306.2s
  6. CPU Affinity: yyyyyyyy
  7. VCPU: 1
  8. CPU: 6
  9. State: running
  10. CPU time: 34546.2s
  11. CPU Affinity: yyyyyyyy
  12. VCPU: 2
  13. CPU: 2
  14. State: running
  15. CPU time: 13293.5s
  16. CPU Affinity: yyyyyyyy
  17. VCPU: 3
  18. CPU: 6
  19. State: running
  20. CPU time: 27101.6s
  21. CPU Affinity: yyyyyyyy
其中yyyyyyyy,每个y对已一个cpu core,上面的输出意为着,每个vcpu可以在8个物理cpu中的任意一个上面来运行,即不固定。

使用vcpupin来锁定vcpu运行在一个NUMA node上。
  1. [root@srv ~]# virsh vcpupin vm1 0 0
  2. [root@srv ~]# virsh vcpupin vm1 1 2
  3. [root@srv ~]# virsh vcpupin vm1 2 4
  4. [root@srv ~]# virsh vcpupin vm1 3 6
验证:
  1. [root@srv ~]# virsh vcpuinfo vm_openxlog
  2. VCPU: 0
  3. CPU: 0
  4. State: running
  5. CPU time: 10322.2s
  6. CPU Affinity: y-------
  7. VCPU: 1
  8. CPU: 2
  9. State: running
  10. CPU time: 34579.4s
  11. CPU Affinity: --y-----
  12. VCPU: 2
  13. CPU: 4
  14. State: running
  15. CPU time: 13302.2s
  16. CPU Affinity: ----y---
  17. VCPU: 3
  18. CPU: 6
  19. State: running
  20. CPU time: 27116.6s
  21. CPU Affinity: ------y-
另外,也可以将一个vcpu固定在2个或2个以上的cpu core上
  1.   [root@srv ~]#  virsh vcpupin vm1 0 0,2  

  2. [root@srv ~]#  virsh vcpupin vm1 1 2,4  

  3. [root@srv ~]#  virsh vcpupin vm1 2 4,6  

  4. [root@srv ~]#  virsh vcpupin vm1 3 6,0 
验证:
  1. [root@srv ~]# virsh vcpuinfo vm1
  2. VCPU: 0
  3. CPU: 2
  4. State: running
  5. CPU time: 10523.7s
  6. CPU Affinity: y-y-----
  7. VCPU: 1
  8. CPU: 4
  9. State: running
  10. CPU time: 35336.8s
  11. CPU Affinity: --y-y---
  12. VCPU: 2
  13. CPU: 6
  14. State: running
  15. CPU time: 13450.3s
  16. CPU Affinity: ----y-y-
  17. VCPU: 3
  18. CPU: 0
  19. State: running
  20. CPU time: 27370.5s
  21. CPU Affinity: y-----y-
阅读(3437) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~