From a9deed62af569608af3e4e076138d788e24b1652 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Sun, 24 Feb 2019 10:00:03 +0300 Subject: [PATCH] zram-swap: Add extra commands for status/compaction This patch adds two new commands: zram status - shows memory stats for all zram swaps zram compaction - trigger compaction for all zram swaps Signed-off-by: Emil Muratov Signed-off-by: Christian Lamparter --- package/system/zram-swap/files/zram.init | 55 +++++++++++++++--------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/package/system/zram-swap/files/zram.init b/package/system/zram-swap/files/zram.init index 6b5714bb1c3..ae432f3b003 100755 --- a/package/system/zram-swap/files/zram.init +++ b/package/system/zram-swap/files/zram.init @@ -2,17 +2,21 @@ START=15 -ram_size() +EXTRA_COMMANDS="compact status" +EXTRA_HELP=" compact trigger compaction for all Z-RAM swap dev's + status print out status information & statistics about Z-RAM swap devices" + +ram_getsize() { local line while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done $zdev/compact + + # If not running interactively, than just return + [ -z "$PS1" ] && return 0 echo "" - echo "Compacting zram device..." - echo 1 > $zdev/compact + echo "Compacting zram device $zdev" 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, "MiB" printf fmt, "Overhead reduced by", (ovr-($3-$2))/ovr*100, "%" }' <$zdev/mm_stat @@ -156,23 +164,13 @@ zram_compact() start() { - local zram_size="$( zram_size )" - local zram_dev - 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 - - # 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 + return 1 fi - zram_dev="$( zram_getdev )" + local zram_size="$( zram_getsize )" + local 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)" @@ -200,3 +198,18 @@ stop() } done } +# show memory stats for all zram swaps +status() +{ + for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do { + zram_stats "$zram_dev" + } done +} + +# trigger compaction for all zram swaps +compact() +{ + for zram_dev in $( grep zram /proc/swaps |awk '{print $1}' ); do { + zram_compact "$zram_dev" + } done +}