ksm.txt 5.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. Like other madvise calls, they are intended for use on mapped areas of
  34. the user address space: they will report ENOMEM if the specified range
  35. includes unmapped gaps (though working on the intervening mapped areas),
  36. and might fail with EAGAIN if not enough memory for internal structures.
  37. Applications should be considerate in their use of MADV_MERGEABLE,
  38. restricting its use to areas likely to benefit. KSM's scans may use a lot
  39. of processing power: some installations will disable KSM for that reason.
  40. The KSM daemon is controlled by sysfs files in /sys/kernel/mm/ksm/,
  41. readable by all but writable only by root:
  42. pages_to_scan - how many present pages to scan before ksmd goes to sleep
  43. e.g. "echo 100 > /sys/kernel/mm/ksm/pages_to_scan"
  44. Default: 100 (chosen for demonstration purposes)
  45. sleep_millisecs - how many milliseconds ksmd should sleep before next scan
  46. e.g. "echo 20 > /sys/kernel/mm/ksm/sleep_millisecs"
  47. Default: 20 (chosen for demonstration purposes)
  48. merge_across_nodes - specifies if pages from different numa nodes can be merged.
  49. When set to 0, ksm merges only pages which physically
  50. reside in the memory area of same NUMA node. That brings
  51. lower latency to access of shared pages. Systems with more
  52. nodes, at significant NUMA distances, are likely to benefit
  53. from the lower latency of setting 0. Smaller systems, which
  54. need to minimize memory usage, are likely to benefit from
  55. the greater sharing of setting 1 (default). You may wish to
  56. compare how your system performs under each setting, before
  57. deciding on which to use. merge_across_nodes setting can be
  58. changed only when there are no ksm shared pages in system:
  59. set run 2 to unmerge pages first, then to 1 after changing
  60. merge_across_nodes, to remerge according to the new setting.
  61. Default: 1 (merging across nodes as in earlier releases)
  62. run - set 0 to stop ksmd from running but keep merged pages,
  63. set 1 to run ksmd e.g. "echo 1 > /sys/kernel/mm/ksm/run",
  64. set 2 to stop ksmd and unmerge all pages currently merged,
  65. but leave mergeable areas registered for next run
  66. Default: 0 (must be changed to 1 to activate KSM,
  67. except if CONFIG_SYSFS is disabled)
  68. The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/:
  69. pages_shared - how many shared pages are being used
  70. pages_sharing - how many more sites are sharing them i.e. how much saved
  71. pages_unshared - how many pages unique but repeatedly checked for merging
  72. pages_volatile - how many pages changing too fast to be placed in a tree
  73. full_scans - how many times all mergeable areas have been scanned
  74. A high ratio of pages_sharing to pages_shared indicates good sharing, but
  75. a high ratio of pages_unshared to pages_sharing indicates wasted effort.
  76. pages_volatile embraces several different kinds of activity, but a high
  77. proportion there would also indicate poor use of madvise MADV_MERGEABLE.
  78. Izik Eidus,
  79. Hugh Dickins, 17 Nov 2009