KVM——CPU独占
文章目录
- 机器现况信息
- 配置CPU独占(pin)
- 启用 CPU 独占(隔离)
- 验证
机器现况信息
[root@kvm-server ~]# virsh list --allId 名称 状态
--------------------------- CULinux-VM 关闭- ubuntu20.04 关闭- ubuntu24.04 关闭[root@kvm-server ~]# lscpu
架构: x86_64CPU 运行模式: 32-bit, 64-bitAddress sizes: 45 bits physical, 48 bits virtual字节序: Little Endian
CPU: 12在线 CPU 列表: 0-11
厂商 ID: GenuineIntelBIOS Vendor ID: GenuineIntel型号名称: Intel(R) Core(TM) i7-14700KFBIOS Model name: Intel(R) Core(TM) i7-14700KFCPU 系列: 6型号: 183每个核的线程数: 1每个座的核数: 3座: 4步进: 1BogoMIPS: 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_perfmon 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_deadline_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_adjust 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 vpclmulqdq 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 节点: 1NUMA 节点0 CPU: 0-11
Vulnerabilities: Gather data sampling: Not affectedItlb multihit: KVM: Mitigation: VMX disabledL1tf: Not affectedMds: Not affectedMeltdown: Not affectedMmio stale data: Unknown: No mitigationsReg file data sampling: Vulnerable: No microcodeRetbleed: Not affectedSpec rstack overflow: Not affectedSpec store bypass: Mitigation; Speculative Store Bypass disabled via prctlSpectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitizationSpectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_SSrbds: Not affectedTsx async abort: Not affected
[root@kvm-server ~]# hostnamectlStatic hostname: kvm-serverIcon name: computer-vmChassis: vm 🖴Machine ID: 309bf8245cd340e287bbe565bd597e3eBoot ID: 8be89e9fcf534a049025c6f77d403675Virtualization: vmware
Operating System: Rocky Linux 9.5 (Blue Onyx) CPE OS Name: cpe:/o:rocky:rocky:9::baseosKernel: Linux 5.14.0-503.14.1.el9_5.x86_64Architecture: x86-64Hardware 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 --allId 名称 状态
--------------------------- 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 --allId 名称 状态
--------------------------1 ubuntu20.04 运行2 CULinux-VM 运行3 ubuntu24.04 运行
验证
[root@kvm-server ~]# virsh vcpupin ubuntu20.04 0VCPU CPU 亲和性
--------------------0 4[root@kvm-server ~]# virsh vcpupin ubuntu20.04 1VCPU CPU 亲和性
--------------------1 5