While the kernel-based virtual machine (KVM) is designed to self-optimize, there are a few parameters that can...
be adjusted to make a KVM host perform better. The most important of these is kernel samepage merging (KSM), a feature that allows the kernel to handle memory more efficiently. KSM allows the Linux kernel to recognize memory pages that contain the same content, then it can merge these memory pages, consolidating the data to one location to be referred to multiple times.
If KVM is used on a host, typically several guest operating systems (OSes) are activated and often these are running the same OS, which means that a huge amount of kernel pages are loaded multiple times. By applying KSM, more virtual machines (VMs) can be started using same amount of memory. In fact, KSM allows over-committing of VMs. But, there is a performance penalty for using KSM. In average environments the performance price is about 10%, which is a reason to turn KSM off in some environments.
In Red Hat Enterprise Linux 6 and Fedora 14, KSM is switched on by default. It is implemented by two services that are started automatically at system initialization: ksmd and ksmtuned. Administrators should judge their environment and determine whether to keep KSM on or turn it off.
Determining whether to use KSM, or turn it off
If the purpose is to run as many VMs as possible, and performance is not an issue, KSM should be kept running. For example, it allows running 40 VMs on a host normally capable of running 30 VMs, which means maximum hardware usage efficiency. But, if a server is running with a relatively small amount of VMs and performance is an issue, KSM should be turned off.
The best choice for any system will depend on the memory calculation made while creating the virtual environment. If there is enough physical RAM in the virtualization host to meet the memory needs of the VMs without KSM, it is better to switch it off. Use chkconfig ksmd off, chkconfig ksmtuned off, service ksmd off and service ksmtuned off to switch off all parts of KSM. But if a host is tight on memory, it’s better to keep it on.
Tuning KSM for best performance
When using KSM, some of its parameters can be tuned for best performance. They are located in a small configuration file named /etc/ksmtuned.conf:
# Configuration file for ksmtuned.
# How long ksmtuned should sleep between tuning adjustments
# Millisecond sleep between ksm scans for 16Gb server.
# Smaller servers sleep more, bigger sleep less.
# uncomment the following if you want ksmtuned debug info
The most important parameters in the configuration file is KSM_SLEEP_MSEC. The default value that is used on Fedora 14 is set for large servers. When running a few VMs on a host while using KSM, it’s better to give the host a higher sleep time. For example, try KSM_SLEEP_MSEC=50 to test the effect on the VMs.
ABOUT THE AUTHOR: Sander van Vugt is an author and independent technical trainer, specializing in Linux since 1994. He is also a technical consultant for high-availability (HA) clustering and performance optimization, as well as an expert on SLED 10 administration.