Skip to content

Instantly share code, notes, and snippets.

@PerseusArkouda
Forked from zouppen/README.md
Last active Dec 5, 2020
Embed
What would you like to do?
Tool for extracting compression ratio and amount of compressed memory from zram swap partitions.

zraminfo

This is a simple tool for displaying memory information including compression ratio and percentage of compressed memory in Linux kernel using zram. This tool works without zram, but in that case it displays only basic memory statistics.

With LC_NUMERIC=en_GB.utf8 the output of ./zraminfo looks like this:

Physical memory:       4,111,052,800 bytes
Buffers and cache:       599,769,088 bytes / 14.6% of physical memory
Unallocated:             166,268,928 bytes /  4.0% of physical memory
Compressed:              348,880,896 bytes /  8.5% of physical memory
Decompressed size:       967,163,904 bytes / 20.5% of decompressed memory
Compression ratio: 2.772

If zram is not familiar to you, see article in Wikipedia. If you are using recent Debian or Ubuntu, you can start using zram by installing zram-config:

sudo apt-get install zram-config
#!/bin/sh -eu
TOTAL_DATA=0
TOTAL_COMP=0
HAS_ZRAM=
# Iterate through swap devices searching for compressed ones
while read NAME _; do
# Filter zram swaps and let's hope your ordinary swap doesn't have
# "zram" in its name :D
case $NAME in
*zram*) ;;
*) continue
esac
DIR=/sys`udevadm info --query=path --name=$NAME`
DATA=$(awk '{print $1}' $DIR/mm_stat)
COMP=$(awk '{print $3}' $DIR/mm_stat)
TOTAL_DATA=$((TOTAL_DATA + DATA))
TOTAL_COMP=$((TOTAL_COMP + COMP))
HAS_ZRAM=1
done </proc/swaps
# Extract physical memory in kibibytes and scale back to bytes
{
read _ MEM_KIB _
read _ FREE_KIB _
read _ BUF_KIB _
read _ CACHE_KIB _
} </proc/meminfo
/usr/bin/printf "\
Physical memory: %'17d bytes
Buffers and cache: %'17d bytes / %4.1f%% of physical memory
Unallocated: %'17d bytes / %4.1f%% of physical memory
" `echo "scale=6; 1024*$MEM_KIB; 1024*($BUF_KIB+$CACHE_KIB); 100*($BUF_KIB+$CACHE_KIB)/$MEM_KIB; 1024*$FREE_KIB; 100*$FREE_KIB/$MEM_KIB"|bc`
if test "$HAS_ZRAM"; then
/usr/bin/printf "\
Compressed: %'17d bytes / %4.1f%% of physical memory
Decompressed size: %'17d bytes / %4.1f%% of decompressed memory
Compression ratio: %.3f
" `echo "scale=6; $TOTAL_COMP; 100*$TOTAL_COMP/$MEM_KIB/1024; $TOTAL_DATA; 100*$TOTAL_DATA/(1024*$MEM_KIB-$TOTAL_COMP+$TOTAL_DATA); $TOTAL_DATA/$TOTAL_COMP"|bc`
else
echo "Compressed: 0 bytes / zram not in use"
fi
@zouppen

This comment has been minimized.

Copy link

@zouppen zouppen commented Dec 4, 2020

Merged to "mainline" at https://gist.github.com/zouppen/6886178 . Thanks for the fix!

@PerseusArkouda

This comment has been minimized.

Copy link
Owner Author

@PerseusArkouda PerseusArkouda commented Dec 5, 2020

Not even mention it. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment