对KVM虚拟机进行cpu pinning配置的方法

2023-11-03

这篇文章主要介绍了对KVM虚拟机进行cpu pinning配置的方法,通过文中的各种virsh命令可进行操作,需要的朋友可以参考下
首先需求了解基本的信息

1 宿主机CPU特性查看


使用virsh nodeinfo可以看到一些基本信息

#virsh nodeinfo
CPU model: x86_64
CPU(s): 32
CPU frequency: 1200 MHz
CPU socket(s): 1
Core(s) per socket: 8
Thread(s) per core: 2
NUMA cell(s): 2
Memory size: 132119080 KiB

使用virsh capabilities可以查看物理机CPU的详细信息,包括物理CPU个数,每个CPU的核数,是否开了超线程。

#virsh capabilities
<capabilities>
 <host>
 <uuid>36353332-3030-3643-5534-3235445a564a</uuid>
 <cpu>
 <arch>x86_64</arch>
 <model>SandyBridge</model>
 <vendor>Intel</vendor>
 <topology sockets='1' cores='8' threads='2'/>
 <feature name='erms'/>
 <feature name='smep'/>
 ...
 </cpu>
 <power_management>
 <suspend_disk/>
 </power_management>
 <migration_features>
 <live/>
 <uri_transports>
 <uri_transport>tcp</uri_transport>
 </uri_transports>
 </migration_features>
 <topology>
 <cells num='2'>
 <cell id='0'>
 <cpus num='16'>
 <cpu id='0' socket_id='0' core_id='0' siblings='0,16'/>
 ...
 <cpu id='23' socket_id='0' core_id='7' siblings='7,23'/>
 </cpus>
 </cell>
 <cell id='1'>
 <cpus num='16'>
 <cpu id='8' socket_id='1' core_id='0' siblings='8,24'/>
 ...
 <cpu id='31' socket_id='1' core_id='7' siblings='15,31'/>
 </cpus>
 </cell>
 </cells>
 </topology>
 <secmodel>
 <model>none</model>
 <doi>0</doi>
 </secmodel>
 <secmodel>
 <model>dac</model>
 <doi>0</doi>
 </secmodel>
 </host>
...
</capabilities>

使用virsh freecell命令查看可以当前空闲内存

#virsh freecell --all
 0: 787288 KiB
 1: 94192 KiB
--------------------
Total: 881480 KiB

物理CPU的特性也可以通过/proc/cpuinfo查看

#cat /proc/cpuinfo
rocessor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 62
model name : Intel(R) Xeon(R) CPU E5-2640 v2 @ 2.00GHz
stepping : 4
cpu MHz : 1200.000
cache size : 20480 KB
physical id : 0
siblings : 16
core id : 0
cpu cores : 8
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat epb xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms
bogomips : 3990.67
clflush size : 64
cache_alignment : 64
address sizes : 46 bits physical, 48 bits virtual
power management:
...

综合上面的信息,我们可以得出以下信息:

1) 物理CPU为 E5-2640V2,为8核2颗,开启了超线程,在物理机系统上可以看到32个CPU;

2) 物理机内存为128G 


2 虚拟机CPU使用情况查看


可以使用virsh vcpuinfo命令查看虚拟机vcpu和物理CPU的对应关系

#virsh vcpuinfo 21
VCPU: 0
CPU: 25
State: running
CPU time: 10393.0s
CPU Affinity: --------yyyyyyyy--------yyyyyyyy
VCPU: 1
CPU: 8
State: running
CPU time: 7221.2s
CPU Affinity: --------yyyyyyyy--------yyyyyyyy
...

可以看到vcpu0被调度到物理机CPU25上,目前是使用状态,使用时间是10393.0s

CPU Affinity: --------yyyyyyyy--------yyyyyyyy

yyyyyyy表示可以使用的物理CPU内部的逻辑核,可以看到这台虚拟机可以在8-15, 24-31这些cpu之间调度,为什么不能使用0-7,16-23这些CPU呢,是因为系统的自动numa平衡服务在发生作用,一个虚拟机默认只能使用同一颗物理CPU内部的逻辑核。

使用emulatorpin可以查看虚拟机可以使用那些物理逻辑CPU

#virsh emulatorpin 21
emulator: CPU Affinity
----------------------------------
 *: 0-31

可以看到0-31我们都可以使用,意味这我们也可以强制将CPU调度到任何CPU上。


3 在线pinning虚拟机的cpu


强制让虚拟机只能在26-31这些cpu之间调度

#virsh emulatorpin 21 26-31 --live

查看结果

#virsh emulatorpin 21
emulator: CPU Affinity
----------------------------------
 *: 26-31

查看vcpu info

#virsh vcpuinfo 21
VCPU: 0
CPU: 28
State: running
CPU time: 10510.5s
CPU Affinity: --------------------------yyyyyy
VCPU: 1
CPU: 28
State: running
CPU time: 7289.7s
CPU Affinity: --------------------------yyyyyy
...</p> <p>

查看xml文件

#virsh  dumpxml 21
<domain type='kvm' id='21'>
 <name>cacti-230</name>
 <uuid>23a6455c-5cd1-20cd-ecfe-2ba89be72c41</uuid>
 <memory unit='KiB'>4194304</memory>
 <currentMemory unit='KiB'>4194304</currentMemory>
 <vcpu placement='static'>4</vcpu>
 <cputune>
 <emulatorpin cpuset='26-31'/>
 </cputune>

我们也可以强制vcpu和物理机cpu一对一的绑定

强制vcpu 0和物理机cpu 28绑定

强制vcpu 1和物理机cpu 29绑定

强制vcpu 2和物理机cpu 30绑定

强制vcpu 3和物理机cpu 31绑定

#virsh vcpupin 21 0 28
#virsh vcpupin 21 1 29
#virsh vcpupin 21 2 30
#virsh vcpupin 21 3 31

查看xml文件,生效了

#virsh dumpxml 21
<domain type='kvm' id='21'>
 <name>cacti-230</name>
 <uuid>23a6455c-5cd1-20cd-ecfe-2ba89be72c41</uuid>
 <memory unit='KiB'>4194304</memory>
 <currentMemory unit='KiB'>4194304</currentMemory>
 <vcpu placement='static'>4</vcpu>
 <cputune>
 <vcpupin vcpu='0' cpuset='28'/>
 <vcpupin vcpu='1' cpuset='29'/>
 <vcpupin vcpu='2' cpuset='30'/>
 <vcpupin vcpu='3' cpuset='31'/>
 <emulatorpin cpuset='26-31'/>
 </cputune>

是vcpuino命令查看,可以看到配置生效了

#virsh vcpuinfo 22
VCPU: 0
CPU: 28
State: running
CPU time: 1.8s
CPU Affinity: ----------------------------y---
VCPU: 1
CPU: 29
State: running
CPU time: 0.0s
CPU Affinity: -----------------------------y--
...

 

4 cpu pinning简单的性能测试


cpu pinning到底对cpu的性能影响有多大,进行了一个简单的测试。

测试环境

硬件:ntel(R) Xeon(R) CPU X5650 @ 2.67GHz 2颗

软件:centos 7 update到内核 3.10.0-123.8.1.el7.x86_64

虚拟机:centos 7 update到内核 3.10.0-123.8.1.el7.x86_64

虚拟机cpu:1颗

测试工具:unixbench 5.1.2

 

不做cpu绑定测试结果

1 CPU in system; running 1 parallel copy of tests
Dhrystone 2 using register variables       28890881.0 lps   (10.0 s, 7 samples)
Double-Precision Whetstone                     3880.4 MWIPS (9.0 s, 7 samples)
Execl Throughput                               4146.3 lps   (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks       1051084.3 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks          286552.2 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       2142638.4 KBps  (30.0 s, 2 samples)
Pipe Throughput                             1726807.0 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                 322865.5 lps   (10.0 s, 7 samples)
Process Creation                              13662.4 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   5955.4 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                    713.1 lpm   (60.0 s, 2 samples)
System Call Overhead                        2138318.1 lps   (10.0 s, 7 samples)
System Benchmarks Index Values               BASELINE       RESULT    INDEX
Dhrystone 2 using register variables         116700.0   28890881.0   2475.7
Double-Precision Whetstone                       55.0       3880.4    705.5
Execl Throughput                                 43.0       4146.3    964.2
File Copy 1024 bufsize 2000 maxblocks          3960.0    1051084.3   2654.3
File Copy 256 bufsize 500 maxblocks            1655.0     286552.2   1731.4
File Copy 4096 bufsize 8000 maxblocks          5800.0    2142638.4   3694.2
Pipe Throughput                               12440.0    1726807.0   1388.1
Pipe-based Context Switching                   4000.0     322865.5    807.2
Process Creation                                126.0      13662.4   1084.3
Shell Scripts (1 concurrent)                     42.4       5955.4   1404.6
Shell Scripts (8 concurrent)                      6.0        713.1   1188.4
System Call Overhead                          15000.0    2138318.1   1425.5
                                                                   ========
System Benchmarks Index Score                                        1444.7

做了cpu绑定测试结果

1 CPU in system; running 1 parallel copy of tests
Dhrystone 2 using register variables       29812559.6 lps   (10.0 s, 7 samples)
Double-Precision Whetstone                     3928.7 MWIPS (8.9 s, 7 samples)
Execl Throughput                               4314.4 lps   (30.0 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks       1068627.9 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks          291834.2 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks       2052612.8 KBps  (30.0 s, 2 samples)
Pipe Throughput                             1737466.2 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                 326839.9 lps   (10.0 s, 7 samples)
Process Creation                              14234.5 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   6040.8 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                    717.4 lpm   (60.1 s, 2 samples)
System Call Overhead                        2149194.4 lps   (10.0 s, 7 samples)
System Benchmarks Index Values               BASELINE       RESULT    INDEX
Dhrystone 2 using register variables         116700.0   29812559.6   2554.6
Double-Precision Whetstone                       55.0       3928.7    714.3
Execl Throughput                                 43.0       4314.4   1003.4
File Copy 1024 bufsize 2000 maxblocks          3960.0    1068627.9   2698.6
File Copy 256 bufsize 500 maxblocks            1655.0     291834.2   1763.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    2052612.8   3539.0
Pipe Throughput                               12440.0    1737466.2   1396.7
Pipe-based Context Switching                   4000.0     326839.9    817.1
Process Creation                                126.0      14234.5   1129.7
Shell Scripts (1 concurrent)                     42.4       6040.8   1424.7
Shell Scripts (8 concurrent)                      6.0        717.4   1195.7
System Call Overhead                          15000.0    2149194.4   1432.8
                                                                   ========
System Benchmarks Index Score                                        1464.1

比较

综合得分

绑定 1464.1  不绑定 1444.7

综合得分 性能提升 1.34%

浮点运算

绑定 3928.7  不绑定  3880.4

浮点运算 性能提升 1.24%

转载于:https://www.cnblogs.com/tcicy/p/10187372.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对KVM虚拟机进行cpu pinning配置的方法 的相关文章

  • sh / Bash shell 脚本中 !# (bang-pound) 的含义是什么?

    我想了解这个 Scala 脚本是如何工作的 usr bin env bash exec scala 0 object HelloWorld def main args Array String println Hello world arg
  • 符合 POSIX 标准的 shell 相当于 Bash“while read -d $'\0' ...”?

    我正在尝试使 Bash 脚本严格符合 POSIX 标准 即消除任何潜在的 Bashisms http mywiki wooledge org Bashism 通过使用checkbashisms px script filename 在给定的
  • bash循环跳过注释行

    我正在循环文件中的行 我只需要跳过以 开头的行 我怎么做 bin sh while read line do if line doesn t start with then echo line fi done lt tmp myfile 谢
  • 如何让 STDOUT 和 STDERR 都转到终端和日志文件?

    我有一个脚本 将由非技术用户交互式运行 该脚本将状态更新写入 STDOUT 以便用户可以确定脚本运行正常 我希望将Stdout和STDERR重定向到终端 以便用户可以看到脚本正在工作 并查看是否存在问题 我还希望将两个流都重定向到日志文件
  • Bash 脚本监听按键以继续

    因此 我想编写一个由一系列步骤组成的 bash 脚本 并将其标识为 task 然而 每个步骤都只能完成并且可以根据用户的需要运行 Do task1 if keypressed stop task1 and move on this is t
  • 这种 bash 文件名提取技术有何用途?

    我有一部分 bash 脚本正在获取不带扩展名的文件名 但我试图了解这里到底发生了什么 是做什么用的 有人可以详细说明 bash 在幕后做了什么吗 如何在一般基础上使用该技术 bin bash for src in tif do txt sr
  • 如何在 Makefile 中定义全局 shell 函数?

    我想定义一个shell函数 bin sh test do some complicated tests 1 2 if something then build thisway 1 2 else build otherway 1 2 fi 这
  • awk 返回两个变量

    现在这就是我正在做的事情 ret ls la awk print 3 9 usr echo ret awk print 1 fil echo ret awk print 2 问题是我没有运行ls我正在运行一个需要时间的命令 因此您可以理解其
  • 在 bash 中,如何除以两个变量并输出四舍五入到小数点后 5 位的答案? [复制]

    这个问题在这里已经有答案了 我将两个变量作为输入 将它们相除后 我希望将输出四舍五入到小数点后 5 位 我已经尝试过这种方法 gt sum 12 n 7 output scale 5 sum n bc echo output 我的代码没有显
  • 在 Shell 中提取匹配模式后的字符串

    如何提取 Shell 脚本中匹配模式后面的任何字符串 我知道 Perl 脚本中的此功能 但不知道 Shell 脚本中的功能 以下是示例 subject 01 这是一个示例主题 可能会有所不同 我必须提取 Subject 01 后面的任何字符
  • 将变量插入 sh 脚本命令[重复]

    这个问题在这里已经有答案了 bin sh f set proj dir OutputDir for projname in lib proj1 proj2 do mv scripts projname BYTECODE proj dir s
  • 如何使用 nohup 获取正在运行的程序列表

    我正在通过 SSH 连接访问运行 CentOS linux 发行版 的服务器 由于我无法始终保持登录状态 因此我使用 nohup command 来运行我的程序 我找不到如何获取我开始使用 nohup 的所有程序的列表 工作 只有在我注销之
  • 批量检测系统是32位还是64位

    有谁知道如何创建一个批处理文件 如果是 64 位系统 可以对一个程序进行 shell 处理 如果是 32 位系统 则可以对另一个程序进行 shell 处理 检查 PROCESSOR ARCHITECTURE being x86 if PRO
  • AJAX 进度条 - 轮询、Comet?

    我想要一些关于如何实施以下内容的建议 我想让我的用户通过 AJAX 了解在我的服务器上运行的任务的进度 我的服务器运行一个 PHP 脚本 该脚本使用 popen 函数通过 shell 命令下载文件 它定期回显 打印正在发生的事情 我想使用
  • 将 apache documentRoot 设置为符号链接(以便于部署)

    我们正在寻找一种将 Apache DocumentRoot 指向符号链接的方法 例如 文档根目录 var www html finalbuild Finalbuild 应该指向 home user build3 之类的文件夹 当我们将新构建
  • 在 bash 中快速引用 stdout(即上一个命令的输出)?

    有没有办法快速 例如通过键盘快捷键等 引用写入到 stdout 的上一个命令的输出 例如 如果我这样做 which rails 它回来了 usr local bin rails然后我想在 textmate 中打开该文件 我可以像这样重新输入
  • ubuntu 中的 echo -e 选项不起作用

    我的同事使用Ubuntu 我使用openSUSE 我们使用相同的makefile编译相同的源代码 我的环境运行良好 但我的同事不能 总是输出无法识别 e选项 我们检查makefile 只发现echo命令使用 e option Ubuntu的
  • C shell 脚本中是否有短路逻辑运算符?

    我认为 C shell 脚本的行为会像 C 一样 并对逻辑运算符使用短路求值 if e cache find monitor newer cache then endif 但在 if 语句中 即使第一个条件为真 也会检查第二个条件 从而给出
  • 如何按文件大小对查找结果进行排序

    如何按文件大小对 find 命令的结果进行排序 我试图对这个 find 命令的结果进行排序 find src type f print0 我不需要目录的大小 我需要仅按大小排序的文件相对路径 这是如何做的using find command
  • 选择多个模式的 awk 代码

    这是我的输入文件 比如modified txt r4544 n479826 2012 08 28 07 12 33 0400 Tue 28 Aug 2012 1 line Changed paths M branches 8 6 0 con

随机推荐