Skip to content

Commit 7c700f9

Browse files
phsmDaanHoogland
authored andcommitted
fix: enforce the cpu shares within allowed range (apache#10221)
To be compatible with older libvirt versions Co-authored-by: dahn <[email protected]>
1 parent eff1ac0 commit 7c700f9

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,16 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
341341
public static final String UBUNTU_WINDOWS_GUEST_CONVERSION_SUPPORTED_CHECK_CMD = "dpkg -l virtio-win";
342342
public static final String UBUNTU_NBDKIT_PKG_CHECK_CMD = "dpkg -l nbdkit";
343343

344+
public static final int LIBVIRT_CGROUP_CPU_SHARES_MIN = 2;
345+
public static final int LIBVIRT_CGROUP_CPU_SHARES_MAX = 262144;
346+
/**
347+
* The minimal value for the LIBVIRT_CGROUPV2_WEIGHT_MIN is actually 1.
348+
* However, due to an old libvirt bug, it is raised to 2.
349+
* See: https://github.com/libvirt/libvirt/commit/38af6497610075e5fe386734b87186731d4c17ac
350+
*/
351+
public static final int LIBVIRT_CGROUPV2_WEIGHT_MIN = 2;
352+
public static final int LIBVIRT_CGROUPV2_WEIGHT_MAX = 10000;
353+
344354
private String modifyVlanPath;
345355
private String ablestackVbmcPath;
346356
private String versionStringPath;
@@ -534,8 +544,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
534544

535545
private static int hostCpuMaxCapacity = 0;
536546

537-
private static final int CGROUP_V2_UPPER_LIMIT = 10000;
538-
539547
private static final String COMMAND_GET_CGROUP_HOST_VERSION = "stat -fc %T /sys/fs/cgroup/";
540548

541549
public static final String CGROUP_V2 = "cgroup2fs";
@@ -3124,14 +3132,24 @@ public int calculateCpuShares(VirtualMachineTO vmTO) {
31243132
int requestedCpuShares = vCpus * cpuSpeed;
31253133
int hostCpuMaxCapacity = getHostCpuMaxCapacity();
31263134

3135+
// cgroup v2 is in use
31273136
if (hostCpuMaxCapacity > 0) {
3128-
int updatedCpuShares = (int) Math.ceil((requestedCpuShares * CGROUP_V2_UPPER_LIMIT) / (double) hostCpuMaxCapacity);
3129-
logger.debug(String.format("This host utilizes cgroupv2 (as the max shares value is [%s]), thus, the VM requested shares of [%s] will be converted to " +
3130-
"consider the host limits; the new CPU shares value is [%s].", hostCpuMaxCapacity, requestedCpuShares, updatedCpuShares));
3137+
3138+
int updatedCpuShares = (int) Math.ceil((requestedCpuShares * LIBVIRT_CGROUPV2_WEIGHT_MAX) / (double) hostCpuMaxCapacity);
3139+
LOGGER.debug("This host utilizes cgroupv2 (as the max shares value is [{}]), thus, the VM requested shares of [{}] will be converted to " +
3140+
"consider the host limits; the new CPU shares value is [{}].", hostCpuMaxCapacity, requestedCpuShares, updatedCpuShares);
3141+
3142+
if (updatedCpuShares < LIBVIRT_CGROUPV2_WEIGHT_MIN) updatedCpuShares = LIBVIRT_CGROUPV2_WEIGHT_MIN;
3143+
if (updatedCpuShares > LIBVIRT_CGROUPV2_WEIGHT_MAX) updatedCpuShares = LIBVIRT_CGROUPV2_WEIGHT_MAX;
31313144
return updatedCpuShares;
31323145
}
3133-
logger.debug(String.format("This host does not have a maximum CPU shares set; therefore, this host utilizes cgroupv1 and the VM requested CPU shares [%s] will not be " +
3134-
"converted.", requestedCpuShares));
3146+
3147+
// cgroup v1 is in use
3148+
LOGGER.debug("This host does not have a maximum CPU shares set; therefore, this host utilizes cgroupv1 and the VM requested CPU shares [{}] will not be " +
3149+
"converted.", requestedCpuShares);
3150+
3151+
if (requestedCpuShares < LIBVIRT_CGROUP_CPU_SHARES_MIN) requestedCpuShares = LIBVIRT_CGROUP_CPU_SHARES_MIN;
3152+
if (requestedCpuShares > LIBVIRT_CGROUP_CPU_SHARES_MAX) requestedCpuShares = LIBVIRT_CGROUP_CPU_SHARES_MAX;
31353153
return requestedCpuShares;
31363154
}
31373155

0 commit comments

Comments
 (0)