summaryrefslogtreecommitdiffstats
path: root/core/modules/zram-swap
diff options
context:
space:
mode:
authorSimon Rettberg2022-07-25 18:42:33 +0200
committerSimon Rettberg2022-07-25 18:42:33 +0200
commit7656f35386a321d5040ae0b4f5d42b0a42149ed5 (patch)
treeeab55f1d8767d6f3dc011d3f60e6f340e2632177 /core/modules/zram-swap
parent[xorg] Using newer mesa libs breaks 3D accel on older intel chips (diff)
downloadmltk-7656f35386a321d5040ae0b4f5d42b0a42149ed5.tar.gz
mltk-7656f35386a321d5040ae0b4f5d42b0a42149ed5.tar.xz
mltk-7656f35386a321d5040ae0b4f5d42b0a42149ed5.zip
[zram-swap/disk-partitions] Disable zswap when using zram and vice-versa
Using zswap when swap is in zram is stupid, as we decompress and recompress pages for no reason. When we have swap on disk, enable zswap and don't create any swap in zram. If we don't have disk-based swap, create swap in zram and disable zswap.
Diffstat (limited to 'core/modules/zram-swap')
-rw-r--r--core/modules/zram-swap/data/etc/systemd/system/zram-swap.service4
-rwxr-xr-xcore/modules/zram-swap/data/opt/openslx/scripts/systemd-zram_swap47
2 files changed, 36 insertions, 15 deletions
diff --git a/core/modules/zram-swap/data/etc/systemd/system/zram-swap.service b/core/modules/zram-swap/data/etc/systemd/system/zram-swap.service
index a458b380..60a23adb 100644
--- a/core/modules/zram-swap/data/etc/systemd/system/zram-swap.service
+++ b/core/modules/zram-swap/data/etc/systemd/system/zram-swap.service
@@ -1,8 +1,10 @@
[Unit]
-Description=Setup zram swap partitions
+Description=Setup zram swap partitions if necessary
DefaultDependencies=no
Wants=swap.target
Before=swap.target
+# As our script checks whether there are other swap partitions available...
+After=setup-partitions.service
[Service]
Type=oneshot
diff --git a/core/modules/zram-swap/data/opt/openslx/scripts/systemd-zram_swap b/core/modules/zram-swap/data/opt/openslx/scripts/systemd-zram_swap
index 413ce215..1b4ebdc0 100755
--- a/core/modules/zram-swap/data/opt/openslx/scripts/systemd-zram_swap
+++ b/core/modules/zram-swap/data/opt/openslx/scripts/systemd-zram_swap
@@ -18,23 +18,27 @@
#############################################################################
+# Already done?
+if grep -q '^/dev/zram' "/proc/swaps"; then
+ echo "Already have zram swap, bailing out"
+ exit 0
+fi
# Add zram swap
-# Some older ubuntu kernels had a problem here, see https://bugs.launchpad.net/ubuntu/+source/linux-lts-raring/+bug/1217189
-# So make sure you're up to date
-make_swap () {
- [ $# -ne 3 ] && echo "make_swap: Wrong parameter count $#" && return 1
- local USE="$1"
- local DEV="$2"
- local STREAMS="$3"
- echo "$USE" > "/sys/block/zram${DEV}/disksize" || return 1
- [ -n "$STREAMS" ] && echo "$STREAMS" > "/sys/block/zram${DEV}/max_comp_streams"
- (
- mkswap "/dev/zram${DEV}"
- swapon "/dev/zram${DEV}" -p 1000 # high priority (in case we have hdd swap 0x82, prefer zram)
- ) &
-}
+# Determine non-zram swap size
+disk_swap="$( gawk 'BEGIN{a=0} $1 ~ /^\/dev\// && $1 !~ /^\/dev\/zram/ {a+= $3} END{print a}' /proc/swaps )"
+
+# If zswap is available and we have a swap partition on disk, do not add
+# zram swap, as having a tiered swap doesn't make sense then.
+# Use a sensible lower bound here, if we have a tiny swap partition, still use zram
+if [ "$disk_swap" -ge 2048000 ]; then # ~2 GB (in KB)
+ echo "Not setting up zram swap as swap partition is in use."
+ echo 1 > /sys/module/zswap/parameters/enabled
+ exit 0
+fi
+
+echo 0 > /sys/module/zswap/parameters/enabled
# Count physical CPUs
CPUS=$(cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | sort -u | wc -l) # cat for *
@@ -61,6 +65,21 @@ elif ! modprobe zram "num_devices=$DEVS"; then
exit 1
fi
+# Go for it
+
+make_swap () {
+ [ $# -ne 3 ] && echo "make_swap: Wrong parameter count $#" && return 1
+ local USE="$1"
+ local DEV="$2"
+ local STREAMS="$3"
+ echo "$USE" > "/sys/block/zram${DEV}/disksize" || return 1
+ [ -n "$STREAMS" ] && echo "$STREAMS" > "/sys/block/zram${DEV}/max_comp_streams"
+ (
+ mkswap "/dev/zram${DEV}"
+ swapon "/dev/zram${DEV}" -p 1000 # high priority (in case we have hdd swap 0x82, prefer zram)
+ ) &
+}
+
TOTAL=$(grep ^MemTotal /proc/meminfo | awk '{print $2}')
USE=$(( TOTAL / ( 2 * DEVS ) ))
echo "Have $CPUS cores, $TOTAL kb mem, use $USE kb zram swap each for $DEVS devices."