From 09ea3e7fb3f166d11d245abf26ba9d02a829ab93 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Mon, 3 Oct 2022 11:10:30 +0200 Subject: [PATCH] memory: set cgroupv2 memory.high to ~99.6% of memory.max hard-limit MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/PVE/LXC.pm | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm index 04a2e5d..e058e17 100644 --- a/src/PVE/LXC.pm +++ b/src/PVE/LXC.pm @@ -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"; -- 2.39.2