KVM——CPU独占

机器现况信息

[root@kvm-server ~]# virsh list --all
 Id   名称          状态
--------------------------
 -    CULinux-VM    关闭
 -    ubuntu20.04   关闭
 -    ubuntu24.04   关闭

[root@kvm-server ~]# lscpu
架构:                    x86_64
  CPU 运行模式:          32-bit, 64-bit
  Address sizes:          45 bits physical, 48 bits virtual
  字节序:                Little Endian
CPU:                      12
  在线 CPU 列表:         0-11
厂商 ID:                 GenuineIntel
  BIOS Vendor ID:         GenuineIntel
  型号名称:              Intel(R) Core(TM) i7-14700KF
    BIOS Model name:      Intel(R) Core(TM) i7-14700KF
    CPU 系列:            6
    型号:                183
    每个核的线程数:      1
    每个座的核数:        3
    座:                  4
    步进:                1
    BogoMIPS:            6835.19
    标记:                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_p
                          erfmon rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_dead
                          line_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow ept vpid ept_ad fsgsbase tsc_adj
                          ust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni arat vnmi umip pku ospke gfni vaes v
                          pclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize flush_l1d arch_capabilities
Virtualization features:  
  虚拟化:                VT-x
  超管理器厂商:          VMware
  虚拟化类型:            完全
Caches (sum of all):      
  L1d:                    576 KiB (12 instances)
  L1i:                    384 KiB (12 instances)
  L2:                     24 MiB (12 instances)
  L3:                     132 MiB (4 instances)
NUMA:                     
  NUMA 节点:             1
  NUMA 节点0 CPU:        0-11
Vulnerabilities:          
  Gather data sampling:   Not affected
  Itlb multihit:          KVM: Mitigation: VMX disabled
  L1tf:                   Not affected
  Mds:                    Not affected
  Meltdown:               Not affected
  Mmio stale data:        Unknown: No mitigations
  Reg file data sampling: Vulnerable: No microcode
  Retbleed:               Not affected
  Spec rstack overflow:   Not affected
  Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl
  Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
  Spectre v2:             Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
  Srbds:                  Not affected
  Tsx async abort:        Not affected
[root@kvm-server ~]# hostnamectl
 Static hostname: kvm-server
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 309bf8245cd340e287bbe565bd597e3e
         Boot ID: 8be89e9fcf534a049025c6f77d403675
  Virtualization: vmware
Operating System: Rocky Linux 9.5 (Blue Onyx)       
     CPE OS Name: cpe:/o:rocky:rocky:9::baseos
          Kernel: Linux 5.14.0-503.14.1.el9_5.x86_64
    Architecture: x86-64
 Hardware Vendor: VMware, Inc.
  Hardware Model: VMware Virtual Platform
Firmware Version: 6.00

根据lscpu输出,有12个物理CPU核心(0-11),每个核心1个线程,NUMA节点0包含所有CPU。

配置CPU独占(pin)

根据上述信息,现在我有三个虚拟机都是关机状态,现在我想将4和5这两个CPU给ubuntu20.04独占

# 建议先将虚拟机关闭
virsh shutdown ubuntu20.04

编辑虚拟机XML配置

virsh edit ubuntu20.04

在XML文件中找到<vcpu>部分,添加或修改以下内容:

配置文件原本cpu相关内容为如下
  <vcpu placement='static'>2</vcpu>
  
  
先修改为:(此处将两个虚拟机CPU绑定到物理机的4,5上)
  <vcpu placement='static'>2</vcpu>
  <cputune>
    <vcpupin vcpu='0' cpuset='4'/>
    <vcpupin vcpu='1' cpuset='5'/>
    <emulatorpin cpuset='0-1'/>
  </cputune>

启用 CPU 独占(隔离)

如果希望这两个 CPU 不被其他进程使用,还需在宿主机的 GRUB 中设置 isolcpus 参数,编辑 /etc/default/grub

# 其他不变,这里...是省略号,系统原有参数也不变,仅仅新增了isolcpus=4,5 nohz_full=4,5 rcu_nocbs=4-5参数
GRUB_CMDLINE_LINUX="... isolcpus=4,5 nohz_full=4,5 rcu_nocbs=4-5"

isolcpus=4,5:将 CPU 核心 4 和 5 隔离,不让系统的其他进程(如内核任务)使用它们,确保这些核心专用于特定任务,如虚拟机或高优先级的进程。

nohz_full=4,5:通过禁用内核定时器中断,减少内核对指定 CPU 核心的干扰,使得这些核心可以专注于用户空间的进程,提高性能。

rcu_nocbs=4-5:禁用 CPU 核心上处理 RCU 回调任务的工作,减少 CPU 上的内核负载,进一步提高核心的计算效率,适用于高性能或实时应用。

然后重新生成 GRUB 并重启:

grub2-mkconfig -o /boot/grub2/grub.cfg
reboot

重启物理机后,再启动虚拟机

[root@kvm-server ~]# virsh list --all
 Id   名称          状态
--------------------------
 -    CULinux-VM    关闭
 -    ubuntu20.04   关闭
 -    ubuntu24.04   关闭

# 这里我直接启动所有虚拟机
[root@kvm-server ~]# for vm in $(virsh list --all --state-shutoff --name); do virsh start $vm; done

[root@kvm-server ~]# virsh list --all
 Id   名称          状态
--------------------------
 1    ubuntu20.04   运行
 2    CULinux-VM    运行
 3    ubuntu24.04   运行

验证

[root@kvm-server ~]# virsh vcpupin ubuntu20.04 0
 VCPU   CPU 亲和性
--------------------
 0      4

[root@kvm-server ~]# virsh vcpupin ubuntu20.04 1
 VCPU   CPU 亲和性
--------------------
 1      5
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZDICT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值