what is ksm

kernel same-page merging (ksm) is a kernel feature that allows hypervisors to share memory pages

KSM was merged into the Linux kernel mainline in version 2.6.32 but is disabled by default on most distros

KSM and KVM

enabling ksm on a machine with multiple kvm virtual machines with the same base image allows ksm to merge the pages used by the vms, this saves memory for a cpu usage tradeoff (cpu has to check if pages are able to be merged)

to check if ksm is enabled run :

cat /sys/kernel/mm/ksm/run

if this returns 0 its disabled

to enable ksm run the following as root :

echo 1 > /sys/kernel/mm/ksm/run

to show the current ksm status

grep -H '' /sys/kernel/mm/ksm/*

output:

/sys/kernel/mm/ksm/full_scans:6 # times the memory has been scanned
/sys/kernel/mm/ksm/merge_across_nodes:1
/sys/kernel/mm/ksm/pages_shared:235254 # amount of pages shared between processes
/sys/kernel/mm/ksm/pages_sharing:970746
/sys/kernel/mm/ksm/pages_to_scan:100
/sys/kernel/mm/ksm/pages_unshared:3151908 # pages able to be shared but no dupes found
/sys/kernel/mm/ksm/pages_volatile:244980 # unsharable pages
/sys/kernel/mm/ksm/run:1 # ksm enabled
/sys/kernel/mm/ksm/sleep_millisecs:20 # timeout between scans