Skip to content

Commit 73ffed4

Browse files
phsmDaanHoogland
andcommitted
fix: enforce the cpu shares within allowed range
To be compatible with older libvirt versions Co-authored-by: dahn <[email protected]>
1 parent a163831 commit 73ffed4

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
@@ -321,6 +321,16 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
321321
public static final String UBUNTU_WINDOWS_GUEST_CONVERSION_SUPPORTED_CHECK_CMD = "dpkg -l virtio-win";
322322
public static final String UBUNTU_NBDKIT_PKG_CHECK_CMD = "dpkg -l nbdkit";
323323

324+
public static final int LIBVIRT_CGROUP_CPU_SHARES_MIN = 2;
325+
public static final int LIBVIRT_CGROUP_CPU_SHARES_MAX = 262144;
326+
/**
327+
* The minimal value for the LIBVIRT_CGROUPV2_WEIGHT_MIN is actually 1.
328+
* However, due to an old libvirt bug, it is raised to 2.
329+
* See: https://github.com/libvirt/libvirt/commit/38af6497610075e5fe386734b87186731d4c17ac
330+
*/
331+
public static final int LIBVIRT_CGROUPV2_WEIGHT_MIN = 2;
332+
public static final int LIBVIRT_CGROUPV2_WEIGHT_MAX = 10000;
333+
324334
private String modifyVlanPath;
325335
private String versionStringPath;
326336
private String patchScriptPath;
@@ -502,8 +512,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
502512

503513
private static int hostCpuMaxCapacity = 0;
504514

505-
private static final int CGROUP_V2_UPPER_LIMIT = 10000;
506-
507515
private static final String COMMAND_GET_CGROUP_HOST_VERSION = "stat -fc %T /sys/fs/cgroup/";
508516

509517
public static final String CGROUP_V2 = "cgroup2fs";
@@ -2794,14 +2802,24 @@ public int calculateCpuShares(VirtualMachineTO vmTO) {
27942802
int requestedCpuShares = vCpus * cpuSpeed;
27952803
int hostCpuMaxCapacity = getHostCpuMaxCapacity();
27962804

2805+
// cgroup v2 is in use
27972806
if (hostCpuMaxCapacity > 0) {
2798-
int updatedCpuShares = (int) Math.ceil((requestedCpuShares * CGROUP_V2_UPPER_LIMIT) / (double) hostCpuMaxCapacity);
2799-
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 " +
2800-
"consider the host limits; the new CPU shares value is [%s].", hostCpuMaxCapacity, requestedCpuShares, updatedCpuShares));
2807+
2808+
int updatedCpuShares = (int) Math.ceil((requestedCpuShares * LIBVIRT_CGROUPV2_WEIGHT_MAX) / (double) hostCpuMaxCapacity);
2809+
LOGGER.debug("This host utilizes cgroupv2 (as the max shares value is [{}]), thus, the VM requested shares of [{}] will be converted to " +
2810+
"consider the host limits; the new CPU shares value is [{}].", hostCpuMaxCapacity, requestedCpuShares, updatedCpuShares);
2811+
2812+
if (updatedCpuShares < LIBVIRT_CGROUPV2_WEIGHT_MIN) updatedCpuShares = LIBVIRT_CGROUPV2_WEIGHT_MIN;
2813+
if (updatedCpuShares > LIBVIRT_CGROUPV2_WEIGHT_MAX) updatedCpuShares = LIBVIRT_CGROUPV2_WEIGHT_MAX;
28012814
return updatedCpuShares;
28022815
}
2803-
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 " +
2804-
"converted.", requestedCpuShares));
2816+
2817+
// cgroup v1 is in use
2818+
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 " +
2819+
"converted.", requestedCpuShares);
2820+
2821+
if (requestedCpuShares < LIBVIRT_CGROUP_CPU_SHARES_MIN) requestedCpuShares = LIBVIRT_CGROUP_CPU_SHARES_MIN;
2822+
if (requestedCpuShares > LIBVIRT_CGROUP_CPU_SHARES_MAX) requestedCpuShares = LIBVIRT_CGROUP_CPU_SHARES_MAX;
28052823
return requestedCpuShares;
28062824
}
28072825

0 commit comments

Comments
 (0)