ksm.txt 10 KB

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