From 9688c96a040e70a0f8e2fe1bf87e596e33f5eab5 Mon Sep 17 00:00:00 2001 From: Thomas Kaiser Date: Mon, 24 Sep 2018 15:48:57 +0200 Subject: [PATCH] Introduce MEM_LIMIT_PERCENTAGE variable for zram (defaulting to 50% so zram will not use more than this treshold anyway --- .../bsp/common/etc/default/armbian-zram-config.dpkg-dist | 9 ++++++++- packages/bsp/common/usr/lib/armbian/armbian-zram-config | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/bsp/common/etc/default/armbian-zram-config.dpkg-dist b/packages/bsp/common/etc/default/armbian-zram-config.dpkg-dist index a3ceb38c28..c563c8edd9 100644 --- a/packages/bsp/common/etc/default/armbian-zram-config.dpkg-dist +++ b/packages/bsp/common/etc/default/armbian-zram-config.dpkg-dist @@ -3,11 +3,18 @@ # enable the armbian-zram-config service? ENABLED=true -# percentage of zram usage compared to physically available DRAM. +# percentage of zram used as swap compared to physically available DRAM. # Huge overcommitment (300) is possible and sometimes desirable. See # https://forum.armbian.com/topic/5565-zram-vs-swap/?do=findComment&comment=61082 +# and don't forget to adjust $MEM_LIMIT_PERCENTAGE below too. # ZRAM_PERCENTAGE=50 +# percentage of DRAM available to zram. If this amount is exceeded the zram +# devices used for swap simply behave as if the device is full. You need to +# adjust/increase this value only if you want to work with massive memory +# overcommitment (ZRAM_PERCENTAGE exceeding 150 for example) +# MEM_LIMIT_PERCENTAGE=50 + # create how many zram devices max for swap # ZRAM_MAX_DEVICES=4 diff --git a/packages/bsp/common/usr/lib/armbian/armbian-zram-config b/packages/bsp/common/usr/lib/armbian/armbian-zram-config index 73d112fc53..8db6b147b5 100755 --- a/packages/bsp/common/usr/lib/armbian/armbian-zram-config +++ b/packages/bsp/common/usr/lib/armbian/armbian-zram-config @@ -35,16 +35,21 @@ activate_zram_swap() { module_args="$(modinfo zram | awk -F" " '/num_devices/ {print $2}' | cut -f1 -d:)" [[ -n ${module_args} ]] && modprobe zram ${module_args}=$(( ${zram_devices} + 2 )) || return - # Use 50% of real memory by default + # Expose 50% of real memory as swap space by default zram_percent=${ZRAM_PERCENTAGE:=50} mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem") memory_total=$(awk '{printf("%d",$2*1024)}' <<<${mem_info}) mem_per_zram_device=$(( ${memory_total} / ${zram_devices} * ${zram_percent} / 100 )) + # Limit memory available to zram to 50% by default + mem_limit_percent=${MEM_LIMIT_PERCENTAGE:=50} + mem_limit_per_zram_device=$(( ${memory_total} / ${zram_devices} * ${mem_limit_percent} / 100 )) + swap_algo=${SWAP_ALGORITHM:=lzo} for (( i=1; i<=zram_devices; i++ )); do [[ -f /sys/block/zram${i}/comp_algorithm ]] && echo ${swap_algo} >/sys/block/zram${i}/comp_algorithm 2>/dev/null echo -n ${mem_per_zram_device} > /sys/block/zram${i}/disksize + echo -n ${mem_limit_per_zram_device} > /sys/block/zram${i}/mem_limit mkswap /dev/zram${i} swapon -p 5 /dev/zram${i} done