zram-swap: dependency fix, stats info output

This commit is contained in:
coolsnowwolf 2018-11-28 19:14:28 +08:00
parent 6e9299a6b3
commit 9050f97a4a
3 changed files with 130 additions and 61 deletions

View File

@ -8,8 +8,8 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=zram-swap
PKG_VERSION:=1
PKG_RELEASE:=2
PKG_VERSION:=1.1
PKG_RELEASE:=3
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
@ -18,7 +18,7 @@ include $(INCLUDE_DIR)/package.mk
define Package/zram-swap
SECTION:=utils
CATEGORY:=Base system
DEPENDS:=+kmod-zram +!BUSYBOX_CONFIG_MKSWAP:swap-utils +!BUSYBOX_CONFIG_SWAPONOFF:block-mount
DEPENDS:=+kmod-zram +!(BUSYBOX_DEFAULT_MKSWAP||BUSYBOX_CONFIG_MKSWAP):swap-utils +!((BUSYBOX_DEFAULT_SWAPON||BUSYBOX_CONFIG_SWAPON)&&(BUSYBOX_DEFAULT_SWAPOFF||BUSYBOX_CONFIG_SWAPOFF)):block-mount
TITLE:=ZRAM swap scripts
PKGARCH:=all
endef
@ -43,4 +43,4 @@ define Package/zram-swap/install
$(INSTALL_BIN) ./files/zram.init $(1)/etc/init.d/zram
endef
$(eval $(call BuildPackage,zram-swap))
$(eval $(call BuildPackage,zram-swap))

175
package/system/zram-swap/files/zram.init Normal file → Executable file
View File

@ -16,7 +16,7 @@ zram_size() # in megabytes
if [ -z "$zram_size" ]; then
# e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
echo $(( $ram_size / 2048 ))
echo $(( ram_size / 2048 ))
else
echo "$zram_size"
fi
@ -26,11 +26,6 @@ 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
@ -54,9 +49,8 @@ zram_applicable()
zram_dev()
{
local core="$1"
echo "/dev/zram${core:-0}"
local idx="$1"
echo "/dev/zram${idx:-0}"
}
zram_reset()
@ -69,64 +63,139 @@ zram_reset()
echo "1" >"$proc_entry"
}
list_cpu_idx()
zram_getdev()
{
# Offset by 1 if /dev/zram0 is in use by /tmp
if [ "$(mount | grep /dev/zram0)" ]; then
local line i=1
# Hot-add new ZRAM device (if necessary)
if [ ! -b /dev/zram1 ]; then
cat /sys/class/zram-control/hot_add
fi
else
local line i=0
#get unallocated zram dev
local zdev=$( zram_dev )
if [ "$(mount | grep $zdev)" ]; then
local idx=`cat /sys/class/zram-control/hot_add`
zdev="$( zram_dev $idx )"
fi
while read line; do {
case "$line" in
[Pp]rocessor*)
echo $i
i=$(( $i + 1 ))
;;
esac
} done <"/proc/cpuinfo"
echo $zdev
}
zram_comp_algo()
{
local dev="$1"
local zram_comp_algo="$( uci -q get system.@system[0].zram_comp_algo )"
if [ -z "$zram_comp_algo" ] || [ ! -e /sys/block/$( basename $dev )/comp_algorithm ]; then
return 0
fi
if [ `grep -c "$zram_comp_algo" /sys/block/$( basename $dev )/comp_algorithm` -ne 0 ]; then
logger -s -t zram_comp_algo -p daemon.debug "Set compression algorithm '$zram_comp_algo' for zram '$dev'"
echo $zram_comp_algo > "/sys/block/$( basename $dev )/comp_algorithm"
else
logger -s -t zram_comp_algo -p daemon.debug "Compression algorithm '$zram_comp_algo' is not supported for '$dev'"
fi
}
zram_comp_streams()
{
local dev="$1"
local logical_cpus=$( grep -ci "^processor" /proc/cpuinfo )
[ $logical_cpus -gt 1 ] || return 1
local zram_comp_streams="$( uci -q get system.@system[0].zram_comp_streams )"
[ -n "$zram_comp_streams" ] && [ "$zram_comp_streams" -le "$logical_cpus" ] || zram_comp_streams=$logical_cpus
if [ -e /sys/block/$( basename $dev )/max_comp_streams ]; then
logger -s -t zram_comp_streams -p daemon.debug "Set max compression streams to '$zram_comp_streams' for zram '$dev'"
echo $zram_comp_streams > /sys/block/$( basename $dev )/max_comp_streams
fi
}
zram_stats()
{
#print various stats info about zram swap device
local zdev="/sys/block/$( basename "$1" )"
printf "\nGathering stats info for zram device \"$( basename "$1" )\"\n\n"
printf "Z-RAM\n-----\n"
printf "%-25s - %s\n" "Block device" $zdev
awk '{ printf "%-25s - %d Mb\n", "Device size", $1/1024/1024 }' <$zdev/disksize
printf "%-25s - %s\n" "Compression algo" "$(cat $zdev/comp_algorithm)"
printf "%-25s - %s\n" "Compression streams" "$( cat $zdev/max_comp_streams)"
awk 'BEGIN { fmt = "%-25s - %.2f %s\n"
fmt2 = "%-25s - %d\n"
print "\nDATA\n----" }
{ printf fmt, "Original data size", $1/1024/1024, "Mb"
printf fmt, "Compressed data size", $2/1024/1024, "Mb"
printf fmt, "Compress ratio", $1/$2
print "\nMEMORY\n------"
printf fmt, "Memory used, total", $3/1024/1024, "Mb"
printf fmt, "Allocator overhead", ($3-$2)/1024/1024, "Mb"
printf fmt, "Allocator efficiency", $2/$3*100, "%"
printf fmt, "Maximum memory ever used", $5/1024/1024, "Mb"
printf fmt, "Memory limit", $4/1024/1024, "Mb"
print "\nPAGES\n-----"
printf fmt2, "Same pages count", $6
printf fmt2, "Pages compacted", $7 }' <$zdev/mm_stat
awk '{ printf "%-25s - %d\n", "Free pages discarded", $4 }' <$zdev/io_stat
}
zram_compact()
{
# compact zram device (reduce memory allocation overhead)
local zdev="/sys/block/$( basename "$1" )"
old_mem_used=`awk '{print $3}' <$zdev/mm_stat`
old_overhead=`awk '{print $3-$2}' <$zdev/mm_stat`
echo ""
echo "Compacting zram device..."
echo 1 > $zdev/compact
awk -v old_mem="$old_mem_used" -v ovr="$old_overhead" 'BEGIN { fmt = "%-25s - %.1f %s\n" }
{ printf fmt, "Memory usage reduced by ", (old_mem-$3)/1024/1024, "Mb"
printf fmt, "Overhead reduced by", (ovr-($3-$2))/ovr*100, "%" }' <$zdev/mm_stat
}
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
local zram_dev
for core in $( list_cpu_idx ); do {
zram_dev="$( zram_dev "$core" )"
zram_applicable "$zram_dev" || return 1
if [ $( grep -cs zram /proc/swaps ) -ne 0 ]; then
logger -s -t zram_start -p daemon.notice "[OK] zram swap is already mounted"
# If not running interactively, than just quit
[ -z "$PS1" ] && return 1
logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
# show memory stats and compact all zram swaps
for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do {
zram_compact "$zram_dev"
zram_stats "$zram_dev"
} done
return
fi
zram_reset "$zram_dev" "enforcing defaults"
echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename $zram_dev )/disksize"
mkswap "$zram_dev"
swapon "$zram_dev"
} done
zram_dev="$( zram_getdev )"
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"
zram_comp_algo "$zram_dev"
zram_comp_streams "$zram_dev"
echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename "$zram_dev" )/disksize"
mkswap "$zram_dev"
swapon "$zram_dev"
}
stop()
{
local zram_dev proc_entry
local zram_dev
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"
for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do {
logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev"
swapoff "$zram_dev" && zram_reset "$zram_dev" "claiming memory back"
local dev_index="$( echo $zram_dev | grep -o "[0-9]*$" )"
if [ $dev_index -ne 0 ]; then
logger -s -t zram_stop -p daemon.debug "removing zram $zram_dev"
echo $dev_index > /sys/class/zram-control/hot_remove
fi
} done
}

View File

@ -1728,16 +1728,16 @@ config BUSYBOX_DEFAULT_SETSID
default n
config BUSYBOX_DEFAULT_SWAPON
bool
default n
default y
config BUSYBOX_DEFAULT_FEATURE_SWAPON_DISCARD
bool
default n
default y
config BUSYBOX_DEFAULT_FEATURE_SWAPON_PRI
bool
default n
default y
config BUSYBOX_DEFAULT_SWAPOFF
bool
default n
default y
config BUSYBOX_DEFAULT_FEATURE_SWAPONOFF_LABEL
bool
default n