From 664269348d67b43a6f0b4d960412f1c137c186a6 Mon Sep 17 00:00:00 2001 From: Eugene Paniot Date: Mon, 23 Jan 2017 10:50:13 +0300 Subject: [PATCH 1/7] Fix num_devices:Number of zram devices - has uint type(max 32). This commit add min(ncpu,32) to calc cpu count function --- zramstart | 1 + 1 file changed, 1 insertion(+) diff --git a/zramstart b/zramstart index ce549fb..bb72265 100755 --- a/zramstart +++ b/zramstart @@ -1,6 +1,7 @@ #!/bin/sh num_cpus=$(nproc) +num_cpus=$(($num_cpus<32?$num_cpus:32)) [ "$num_cpus" != 0 ] || num_cpus=1 last_cpu=$((num_cpus - 1)) From 296231a34ed2fe5f93427ee3755da864b40fb517 Mon Sep 17 00:00:00 2001 From: Eugene Paniot Date: Mon, 23 Jan 2017 11:14:58 +0300 Subject: [PATCH 2/7] Enable bash verbose mode. Set pipefail to produce a failure return code if any command errors. Set nounset to exit when your script tries to use undeclared variables. Set "-e" to exit immediately when a command fails. Change max devices to max 28: https://bugzilla.redhat.com/show_bug.cgi?id=1383283 MAX_SWAPFILES is defined as ((1 << MAX_SWAPFILES_SHIFT) - SWP_MIGRATION_NUM - SWP_HWPOISON_NUM) which limits the number of swap files below 32 (depending on kernel config), quite possibly to 28. --- zramstart | 12 +++++++++--- zramstop | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/zramstart b/zramstart index bb72265..4d7069c 100755 --- a/zramstart +++ b/zramstart @@ -1,7 +1,13 @@ #!/bin/sh -num_cpus=$(nproc) -num_cpus=$(($num_cpus<32?$num_cpus:32)) +set -o pipefail +set -o nounset +set -e +set -x + +num_cpus=$(grep -c processor /proc/cpuinfo) +num_cpus=$(($num_cpus<28?$num_cpus:28)) + [ "$num_cpus" != 0 ] || num_cpus=1 last_cpu=$((num_cpus - 1)) @@ -16,7 +22,7 @@ modprobe -q zram num_devices=$num_cpus for i in $(seq 0 $last_cpu); do #enable lz4 if that supported - grep -q lz4 /sys/block/zram$i/comp_algorithm && echo lz4 > /sys/block/zram$i/comp_algorithm + [ -f /sys/block/zram$i/comp_algorithm ] && grep -q lz4 /sys/block/zram$i/comp_algorithm && echo lz4 > /sys/block/zram$i/comp_algorithm echo $mem_by_cpu > /sys/block/zram$i/disksize mkswap /dev/zram$i swapon -p 100 /dev/zram$i diff --git a/zramstop b/zramstop index 42eea41..88ca075 100755 --- a/zramstop +++ b/zramstop @@ -1,5 +1,7 @@ #!/bin/sh +set -x + for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do swapoff "$i" done From 5fb69b01ace3649363e06e39f61da6ab675bd0da Mon Sep 17 00:00:00 2001 From: Eugene Paniot Date: Tue, 14 Mar 2017 13:08:38 +0300 Subject: [PATCH 3/7] Solve loop in systemd dependencies --- mkzram.service | 2 +- zram.spec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mkzram.service b/mkzram.service index 340b877..d3f4d28 100644 --- a/mkzram.service +++ b/mkzram.service @@ -1,6 +1,6 @@ [Unit] Description=Enable compressed swap in memory using zram -After=multi-user.target +DefaultDependencies=no [Service] RemainAfterExit=yes diff --git a/zram.spec b/zram.spec index 13b4ac1..adec16f 100644 --- a/zram.spec +++ b/zram.spec @@ -1,6 +1,6 @@ Summary: Enable compressed swap in memory Name: zram -Version: 1.0.0 +Version: 1.0.1 Release: 2%{?dist} License: GPLv2 Group: System Environment/Daemons From 8f31584a39c1bd29e630649b326c951e6c5c79f4 Mon Sep 17 00:00:00 2001 From: Eugene Paniot Date: Wed, 28 Jun 2017 13:19:04 +0300 Subject: [PATCH 4/7] Update stop/start logick --- zramstart | 26 ++++++++++++++++++-------- zramstop | 13 +++++++------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/zramstart b/zramstart index 4d7069c..9fc6941 100755 --- a/zramstart +++ b/zramstart @@ -5,25 +5,35 @@ set -o nounset set -e set -x +FACTOR=33 +COMP_ALGORITHM=lz4 + num_cpus=$(grep -c processor /proc/cpuinfo) num_cpus=$(($num_cpus<28?$num_cpus:28)) [ "$num_cpus" != 0 ] || num_cpus=1 +max_comp_streams=$(($num_cpus>2?2:1)) last_cpu=$((num_cpus - 1)) -FACTOR=33 + [ -f /etc/sysconfig/zram ] && source /etc/sysconfig/zram || true -factor=$FACTOR # percentage memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ') -mem_by_cpu=$(($memtotal/$num_cpus*$factor/100*1024)) +mem_by_cpu=$(($memtotal/$num_cpus*${FACTOR}/100*1024)) -modprobe -q zram num_devices=$num_cpus +#modprobe -q zram num_devices=$num_cpus +modprobe -av zram for i in $(seq 0 $last_cpu); do + n=$(cat /sys/class/zram-control/hot_add) + block=/sys/block/zram${n} + #enable lz4 if that supported - [ -f /sys/block/zram$i/comp_algorithm ] && grep -q lz4 /sys/block/zram$i/comp_algorithm && echo lz4 > /sys/block/zram$i/comp_algorithm - echo $mem_by_cpu > /sys/block/zram$i/disksize - mkswap /dev/zram$i - swapon -p 100 /dev/zram$i + [ -f ${block}/comp_algorithm ] && grep -q ${COMP_ALGORITHM} ${block}/comp_algorithm && echo ${COMP_ALGORITHM} > ${block}/comp_algorithm + + echo $mem_by_cpu > ${block}/disksize + #echo ${max_comp_streams} > ${block}/max_comp_streams + + mkswap /dev/zram$n + swapon -v -p 100 /dev/zram$n done diff --git a/zramstop b/zramstop index 88ca075..3fd1c42 100755 --- a/zramstop +++ b/zramstop @@ -1,12 +1,13 @@ #!/bin/sh +set -o pipefail +set -o nounset +set -e set -x for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do - swapoff "$i" + swapoff -v "$i" + n=$(echo $i | sed 's/[^0-9]//g') + echo 1 > /sys/block/zram${n}/reset + echo $n > /sys/class/zram-control/hot_remove done - -if grep -q "^zram " /proc/modules; then - sleep 1 - rmmod zram -fi From c6825f2d4f2091a0af2f39b29d05c7da0c8efaa3 Mon Sep 17 00:00:00 2001 From: Eugene Paniot Date: Wed, 28 Jun 2017 14:27:32 +0300 Subject: [PATCH 5/7] Add kernel major version detection for zram-control --- zram.spec | 2 +- zramstart | 13 +++++++++---- zramstop | 12 +++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/zram.spec b/zram.spec index adec16f..40d272d 100644 --- a/zram.spec +++ b/zram.spec @@ -1,7 +1,7 @@ Summary: Enable compressed swap in memory Name: zram Version: 1.0.1 -Release: 2%{?dist} +Release: 3%{?dist} License: GPLv2 Group: System Environment/Daemons Source0: %{name}-%{version}.tar.bz2 diff --git a/zramstart b/zramstart index 9fc6941..5d9cb40 100755 --- a/zramstart +++ b/zramstart @@ -21,18 +21,23 @@ last_cpu=$((num_cpus - 1)) memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ') mem_by_cpu=$(($memtotal/$num_cpus*${FACTOR}/100*1024)) -#modprobe -q zram num_devices=$num_cpus -modprobe -av zram +modprobe zram num_devices=$num_cpus + +function getKernelMajorVersion() { + head -c 1 /proc/sys/kernel/osrelease +} for i in $(seq 0 $last_cpu); do - n=$(cat /sys/class/zram-control/hot_add) + n=$i + if [ $(getKernelMajorVersion) -gt 3 ]; then + n=$(cat /sys/class/zram-control/hot_add) + fi block=/sys/block/zram${n} #enable lz4 if that supported [ -f ${block}/comp_algorithm ] && grep -q ${COMP_ALGORITHM} ${block}/comp_algorithm && echo ${COMP_ALGORITHM} > ${block}/comp_algorithm echo $mem_by_cpu > ${block}/disksize - #echo ${max_comp_streams} > ${block}/max_comp_streams mkswap /dev/zram$n swapon -v -p 100 /dev/zram$n diff --git a/zramstop b/zramstop index 3fd1c42..a89c89f 100755 --- a/zramstop +++ b/zramstop @@ -5,9 +5,19 @@ set -o nounset set -e set -x +function getKernelMajorVersion() { + head -c 1 /proc/sys/kernel/osrelease +} + for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do swapoff -v "$i" n=$(echo $i | sed 's/[^0-9]//g') echo 1 > /sys/block/zram${n}/reset - echo $n > /sys/class/zram-control/hot_remove + if [ $(getKernelMajorVersion) -gt 3 ]; then + echo $n > /sys/class/zram-control/hot_remove + fi done + +if [ $(getKernelMajorVersion) -le 3 ]; then + grep zram /proc/modules && rmmod -v zram +fi From e17bbd1d43b7dfd16904c02d52e4cab50543c11e Mon Sep 17 00:00:00 2001 From: Eugene Paniot Date: Wed, 28 Jun 2017 14:33:10 +0300 Subject: [PATCH 6/7] Modify zramstat to use mm_stat --- zramstat | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/zramstat b/zramstat index 66274c0..8a57f3b 100755 --- a/zramstat +++ b/zramstat @@ -1,10 +1,20 @@ #!/bin/sh +function getKernelMajorVersion() { + head -c 1 /proc/sys/kernel/osrelease +} + ls /sys/block/zram* > /dev/null 2>&1 || exit 0 for i in /sys/block/zram*; do - compr=$(< $i/compr_data_size) - orig=$(< $i/orig_data_size) + if [ $(getKernelMajorVersion) -gt 3 ]; then + compr=$(cat $i/mm_stat | awk '{ print $2 }') + orig=$(cat $i/mm_stat | awk '{ print $1 }') + else + compr=$(< $i/compr_data_size) + orig=$(< $i/orig_data_size) + fi + ratio=0 if [ $compr -gt 0 ]; then ratio=$(echo "scale=2; $orig*100/$compr" | bc -q) From 570ef6ec13d6384c077532b231586fd40cfd73ca Mon Sep 17 00:00:00 2001 From: Eugene Paniot Date: Wed, 5 Jul 2017 12:27:19 +0300 Subject: [PATCH 7/7] Dont try init disksize if it already initilized --- zram.spec | 2 +- zramstart | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/zram.spec b/zram.spec index 40d272d..d73fc0b 100644 --- a/zram.spec +++ b/zram.spec @@ -1,7 +1,7 @@ Summary: Enable compressed swap in memory Name: zram Version: 1.0.1 -Release: 3%{?dist} +Release: 4%{?dist} License: GPLv2 Group: System Environment/Daemons Source0: %{name}-%{version}.tar.bz2 diff --git a/zramstart b/zramstart index 5d9cb40..22fdf1d 100755 --- a/zramstart +++ b/zramstart @@ -28,17 +28,18 @@ function getKernelMajorVersion() { } for i in $(seq 0 $last_cpu); do - n=$i - if [ $(getKernelMajorVersion) -gt 3 ]; then - n=$(cat /sys/class/zram-control/hot_add) - fi - block=/sys/block/zram${n} - - #enable lz4 if that supported - [ -f ${block}/comp_algorithm ] && grep -q ${COMP_ALGORITHM} ${block}/comp_algorithm && echo ${COMP_ALGORITHM} > ${block}/comp_algorithm - - echo $mem_by_cpu > ${block}/disksize - - mkswap /dev/zram$n - swapon -v -p 100 /dev/zram$n + n=$i + if [ $(getKernelMajorVersion) -gt 3 ]; then + n=$(cat /sys/class/zram-control/hot_add) + fi + block=/sys/block/zram${n} + + if [ "$(cat ${block}/disksize)" -eq 0 ] ; then + [ -f ${block}/comp_algorithm ] && grep -q ${COMP_ALGORITHM} ${block}/comp_algorithm && echo ${COMP_ALGORITHM} > ${block}/comp_algorithm + + echo $mem_by_cpu > ${block}/disksize + mkswap /dev/zram$n + swapon -v -p 100 /dev/zram$n + fi done +