]> git.proxmox.com Git - pve-container.git/commitdiff
memory: enforce memory.high also on hotplug changes
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 26 Apr 2023 14:21:21 +0000 (16:21 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 26 Apr 2023 14:58:10 +0000 (16:58 +0200)
Factor out the calculation into a method to ensure it keeps in sync
and then use the newly added parameter of the change_memory_limit
PVE::CGroup method, bump the dependency in d/control respectively.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
debian/control
src/PVE/LXC.pm
src/PVE/LXC/Config.pm

index dc5980d5e658352327b3a7063525ff2c68e886ef..a101664aa8beba14d0a3ad565002f5cf0ab6a0e3 100644 (file)
@@ -22,7 +22,7 @@ Depends: binutils,
          file,
          libpve-access-control(>= 7.2-5),
          libpve-cluster-perl,
-         libpve-common-perl (>= 7.2-8),
+         libpve-common-perl (>= 7.4-1),
          libpve-guest-common-perl (>= 4.2-3),
          libpve-storage-perl (>= 7.2-10),
          lxc-pve,
index e058e173c88b32c4b437fb1561536bc075450f98..d138161dbf7cec10dd5eb0e42f326905e9ba28da 100644 (file)
@@ -687,11 +687,8 @@ sub update_lxc_config {
 
        # cgroup memory usage is limited by the hard 'max' limit (OOM-killer enforced) and the soft
        # 'high' limit (cgroup processes get throttled and put under heavy reclaim pressure).
-       my $lxc_mem_max = int($memory * 1024 * 1024);
+       my ($lxc_mem_max, $lxc_mem_high) = PVE::LXC::Config::calculate_memory_constraints($memory);
        $raw .= "lxc.cgroup2.memory.max = $lxc_mem_max\n";
-       # Set the high to 1016/1024 (~99.2%) of the 'max' hard limit clamped to 128 MiB max, to
-       # scale it for the lower range while having a decent 2^x based rest for 2^y memory configs.
-       my $lxc_mem_high = $memory >= 16 * 1024 ? int(($memory - 128) * 1024 * 1024) : int($memory * 1024 * 1016);
        $raw .= "lxc.cgroup2.memory.high = $lxc_mem_high\n";
 
        my $lxcswap = int($swap*1024*1024);
index bf424f9f4afa7e34bf33c814854f659bd0b93605..ac9db945b78a45e96f2a6e40c61023aa70fb8778 100644 (file)
@@ -1303,6 +1303,22 @@ sub option_exists {
 }
 # END JSON config code
 
+# takes a max memory value as KiB and returns an tuple with max and high values
+sub calculate_memory_constraints {
+    my ($memory) = @_;
+
+    return if !defined($memory);
+
+    # cgroup memory usage is limited by the hard 'max' limit (OOM-killer enforced) and the soft
+    # 'high' limit (cgroup processes get throttled and put under heavy reclaim pressure).
+    my $memory_max = int($memory * 1024 * 1024);
+    # Set the high to 1016/1024 (~99.2%) of the 'max' hard limit clamped to 128 MiB max, to scale
+    # it for the lower range while having a decent 2^x based rest for 2^y memory configs.
+    my $memory_high = $memory >= 16 * 1024 ? int(($memory - 128) * 1024 * 1024) : int($memory * 1024 * 1016);
+
+    return ($memory_max, $memory_high);
+}
+
 my $LXC_FASTPLUG_OPTIONS= {
     'description' => 1,
     'onboot' => 1,
@@ -1339,11 +1355,11 @@ sub vmconfig_hotplug_pending {
     # memory (iow. memory+swap). This means we have to change them together.
     my $hotplug_memory_done;
     my $hotplug_memory = sub {
-       my ($wanted_memory, $wanted_swap) = @_;
+       my ($new_memory, $new_swap) = @_;
 
-       $wanted_memory = int($wanted_memory * 1024 * 1024) if defined($wanted_memory);
-       $wanted_swap = int($wanted_swap * 1024 * 1024) if defined($wanted_swap);
-       $cgroup->change_memory_limit($wanted_memory, $wanted_swap);
+       ($new_memory, my $new_memory_high) = calculate_memory_constraints($new_memory);
+       $new_swap = int($new_swap * 1024 * 1024) if defined($new_swap);
+       $cgroup->change_memory_limit($new_memory, $new_swap, $new_memory_high);
 
        $hotplug_memory_done = 1;
     };