diff options
author | Hauke Mehrtens <hauke@openwrt.org> | 2013-01-06 16:21:29 +0000 |
---|---|---|
committer | Hauke Mehrtens <hauke@openwrt.org> | 2013-01-06 16:21:29 +0000 |
commit | 71830c60f7dbf02b8be097ddfc942e01dc4fb0e4 (patch) | |
tree | c8f83b7a719fb7d168480afea6939577d79af1ff /package/zram-swap/files/etc/init.d | |
parent | 009b82e8ef3dedb2ff0534e2219f5b137a5da2b6 (diff) | |
download | upstream-71830c60f7dbf02b8be097ddfc942e01dc4fb0e4.tar.gz upstream-71830c60f7dbf02b8be097ddfc942e01dc4fb0e4.tar.bz2 upstream-71830c60f7dbf02b8be097ddfc942e01dc4fb0e4.zip |
zram: add package to use compressed ram disk for swap
The busybox size is increased by 3.2KByte uncompressed on bcm47xx with
this commit.
This zram-swap automatically creates a zram device, a swap partition on
it and make the kernel swap pages to it.
Thank you Bastian Bittorf <bittorf@bluebottle.com> for idea and the
script.
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@35025 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/zram-swap/files/etc/init.d')
-rw-r--r-- | package/zram-swap/files/etc/init.d/zram | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/package/zram-swap/files/etc/init.d/zram b/package/zram-swap/files/etc/init.d/zram new file mode 100644 index 0000000000..23de915c8f --- /dev/null +++ b/package/zram-swap/files/etc/init.d/zram @@ -0,0 +1,123 @@ +#!/bin/sh /etc/rc.common + +START=15 + +ram_size() +{ + local line + + while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done </proc/meminfo +} + +zram_size() # in megabytes +{ + local zram_size="$( uci -q get system.@system[0].zram_size_mb )" + local ram_size="$( ram_size )" + + if [ -z "$zram_size" ]; then + # e.g. 6mb for 16mb-routers or 61mb for 128mb-routers + echo $(( $ram_size / 2048 )) + else + echo "$zram_size" + fi +} + +zram_applicable() +{ + local zram_dev="$1" + + grep -sq ^"$zram_dev " /proc/swaps && { + logger -s -t zram_applicable -p daemon.notice "[OK] '$zram_dev' already active" + return 1 + } + + [ -e "$zram_dev" ] || { + logger -s -t zram_applicable -p daemon.crit "[ERROR] device '$zram_dev' not found" + return 1 + } + + which mkswap >/dev/null || { + logger -s -t zram_applicable -p daemon.err "[ERROR] 'mkswap' not installed" + return 1 + } + + which swapon >/dev/null || { + logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapon' not installed" + return 1 + } + + which swapoff >/dev/null || { + logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapoff' not installed" + return 1 + } +} + +zram_dev() +{ + local core="$1" + + echo "/dev/zram${core:-0}" +} + +zram_reset() +{ + local dev="$1" + local message="$2" + local proc_entry="/sys/block/$( basename "$dev" )/reset" + + logger -s -t zram_reset -p daemon.debug "$message via $proc_entry" + echo "1" >"$proc_entry" +} + +list_cpu_idx() +{ + local line i=0 + + while read line; do { + case "$line" in + [Pp]rocessor*) + echo $i + i=$(( $i + 1 )) + ;; + esac + } done <"/proc/cpuinfo" +} + +start() +{ + # http://shmilyxbq-compcache.googlecode.com/hg/README + # if >1 cpu_core, reinit kmodule with e.g. num_devices=4 + + local zram_size="$( zram_size )" + local zram_dev core + + for core in $( list_cpu_idx ); do { + zram_dev="$( zram_dev "$core" )" + zram_applicable "$zram_dev" || return 1 + + logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)" + + zram_reset "$zram_dev" "enforcing defaults" + echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename $zram_dev )/disksize" + mkswap "$zram_dev" + swapon "$zram_dev" + } done +} + +stop() +{ + local zram_dev proc_entry + + for core in $( list_cpu_idx ); do { + zram_dev="$( zram_dev "$core" )" + proc_entry="/sys/block/$( basename "$zram_dev" )/reset" + + grep -sq ^"$zram_dev " /proc/swaps && { + logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev" + swapoff "$zram_dev" + } + + zram_reset "$zram_dev" "claiming memory back" + } done +} + |