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..d73fc0b 100644 --- a/zram.spec +++ b/zram.spec @@ -1,7 +1,7 @@ Summary: Enable compressed swap in memory Name: zram -Version: 1.0.0 -Release: 2%{?dist} +Version: 1.0.1 +Release: 4%{?dist} License: GPLv2 Group: System Environment/Daemons Source0: %{name}-%{version}.tar.bz2 diff --git a/zramstart b/zramstart index ce549fb..22fdf1d 100755 --- a/zramstart +++ b/zramstart @@ -1,22 +1,45 @@ #!/bin/sh -num_cpus=$(nproc) +set -o pipefail +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 zram num_devices=$num_cpus -modprobe -q zram num_devices=$num_cpus +function getKernelMajorVersion() { + head -c 1 /proc/sys/kernel/osrelease +} 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 - echo $mem_by_cpu > /sys/block/zram$i/disksize - mkswap /dev/zram$i - swapon -p 100 /dev/zram$i + 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 + 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) diff --git a/zramstop b/zramstop index 42eea41..a89c89f 100755 --- a/zramstop +++ b/zramstop @@ -1,10 +1,23 @@ #!/bin/sh +set -o pipefail +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 "$i" + swapoff -v "$i" + n=$(echo $i | sed 's/[^0-9]//g') + echo 1 > /sys/block/zram${n}/reset + if [ $(getKernelMajorVersion) -gt 3 ]; then + echo $n > /sys/class/zram-control/hot_remove + fi done -if grep -q "^zram " /proc/modules; then - sleep 1 - rmmod zram +if [ $(getKernelMajorVersion) -le 3 ]; then + grep zram /proc/modules && rmmod -v zram fi