qemu-kvm绑定cpu的核上

使用qemu-kvm建立一个虚拟机,然后虚拟机的线程绑定到具体的一个cpu的核上
环境,centos6.5,双核,

1,在主机上修改grub,把主机的核隔离
2.使用taskset 把qemu的线程绑定到隔离的核上。


yum install glib2 glib2-devel libtool gcc-c++ gcc zlib zlib-devel flex bison
lsmod |grep kvm
ls -l /dev/kvm

打开vmware或者bios的cpu的VT
kvm在/usr/libexec/qemu-kvm

新建一个硬盘
dd if=/dev/zero of=rhel65.img bs=1M count=8192 

第一次使用iso新建系统
/usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd  -hda /root/kvm/rhel65.img -cdrom /root/kvm/CentOS-6.5-x86_64.iso 

第二次就不用iso了
/usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd  -hda /root/kvm/rhel65.img  -vnc 192.168.137.188:1 &

如果模拟具体某种cpu
/usr/libexec/qemu-kvm -cpu SandyBridge -m 2048 -smp 2 -boot order=cd  -hda /root/kvm/rhel65.img  -vnc 192.168.137.188:1 -daemonize

看支持的cpu
/usr/libexec/qemu-kvm -cpu ?

启动之后用vnc连上去
ctrl+alt+2 切换到qemu
info cpus
info kvm
qemu-kvm绑定cpu的核上
修改grup,然后重启主机,
[root@haoning kvm]# cat /boot/grub/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=89c4d858-c966-44b9-b2a8-df121e83cb78 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet isolcpus=1
        initrd /initramfs-2.6.32-431.el6.x86_64.img

把 isolcpus=1 把第二个核隔离,
重启之后
[root@haoning kvm]# ps -eLo psr|grep 0|wc -l
351
[root@haoning kvm]# 
[root@haoning kvm]# 
[root@haoning kvm]# ps -eLo psr|grep 1|wc -l 
35
[root@haoning kvm]#

看出差别
第二个核上的线程数很少,只有linux基本的
[root@haoning ~]# ps -eLo ruser,pid,ppid,lwp,psr,args |awk '{if($5==1) print $0}' 
root          2      0      2   1 [kthreadd]
root          7      2      7   1 [migration/1]
root          8      2      8   1 [migration/1]
root          9      2      9   1 [ksoftirqd/1]
root         10      2     10   1 [watchdog/1]
root         12      2     12   1 [events/1]
root         13      2     13   1 [cgroup]
root         14      2     14   1 [khelper]
root         15      2     15   1 [netns]
root         19      2     19   1 [bdi-default]
root         21      2     21   1 [kintegrityd/1]
root         23      2     23   1 [kblockd/1]
root         29      2     29   1 [ata_sff/1]
root         31      2     31   1 [khubd]
root         34      2     34   1 [md/1]
root         36      2     36   1 [md_misc/1]
root         38      2     38   1 [khungtaskd]
root         43      2     43   1 [aio/1]
root         45      2     45   1 [crypto/1]
root         51      2     51   1 [kthrotld/1]
root        164      2    164   1 [mpt_poll_0]
root        844      2    844   1 [vmmemctl]
root        990      2    990   1 [xfslogd/1]
root        992      2    992   1 [xfsdatad/1]
root        994      2    994   1 [xfsconvertd/1]
root        996      2    996   1 [xfsalloc/1]
root        998      2    998   1 [xfseofblocks/1]
root       1000      2   1000   1 [xfssyncd/dm-1]
root       1054      2   1054   1 [kauditd]

看qemu的进程绑定到哪个cpu
[root@haoning kvm]# ps -eLo ruser,pid,ppid,lwp,psr,args |grep qemu|grep -v grep      
root      29518   4956  29518   0 /usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd -hda /root/kvm/rhel65.img -vnc 192.168.137.188:1
root      29518   4956  29524   0 /usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd -hda /root/kvm/rhel65.img -vnc 192.168.137.188:1
root      29518   4956  29525   0 /usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd -hda /root/kvm/rhel65.img -vnc 192.168.137.188:1
root      29518   4956  29842   0 /usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd -hda /root/kvm/rhel65.img -vnc 192.168.137.188:1
[root@haoning kvm]# 

注意第五列,都是0

把id绑定到1的核上
注意,第一个核实0x1 第二个是0x2,第三个是0x4,第四个是0x8
[root@haoning kvm]# taskset -p 0x2 29524
pid 29524's current affinity mask: 1
pid 29524's new affinity mask: 2
[root@haoning kvm]# 
[root@haoning kvm]# 
[root@haoning kvm]# taskset -p 0x2 29525
pid 29525's current affinity mask: 1
pid 29525's new affinity mask: 2
[root@haoning kvm]# 
[root@haoning kvm]# 
[root@haoning kvm]# ps -eLo ruser,pid,ppid,lwp,psr,args |grep qemu|grep -v grep
root      29518   4956  29518   1 /usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd -hda /root/kvm/rhel65.img -vnc 192.168.137.188:1
root      29518   4956  29524   1 /usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd -hda /root/kvm/rhel65.img -vnc 192.168.137.188:1
root      29518   4956  29525   1 /usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd -hda /root/kvm/rhel65.img -vnc 192.168.137.188:1
root      29518   4956  29862   1 /usr/libexec/qemu-kvm -m 2048 -smp 2 -boot order=cd -hda /root/kvm/rhel65.img -vnc 192.168.137.188:1
[root@haoning kvm]# 

发现第五列的核的编号都变成1了
ps -eLo ruser,pid,ppid,lwp,psr,args |awk '{if($5==1) print $0}'
再对比一下,第二个核上,除了linux基本的进程,多qemu相关的进程
除了开始的基本的linux进程,又多了这几个qemu的进程
实现了,把qemu绑定到特殊的核上,进行了qemu独享某个核,资源的合理分配和高校利用





cpu的其他相关的
/usr/libexec/qemu-kvm -m 2048 -smp 8,sockets=2,cores=2,threads=2  -hda /root/kvm/rhel65.img  -vnc 192.168.137.188:1 
(1)socket就是主板上插cpu的槽的数目,也即管理员说的"路"
(2)core就是我们平时说的”核“,即双核,4核等
(3)thread就是每个core的硬件线程数,即超线程
http://labs.chinamobile.com/mblog/854855_199265

其他的调用
/usr/libexec/qemu-kvm -name http1 -S -M rhel6.5.0 -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 0e4189c4-4006-6ee7-d1e5-fa03c8cf0130 -nodefconfig -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/http1.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/opt/zhangjl/pacemaker/http1.img,if=none,id=drive-virtio-disk0,format=qcow2,cache=none -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -netdev tap,fd=24,id=hostnet0,vhost=on,vhostfd=26 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:8f:ab:fd,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -device usb-tablet,id=input0 -vnc 0.0.0.0:4109 -vga cirrus -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x6

qemu-system-x86_64 -name http1 -S -machine rhel6.5.0 -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 0e4189c4-4006-6ee7-d1e5-fa03c8cf0130 -nodefconfig -nodefaults -chardev socket,id=charmonitor -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=cirros-0.3.1-x86_64-disk.img,if=none,id=drive-virtio-disk0,format=qcow2,cache=none -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -netdev tap,fd=24,id=hostnet0,vhost=on,vhostfd=26 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:8f:ab:fd,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -device usb-tablet,id=input0 -vnc 0.0.0.0:4109 -vga cirrus -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x6

你可能感兴趣的:(qemu,kvm)