ksm.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. .. _admin_guide_ksm:
  2. =======================
  3. Kernel Samepage Merging
  4. =======================
  5. Overview
  6. ========
  7. KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
  8. added to the Linux kernel in 2.6.32. See ``mm/ksm.c`` for its implementation,
  9. and http://lwn.net/Articles/306704/ and http://lwn.net/Articles/330589/
  10. KSM was originally developed for use with KVM (where it was known as
  11. Kernel Shared Memory), to fit more virtual machines into physical memory,
  12. by sharing the data common between them. But it can be useful to any
  13. application which generates many instances of the same data.
  14. The KSM daemon ksmd periodically scans those areas of user memory
  15. which have been registered with it, looking for pages of identical
  16. content which can be replaced by a single write-protected page (which
  17. is automatically copied if a process later wants to update its
  18. content). The amount of pages that KSM daemon scans in a single pass
  19. and the time between the passes are configured using :ref:`sysfs
  20. intraface <ksm_sysfs>`
  21. KSM only merges anonymous (private) pages, never pagecache (file) pages.
  22. KSM's merged pages were originally locked into kernel memory, but can now
  23. be swapped out just like other user pages (but sharing is broken when they
  24. are swapped back in: ksmd must rediscover their identity and merge again).
  25. Controlling KSM with madvise
  26. ============================
  27. KSM only operates on those areas of address space which an application
  28. has advised to be likely candidates for merging, by using the madvise(2)
  29. system call::
  30. int madvise(addr, length, MADV_MERGEABLE)
  31. The app may call
  32. ::
  33. int madvise(addr, length, MADV_UNMERGEABLE)
  34. to cancel that advice and restore unshared pages: whereupon KSM
  35. unmerges whatever it merged in that range. Note: this unmerging call
  36. may suddenly require more memory than is available - possibly failing
  37. with EAGAIN, but more probably arousing the Out-Of-Memory killer.
  38. If KSM is not configured into the running kernel, madvise MADV_MERGEABLE
  39. and MADV_UNMERGEABLE simply fail with EINVAL. If the running kernel was
  40. built with CONFIG_KSM=y, those calls will normally succeed: even if the
  41. the KSM daemon is not currently running, MADV_MERGEABLE still registers
  42. the range for whenever the KSM daemon is started; even if the range
  43. cannot contain any pages which KSM could actually merge; even if
  44. MADV_UNMERGEABLE is applied to a range which was never MADV_MERGEABLE.
  45. If a region of memory must be split into at least one new MADV_MERGEABLE
  46. or MADV_UNMERGEABLE region, the madvise may return ENOMEM if the process
  47. will exceed ``vm.max_map_count`` (see Documentation/sysctl/vm.txt).
  48. Like other madvise calls, they are intended for use on mapped areas of
  49. the user address space: they will report ENOMEM if the specified range
  50. includes unmapped gaps (though working on the intervening mapped areas),
  51. and might fail with EAGAIN if not enough memory for internal structures.
  52. Applications should be considerate in their use of MADV_MERGEABLE,
  53. restricting its use to areas likely to benefit. KSM's scans may use a lot
  54. of processing power: some installations will disable KSM for that reason.
  55. .. _ksm_sysfs:
  56. KSM daemon sysfs interface
  57. ==========================
  58. The KSM daemon is controlled by sysfs files in ``/sys/kernel/mm/ksm/``,
  59. readable by all but writable only by root:
  60. pages_to_scan
  61. how many pages to scan before ksmd goes to sleep
  62. e.g. ``echo 100 > /sys/kernel/mm/ksm/pages_to_scan``.
  63. Default: 100 (chosen for demonstration purposes)
  64. sleep_millisecs
  65. how many milliseconds ksmd should sleep before next scan
  66. e.g. ``echo 20 > /sys/kernel/mm/ksm/sleep_millisecs``
  67. Default: 20 (chosen for demonstration purposes)
  68. merge_across_nodes
  69. specifies if pages from different NUMA nodes can be merged.
  70. When set to 0, ksm merges only pages which physically reside
  71. in the memory area of same NUMA node. That brings lower
  72. latency to access of shared pages. Systems with more nodes, at
  73. significant NUMA distances, are likely to benefit from the
  74. lower latency of setting 0. Smaller systems, which need to
  75. minimize memory usage, are likely to benefit from the greater
  76. sharing of setting 1 (default). You may wish to compare how
  77. your system performs under each setting, before deciding on
  78. which to use. ``merge_across_nodes`` setting can be changed only
  79. when there are no ksm shared pages in the system: set run 2 to
  80. unmerge pages first, then to 1 after changing
  81. ``merge_across_nodes``, to remerge according to the new setting.
  82. Default: 1 (merging across nodes as in earlier releases)
  83. run
  84. * set to 0 to stop ksmd from running but keep merged pages,
  85. * set to 1 to run ksmd e.g. ``echo 1 > /sys/kernel/mm/ksm/run``,
  86. * set to 2 to stop ksmd and unmerge all pages currently merged, but
  87. leave mergeable areas registered for next run.
  88. Default: 0 (must be changed to 1 to activate KSM, except if
  89. CONFIG_SYSFS is disabled)
  90. use_zero_pages
  91. specifies whether empty pages (i.e. allocated pages that only
  92. contain zeroes) should be treated specially. When set to 1,
  93. empty pages are merged with the kernel zero page(s) instead of
  94. with each other as it would happen normally. This can improve
  95. the performance on architectures with coloured zero pages,
  96. depending on the workload. Care should be taken when enabling
  97. this setting, as it can potentially degrade the performance of
  98. KSM for some workloads, for example if the checksums of pages
  99. candidate for merging match the checksum of an empty
  100. page. This setting can be changed at any time, it is only
  101. effective for pages merged after the change.
  102. Default: 0 (normal KSM behaviour as in earlier releases)
  103. max_page_sharing
  104. Maximum sharing allowed for each KSM page. This enforces a
  105. deduplication limit to avoid high latency for virtual memory
  106. operations that involve traversal of the virtual mappings that
  107. share the KSM page. The minimum value is 2 as a newly created
  108. KSM page will have at least two sharers. The higher this value
  109. the faster KSM will merge the memory and the higher the
  110. deduplication factor will be, but the slower the worst case
  111. virtual mappings traversal could be for any given KSM
  112. page. Slowing down this traversal means there will be higher
  113. latency for certain virtual memory operations happening during
  114. swapping, compaction, NUMA balancing and page migration, in
  115. turn decreasing responsiveness for the caller of those virtual
  116. memory operations. The scheduler latency of other tasks not
  117. involved with the VM operations doing the virtual mappings
  118. traversal is not affected by this parameter as these
  119. traversals are always schedule friendly themselves.
  120. stable_node_chains_prune_millisecs
  121. specifies how frequently KSM checks the metadata of the pages
  122. that hit the deduplication limit for stale information.
  123. Smaller milllisecs values will free up the KSM metadata with
  124. lower latency, but they will make ksmd use more CPU during the
  125. scan. It's a noop if not a single KSM page hit the
  126. ``max_page_sharing`` yet.
  127. The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``:
  128. pages_shared
  129. how many shared pages are being used
  130. pages_sharing
  131. how many more sites are sharing them i.e. how much saved
  132. pages_unshared
  133. how many pages unique but repeatedly checked for merging
  134. pages_volatile
  135. how many pages changing too fast to be placed in a tree
  136. full_scans
  137. how many times all mergeable areas have been scanned
  138. stable_node_chains
  139. the number of KSM pages that hit the ``max_page_sharing`` limit
  140. stable_node_dups
  141. number of duplicated KSM pages
  142. A high ratio of ``pages_sharing`` to ``pages_shared`` indicates good
  143. sharing, but a high ratio of ``pages_unshared`` to ``pages_sharing``
  144. indicates wasted effort. ``pages_volatile`` embraces several
  145. different kinds of activity, but a high proportion there would also
  146. indicate poor use of madvise MADV_MERGEABLE.
  147. The maximum possible ``pages_sharing/pages_shared`` ratio is limited by the
  148. ``max_page_sharing`` tunable. To increase the ratio ``max_page_sharing`` must
  149. be increased accordingly.
  150. --
  151. Izik Eidus,
  152. Hugh Dickins, 17 Nov 2009