Zero's Blog

kvm虚拟机CPU与物理主机CPU的隔离性检测

查看CPU的基本信息:

####查看物理CPU的个数

1
cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l

查看逻辑CPU的个数

1
cat /proc/cpuinfo |grep "processor"|wc -l

查看CPU是几核

1
cat /proc/cpuinfo |grep "cores"|uniq

查看CPU的主频

1
cat /proc/cpuinfo |grep MHz|uniq

指定进程到具体CPU上运行:

taskset

  • -p, 设定一个已存在的pid,而不是重新开启的一个新任务

  • -c, 指定一个处理,可以指定多个,以逗号分隔,也可指定方位,如2,4,5,6-8

切换某个进程到指定的CPU上

1
taskset -cp 3 13290

让某程序运行在指定的CPU上

1
2
3
4
taskset -c 1,2,4-7 tar jcf test.tar.gz test
```
#### 查看物理主机的CPU个数为16个:

[root@test8 home]# cat /proc/cpuinfo |grep “processor”|wc -l

1
2
将CPU14和CPU15进行隔离:修改/boot/grub/grub.conf文件,在kernel一行的后面添加isolcpus=14,15

root (hd0,0)
kernel /boot/vmlinuz-2.6.32-279.2.1.el6.x86_64 ro root=UUID=4f8c5d8b-fc5f-49d2-96c8-71effe86e58f rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=zh_CN.UTF-8 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet isolcpus=14,15
initrd /boot/initramfs-2.6.32-279.2.1.el6.x86_64.img

1
2
3
注意:grub.conf是启动的配置文件,非常重要,提示在修改前对grub.conf做一下备份
配置grub.conf完成后,重新启动主机,使用命令ps -eLo psr | grep 0 | wc -l可以查看CPU的使用情况

[root@test8 home]# ps -eLo psr | grep 0 | wc -l
131
[root@test8 home]# ps -eLo psr | grep 1 | wc -l
310
[root@test8 home]# ps -eLo psr | grep 2 | wc -l
84
[root@test8 home]# ps -eLo psr | grep 14 | wc -l
21
[root@test8 home]# ps -eLo psr | grep 15 | wc -l
21

1
2
3
显示CPU14和CPU15运行了21个线程(这些线程都是系统启动的守护线程,CPU14和CPU15中运行的相同)
创建两个相同的虚拟机,查看系统中的虚拟机程序运行的进程号

[root@test8 home]# ps -eLo ruser,pid,ppid,lwp,psr,args | grep qemu | grep -v grep
qemu 11535 1 11535 3 /usr/libexec/qemu-kvm -name centosb -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 ……
qemu 11535 1 11556 0 /usr/libexec/qemu-kvm -name centosb -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 ……
qemu 11535 1 13001 3 /usr/libexec/qemu-kvm -name centosb -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 ……
qemu 13224 1 13224 15 /usr/libexec/qemu-kvm -name test1 -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1……
qemu 13224 1 13248 4 /usr/libexec/qemu-kvm -name test1 -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1……
qemu 13224 1 17191 0 /usr/libexec/qemu-kvm -name test1 -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1…..

1
2
3
4
5
大致的意思是,创建两个虚拟机centosb和test1的进程号分别是11535、13224。
将进程11535和13224分别绑定到CPU14(centosb)和CPU15(test1)上。在centosb上运行程序测试CPU性能。
#### 进程测试之前的CPU情况:

[root@test8 home]# mpstat -P ALL
Linux 2.6.32-279.2.1.el6.x86_64 (test8.localdomain) 2013年12月17日 _x8664 (16 CPU)

21时01分14秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
21时01分14秒 all 4.87 0.00 0.40 0.59 0.00 0.02 0.00 0.42 93.71
21时01分14秒 0 22.44 0.00 1.30 4.55 0.00 0.19 0.00 0.23 71.28
21时01分14秒 1 16.33 0.00 1.04 3.35 0.00 0.07 0.00 0.18 79.03
21时01分14秒 2 6.04 0.00 0.42 0.09 0.00 0.00 0.00 0.10 93.34
21时01分14秒 3 3.54 0.00 0.37 0.12 0.00 0.00 0.00 0.12 95.84
21时01分14秒 4 4.07 0.00 0.25 0.08 0.00 0.00 0.00 0.05 95.55
21时01分14秒 5 3.58 0.00 0.34 0.04 0.00 0.00 0.00 0.10 95.94
21时01分14秒 6 2.31 0.00 0.19 0.04 0.00 0.00 0.00 0.10 97.35
21时01分14秒 7 3.71 0.00 0.26 0.06 0.00 0.00 0.00 0.05 95.91
21时01分14秒 8 3.46 0.00 0.39 0.36 0.00 0.01 0.00 0.02 95.76
21时01分14秒 9 4.06 0.00 0.41 0.24 0.00 0.01 0.00 0.04 95.25
21时01分14秒 10 3.15 0.00 0.41 0.09 0.00 0.00 0.00 0.02 96.33
21时01分14秒 11 3.08 0.00 0.42 0.08 0.00 0.01 0.00 0.04 96.38
21时01分14秒 12 1.52 0.00 0.23 0.08 0.00 0.01 0.00 0.06 98.11
21时01分14秒 13 0.62 0.00 0.17 0.09 0.00 0.00 0.00 0.01 99.10
21时01分14秒 14 0.01 0.00 0.06 0.03 0.00 0.00 0.00 0.03 99.88
21时01分14秒 15 0.01 0.00 0.06 0.03 0.00 0.00 0.00 0.03 99.88

1
#### CPU14和CPU15的情况一样,运行一段测试程序后的使用情况

[root@test8 ~]# mpstat -P ALL
Linux 2.6.32-279.2.1.el6.x86_64 (test8.localdomain) 2013年12月18日 _x8664 (16 CPU)

09时12分53秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09时12分53秒 all 4.74 0.00 0.35 0.44 0.00 0.02 0.00 0.18 94.26
09时12分53秒 0 25.30 0.00 1.33 4.11 0.00 0.23 0.00 0.03 69.01
09时12分53秒 1 15.11 0.01 0.90 2.33 0.00 0.06 0.00 0.02 81.57
09时12分53秒 2 5.39 0.01 0.37 0.03 0.00 0.00 0.00 0.01 94.19
09时12分53秒 3 3.42 0.00 0.31 0.03 0.00 0.00 0.00 0.01 96.22
09时12分53秒 4 3.86 0.00 0.23 0.02 0.00 0.00 0.00 0.01 95.89
09时12分53秒 5 2.96 0.00 0.20 0.01 0.00 0.00 0.00 0.01 96.82
09时12分53秒 6 2.19 0.00 0.17 0.01 0.00 0.00 0.00 0.01 97.61
09时12分53秒 7 3.33 0.00 0.21 0.01 0.00 0.00 0.00 0.01 96.44
09时12分53秒 8 3.03 0.00 0.34 0.20 0.00 0.00 0.00 0.00 96.42
09时12分53秒 9 3.75 0.01 0.38 0.11 0.00 0.00 0.00 0.00 95.75
09时12分53秒 10 2.53 0.00 0.30 0.02 0.00 0.00 0.00 0.00 97.15
09时12分53秒 11 2.64 0.00 0.30 0.02 0.00 0.00 0.00 0.00 97.03
09时12分53秒 12 1.48 0.00 0.18 0.01 0.00 0.01 0.00 0.01 98.30
09时12分53秒 13 0.91 0.01 0.18 0.02 0.00 0.00 0.00 0.00 98.89
09时12分53秒 14 0.01 0.00 0.11 0.10 0.00 0.00 0.00 2.64 97.14
09时12分53秒 15 0.02 0.00 0.14 0.09 0.00 0.00 0.00 0.10 99.65

1
2
3
可以看到CPU14在%guest中的数据2.64是明显高于CPU15的%guest的0.10,如果你运行测试程序更长的时间,这个值会更高(为什么是%guest 这个值高,因为我是远程登录到测试主机,并运行程序的,所以是guest)### 查看CPU的基本信息:
####查看物理CPU的个数

cat /proc/cpuinfo |grep “physical id”|sort |uniq|wc -l

1
#### 查看逻辑CPU的个数

cat /proc/cpuinfo |grep “processor”|wc -l

1
2
#### 查看CPU是几核

cat /proc/cpuinfo |grep “cores”|uniq

1
2
#### 查看CPU的主频

cat /proc/cpuinfo |grep MHz|uniq

1
2
3
4
5
6
7
8
9
10
11
### 指定进程到具体CPU上运行:
taskset
* -p, 设定一个已存在的pid,而不是重新开启的一个新任务
* -c, 指定一个处理,可以指定多个,以逗号分隔,也可指定方位,如2,4,5,6-8
#### 切换某个进程到指定的CPU上

taskset -cp 3 13290

1
#### 让某程序运行在指定的CPU上

taskset -c 1,2,4-7 tar jcf test.tar.gz test

1
2
#### 查看物理主机的CPU个数为16个:

[root@test8 home]# cat /proc/cpuinfo |grep “processor”|wc -l

1
2
将CPU14和CPU15进行隔离:修改/boot/grub/grub.conf文件,在kernel一行的后面添加isolcpus=14,15

root (hd0,0)
kernel /boot/vmlinuz-2.6.32-279.2.1.el6.x86_64 ro root=UUID=4f8c5d8b-fc5f-49d2-96c8-71effe86e58f rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=zh_CN.UTF-8 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet isolcpus=14,15
initrd /boot/initramfs-2.6.32-279.2.1.el6.x86_64.img

1
2
3
注意:grub.conf是启动的配置文件,非常重要,提示在修改前对grub.conf做一下备份
配置grub.conf完成后,重新启动主机,使用命令ps -eLo psr | grep 0 | wc -l可以查看CPU的使用情况

[root@test8 home]# ps -eLo psr | grep 0 | wc -l
131
[root@test8 home]# ps -eLo psr | grep 1 | wc -l
310
[root@test8 home]# ps -eLo psr | grep 2 | wc -l
84
[root@test8 home]# ps -eLo psr | grep 14 | wc -l
21
[root@test8 home]# ps -eLo psr | grep 15 | wc -l
21

1
2
3
显示CPU14和CPU15运行了21个线程(这些线程都是系统启动的守护线程,CPU14和CPU15中运行的相同)
创建两个相同的虚拟机,查看系统中的虚拟机程序运行的进程号

[root@test8 home]# ps -eLo ruser,pid,ppid,lwp,psr,args | grep qemu | grep -v grep
qemu 11535 1 11535 3 /usr/libexec/qemu-kvm -name centosb -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 ……
qemu 11535 1 11556 0 /usr/libexec/qemu-kvm -name centosb -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 ……
qemu 11535 1 13001 3 /usr/libexec/qemu-kvm -name centosb -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 ……
qemu 13224 1 13224 15 /usr/libexec/qemu-kvm -name test1 -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1……
qemu 13224 1 13248 4 /usr/libexec/qemu-kvm -name test1 -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1……
qemu 13224 1 17191 0 /usr/libexec/qemu-kvm -name test1 -S -M rhel6.4.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1…..

1
2
3
4
5
大致的意思是,创建两个虚拟机centosb和test1的进程号分别是11535、13224。
将进程11535和13224分别绑定到CPU14(centosb)和CPU15(test1)上。在centosb上运行程序测试CPU性能。
#### 进程测试之前的CPU情况:

[root@test8 home]# mpstat -P ALL
Linux 2.6.32-279.2.1.el6.x86_64 (test8.localdomain) 2013年12月17日 _x8664 (16 CPU)

21时01分14秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
21时01分14秒 all 4.87 0.00 0.40 0.59 0.00 0.02 0.00 0.42 93.71
21时01分14秒 0 22.44 0.00 1.30 4.55 0.00 0.19 0.00 0.23 71.28
21时01分14秒 1 16.33 0.00 1.04 3.35 0.00 0.07 0.00 0.18 79.03
21时01分14秒 2 6.04 0.00 0.42 0.09 0.00 0.00 0.00 0.10 93.34
21时01分14秒 3 3.54 0.00 0.37 0.12 0.00 0.00 0.00 0.12 95.84
21时01分14秒 4 4.07 0.00 0.25 0.08 0.00 0.00 0.00 0.05 95.55
21时01分14秒 5 3.58 0.00 0.34 0.04 0.00 0.00 0.00 0.10 95.94
21时01分14秒 6 2.31 0.00 0.19 0.04 0.00 0.00 0.00 0.10 97.35
21时01分14秒 7 3.71 0.00 0.26 0.06 0.00 0.00 0.00 0.05 95.91
21时01分14秒 8 3.46 0.00 0.39 0.36 0.00 0.01 0.00 0.02 95.76
21时01分14秒 9 4.06 0.00 0.41 0.24 0.00 0.01 0.00 0.04 95.25
21时01分14秒 10 3.15 0.00 0.41 0.09 0.00 0.00 0.00 0.02 96.33
21时01分14秒 11 3.08 0.00 0.42 0.08 0.00 0.01 0.00 0.04 96.38
21时01分14秒 12 1.52 0.00 0.23 0.08 0.00 0.01 0.00 0.06 98.11
21时01分14秒 13 0.62 0.00 0.17 0.09 0.00 0.00 0.00 0.01 99.10
21时01分14秒 14 0.01 0.00 0.06 0.03 0.00 0.00 0.00 0.03 99.88
21时01分14秒 15 0.01 0.00 0.06 0.03 0.00 0.00 0.00 0.03 99.88

1
#### CPU14和CPU15的情况一样,运行一段测试程序后的使用情况

[root@test8 ~]# mpstat -P ALL
Linux 2.6.32-279.2.1.el6.x86_64 (test8.localdomain) 2013年12月18日 _x8664 (16 CPU)

09时12分53秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
09时12分53秒 all 4.74 0.00 0.35 0.44 0.00 0.02 0.00 0.18 94.26
09时12分53秒 0 25.30 0.00 1.33 4.11 0.00 0.23 0.00 0.03 69.01
09时12分53秒 1 15.11 0.01 0.90 2.33 0.00 0.06 0.00 0.02 81.57
09时12分53秒 2 5.39 0.01 0.37 0.03 0.00 0.00 0.00 0.01 94.19
09时12分53秒 3 3.42 0.00 0.31 0.03 0.00 0.00 0.00 0.01 96.22
09时12分53秒 4 3.86 0.00 0.23 0.02 0.00 0.00 0.00 0.01 95.89
09时12分53秒 5 2.96 0.00 0.20 0.01 0.00 0.00 0.00 0.01 96.82
09时12分53秒 6 2.19 0.00 0.17 0.01 0.00 0.00 0.00 0.01 97.61
09时12分53秒 7 3.33 0.00 0.21 0.01 0.00 0.00 0.00 0.01 96.44
09时12分53秒 8 3.03 0.00 0.34 0.20 0.00 0.00 0.00 0.00 96.42
09时12分53秒 9 3.75 0.01 0.38 0.11 0.00 0.00 0.00 0.00 95.75
09时12分53秒 10 2.53 0.00 0.30 0.02 0.00 0.00 0.00 0.00 97.15
09时12分53秒 11 2.64 0.00 0.30 0.02 0.00 0.00 0.00 0.00 97.03
09时12分53秒 12 1.48 0.00 0.18 0.01 0.00 0.01 0.00 0.01 98.30
09时12分53秒 13 0.91 0.01 0.18 0.02 0.00 0.00 0.00 0.00 98.89
09时12分53秒 14 0.01 0.00 0.11 0.10 0.00 0.00 0.00 2.64 97.14
09时12分53秒 15 0.02 0.00 0.14 0.09 0.00 0.00 0.00 0.10 99.65
```
可以看到CPU14在%guest中的数据2.64是明显高于CPU15的%guest的0.10,如果你运行测试程序更长的时间,这个值会更高(为什么是%guest 这个值高,因为我是远程登录到测试主机,并运行程序的,所以是guest)