Kernel Samepage Merging (KSM) is essentially active memory deduplication.  What does this means for our KVM guests?  Basically the more similar guests we have running, the more of a memory footprint they share, which means less memory they actually hold.  These free pages can then be reutilized by the host to provide improved i/o through some of the caching that KVM does or by an increased capacity for other guests.

Verify Support for KSM in the Linux Kernel

bash
grep KSM /boot/config-`uname -r`
CONFIG_KSM=y

View KSM Parameters

bash
ls /sys/kernel/mm/ksm/
full_scans    pages_sharing  pages_unshared  run
pages_shared  pages_to_scan  pages_volatile  sleep_millisecs

Each file in this directory has a file which contains statistics on how KSM is being used.  If you would like to see if KSM is working check the pages_shared, any value higher than zero indicates that it is working.

View Efficacy of KSM

bash
cat /sys/kernel/mm/ksm/pages_shared
60887
bash
cat /sys/kernel/mm/ksm/pages_sharing
835511
bash
cat /sys/kernel/mm/ksm/pages_unshared
124818

On my machine I am sharing 60887 unique pages between 835511 pages with only 124818 pages that cannot be shared at this particular moment.  With 4KB per page this means I have 237MB of unique pages which are being shared to eliminate the need for 3.2GB of RAM, while only having 487MB of unshared memory.  Which isn’t bad considering this is a very small host which only runs two VMs with 2GB of RAM each.