Proxmox VE (PVE 8.0) 中 CPU 相关选项的作用

  这是关于 Proxmox VE (PVE 8.0) 中 CPU 相关选项的作用的笔记,可能有错误理解,仅供参考。

一.CPU 权重(CPU units)

官方解释
The second CPU resource limiting setting, cpuunits (nowadays often called CPU shares or CPU weight), controls how much CPU time a VM gets compared to other running VMs. It is a relative weight which defaults to 100 (or 1024 if the host uses legacy cgroup v1). If you increase this for a VM it will be prioritized by the scheduler in comparison to other VMs with lower weight. For example, if VM 100 has set the default 100 and VM 200 was changed to 200, the latter VM 200 would receive twice the CPU bandwidth than the first VM 100.

For more information see man systemd.resource-control, here CPUQuota corresponds to cpulimit and CPUWeight corresponds to our cpuunits setting, visit its Notes section for references and implementation details.

  CPU 权重默认数值为 100,此设置决定了这个虚拟机相对同一主机上的其他虚拟机的性能优先级。

  举例:

  [虚拟机 VM ID 100]的 CPU 权重为 100。

  [虚拟机 VM ID 101]的 CPU 权重为 200。

  这种情况下,[虚拟机 VM ID 101]拥有更高的 CPU 权重,它将获得比[虚拟机 VM ID 100]多两倍的 CPU 带宽。


二.CPU 限制(CPU limit)

官方解释
In addition to the number of virtual cores, you can configure how much resources a VM can get in relation to the host CPU time and also in relation to other VMs. With the cpulimit (“Host CPU Time”) option you can limit how much CPU time the whole VM can use on the host. It is a floating point value representing CPU time in percent, so 1.0 is equal to 100%, 2.5 to 250% and so on. If a single process would fully use one single core it would have 100% CPU Time usage. If a VM with four cores utilizes all its cores fully it would theoretically use 400%. In reality the usage may be even a bit higher as QEMU can have additional threads for VM peripherals besides the vCPU core ones. This setting can be useful if a VM should have multiple vCPUs, as it runs a few processes in parallel, but the VM as a whole should not be able to run all vCPUs at 100% at the same time. Using a specific example: lets say we have a VM which would profit from having 8 vCPUs, but at no time all of those 8 cores should run at full load - as this would make the server so overloaded that other VMs and CTs would get to less CPU. So, we set the cpulimit limit to 4.0 (=400%). If all cores do the same heavy work they would all get 50% of a real host cores CPU time. But, if only 4 would do work they could still get almost 100% of a real core each.

VMs can, depending on their configuration, use additional threads, such as for networking or IO operations but also live migration. Thus a VM can show up to use more CPU time than just its virtual CPUs could use. To ensure that a VM never uses more CPU time than virtual CPUs assigned set the cpulimit setting to the same value as the total core count.

  这个功能用于设置给虚拟机分配的 vCPU 能够使用多少 CPU 时间,设置 1.0 就等于 100%,设置 2.5 等于 250%,依此类推。

  对于这个选项,个人的理解为:

  一个虚拟机,分配了8个 vCPU 核心,CPU 限制(CPU limit)设置为 4(也就是 400%)。

  这时,在虚拟机中的 8 个 vCPU 核心满载的情况下,这个虚拟机实际上可以获得相当于真实主机 8 个核心每个 50% 的 CPU 时间。

  而当这个虚拟机只需要 4 个 vCPU 核心满载时,就能获得相当于真实主机 4 个核心每个 100% 的 CPU 时间。

  这个设置在虚拟机拥有多个 vCPU 且运行多个进程时非常有用。

PS:设置 CPU limit 时要考虑到一个 vCPU 的性能是略低于一个物理核心的性能的(因为虚拟化的额外开销和其他的硬件等因素)。


三.CPU 相关性(CPU Affinity)

官方解释
The third CPU resource limiting setting, affinity, controls what host cores the virtual machine will be permitted to execute on. E.g., if an affinity value of 0-3,8-11 is provided, the virtual machine will be restricted to using the host cores 0,1,2,3,8,9,10, and 11. Valid affinity values are written in cpuset List Format. List Format is a comma-separated list of CPU numbers and ranges of numbers, in ASCII decimal.

CPU affinity uses the taskset command to restrict virtual machines to a given set of cores. This restriction will not take effect for some types of processes that may be created for IO. CPU affinity is not a security feature.
For more information regarding affinity see man cpuset. Here the List Format corresponds to valid affinity values. Visit its Formats section for more examples.

  CPU 相关性可以限制虚拟机在主机指定的核心上运行。

  例如,如果设置了 CPU 相关性为 0-3,8-11,则虚拟机将被限制在使用主机核心 0、1、2、3、8、9、10 和 11。

  CPU 相关性的一般使用场景:

  (1)针对一些对延迟和响应时间有严格要求,避免资源竞争,保持性能稳定。

  (2)为同一台主机上的多个虚拟机设置不同的 CPU 相关性数值,保证虚拟机使用 CPU 资源互不干扰。

  (3)某些应用程序对 CPU 相关性有特定需求。例如一些密集计算的工作负载,通过将虚拟机限制在一组紧密相邻的主机核心上,可以获得更好的数据传输和通信性能。