]> git.proxmox.com Git - pve-common.git/commitdiff
cgroup: move get_cpuunits helper from qemu-server as clamp_cpu_shares
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 19 Oct 2022 10:29:35 +0000 (12:29 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 19 Oct 2022 10:30:21 +0000 (12:30 +0200)
Based on a patch from Fiona[0] that proposed to move it to
guest-common, rather go for common where the CGroup module resides to
avoid having to touch multiple sites if this changes another time
(hopefully not)

[0]: https://lists.proxmox.com/pipermail/pve-devel/2022-October/054225.html

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
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.