aboutsummaryrefslogtreecommitdiffstats
path: root/package/system/zram-swap/files
diff options
context:
space:
mode:
authorEmil Muratov <gpm@hotplug.ru>2018-11-01 00:16:58 +0300
committerChristian Lamparter <chunkeey@gmail.com>2019-07-07 13:02:06 +0200
commitb062c90f47dd0745562a8f2618c2a43136a4c7e1 (patch)
treedbb9373fb9d1d0ee3992dfacf16361075bb517f9 /package/system/zram-swap/files
parentc0d93432f2446fbf193ec15e4880cbafa9980779 (diff)
downloadupstream-b062c90f47dd0745562a8f2618c2a43136a4c7e1.tar.gz
upstream-b062c90f47dd0745562a8f2618c2a43136a4c7e1.tar.bz2
upstream-b062c90f47dd0745562a8f2618c2a43136a4c7e1.zip
zram-swap: Add zram compaction and statistics info output
Executing '/etc/init.d/zram start' during runtime (with a swap being already mounted) triggers zram device compaction and prints out nice stats info about zram memory usage Signed-off-by: Emil Muratov <gpm@hotplug.ru> Signed-off-by: Christian Lamparter <chunkeey@gmail.com> [use IEC's MiB unit]
Diffstat (limited to 'package/system/zram-swap/files')
-rwxr-xr-xpackage/system/zram-swap/files/zram.init58
1 files changed, 57 insertions, 1 deletions
diff --git a/package/system/zram-swap/files/zram.init b/package/system/zram-swap/files/zram.init
index 9fd4089c6b..6b5714bb1c 100755
--- a/package/system/zram-swap/files/zram.init
+++ b/package/system/zram-swap/files/zram.init
@@ -106,6 +106,54 @@ zram_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 MiB\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, "MiB"
+ printf fmt, "Compressed data size", $2/1024/1024, "MiB"
+ printf fmt, "Compress ratio", $1/$2, ""
+ print "\nMEMORY\n------"
+ printf fmt, "Memory used, total", $3/1024/1024, "MiB"
+ printf fmt, "Allocator overhead", ($3-$2)/1024/1024, "MiB"
+ printf fmt, "Allocator efficiency", $2/$3*100, "%"
+ printf fmt, "Maximum memory ever used", $5/1024/1024, "MiB"
+ printf fmt, "Memory limit", $4/1024/1024, "MiB"
+ 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, "MiB"
+ printf fmt, "Overhead reduced by", (ovr-($3-$2))/ovr*100, "%" }' <$zdev/mm_stat
+}
+
start()
{
local zram_size="$( zram_size )"
@@ -113,7 +161,15 @@ start()
if [ $( grep -cs zram /proc/swaps ) -ne 0 ]; then
logger -s -t zram_start -p daemon.notice "[OK] zram swap is already mounted"
- return 1
+ # 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
fi
zram_dev="$( zram_getdev )"