]> git.proxmox.com Git - pve-container.git/commitdiff
memory: set cgroupv2 memory.high to ~99.6% of memory.max hard-limit
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 3 Oct 2022 09:10:30 +0000 (11:10 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 26 Apr 2023 14:58:10 +0000 (16:58 +0200)
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). Set the latter high limit to
1016/1024 (~99.2%) of the 'max' hard limit, this scales with CT
memory allocations, & gives a decent 2^x based rest for 2^y memory
config which is still quite near the upper bound – clamp the maximum
gap between high and max at 128 MiB to avoid that huge container pay
quite an high amount of absolute cost.

A few example for differences between max & high for a few mem sizes:
- 2 MiB lower for 256 MiB max
- 16 MiB lower for 2 GiB max
- 128 MiB for 16 GiB and above

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

index 04a2e5da1f93b4f99592ff1336ac05df2c61422f..e058e173c88b32c4b437fb1561536bc075450f98 100644 (file)
@@ -685,8 +685,14 @@ sub update_lxc_config {
        my $memory = $conf->{memory} || 512;
        my $swap = $conf->{swap} // 0;
 
-       my $lxcmem = int($memory*1024*1024);
-       $raw .= "lxc.cgroup2.memory.max = $lxcmem\n";
+       # 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);
+       $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);
        $raw .= "lxc.cgroup2.memory.swap.max = $lxcswap\n";