首先了解一下須要用到的命令:html
查看CPU的基本信息:centos
查看物理CPU的個數
#cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
查看邏輯CPU的個數
#cat /proc/cpuinfo |grep "processor"|wc -l
查看CPU是幾核
#cat /proc/cpuinfo |grep "cores"|uniq
查看CPU的主頻
#cat /proc/cpuinfo |grep MHz|uniqdom
指定進程到具體CPU上運行:socket
# taskset性能
-p, 設定一個已存在的pid,而不是從新開啓的一個新任務測試
-c, 指定一個處理,能夠指定多個,以逗號分隔,也可指定方位,如2,4,5,6-8ui
1,切換某個進程到指定的CPU上線程
taskset -cp 3 13290code
2,讓某程序運行在指定的CPU上htm
taskset -c 1,2,4-7 tar jcf test.tar.gz test
查看物理主機的CPU個數爲16個:
[root@test8 home]# cat /proc/cpuinfo |grep "processor"|wc -l 16
將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
注意: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
顯示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.....
大體的意思是,建立兩個虛擬機centosb和test1的進程號分別是1153五、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日 _x86_64_ (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
CPU14和CPU15的狀況同樣,運行一段測試程序後的使用狀況
[root@test8 ~]# mpstat -P ALL Linux 2.6.32-279.2.1.el6.x86_64 (test8.localdomain) 2013年12月18日 _x86_64_ (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)
相關連接:
進程的處理器親和性和vCPU的綁定:http://smilejay.com/2012/08/kvm-vcpu-binding/
查看物理CPU個數:http://sundful.iteye.com/blog/1821856
Linux下調節CPU使用的幾種方法:http://yunwei.org/123/