Kernel SamePage Merging is a recent linux kernel feature which combines identical memory pages from multiple processes into one copy on write memory region. Because kvm guest virtual machines run as processes under linux, this feature provides the memory overcommit feature to kvm so important to hypervisors for more efficient use of memory. So if you need to run multiple virtual machines on a host where memory is a constraint, then KSM is your solution. Even in cases where memory is not a constraint you might have a particular application that you want separated onto individual virtual machines and want maximum scalability. One case that might come to mind is webhosting where you might want to maximize scalability. Redhat claims that tests with KSM were able to achieve 600 vms on a host with 48 cores and 256 GB RAM.
In this post I’ll be walking through the basics of using KSM with kvm and most of it will apply to other applications as well. This feature has been accepted for inclusion in linux kernel 2.6.32 release but is already backported for testing in some kernel builds. Fedora 12 is one distribution I know where you can start testing KSM but note that Fedora 12 is considered unstable at this time so only use it for testing. For other distributions you’ll have to check with the corresponding maintainers.
First thing you want to do is verify that KSM is available and enabled in your kernel. Future releases of the kernel will enable this by default but you should verify this at least for now. Run the following command to verify that KSM is enabled in your kernel.
grep KSM /boot/config-`uname –r`
Below is a snapshot of what you should see if KSM is enabled.
You will also see a subdirectory for KSM under /sys/kernel/mm/KSM filesystem showing in-kernel values related to KSM.
By default KSM is limited to 2000 kernel pages which can be seen by inspecting the max_kernel_pages file under the /sys/kernel/mm/KSM filesystem.
# cat /sys/kernel/mm/KSM/max_kernel_pages 2000
Once you have verified that your kernel has KSM support, the next step is to verify that your version of qemu-kvm supports samepage merging.
In order for your KVM guests to take advantage of KSM, your version of qemu-kvm must explicitly request from the kernel that identical pages be merged using the new madvise interface. The patch for this feature was added to the kvm development tree just recently following the kvm-88 release. If you’re compiling kvm yourself you can verify whether your version of kvm will support KSM by inspecting exec.c source file for the following lines of code.
#ifdef MADV_MERGEABLE madvise(new_block->host, size, MADV_MERGEABLE); #endif
If you don’t see these lines in your exec.c file then your kvm process will still run fine but but it won’t take advantage of KSM. If you do have KSM support in your version kvm then you can expect to see much more efficient use of memory on your host when running multiple kvm guests.
Once you’ve verified support for KSM in your kernel and in kvm as described above you simply need to run multiple similar guests to take advantage of kernel samepage merging. It doesn’t matter whether you use command line, virt-manager or any other management tool as long as your underlying kvm process supports KSM. With multiple virtual machines running, you can verify that KSM is working by inspecting the following file to see how many pages are being shared between your kvm guests.
Once this value is greater than zero, you’re taking advantage of KSM.
One of the most popular applications of server virtualization technology is consolidating physical servers into virtual machines on a single server. Most enterprise hypervisors provide memory overcommit features in order to gain even more usage out of the same physical memory resources and thereby more dollar savings. KSM in the linux kernel adds this important feature which allows kvm to also provide these savings so important to enterprises. Kernel Samepage Merging has been accepted for inclusion in the 2.6.32 linux kernel so this is a feature you can expect to benefit from very soon. If you can get your hands on a earlier kernel backported to KSM, give it a try.
KSM has been in development for some time now. At the KVM 2008 forum, a presentation was made by Izik Eidus who developed the kernel feature explaining some of the architecture behind KSM. I’ve attached the slides from the presentation below for reference in case you haven’t seen it yet.