From 86735992c8afa34393aade4e0e03d2a40b8497f2 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Thu, 1 Nov 2018 00:16:58 +0300 Subject: [PATCH] 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 Signed-off-by: Christian Lamparter [use IEC's MiB unit] (cherry picked from commit b062c90f47dd0745562a8f2618c2a43136a4c7e1) --- package/system/zram-swap/Makefile | 2 +- package/system/zram-swap/files/zram.init | 58 +++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/package/system/zram-swap/Makefile b/package/system/zram-swap/Makefile index 527800501b..bcada98a81 100644 --- a/package/system/zram-swap/Makefile +++ b/package/system/zram-swap/Makefile @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=zram-swap PKG_VERSION:=1.1 -PKG_RELEASE:=2 +PKG_RELEASE:=3 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) 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 )" -- 2.30.2