Skip to content

Instantly share code, notes, and snippets.

@mclang
Forked from wankdanker/ksmstat
Last active October 4, 2021 14:37
Show Gist options
  • Save mclang/050fb2c3ee1f37ad87b7ba60c15de23d to your computer and use it in GitHub Desktop.
Save mclang/050fb2c3ee1f37ad87b7ba60c15de23d to your computer and use it in GitHub Desktop.
Simple bash script to calculate ksm stats in MB
#!/bin/bash
if [ "$1" != "" ]; then
echo "
----------------------------------------------------------------------------
http://www.kernel.org/doc/Documentation/vm/ksm.txt :
The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/:
pages_shared - how many shared pages are being used
pages_sharing - how many more sites are sharing them i.e. how much saved
pages_unshared - how many pages unique but repeatedly checked for merging
pages_volatile - how many pages changing too fast to be placed in a tree
full_scans - how many times all mergeable areas have been scanned
A high ratio of pages_sharing to pages_shared indicates good sharing, but
a high ratio of pages_unshared to pages_sharing indicates wasted effort.
pages_volatile embraces several different kinds of activity, but a high
proportion there would also indicate poor use of madvise MADV_MERGEABLE.
----------------------------------------------------------------------------
"
fi;
pages_shared=`cat /sys/kernel/mm/ksm/pages_shared`;
pages_sharing=`cat /sys/kernel/mm/ksm/pages_sharing`;
pages_unshared=`cat /sys/kernel/mm/ksm/pages_unshared`;
pages_volatile=`cat /sys/kernel/mm/ksm/pages_volatile`;
page_size=`getconf PAGESIZE`;
ratio_sharing_to_shared=$(echo "scale=2;$pages_sharing / $pages_shared"|bc);
ratio_unshared_to_sharing=$(echo "scale=2;$pages_unshared / $pages_sharing"|bc);
saved=$(echo "scale=0;$pages_sharing * $page_size / 1024 / 1024"|bc);
printf "%10s %10s %10s %10s %10s:%-8s %10s:%-8s %10s\n" "Shared" "Sharing" "Unshared" "Volatile" "Sharing" "Shared" "Unshared" "Sharing" "Saved (MB)";
printf "%10d %10d %10d %10d %10.1f:%-8s %10.1f:%-8s %10d\n" $pages_shared $pages_sharing $pages_unshared $pages_volatile $ratio_sharing_to_shared "1" $ratio_unshared_to_sharing "1" $saved;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment