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-kvmprocess. Once the guest is running the contents of the guest operating system p_w_picpath 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:
ksmservice starts and stops the KSM kernel thread.
ksmtunedservice controls and tunes the
ksm, dynamically managing same-page merging. The
ksmand stops the
ksmservice if memory sharing is not necessary. The
ksmtunedservice must be told with the
retuneparameter to run when new virtualized guests are created or destroyed.
Both of these services are controlled with the standard service management tools.
The KSM serviceThe
ksmservice 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
ksmservice is not started, KSM shares only 2000 pages. This default is low and provides limited memory saving benefits.
ksmservice is started, KSM will share up to half of the host system's main memory. Start the
ksmservice to enable KSM to share more memory.
# service ksm start Starting ksm: [ OK ]
ksmservice can be added to the default startup sequence. Make the
ksmservice persistent with the chkconfig command.
# chkconfig ksm on
The KSM tuning serviceThe
ksmtunedservice does not have any options. The
ksmtunedservice loops and adjusts
ksmtunedservice is notified by libvirt when a virtualized guest is created or destroyed.
# service ksmtuned start Starting ksmtuned: [ OK ]
ksmtunedservice can be tuned with the
ksmtunedto run tuning functions manually.
/etc/ksmtuned.conffile is the configuration file for the
ksmtunedservice. The file output below is the default
# Configuration file for ksmtuned. # How long ksmtuned should sleep between tuning adjustments # KSM_MONITOR_INTERVAL=60 # Millisecond sleep between ksm scans for 16Gb server. # Smaller servers sleep more, bigger sleep less. # KSM_SLEEP_MSEC=10 # KSM_NPAGES_BOOST=300 # KSM_NPAGES_DECAY=-50 # KSM_NPAGES_MIN=64 # KSM_NPAGES_MAX=1250 # KSM_THRES_COEF=20 # KSM_THRES_CONST=2048 # uncomment the following to enable ksmtuned debug information # LOGFILE=/var/log/ksmtuned # DEBUG=1
KSM variables and monitoringKSM stores monitoring data in the
/sys/kernel/mm/ksm/directory. Files in this directory are updated by the kernel and are an accurate record of KSM usage and statistics.
The variables in the list below are also configurable variables in the
/etc/ksmtuned.conffile as noted below.
Full scans run.
Total pages shared.
Pages presently shared.
Pages not scanned.
Pages no longer shared.
Number of volatile pages.
Whether the KSM process is running.
KSM tuning activity is stored in the
/var/log/ksmtunedlog file if the
DEBUG=1line is added to the
/etc/ksmtuned.conffile. The log file location can be changed with the
LOGFILEparameter. Changing the log file location is not advised and may require special configuration of SELinux settings.
/etc/sysconfig/ksmfile can manually set a number or all pages used by KSM as not swappable.
/etc/sysconfig/ksmfile with a text editor.
# The maximum number of unswappable kernel pages # which may be allocated by ksm (0 for unlimited) # If unset, defaults to half of total memory # KSM_MAX_KERNEL_PAGES=
KSM_MAX_KERNEL_PAGESline to manually configure the number of unswappable pages for KSM. Setting this variable to
0configures KSM to keep all identical pages in main memory which can improve performance if the system has sufficient main memory.
# The maximum number of unswappable kernel pages # which may be allocated by ksm (0 for unlimited) # If unset, defaults to half of total memory KSM_MAX_KERNEL_PAGES=0
Deactivating KSMKSM has a performance overhead which may be too large for certain environments or host systems.
KSM can be deactivated by stopping the
ksmservice and the
ksmtunedservice. Stopping the services deactivates KSM but does not persist after restarting.
# service ksm stop Stopping ksm: [ OK ] # service ksmtuned stop Stopping ksmtuned: [ OK ]
Persistently deactivate KSM with the
chkconfigcommand. To turn off the services, run the following commands:
# chkconfig ksm off # chkconfig ksmtuned off