virsh - vcpu

最後更新: 2021-06-21

目錄

 


vcpucount

 

查閱 active 的 domain 的 cpu 情況

virsh vcpucount centos6.3

maximum      config         2
maximum      live           2
current      config         1
current      live           1

 


vcpuinfo

 

virsh vcpuinfo centos6.3

單 vcpu 及 domain running 時:

VCPU:           0
CPU:            1             # VCPU0 現在對應 Phy. CPU 1
State:          running
CPU time:       278.2s
CPU Affinity:   yy

雙 vcpu 及 domain running 時:

VCPU:           0
CPU:            1
State:          running
CPU time:       14.9s
CPU Affinity:   yy

VCPU:           1
CPU:            1
State:          running
CPU Affinity:   yy

 


setvcpus

 

Opts:

  • --live           即時影響 active 的 domain (default)
  • --config         affect next boot

Example:

 * next boot 才生效

setvcpus centos6.3  --config  --maximum  2

setvcpus centos6.3  --config 2                                  // 一定要 max > config

 


vcpupin

 

# 查看或設定 vcpu 在實體 CPU 上的分配情況

Usage:

vcpupin <domain> [--vcpu <number>] [<cpulist>]

i.e. vcpupin centos6.3

單 vcpu 時的 cli output

VCPU: CPU Affinity
----------------------------------
   0: 0-1

雙 vcpu 時的 cli output

VCPU: CPU Affinity
----------------------------------
   0: 0-1
   1: 0-1

i.e. 設定 vcpu0 只在 cpu0 行

#事前查看

virsh # vcpuinfo centos6.3
VCPU:           0
CPU:            1                   <-- 現在 Pin 在那 Host 的 CPU 上行
State:          running
CPU time:       15.6s
CPU Affinity:   yy

VCPU:           1
CPU:            0
State:          running
CPU time:       10.7s
CPU Affinity:   yy

# 設定

vcpupin centos6.3 0 0

# 設定後再看

virsh # vcpuinfo centos6.3

VCPU:           0
CPU:            0
State:          running
CPU time:       15.8s
CPU Affinity:   y-

VCPU:           1
CPU:            1
State:          running
CPU time:       11.0s
CPU Affinity:   yy

 


Hotplug CPU

 

查看 CPU 數量:

nproc

1

在 VM 內, 查看某些 CPU online 情況

cat /sys/devices/system/cpu/online

0-1

cat /sys/devices/system/cpu/offline

2-31

控制 CPU 的 online 及 offline:

cat /sys/devices/system/cpu/cpu1/online

1

1 代表 online, 現在我們 offline 它

echo 0 > /sys/devices/system/cpu/cpu1/online

查看 CPU 一般資料:

lscpu

Architecture:          i686
CPU op-mode(s):        32-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
CPU socket(s):         1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 3
Stepping:              3
CPU MHz:               2592.820
BogoMIPS:              5185.64
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K

CLI - setvcpus:

setvcpus - Change the number of virtual CPUs active in a guest domain

usage:

setvcpus domain-id count [opts]

remark

* Default --live
* 用 --maximum 時, 要有組合 --maximum --config

QMI - cpu_set:

virsh # qemu-monitor-command --hmp c6a info cpus

* CPU #0: pc=0x00000000c0437482 (halted) thread_id=1199

qemu-monitor-command c6a --hmp cpu_set 1 online

/var/log/libvirt/qemu/c6a.log

cpu hotplug not supported

 


virt-top (CPU 的即時使用情況)

 

安裝

apt-get install virt-top

virt-top

virt-top 17:16:51 - x86_64 2/2CPU 1400MHz 7903MB 20.7% 22.3% 21.8% 21.8%
17 domains, 3 active, 3 running, 0 sleeping, 0 paused, 14 inactive D:0 O:0 X:0
CPU: 21.7%  Mem: 3512 MB (3512 MB by guests)

   ID S RDRQ WRRQ RXBY TXBY %CPU %MEM    TIME   NAME
    3 R    0    1 3321 2293 12.5 25.0 276:45:22 win7
    1 R    0    0  126    0  6.7 12.0 175:33:58 admin
    8 R    0    0  390  264  0.2  6.0   0:20.31 c6a
    -                                           (debian7)
    -                                           (fo1)
    .............................................

opts:

-1  Display physical CPUs

PHYCPU %CPU c6c    webpy  admin  c6a    c6d    c6b    
   0    5.5  0.2=#         4.8 #  0.2=          0.2=  
   1    6.4  0.2=          5.8 #  0.2=#         0.2=#

-2  Display network interfaces

BY: byte

PK: package

   ID S RXBY TXBY RXPK TXPK DOMAIN       INTERFACE
   14 R  288    0    5    0 admin        admin
   13 R  288    0    5    0 c6a          c6a
   20 R  288    0    5    0 c6c          c6c
   21 R  288    0    5    0 c6d          c6d

-3  Display block devices

   ID S RDBY WRBY RDRQ WRRQ DOMAIN       DEVICE
   13 R    0    0    0    0 c6a          vda
   12 R    0    0    0    0 c6b          vda
   20 R    0    0    0    0 c6c          vda
   21 R    0    0    0    0 c6d          vda

# The program will exit at the time given. (+secs)

--end-time HH:MM:SS

# Write the statistics to file file.csv

--csv file.csv

# cpu (P) | mem (M) | id (N) | name | netrx |  nettx | blockrdrq | blockwrrq

-o sort

 


CPU Tuning XML

 

<domain>
  ...
  <cputune>
 
    # the vCPU is pinned to all the physical CPUs by default
    <vcpupin vcpu="0" cpuset="1-4,^2"/>
    <vcpupin vcpu="1" cpuset="0,1"/>
    <vcpupin vcpu="2" cpuset="2,3"/>
    <vcpupin vcpu="3" cpuset="0,4"/>

    # proportional weighted share for the domain, Default: 1024
    <shares>2048</shares>
    
    # Within period, each vcpu of the domain will not be allowed to consume more than quota worth of runtime. 
    # (unit: microseconds)
    <period>1000000</period>
    # The value should be in range [1000, 18446744073709551]
    <quota>-1</quota>
    
    # which of host physical CPUs the "emulator", a subset of a domain not including vcpu, will be pinned to.
    <emulatorpin cpuset="1-3"/>
    <emulator_period>1000000</emulator_period>
    <emulator_quota>-1</emulator_quota>
    
  </cputune>
  ...
</domain>

 

Creative Commons license icon Creative Commons license icon