blob: 61df8f81c80369138aff22b88e600c6a6c6b87b6 [file] [log] [blame]
Balbir Singh1b6df3a2008-02-07 00:13:46 -08001Memory Controller
2
3Salient features
4
5a. Enable control of both RSS (mapped) and Page Cache (unmapped) pages
6b. The infrastructure allows easy addition of other types of memory to control
7c. Provides *zero overhead* for non memory controller users
8d. Provides a double LRU: global memory pressure causes reclaim from the
9 global LRU; a cgroup on hitting a limit, reclaims from the per
10 cgroup LRU
11
12NOTE: Page Cache (unmapped) also includes Swap Cache pages as a subset
13and will not be referred to explicitly in the rest of the documentation.
14
15Benefits and Purpose of the memory controller
16
17The memory controller isolates the memory behaviour of a group of tasks
18from the rest of the system. The article on LWN [12] mentions some probable
19uses of the memory controller. The memory controller can be used to
20
21a. Isolate an application or a group of applications
22 Memory hungry applications can be isolated and limited to a smaller
23 amount of memory.
24b. Create a cgroup with limited amount of memory, this can be used
25 as a good alternative to booting with mem=XXXX.
26c. Virtualization solutions can control the amount of memory they want
27 to assign to a virtual machine instance.
28d. A CD/DVD burner could control the amount of memory used by the
29 rest of the system to ensure that burning does not fail due to lack
30 of available memory.
31e. There are several other use cases, find one or use the controller just
32 for fun (to learn and hack on the VM subsystem).
33
341. History
35
36The memory controller has a long history. A request for comments for the memory
37controller was posted by Balbir Singh [1]. At the time the RFC was posted
38there were several implementations for memory control. The goal of the
39RFC was to build consensus and agreement for the minimal features required
40for memory control. The first RSS controller was posted by Balbir Singh[2]
41in Feb 2007. Pavel Emelianov [3][4][5] has since posted three versions of the
42RSS controller. At OLS, at the resource management BoF, everyone suggested
43that we handle both page cache and RSS together. Another request was raised
44to allow user space handling of OOM. The current memory controller is
45at version 6; it combines both mapped (RSS) and unmapped Page
46Cache Control [11].
47
482. Memory Control
49
50Memory is a unique resource in the sense that it is present in a limited
51amount. If a task requires a lot of CPU processing, the task can spread
52its processing over a period of hours, days, months or years, but with
53memory, the same physical memory needs to be reused to accomplish the task.
54
55The memory controller implementation has been divided into phases. These
56are:
57
581. Memory controller
592. mlock(2) controller
603. Kernel user memory accounting and slab control
614. user mappings length controller
62
63The memory controller is the first controller developed.
64
652.1. Design
66
67The core of the design is a counter called the res_counter. The res_counter
68tracks the current memory usage and limit of the group of processes associated
69with the controller. Each cgroup has a memory controller specific data
70structure (mem_cgroup) associated with it.
71
722.2. Accounting
73
74 +--------------------+
75 | mem_cgroup |
76 | (res_counter) |
77 +--------------------+
78 / ^ \
79 / | \
80 +---------------+ | +---------------+
81 | mm_struct | |.... | mm_struct |
82 | | | | |
83 +---------------+ | +---------------+
84 |
85 + --------------+
86 |
87 +---------------+ +------+--------+
88 | page +----------> page_cgroup|
89 | | | |
90 +---------------+ +---------------+
91
92 (Figure 1: Hierarchy of Accounting)
93
94
95Figure 1 shows the important aspects of the controller
96
971. Accounting happens per cgroup
982. Each mm_struct knows about which cgroup it belongs to
993. Each page has a pointer to the page_cgroup, which in turn knows the
100 cgroup it belongs to
101
102The accounting is done as follows: mem_cgroup_charge() is invoked to setup
103the necessary data structures and check if the cgroup that is being charged
104is over its limit. If it is then reclaim is invoked on the cgroup.
105More details can be found in the reclaim section of this document.
106If everything goes well, a page meta-data-structure called page_cgroup is
107allocated and associated with the page. This routine also adds the page to
108the per cgroup LRU.
109
1102.2.1 Accounting details
111
112All mapped pages (RSS) and unmapped user pages (Page Cache) are accounted.
113RSS pages are accounted at the time of page_add_*_rmap() unless they've already
114been accounted for earlier. A file page will be accounted for as Page Cache;
115it's mapped into the page tables of a process, duplicate accounting is carefully
116avoided. Page Cache pages are accounted at the time of add_to_page_cache().
117The corresponding routines that remove a page from the page tables or removes
118a page from Page Cache is used to decrement the accounting counters of the
119cgroup.
120
1212.3 Shared Page Accounting
122
123Shared pages are accounted on the basis of the first touch approach. The
124cgroup that first touches a page is accounted for the page. The principle
125behind this approach is that a cgroup that aggressively uses a shared
126page will eventually get charged for it (once it is uncharged from
127the cgroup that brought it in -- this will happen on memory pressure).
128
1292.4 Reclaim
130
131Each cgroup maintains a per cgroup LRU that consists of an active
132and inactive list. When a cgroup goes over its limit, we first try
133to reclaim memory from the cgroup so as to make space for the new
134pages that the cgroup has touched. If the reclaim is unsuccessful,
135an OOM routine is invoked to select and kill the bulkiest task in the
136cgroup.
137
138The reclaim algorithm has not been modified for cgroups, except that
139pages that are selected for reclaiming come from the per cgroup LRU
140list.
141
1422. Locking
143
144The memory controller uses the following hierarchy
145
1461. zone->lru_lock is used for selecting pages to be isolated
1472. mem->lru_lock protects the per cgroup LRU
1483. lock_page_cgroup() is used to protect page->page_cgroup
149
1503. User Interface
151
1520. Configuration
153
154a. Enable CONFIG_CGROUPS
155b. Enable CONFIG_RESOURCE_COUNTERS
156c. Enable CONFIG_CGROUP_MEM_CONT
157
1581. Prepare the cgroups
159# mkdir -p /cgroups
160# mount -t cgroup none /cgroups -o memory
161
1622. Make the new group and move bash into it
163# mkdir /cgroups/0
164# echo $$ > /cgroups/0/tasks
165
166Since now we're in the 0 cgroup,
167We can alter the memory limit:
Balbir Singh0eea1032008-02-07 00:13:57 -0800168# echo -n 4M > /cgroups/0/memory.limit_in_bytes
169
170NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
171mega or gigabytes.
172
173# cat /cgroups/0/memory.limit_in_bytes
1744194304 Bytes
175
176NOTE: The interface has now changed to display the usage in bytes
177instead of pages
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800178
179We can check the usage:
Balbir Singh0eea1032008-02-07 00:13:57 -0800180# cat /cgroups/0/memory.usage_in_bytes
1811216512 Bytes
182
183A successful write to this file does not guarantee a successful set of
184this limit to the value written into the file. This can be due to a
185number of factors, such as rounding up to page boundaries or the total
186availability of memory on the system. The user is required to re-read
187this file after a write to guarantee the value committed by the kernel.
188
189# echo -n 1 > memory.limit_in_bytes
190# cat memory.limit_in_bytes
1914096 Bytes
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800192
193The memory.failcnt field gives the number of times that the cgroup limit was
194exceeded.
195
1964. Testing
197
198Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
199Apart from that v6 has been tested with several applications and regular
200daily use. The controller has also been tested on the PPC64, x86_64 and
201UML platforms.
202
2034.1 Troubleshooting
204
205Sometimes a user might find that the application under a cgroup is
206terminated. There are several causes for this:
207
2081. The cgroup limit is too low (just too low to do anything useful)
2092. The user is using anonymous memory and swap is turned off or too low
210
211A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
212some of the pages cached in the cgroup (page cache pages).
213
2144.2 Task migration
215
216When a task migrates from one cgroup to another, it's charge is not
217carried forward. The pages allocated from the original cgroup still
218remain charged to it, the charge is dropped when the page is freed or
219reclaimed.
220
2214.3 Removing a cgroup
222
223A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
224cgroup might have some charge associated with it, even though all
225tasks have migrated away from it. If some pages are still left, after following
226the steps listed in sections 4.1 and 4.2, check the Swap Cache usage in
227/proc/meminfo to see if the Swap Cache usage is showing up in the
Balbir Singh0eea1032008-02-07 00:13:57 -0800228cgroups memory.usage_in_bytes counter. A simple test of swapoff -a and
229swapon -a should free any pending Swap Cache usage.
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800230
2314.4 Choosing what to account -- Page Cache (unmapped) vs RSS (mapped)?
232
233The type of memory accounted by the cgroup can be limited to just
234mapped pages by writing "1" to memory.control_type field
235
236echo -n 1 > memory.control_type
237
2385. TODO
239
2401. Add support for accounting huge pages (as a separate controller)
2412. Improve the user interface to accept/display memory limits in KB or MB
242 rather than pages (since page sizes can differ across platforms/machines).
2433. Make cgroup lists per-zone
2444. Make per-cgroup scanner reclaim not-shared pages first
2455. Teach controller to account for shared-pages
2466. Start reclamation when the limit is lowered
2477. Start reclamation in the background when the limit is
248 not yet hit but the usage is getting closer
2498. Create per zone LRU lists per cgroup
250
251Summary
252
253Overall, the memory controller has been a stable controller and has been
254commented and discussed quite extensively in the community.
255
256References
257
2581. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
2592. Singh, Balbir. Memory Controller (RSS Control),
260 http://lwn.net/Articles/222762/
2613. Emelianov, Pavel. Resource controllers based on process cgroups
262 http://lkml.org/lkml/2007/3/6/198
2634. Emelianov, Pavel. RSS controller based on process cgroups (v2)
264 http://lkml.org/lkml/2007/4/9/74
2655. Emelianov, Pavel. RSS controller based on process cgroups (v3)
266 http://lkml.org/lkml/2007/5/30/244
2676. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
2687. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
269 subsystem (v3), http://lwn.net/Articles/235534/
2708. Singh, Balbir. RSS controller V2 test results (lmbench),
271 http://lkml.org/lkml/2007/5/17/232
2729. Singh, Balbir. RSS controller V2 AIM9 results
273 http://lkml.org/lkml/2007/5/18/1
27410. Singh, Balbir. Memory controller v6 results,
275 http://lkml.org/lkml/2007/8/19/36
27611. Singh, Balbir. Memory controller v6, http://lkml.org/lkml/2007/8/17/69
27712. Corbet, Jonathan, Controlling memory use in cgroups,
278 http://lwn.net/Articles/243795/