Installation guide

Chapter 21.
165
KSM
The concept of shared memory is common in modern operating systems. For example, when a
program is first started it shares all of its memory with the parent program. When either the child or
parent program tries to modify this memory, the kernel allocates a new memory region, copies the
original contents and allows the program to modify this new region. This is known as copy on write.
KSM is a new Linux feature which uses this concept in reverse. KSM enables the kernel to examine
two or more already running programs and compare their memory. If any memory regions or pages
are identical, KSM reduces multiple references to multiple identical memory pages to a single
reference to a single page. This page is then marked copy on write. If the contents of the page is
modified, a new page is created.
This is useful for virtualization with KVM. When a virtualized guest is started, it only inherits the
memory from the parent qemu-kvm process. Once the guest is running the contents of the guest
operating system image can be shared when guests are running the same operating system or
applications. KSM only identifies and merges identical pages which does not interfere with the guest
or impact the security of the host or the guests. KSM allows KVM to request that these identical guest
memory regions be shared.
KSM provides enhanced memory speed and utilization. With KSM, common process data is stored
in cache or in main memory. This reduces cache misses for the KVM guests which can improve
performance for some applications and operating systems. Secondly, sharing memory reduces the
overall memory usage of guests which allows for higher densities and greater utilization of resources.
Red Hat Enterprise Linux uses two separate methods for controlling KSM:
The ksm service starts and stops the KSM kernel thread.
The ksmtuned service controls and tunes the ksm, dynamically managing same-page merging. The
ksmtuned service starts ksm and stops the ksm service if memory sharing is not necessary. The
ksmtuned service must be told with the retune parameter to run when new virtualized guests are
created or destroyed.
Both of these services are controlled with the standard service management tools.
The KSM service
The ksm service is a standard Linux daemon that uses the KSM kernel features.
KSM is included in the qemu-common package, which is a dependency of KVM. KSM is enabled
by default in Red Hat Enterprise Linux. When the ksm service is not started, KSM shares only 2000
pages. This default is low and provides limited memory saving benefits.
When the ksm service is started, KSM will share up to half of the host system's main memory. Start the
ksm service to enable KSM to share more memory.
# service ksm start
Starting ksm: [ OK ]
The ksm service can be added to the default startup sequence. Make the ksm service persistent with
the chkconfig command.
# chkconfig ksm on