From 75dcb932f8b511bee5556a40ef15b50a92dd2d65 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Wed, 22 Mar 2023 11:08:05 +0100 Subject: [PATCH] Use zswap instead of swap in zram (#2420) * Use zswap instead of swap in zram This requires a swap file which will get generated automatically on startup. * Fix file size and free disk space comparison * Set zswap factor to 33% * Set vm.swappiness to 1 Decrease swapping to a minimum. This is also recommended for database work loads by the MariaDB documentation. In practice it causes the least amount of writes to disk when under memory pressure, while still making swap available when needed. --- buildroot-external/kernel/hassos.config | 3 +++ .../rootfs-overlay/etc/sysctl.d/15-vm.conf | 2 ++ .../usr/lib/systemd/system/dev-zram0.swap | 10 --------- .../lib/systemd/system/haos-swapfile.service | 13 ++++++++++++ .../lib/systemd/system/mnt-data-swapfile.swap | 8 +++++++ .../usr/lib/systemd/system/zram-swap.service | 15 ------------- .../rootfs-overlay/usr/libexec/haos-swapfile | 21 +++++++++++++++++++ 7 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 buildroot-external/rootfs-overlay/etc/sysctl.d/15-vm.conf delete mode 100644 buildroot-external/rootfs-overlay/usr/lib/systemd/system/dev-zram0.swap create mode 100644 buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-swapfile.service create mode 100644 buildroot-external/rootfs-overlay/usr/lib/systemd/system/mnt-data-swapfile.swap delete mode 100644 buildroot-external/rootfs-overlay/usr/lib/systemd/system/zram-swap.service create mode 100755 buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile diff --git a/buildroot-external/kernel/hassos.config b/buildroot-external/kernel/hassos.config index 31e076ef7..800d0b037 100644 --- a/buildroot-external/kernel/hassos.config +++ b/buildroot-external/kernel/hassos.config @@ -8,6 +8,9 @@ CONFIG_IKCONFIG_PROC=y CONFIG_MODULE_COMPRESS_NONE=y CONFIG_ZRAM=y +CONFIG_ZSWAP=y +CONFIG_ZSWAP_DEFAULT_ON=y +CONFIG_ZSWAP_COMPRESSOR_DEFAULT_LZ4=y CONFIG_ZSMALLOC=y CONFIG_LRU_GEN=y diff --git a/buildroot-external/rootfs-overlay/etc/sysctl.d/15-vm.conf b/buildroot-external/rootfs-overlay/etc/sysctl.d/15-vm.conf new file mode 100644 index 000000000..8bd5d9135 --- /dev/null +++ b/buildroot-external/rootfs-overlay/etc/sysctl.d/15-vm.conf @@ -0,0 +1,2 @@ +# Decrease swapping to a minimum +vm.swappiness=1 diff --git a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/dev-zram0.swap b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/dev-zram0.swap deleted file mode 100644 index 8b12ac06d..000000000 --- a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/dev-zram0.swap +++ /dev/null @@ -1,10 +0,0 @@ -[Unit] -Description=HassOS ZRAM swap -Requires=zram-swap.service -After=zram-swap.service - -[Swap] -What=/dev/zram0 - -[Install] -WantedBy=swap.target diff --git a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-swapfile.service b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-swapfile.service new file mode 100644 index 000000000..6d6b49646 --- /dev/null +++ b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-swapfile.service @@ -0,0 +1,13 @@ +[Unit] +Description=HAOS swap +DefaultDependencies=no +Requires=mnt-data.mount +After=mnt-data.mount systemd-growfs@mnt-data.service +Before=mnt-data-swapfile.swap + +[Service] +Type=oneshot +ExecStart=/usr/libexec/haos-swapfile + +[Install] +WantedBy=multi-user.target diff --git a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/mnt-data-swapfile.swap b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/mnt-data-swapfile.swap new file mode 100644 index 000000000..e7eca95db --- /dev/null +++ b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/mnt-data-swapfile.swap @@ -0,0 +1,8 @@ +[Unit] +Description=HAOS swap file + +[Swap] +What=/mnt/data/swapfile + +[Install] +WantedBy=swap.target diff --git a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/zram-swap.service b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/zram-swap.service deleted file mode 100644 index 8ec712237..000000000 --- a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/zram-swap.service +++ /dev/null @@ -1,15 +0,0 @@ -[Unit] -Description=HassOS ZRAM swap -DefaultDependencies=no -Before=dev-zram0.swap -RefuseManualStart=true -RefuseManualStop=true - -[Service] -Type=oneshot -ExecStart=/usr/libexec/hassos-zram -t swap -RemainAfterExit=true -StandardOutput=null - -[Install] -WantedBy=hassos-zram.target diff --git a/buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile b/buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile new file mode 100755 index 000000000..8a3eb399a --- /dev/null +++ b/buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile @@ -0,0 +1,21 @@ +#!/bin/sh +set -e + +swapfile="/mnt/data/swapfile" +swapsize="$(awk '/MemTotal/{ print $2 * 0.33 }' /proc/meminfo)" + + +if [ ! -s "${swapfile}" ] || [ "$(stat "${swapfile}" -c '%s')" -lt $((swapsize * 1024)) ]; then + # Check free space (in 4k blocks) + if [ "$(stat -f /mnt/data -c '%f')" -lt $((swapsize / 4)) ]; then + echo "[WARNING] Not enough space to allocate swapfile" + exit 1 + fi + + dd if=/dev/zero of="${swapfile}" bs=1k count="${swapsize}" +fi + +if ! swaplabel "${swapfile}" > /dev/null 2>&1; then + /usr/lib/systemd/systemd-makefs swap "${swapfile}" +fi +