]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CGroup.pm
cgroup: move get_cpuunits helper from qemu-server as clamp_cpu_shares
[pve-common.git] / src / PVE / CGroup.pm
index 45c0788fef3463950ccf3021dccf989381c7c865..d18d0728d00e42ca097bd891fa88938ee0b47491 100644 (file)
@@ -486,6 +486,24 @@ sub change_cpu_quota {
     return 1;
 }
 
+# Clamp an integer to the supported range of CPU shares from the booted CGroup version
+#
+# Returns the default if called with an undefined value.
+sub clamp_cpu_shares {
+    my ($shares) = @_;
+
+    my $is_cgroupv2 = cgroup_mode() == 2;
+
+    return $is_cgroupv2 ? 100 : 1024 if !defined($shares);
+
+    if ($is_cgroupv2) {
+       $shares = 10000 if $shares >= 10000; # v1 can be higher, so clamp v2 there
+    } else {
+       $shares = 2 if $shares < 2; # v2 can be lower, so clamp v1 there
+    }
+    return $shares;
+}
+
 # Change the cpu "shares" for a container.
 #
 # In cgroupv1 we used a value in `[0..500000]` with a default of 1024.