Without a sufficient vm.min_free_kbytes value, the kernel may not start
reclaiming memory early enough to make use of zram swap before the
kernel OOM killer fires. Set a minimum of 100000 KB on devices with >=
1GB of RAM where the kernel's calculated value is lower.
This ignores devices with <1GB of RAM, because I think with so little
RAM isn't unlikely to really matter and if we set this too high then we
risk insta-OOMs.
Signed-off-by: Clayton Craft <craftyguy@postmarketos.org>
Part-of: <https://gitlab.postmarketos.org/postmarketOS/pmaports/-/merge_requests/8157>
This "size"/factor/percentage represents the total amount of
**uncompressed** data that is allowed to be written to the zram-backed
swap device. It is **not** the amount of RAM to use for zram swap (as I
had previously and erroneously thought.) What this means is that if we
set the percent to 50% on a device with 3GB of RAM then up to 1.5GB of
*uncompressed* data can be written to it, BUT since compression algos
usually do better than a ratio of 1:1... less than 1.5GB of RAM will
actually be occupied by this data when it is compressed.
A 1.5:1 compression ratio seems relatively safe to assume, even if we
use lz4 (which is less "good" at compressing than zstd), so I propose we
set the size to 150%. Some distros (PureOS) set this higher (200%), but
I think we should be somewhat conservative since we want to avoid any
thrashing in case the compression ratio at runtime ends up being less
than 2:1 for some reason.
This does set a limit of 50% for devices with >32GB of RAM, to
reduce overhead with using a larger amount of zram swap. The kernel
documentation for zram suggests there's a 0.1% overhead, so if we let
this amount get large then it could be tens or hundreds of MB of RAM
"wasted." The amount I chose is kinda arbitrary, I think this can be
improved later. At the very least, users can change this amount in the
deviceinfo variable.
Fixes#4250
Signed-off-by: Clayton Craft <craftyguy@postmarketos.org>
Part-of: <https://gitlab.postmarketos.org/postmarketOS/pmaports/-/merge_requests/7754>
This is meant to replace zram-init. It's much simpler, since it only
sets up zram swap and doesn't try to do all the things that zram-init
does. This also lets us add additional logic, such as selecting
different default algorithms based on the runtime CPU arch and
conditionally applying sysctl settings that might be inappropriate when
*not* using zram swap.
To create zramstart, I took the zram-configurator, which reads config
from deviceinfo vars, and extended it to create the zram swap device.
zramstart and zramstop were inspired by what Fedora does. The sysctl
config is from the work done here:
https://source.puri.sm/Librem5/librem5-base/-/merge_requests/381
Signed-off-by: Clayton Craft <craftyguy@postmarketos.org>
Part-of: <https://gitlab.postmarketos.org/postmarketOS/pmaports/-/merge_requests/7754>