KVM虚拟化之(1):CPU技术
⽬录
thread技术
<cpus>
<arch name='x86'>
<!-- vendor definitions -->
<vendor name='Intel' string='GenuineIntel'/>
<vendor name='AMD' string='AuthenticAMD'/>
<!-- standard features, EDX -->
<feature name='fpu'>
<cpuid eax_in='0x01' edx='0x00000001'/>
</feature>
<model name='xxx_model'>
<signature family='23' model='1'/>
<vendor name='⼚商'/>
<feature name='xxx_instruction'/>
</model>
……
</arch>
<arch name='ppc64'>
<!-- vendor definitions -->
<vendor name='IBM'/>
<vendor name='Freescale'/>
<!-- IBM-based CPU models -->
<model name='POWER6'>
<vendor name='IBM'/>
<pvr value='0x003e0000' mask='0xffff0000'/>
</model>
……
</arch>
</cpus>
CPU mode
1)custom模式
三种mode的性能排序是:host-passthrough > host-model > custom
三种mode的热迁移通⽤性是: custom > host-model > host-passthrough
本模式下虚拟机 CPU 指令集数最少,故性能相对最差,但是它在热迁移时跨不同型号 CPU 的能⼒最强。此外,custom 模式下⽀持⽤户添加额外的指令集。l中配置如下:
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Westmere-IBRS</model>
</cpu>
启动参数如下:
-cpu Westmere-IBRS
2)host-model模式
libvirt 根据当前宿主机 CPU 指令集从配置⽂件 /usr/share/libvirt/l 选择⼀种最相配的 CPU 型号。在这种 mode
下,虚拟机的指令集往往⽐宿主机少,性能相对 host-passthrough 要差⼀点,但是热迁移时,它允许⽬的节点 CPU 和源节点的存在⼀定的差异。
xml配置⽂件如下:
<cpu mode='host-model' check='partial'>
<model fallback='allow'/>
</cpu>
等效于(仅对于某种场景下):
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Westmere-IBRS</model>
<feature policy='require' name='vme'/>
<feature policy='require' name='ss'/>
<feature policy='require' name='ht'/>
<feature policy='require' name='osxsave'/>
<feature policy='require' name='f16c'/>
<feature policy='require' name='rdrand'/>
<feature policy='require' name='hypervisor'/>
<feature policy='require' name='arat'/>
<feature policy='require' name='stibp'/>
<feature policy='require' name='pdpe1gb'/>
<feature policy='disable' name='tsc-deadline'/>
<feature policy='disable' name='bmi1'/>
<feature policy='disable' name='hle'/>
<feature policy='disable' name='avx2'/>
<feature policy='disable' name='bmi2'/>
<feature policy='disable' name='invpcid'/>
<feature policy='disable' name='rtm'/>
<feature policy='disable' name='rdseed'/>
<feature policy='disable' name='adx'/>
<feature policy='disable' name='smap'/>
</cpu>
启动参数如下(在Broadwell-IBRS的指令集基础上,增加和删除部分):
-cpu Broadwell-IBRS,+vme,+ss,+ht,+vmx,+osxsave,+f16c,+rdrand,+hypervisor,+arat,+stibp,+pdpe1gb,-tsc-deadline,-bmi1,-hle,-avx2,-bmi2,-invpcid,-rtm,-rdseed,-Host-model type
Libvirt对CPU提炼出标准的⼏种类型,在/usr/share/libvirt/l中可以查到。
cat /usr/share/libvirt/l |grep 'model name'
<model name='486'>
……
<model name='Westmere-IBRS'>
……
<model name='POWERPC_e6500'>
3)host-passthrough模式
libvirt 令 KVM 把宿主机的 CPU 指令集全部透传给虚拟机。因此虚拟机能够最⼤限度的使⽤宿主机 CPU 指令集,故性能是最好的。
但是在热迁移时,它要求⽬的节点的 CPU 和源节点的⼀致。xml配置⽂件如下:
<cpu mode='host-passthrough' check='none'>
<topology sockets='4' cores='1' threads='1'/>
</cpu>
启动参数如下:
-cpu host
1)需要将物理CPU的⼀些特性传给虚拟机使⽤,⽐如使⽤虚拟机嵌套的nested技术的时候
2)需要在虚拟机⾥⾯看到和物理CPU⼀模⼀样的CPU品牌型号,这个在公有云很有意义,⽤户体验⽐较好
注意:使⽤CPU host-passthrough技术需要注意,不同型号的CPU宿主机之间虚拟机不能迁移
CPU topology
不配置CPU topology 默认是1 socket à 1 cores à 1 thread;也就是配置n个vcpu,则在虚拟机中就有n个physical cpu,每个物理cpu1个core。例如:
<vcpu placement='static'>4</vcpu>
<cpu>
<topology sockets='4' cores='1' threads='1'/>
</cpu>
Vcpu = sockets*cores*threads
socket
A CPU socket or a CPU package refers to a physical unit of CPU which is plugged into a system board.For example, a
4-way system or a 4-socket system can contain up to four CPUpackages Core。
翻译过来就是:socket就是主板上插cpu的槽的数⽬,也即管理员说的“路”。socket是指主板上能够放置的处理器芯⽚的个数,是⼀个物理的概念,⼀个socket上的处理器可能包含多个CPU core。
core
core就是我们平时说的“核”,即双核,4核等,这个core其实可以是指物理的实际的core,也可以是虚拟的core。
thread
thread就是每个core的硬件线程数,即超线程。
Hyper threading
Hyper threading allows a single physical processor core to behave like two logical processors.The processor can run two independent applications at the same time. A processor core may have multiple logical processors that share computer resources of the core。
SMP,对称多处理器(Symmetric Multi-Processors,简称SMP)
有些⽐较⽼的操作系统⽀持的CPU个数(插槽数)⽐较少(⽐如:Windows Server 2003标准版⽀持最多4个processor),所以通过<topology sockets='<=4' cores='>1' threads='>1'/>令2003可以使⽤更多vcpu数量。
windows server 2008 r2⽀持sockets
标准版4
企业版8
数据中⼼64
如果限制sockets为4,使得参数为sockets=4,cores=2,threads=1,则guest OS能看到全部8个core。
Openstack上flavor默认是cores=1,threads=1,上⾯这种情况需要更改flavor的元数据
$ openstack flavor set --property hw:cpu_max_cores='4' win10.c4m2d20
$ openstack flavor show win10.c4m2d20
hw:cpu_max_cores='4', hw:cpu_max_sockets='2', hw:cpu_max_threads='2'
VCPU映射CPU
不配置,默认vcpu在host的所有cpu之间进⾏调度。其原理是libvirt通过Cgroup来实现的。
CPU Affinity:  yyyyyyyyyyyyyyyyyyyyyyyy
设置CPU调度范围
cat /proc/cpuinfo |grep cores |wc -l
24
<cputune>
<emulatorpin cpuset=’20-24’/>
</cputune>
CPU Affinity:  --------------------yyyyyy
强制vCPU和CPU⼀⼀对应
<cputune>
<vcpupin vcpu=’0’ cpuset=’20’/>
<vcpupin vcpu=’0’ cpuset=’21’/>
<vcpupin vcpu=’0’ cpuset=’22’/>
<vcpupin vcpu=’0’ cpuset=’23’/>
</cputune>
# virsh vcpuinfo 3
……
CPU Affinity:  --------------------y---
CPU Affinity:  ---------------------y--
CPU Affinity:  ----------------------y-
CPU Affinity:  -----------------------y
通过stress CPU压测⼯具压测:
stress -c 4 --timeout 60s
在宿主机上使⽤top查看只有20-23 四颗CPU利⽤率是100%,其他CPU都是空闲状态。
CPU热插拔
类似KVM 内存管理,可以使⽤virsh命令给正在运⾏的VM添加或删除vCPU。不过,这个⼯作是需要预先配置KVM虚拟机的最⼤vCPU参数才能使⽤。
<vcpu placement='static' current='6'>8</vcpu>
给KVM虚拟机设置最⼤vCPU数量8,并不会对资源有所消耗,因为虚拟机只会使⽤current指定的vCPU。
<vcpu placement='static' current="4">6</vcpu>
<cputune>
<shares>8192</shares>
<vcpupin vcpu='0' cpuset='17'/>
<vcpupin vcpu='1' cpuset='18'/>
<vcpupin vcpu='2' cpuset='19'/>
<vcpupin vcpu='3' cpuset='20'/>
<vcpupin vcpu='4' cpuset='21'/>
<vcpupin vcpu='5' cpuset='22'/>
</cputune>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。