ksm.rst 9.7 KB
Newer Older
1 2 3 4 5
.. _ksm:

=======================
Kernel Samepage Merging
=======================
H
Hugh Dickins 已提交
6

7 8 9
Overview
========

H
Hugh Dickins 已提交
10
KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
11
added to the Linux kernel in 2.6.32.  See ``mm/ksm.c`` for its implementation,
H
Hugh Dickins 已提交
12 13 14 15 16 17 18
and http://lwn.net/Articles/306704/ and http://lwn.net/Articles/330589/

KSM was originally developed for use with KVM (where it was known as
Kernel Shared Memory), to fit more virtual machines into physical memory,
by sharing the data common between them.  But it can be useful to any
application which generates many instances of the same data.

19 20 21 22 23 24 25 26
The KSM daemon ksmd periodically scans those areas of user memory
which have been registered with it, looking for pages of identical
content which can be replaced by a single write-protected page (which
is automatically copied if a process later wants to update its
content). The amount of pages that KSM daemon scans in a single pass
and the time between the passes are configured using :ref:`sysfs
intraface <ksm_sysfs>`

H
Hugh Dickins 已提交
27
KSM only merges anonymous (private) pages, never pagecache (file) pages.
28 29 30
KSM's merged pages were originally locked into kernel memory, but can now
be swapped out just like other user pages (but sharing is broken when they
are swapped back in: ksmd must rediscover their identity and merge again).
H
Hugh Dickins 已提交
31

32 33 34
Controlling KSM with madvise
============================

H
Hugh Dickins 已提交
35 36
KSM only operates on those areas of address space which an application
has advised to be likely candidates for merging, by using the madvise(2)
37 38 39 40 41 42 43 44 45
system call::

	int madvise(addr, length, MADV_MERGEABLE)

The app may call

::

	int madvise(addr, length, MADV_UNMERGEABLE)
H
Hugh Dickins 已提交
46

47 48 49 50
to cancel that advice and restore unshared pages: whereupon KSM
unmerges whatever it merged in that range.  Note: this unmerging call
may suddenly require more memory than is available - possibly failing
with EAGAIN, but more probably arousing the Out-Of-Memory killer.
H
Hugh Dickins 已提交
51 52 53 54 55 56 57 58 59

If KSM is not configured into the running kernel, madvise MADV_MERGEABLE
and MADV_UNMERGEABLE simply fail with EINVAL.  If the running kernel was
built with CONFIG_KSM=y, those calls will normally succeed: even if the
the KSM daemon is not currently running, MADV_MERGEABLE still registers
the range for whenever the KSM daemon is started; even if the range
cannot contain any pages which KSM could actually merge; even if
MADV_UNMERGEABLE is applied to a range which was never MADV_MERGEABLE.

60 61
If a region of memory must be split into at least one new MADV_MERGEABLE
or MADV_UNMERGEABLE region, the madvise may return ENOMEM if the process
62
will exceed ``vm.max_map_count`` (see Documentation/sysctl/vm.txt).
63

H
Hugh Dickins 已提交
64 65 66 67 68 69
Like other madvise calls, they are intended for use on mapped areas of
the user address space: they will report ENOMEM if the specified range
includes unmapped gaps (though working on the intervening mapped areas),
and might fail with EAGAIN if not enough memory for internal structures.

Applications should be considerate in their use of MADV_MERGEABLE,
70 71
restricting its use to areas likely to benefit.  KSM's scans may use a lot
of processing power: some installations will disable KSM for that reason.
H
Hugh Dickins 已提交
72

73 74 75 76 77
.. _ksm_sysfs:

KSM daemon sysfs interface
==========================

78
The KSM daemon is controlled by sysfs files in ``/sys/kernel/mm/ksm/``,
H
Hugh Dickins 已提交
79 80
readable by all but writable only by root:

81
pages_to_scan
82 83 84 85
        how many pages to scan before ksmd goes to sleep
        e.g. ``echo 100 > /sys/kernel/mm/ksm/pages_to_scan``.

        Default: 100 (chosen for demonstration purposes)
86 87 88

sleep_millisecs
        how many milliseconds ksmd should sleep before next scan
89 90 91
        e.g. ``echo 20 > /sys/kernel/mm/ksm/sleep_millisecs``

        Default: 20 (chosen for demonstration purposes)
92 93

merge_across_nodes
94
        specifies if pages from different NUMA nodes can be merged.
95 96 97 98 99 100 101 102
        When set to 0, ksm merges only pages which physically reside
        in the memory area of same NUMA node. That brings lower
        latency to access of shared pages. Systems with more nodes, at
        significant NUMA distances, are likely to benefit from the
        lower latency of setting 0. Smaller systems, which need to
        minimize memory usage, are likely to benefit from the greater
        sharing of setting 1 (default). You may wish to compare how
        your system performs under each setting, before deciding on
103 104
        which to use. ``merge_across_nodes`` setting can be changed only
        when there are no ksm shared pages in the system: set run 2 to
105
        unmerge pages first, then to 1 after changing
106 107
        ``merge_across_nodes``, to remerge according to the new setting.

108 109 110
        Default: 1 (merging across nodes as in earlier releases)

run
111 112 113 114 115 116 117
        * set to 0 to stop ksmd from running but keep merged pages,
        * set to 1 to run ksmd e.g. ``echo 1 > /sys/kernel/mm/ksm/run``,
        * set to 2 to stop ksmd and unmerge all pages currently merged, but
	  leave mergeable areas registered for next run.

        Default: 0 (must be changed to 1 to activate KSM, except if
        CONFIG_SYSFS is disabled)
118 119 120 121 122 123 124 125 126 127 128 129

use_zero_pages
        specifies whether empty pages (i.e. allocated pages that only
        contain zeroes) should be treated specially.  When set to 1,
        empty pages are merged with the kernel zero page(s) instead of
        with each other as it would happen normally. This can improve
        the performance on architectures with coloured zero pages,
        depending on the workload. Care should be taken when enabling
        this setting, as it can potentially degrade the performance of
        KSM for some workloads, for example if the checksums of pages
        candidate for merging match the checksum of an empty
        page. This setting can be changed at any time, it is only
130 131 132
        effective for pages merged after the change.

        Default: 0 (normal KSM behaviour as in earlier releases)
133 134 135 136 137 138 139 140

max_page_sharing
        Maximum sharing allowed for each KSM page. This enforces a
        deduplication limit to avoid the virtual memory rmap lists to
        grow too large. The minimum value is 2 as a newly created KSM
        page will have at least two sharers. The rmap walk has O(N)
        complexity where N is the number of rmap_items (i.e. virtual
        mappings) that are sharing the page, which is in turn capped
141
        by ``max_page_sharing``. So this effectively spreads the linear
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        O(N) computational complexity from rmap walk context over
        different KSM pages. The ksmd walk over the stable_node
        "chains" is also O(N), but N is the number of stable_node
        "dups", not the number of rmap_items, so it has not a
        significant impact on ksmd performance. In practice the best
        stable_node "dup" candidate will be kept and found at the head
        of the "dups" list. The higher this value the faster KSM will
        merge the memory (because there will be fewer stable_node dups
        queued into the stable_node chain->hlist to check for pruning)
        and the higher the deduplication factor will be, but the
        slowest the worst case rmap walk could be for any given KSM
        page. Slowing down the rmap_walk means there will be higher
        latency for certain virtual memory operations happening during
        swapping, compaction, NUMA balancing and page migration, in
        turn decreasing responsiveness for the caller of those virtual
        memory operations. The scheduler latency of other tasks not
        involved with the VM operations doing the rmap walk is not
        affected by this parameter as the rmap walks are always
        schedule friendly themselves.

stable_node_chains_prune_millisecs
        How frequently to walk the whole list of stable_node "dups"
        linked in the stable_node "chains" in order to prune stale
        stable_nodes. Smaller milllisecs values will free up the KSM
        metadata with lower latency, but they will make ksmd use more
        CPU during the scan. This only applies to the stable_node
        chains so it's a noop if not a single KSM page hit the
169
        ``max_page_sharing`` yet (there would be no stable_node chains in
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        such case).

The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``:

pages_shared
        how many shared pages are being used
pages_sharing
        how many more sites are sharing them i.e. how much saved
pages_unshared
        how many pages unique but repeatedly checked for merging
pages_volatile
        how many pages changing too fast to be placed in a tree
full_scans
        how many times all mergeable areas have been scanned
stable_node_chains
        number of stable node chains allocated, this is effectively
186
        the number of KSM pages that hit the ``max_page_sharing`` limit
187 188
stable_node_dups
        number of stable node dups queued into the stable_node chains
189

190 191 192 193 194
A high ratio of ``pages_sharing`` to ``pages_shared`` indicates good
sharing, but a high ratio of ``pages_unshared`` to ``pages_sharing``
indicates wasted effort.  ``pages_volatile`` embraces several
different kinds of activity, but a high proportion there would also
indicate poor use of madvise MADV_MERGEABLE.
H
Hugh Dickins 已提交
195

196 197
The maximum possible ``pages_sharing/pages_shared`` ratio is limited by the
``max_page_sharing`` tunable. To increase the ratio ``max_page_sharing`` must
198 199
be increased accordingly.

200 201
The ``stable_node_dups/stable_node_chains`` ratio is also affected by the
``max_page_sharing`` tunable, and an high ratio may indicate fragmentation
202 203
in the stable_node dups, which could be solved by introducing
fragmentation algorithms in ksmd which would refile rmap_items from
204
one stable_node dup to another stable_node dup, in order to free up
205 206 207 208
stable_node "dups" with few rmap_items in them, but that may increase
the ksmd CPU usage and possibly slowdown the readonly computations on
the KSM pages of the applications.

209
--
H
Hugh Dickins 已提交
210
Izik Eidus,
211
Hugh Dickins, 17 Nov 2009