Skip to content

Instantly share code, notes, and snippets.

@jriguera
Created November 14, 2015 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jriguera/2cb3686176323a95470d to your computer and use it in GitHub Desktop.
Save jriguera/2cb3686176323a95470d to your computer and use it in GitHub Desktop.
KSM
#!/usr/bin/env bash
cat <<EOF
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.
EOF
if [ "`cat /sys/kernel/mm/ksm/run`" -ne 1 ] ; then
echo 'KSM is not enabled. Run echo 1 > /sys/kernel/mm/ksm/run' to enable it.
exit 1
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)
if [ "$pages_sharing" -ne 0 ] ; then
ratio_sharing_to_shared=$(echo "scale=2; $pages_sharing / $pages_shared" | bc);
ratio_unshared_to_sharing=$(echo "scale=2; $pages_unshared / $pages_sharing" | bc);
fi
saved=$(echo "scale=0; $pages_sharing * $page_size/1024/1024" | bc);
echo
echo Shared memory is $(( $pages_shared * $page_size / 1024 / 1024)) MB
echo Saved memory is $(( $pages_sharing * $page_size / 1024 / 1024)) MB
echo
printf "Shared\tSharing\tUnshared\tVolatile\tSharing:Shared\tUnshared:Sharing\tSaved\n";
printf "%'d\t%'d\t%'d\t\t%'d\t\t%'f:1\t%'f:1\t\t%'dM\n" $pages_shared $pages_sharing $pages_unshared
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment