Improved zram script from DebianWiki

Share your own howto's etc. Not for support questions!

Improved zram script from DebianWiki

Postby lbuiyzxl » 2017-08-05 14:29

Improved zram script from DebianWiki
I test this script on Debian Stretch with Linux 4.9 .
Some commented code is some unsure or failed optimize,maybe useful to you make more tricks.
Code: Select all
#!/bin/sh
### BEGIN INIT INFO
# Provides:          zram
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 1 6
# Short-Description: Use compressed RAM as in-memory swap
# Description:       Use compressed RAM as in-memory swap
### END INIT INFO

# Author: Antonio Galea <antonio.galea@gmail.com>
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or above, see terms at
#      https://gnu.org/licenses/gpl-3.0.txt

FRACTION=200

MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' </proc/meminfo`
#CPUS=`grep -c processor /proc/cpuinfo`
#swap_cache_pressure=29
#max_swap_priority=32767
#CPUS=$swap_cache_pressure
CPUS=1
SIZE=$(( MEMORY * FRACTION / 100 / CPUS ))
case "$1" in
  "start")
    echo 100 >/proc/sys/vm/swappiness
    echo 0 >/proc/sys/vm/page-cluster
    echo 0 >/proc/sys/vm/extfrag_threshold
    echo 1 >/proc/sys/vm/watermark_scale_factor
    #echo 1 > /sys/kernel/mm/ksm/run
    param=`modinfo zram|grep num_devices|cut -f2 -d:|tr -d ' '`
    modprobe zram $param=$CPUS
    for n in `seq $CPUS`; do
      i=$((n - 1))
      #echo 2 >/sys/block/zram$i/max_comp_streams
      #echo $2 >/sys/block/zram$i/comp_algorithm
      echo deflate >/sys/block/zram$i/comp_algorithm
      echo $SIZE >/sys/block/zram$i/disksize
      echo 0 >/sys/block/zram$i/queue/read_ahead_kb
      echo 2 >/sys/block/zram$i/queue/nomerges
      echo 2 >/sys/block/zram$i/queue/rq_affinity
      mkswap /dev/zram$i
      swapon -p 10 /dev/zram$i
      #swapon /dev/zram$i -d -p $((max_swap_priority-i))
    done
    #echo 32767 >/proc/sys/vm/vfs_cache_pressure
    echo 1 >/proc/sys/vm/compact_memory
    #echo 0 >/proc/sys/vm/min_free_kbytes
    #echo 1 >/proc/sys/vm/lowmem_reserve_ratio
    #echo 0 >/proc/sys/vm/admin_reserve_kbytes
    ;;
  "stop")
    echo 60 >/proc/sys/vm/swappiness
    echo 3 >/proc/sys/vm/page-cluster
    echo 500 >/proc/sys/vm/extfrag_threshold
    echo 10 >/proc/sys/vm/watermark_scale_factor
    #echo 0 > /sys/kernel/mm/ksm/run
    #echo 100 >/proc/sys/vm/vfs_cache_pressure
    #echo 42980 >/proc/sys/vm/min_free_kbytes
    #echo 256 >/proc/sys/vm/lowmem_reserve_ratio
    #echo 8192 >/proc/sys/vm/admin_reserve_kbytes
    for n in `seq $CPUS`; do
      i=$((n - 1))
      swapoff /dev/zram$i && echo "disabled disk $n of $CPUS"
    done
    wait
    sleep .5
    modprobe -r zram
    ;;
  "compact")
    for n in `seq $CPUS`; do
      i=$((n - 1))
      echo 1 >/sys/block/zram$i/compact
    done
    echo 1 >/proc/sys/vm/compact_memory
    ;;
  *)
    echo "Usage: `basename $0` (start | stop | compact)"
    exit 1
    ;;
esac
I'm sorry for my bad English skills
I'm don't want to be a nuisance.
lbuiyzxl
 
Posts: 64
Joined: 2017-05-11 08:35

Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable