Skip to content

Instantly share code, notes, and snippets.

@jdef
Forked from anonymous/zram
Last active January 4, 2016 00:49
Show Gist options
  • Save jdef/8544249 to your computer and use it in GitHub Desktop.
Save jdef/8544249 to your computer and use it in GitHub Desktop.
#!/bin/bash
### BEGIN INIT INFO
# Provides: zram
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
### END INIT INFO
source /etc/init.d/functions
config_file=/etc/sysconfig/zram
FACTOR=50 # percent of memory dedicated to zram
[ -f $config_file ] && source $config_file || true
# safety valve? sanity check?
[ $FACTOR -gt 90 ] && FACTOR=90 || true
start() {
is_running && {
echo -n "; zram already configured, aborting"
return 1
} || true
# get the number of CPUs
num_devs=$(grep -c processor /proc/cpuinfo)
# if something goes wrong, assume we have 1
[ "$num_devs" != 0 ] || num_devs=1
# current swap devs
num_swap_devs=$(swapon -s|sed -e 1d|wc -l)
# max swap devs
include=$(echo /lib/modules/$(uname -r)/source/include)
x=$( { cat $include/linux/swap.h; cat << __EOF__ ;
#define VALUE_TO_STRING(x) #x
#define VALUE(x) VALUE_TO_STRING(x)
#pragma message(MAXSWAPFILES=VALUE(MAX_SWAPFILES))
__EOF__
} | cpp -I$include - - 2>/dev/null \
| sed -e 's/\(^#pragma.*e(\|)$\)//g' \
| grep -e ^MAXSWAPFILES= )
[ -n "$x" ] && {
eval $x
[ -z "$(echo $(( $MAXSWAPFILES )) | tr -d '[0-9]')" -a "$(( $MAXSWAPFILES ))" -gt 0 ]
} || {
MAXSWAPFILES=29; # reasonable guess? based on centos 6 kernel source defaults
echo -n -e "\nFailed to determine max swap files from kernel source"
warning ; echo
}
max_swapfiles=$(( $MAXSWAPFILES ))
# echo max_swapfiles=$max_swapfiles
[ $num_swap_devs -lt $max_swapfiles ] || {
echo "Number of swap devices is currently maxed out, no room for additional devices"
return 1
}
avail_swap_slots=$(( $max_swapfiles - $num_swap_devs ))
[ $num_devs -gt $avail_swap_slots ] && {
echo -n -e "\nShortage of swap file slots remaining: $num_devs > $avail_swap_slots"
warning ; echo
num_devs=$avail_swap_slots
} || true
# set decremented number of CPUs
last_dev=$((num_devs - 1))
# echo num_devs=$num_devs
# echo last_dev=$last_dev
# exit 0
# get the amount of memory in the machine
mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
#we will only assign 50% of system memory to zram
mem_total_kb=$((mem_total_kb * $FACTOR / 100))
mem_total=$((mem_total_kb * 1024))
# load dependency modules
modprobe zram num_devices=$num_devs
# initialize the devices
# Creating swap filesystems
# Switch the swaps on
for i in $(seq 0 $last_dev); do
echo $((mem_total / num_devs)) > /sys/block/zram$i/disksize
mkswap /dev/zram$i && swapon -p 100 /dev/zram$i
done
}
stop() {
for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do
swapoff "$i"
done
if grep -q "^zram " /proc/modules; then
sleep 1
rmmod zram
fi
}
is_running() {
ls /sys/block/zram* > /dev/null 2>&1
}
status() {
is_running || { echo "zram not running"; exit 0; }
echo -e "-------\nzram Compression Stats:\n-------"
for i in /sys/block/zram*; do
compr=$(< $i/compr_data_size)
orig=$(< $i/orig_data_size)
ratio=0
if [ $compr -gt 0 ]; then
ratio=$(echo "scale=2; $orig*100/$compr" | bc -q)
fi
echo -e "/dev/${i/*\/}:\t$ratio% ($orig -> $compr)"
done
echo -e "-------\nSWAP Stats:\n-------"
swapon -s | grep zram
echo -e "-------\nMemory Stats:\n-------"
free -m -l -t
}
case "$1" in
start)
action "Starting zram" start
;;
stop)
action "Stopping zram" stop
;;
restart)
"$0" stop
sleep 3
"$0" start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
RETVAL=1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment