memcg: remove mem_cgroup_cal_reclaim()
[deliverable/linux.git] / Documentation / controllers / memory.txt
CommitLineData
00f0b825
BS
1Memory Resource Controller
2
3NOTE: The Memory Resource Controller has been generically been referred
4to as the memory controller in this document. Do not confuse memory controller
5used here with the memory controller that is used in hardware.
1b6df3aa
BS
6
7Salient features
8
9a. Enable control of both RSS (mapped) and Page Cache (unmapped) pages
10b. The infrastructure allows easy addition of other types of memory to control
11c. Provides *zero overhead* for non memory controller users
12d. Provides a double LRU: global memory pressure causes reclaim from the
13 global LRU; a cgroup on hitting a limit, reclaims from the per
14 cgroup LRU
15
dfc05c25 16NOTE: Swap Cache (unmapped) is not accounted now.
1b6df3aa
BS
17
18Benefits and Purpose of the memory controller
19
20The memory controller isolates the memory behaviour of a group of tasks
21from the rest of the system. The article on LWN [12] mentions some probable
22uses of the memory controller. The memory controller can be used to
23
24a. Isolate an application or a group of applications
25 Memory hungry applications can be isolated and limited to a smaller
26 amount of memory.
27b. Create a cgroup with limited amount of memory, this can be used
28 as a good alternative to booting with mem=XXXX.
29c. Virtualization solutions can control the amount of memory they want
30 to assign to a virtual machine instance.
31d. A CD/DVD burner could control the amount of memory used by the
32 rest of the system to ensure that burning does not fail due to lack
33 of available memory.
34e. There are several other use cases, find one or use the controller just
35 for fun (to learn and hack on the VM subsystem).
36
371. History
38
39The memory controller has a long history. A request for comments for the memory
40controller was posted by Balbir Singh [1]. At the time the RFC was posted
41there were several implementations for memory control. The goal of the
42RFC was to build consensus and agreement for the minimal features required
43for memory control. The first RSS controller was posted by Balbir Singh[2]
44in Feb 2007. Pavel Emelianov [3][4][5] has since posted three versions of the
45RSS controller. At OLS, at the resource management BoF, everyone suggested
46that we handle both page cache and RSS together. Another request was raised
47to allow user space handling of OOM. The current memory controller is
48at version 6; it combines both mapped (RSS) and unmapped Page
49Cache Control [11].
50
512. Memory Control
52
53Memory is a unique resource in the sense that it is present in a limited
54amount. If a task requires a lot of CPU processing, the task can spread
55its processing over a period of hours, days, months or years, but with
56memory, the same physical memory needs to be reused to accomplish the task.
57
58The memory controller implementation has been divided into phases. These
59are:
60
611. Memory controller
622. mlock(2) controller
633. Kernel user memory accounting and slab control
644. user mappings length controller
65
66The memory controller is the first controller developed.
67
682.1. Design
69
70The core of the design is a counter called the res_counter. The res_counter
71tracks the current memory usage and limit of the group of processes associated
72with the controller. Each cgroup has a memory controller specific data
73structure (mem_cgroup) associated with it.
74
752.2. Accounting
76
77 +--------------------+
78 | mem_cgroup |
79 | (res_counter) |
80 +--------------------+
81 / ^ \
82 / | \
83 +---------------+ | +---------------+
84 | mm_struct | |.... | mm_struct |
85 | | | | |
86 +---------------+ | +---------------+
87 |
88 + --------------+
89 |
90 +---------------+ +------+--------+
91 | page +----------> page_cgroup|
92 | | | |
93 +---------------+ +---------------+
94
95 (Figure 1: Hierarchy of Accounting)
96
97
98Figure 1 shows the important aspects of the controller
99
1001. Accounting happens per cgroup
1012. Each mm_struct knows about which cgroup it belongs to
1023. Each page has a pointer to the page_cgroup, which in turn knows the
103 cgroup it belongs to
104
105The accounting is done as follows: mem_cgroup_charge() is invoked to setup
106the necessary data structures and check if the cgroup that is being charged
107is over its limit. If it is then reclaim is invoked on the cgroup.
108More details can be found in the reclaim section of this document.
109If everything goes well, a page meta-data-structure called page_cgroup is
110allocated and associated with the page. This routine also adds the page to
111the per cgroup LRU.
112
1132.2.1 Accounting details
114
5b4e655e
KH
115All mapped anon pages (RSS) and cache pages (Page Cache) are accounted.
116(some pages which never be reclaimable and will not be on global LRU
117 are not accounted. we just accounts pages under usual vm management.)
118
119RSS pages are accounted at page_fault unless they've already been accounted
120for earlier. A file page will be accounted for as Page Cache when it's
121inserted into inode (radix-tree). While it's mapped into the page tables of
122processes, duplicate accounting is carefully avoided.
123
124A RSS page is unaccounted when it's fully unmapped. A PageCache page is
125unaccounted when it's removed from radix-tree.
126
127At page migration, accounting information is kept.
128
129Note: we just account pages-on-lru because our purpose is to control amount
130of used pages. not-on-lru pages are tend to be out-of-control from vm view.
1b6df3aa
BS
131
1322.3 Shared Page Accounting
133
134Shared pages are accounted on the basis of the first touch approach. The
135cgroup that first touches a page is accounted for the page. The principle
136behind this approach is that a cgroup that aggressively uses a shared
137page will eventually get charged for it (once it is uncharged from
138the cgroup that brought it in -- this will happen on memory pressure).
139
8c7c6e34
KH
140Exception: If CONFIG_CGROUP_CGROUP_MEM_RES_CTLR_SWAP is not used..
141When you do swapoff and make swapped-out pages of shmem(tmpfs) to
d13d1443
KH
142be backed into memory in force, charges for pages are accounted against the
143caller of swapoff rather than the users of shmem.
144
145
8c7c6e34
KH
1462.4 Swap Extension (CONFIG_CGROUP_MEM_RES_CTLR_SWAP)
147Swap Extension allows you to record charge for swap. A swapped-in page is
148charged back to original page allocator if possible.
149
150When swap is accounted, following files are added.
151 - memory.memsw.usage_in_bytes.
152 - memory.memsw.limit_in_bytes.
153
154usage of mem+swap is limited by memsw.limit_in_bytes.
155
156Note: why 'mem+swap' rather than swap.
157The global LRU(kswapd) can swap out arbitrary pages. Swap-out means
158to move account from memory to swap...there is no change in usage of
159mem+swap.
160
161In other words, when we want to limit the usage of swap without affecting
162global LRU, mem+swap limit is better than just limiting swap from OS point
163of view.
164
1652.5 Reclaim
1b6df3aa
BS
166
167Each cgroup maintains a per cgroup LRU that consists of an active
168and inactive list. When a cgroup goes over its limit, we first try
169to reclaim memory from the cgroup so as to make space for the new
170pages that the cgroup has touched. If the reclaim is unsuccessful,
171an OOM routine is invoked to select and kill the bulkiest task in the
172cgroup.
173
174The reclaim algorithm has not been modified for cgroups, except that
175pages that are selected for reclaiming come from the per cgroup LRU
176list.
177
1782. Locking
179
180The memory controller uses the following hierarchy
181
1821. zone->lru_lock is used for selecting pages to be isolated
dfc05c25 1832. mem->per_zone->lru_lock protects the per cgroup LRU (per zone)
1b6df3aa
BS
1843. lock_page_cgroup() is used to protect page->page_cgroup
185
1863. User Interface
187
1880. Configuration
189
190a. Enable CONFIG_CGROUPS
191b. Enable CONFIG_RESOURCE_COUNTERS
00f0b825 192c. Enable CONFIG_CGROUP_MEM_RES_CTLR
1b6df3aa
BS
193
1941. Prepare the cgroups
195# mkdir -p /cgroups
196# mount -t cgroup none /cgroups -o memory
197
1982. Make the new group and move bash into it
199# mkdir /cgroups/0
200# echo $$ > /cgroups/0/tasks
201
202Since now we're in the 0 cgroup,
203We can alter the memory limit:
fb78922c 204# echo 4M > /cgroups/0/memory.limit_in_bytes
0eea1030
BS
205
206NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
207mega or gigabytes.
208
209# cat /cgroups/0/memory.limit_in_bytes
2324c5dd 2104194304
0eea1030
BS
211
212NOTE: The interface has now changed to display the usage in bytes
213instead of pages
1b6df3aa
BS
214
215We can check the usage:
0eea1030 216# cat /cgroups/0/memory.usage_in_bytes
2324c5dd 2171216512
0eea1030
BS
218
219A successful write to this file does not guarantee a successful set of
220this limit to the value written into the file. This can be due to a
221number of factors, such as rounding up to page boundaries or the total
222availability of memory on the system. The user is required to re-read
223this file after a write to guarantee the value committed by the kernel.
224
fb78922c 225# echo 1 > memory.limit_in_bytes
0eea1030 226# cat memory.limit_in_bytes
2324c5dd 2274096
1b6df3aa
BS
228
229The memory.failcnt field gives the number of times that the cgroup limit was
230exceeded.
231
dfc05c25
KH
232The memory.stat file gives accounting information. Now, the number of
233caches, RSS and Active pages/Inactive pages are shown.
234
1b6df3aa
BS
2354. Testing
236
237Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
238Apart from that v6 has been tested with several applications and regular
239daily use. The controller has also been tested on the PPC64, x86_64 and
240UML platforms.
241
2424.1 Troubleshooting
243
244Sometimes a user might find that the application under a cgroup is
245terminated. There are several causes for this:
246
2471. The cgroup limit is too low (just too low to do anything useful)
2482. The user is using anonymous memory and swap is turned off or too low
249
250A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
251some of the pages cached in the cgroup (page cache pages).
252
2534.2 Task migration
254
255When a task migrates from one cgroup to another, it's charge is not
256carried forward. The pages allocated from the original cgroup still
257remain charged to it, the charge is dropped when the page is freed or
258reclaimed.
259
2604.3 Removing a cgroup
261
262A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
263cgroup might have some charge associated with it, even though all
f817ed48 264tasks have migrated away from it.
c1e862c1
KH
265Such charges are freed(at default) or moved to its parent. When moved,
266both of RSS and CACHES are moved to parent.
267If both of them are busy, rmdir() returns -EBUSY. See 5.1 Also.
1b6df3aa 268
8c7c6e34
KH
269Charges recorded in swap information is not updated at removal of cgroup.
270Recorded information is discarded and a cgroup which uses swap (swapcache)
271will be charged as a new owner of it.
272
273
c1e862c1
KH
2745. Misc. interfaces.
275
2765.1 force_empty
277 memory.force_empty interface is provided to make cgroup's memory usage empty.
278 You can use this interface only when the cgroup has no tasks.
279 When writing anything to this
280
281 # echo 0 > memory.force_empty
282
283 Almost all pages tracked by this memcg will be unmapped and freed. Some of
284 pages cannot be freed because it's locked or in-use. Such pages are moved
285 to parent and this cgroup will be empty. But this may return -EBUSY in
286 some too busy case.
287
288 Typical use case of this interface is that calling this before rmdir().
289 Because rmdir() moves all pages to parent, some out-of-use page caches can be
290 moved to the parent. If you want to avoid that, force_empty will be useful.
291
52bc0d82 2926. Hierarchy support
c1e862c1 293
52bc0d82
BS
294The memory controller supports a deep hierarchy and hierarchical accounting.
295The hierarchy is created by creating the appropriate cgroups in the
296cgroup filesystem. Consider for example, the following cgroup filesystem
297hierarchy
298
299 root
300 / | \
301 / | \
302 a b c
303 | \
304 | \
305 d e
306
307In the diagram above, with hierarchical accounting enabled, all memory
308usage of e, is accounted to its ancestors up until the root (i.e, c and root),
309that has memory.use_hierarchy enabled. If one of the ancestors goes over its
310limit, the reclaim algorithm reclaims from the tasks in the ancestor and the
311children of the ancestor.
312
3136.1 Enabling hierarchical accounting and reclaim
314
315The memory controller by default disables the hierarchy feature. Support
316can be enabled by writing 1 to memory.use_hierarchy file of the root cgroup
317
318# echo 1 > memory.use_hierarchy
319
320The feature can be disabled by
321
322# echo 0 > memory.use_hierarchy
323
324NOTE1: Enabling/disabling will fail if the cgroup already has other
325cgroups created below it.
326
327NOTE2: This feature can be enabled/disabled per subtree.
328
3297. TODO
1b6df3aa
BS
330
3311. Add support for accounting huge pages (as a separate controller)
dfc05c25
KH
3322. Make per-cgroup scanner reclaim not-shared pages first
3333. Teach controller to account for shared-pages
628f4235 3344. Start reclamation in the background when the limit is
1b6df3aa 335 not yet hit but the usage is getting closer
1b6df3aa
BS
336
337Summary
338
339Overall, the memory controller has been a stable controller and has been
340commented and discussed quite extensively in the community.
341
342References
343
3441. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
3452. Singh, Balbir. Memory Controller (RSS Control),
346 http://lwn.net/Articles/222762/
3473. Emelianov, Pavel. Resource controllers based on process cgroups
348 http://lkml.org/lkml/2007/3/6/198
3494. Emelianov, Pavel. RSS controller based on process cgroups (v2)
2324c5dd 350 http://lkml.org/lkml/2007/4/9/78
1b6df3aa
BS
3515. Emelianov, Pavel. RSS controller based on process cgroups (v3)
352 http://lkml.org/lkml/2007/5/30/244
3536. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
3547. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
355 subsystem (v3), http://lwn.net/Articles/235534/
2324c5dd 3568. Singh, Balbir. RSS controller v2 test results (lmbench),
1b6df3aa 357 http://lkml.org/lkml/2007/5/17/232
2324c5dd 3589. Singh, Balbir. RSS controller v2 AIM9 results
1b6df3aa 359 http://lkml.org/lkml/2007/5/18/1
2324c5dd 36010. Singh, Balbir. Memory controller v6 test results,
1b6df3aa 361 http://lkml.org/lkml/2007/8/19/36
2324c5dd
LZ
36211. Singh, Balbir. Memory controller introduction (v6),
363 http://lkml.org/lkml/2007/8/17/69
1b6df3aa
BS
36412. Corbet, Jonathan, Controlling memory use in cgroups,
365 http://lwn.net/Articles/243795/
This page took 0.119237 seconds and 5 git commands to generate.