blob: b5bbea92a61ad1c0c8ef2d6677cf8e91f7bbc9c3 [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
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -080012NOTE: Swap Cache (unmapped) is not accounted now.
Balbir Singh1b6df3a2008-02-07 00:13:46 -080013
14Benefits and Purpose of the memory controller
15
16The memory controller isolates the memory behaviour of a group of tasks
17from the rest of the system. The article on LWN [12] mentions some probable
18uses of the memory controller. The memory controller can be used to
19
20a. Isolate an application or a group of applications
21 Memory hungry applications can be isolated and limited to a smaller
22 amount of memory.
23b. Create a cgroup with limited amount of memory, this can be used
24 as a good alternative to booting with mem=XXXX.
25c. Virtualization solutions can control the amount of memory they want
26 to assign to a virtual machine instance.
27d. A CD/DVD burner could control the amount of memory used by the
28 rest of the system to ensure that burning does not fail due to lack
29 of available memory.
30e. There are several other use cases, find one or use the controller just
31 for fun (to learn and hack on the VM subsystem).
32
331. History
34
35The memory controller has a long history. A request for comments for the memory
36controller was posted by Balbir Singh [1]. At the time the RFC was posted
37there were several implementations for memory control. The goal of the
38RFC was to build consensus and agreement for the minimal features required
39for memory control. The first RSS controller was posted by Balbir Singh[2]
40in Feb 2007. Pavel Emelianov [3][4][5] has since posted three versions of the
41RSS controller. At OLS, at the resource management BoF, everyone suggested
42that we handle both page cache and RSS together. Another request was raised
43to allow user space handling of OOM. The current memory controller is
44at version 6; it combines both mapped (RSS) and unmapped Page
45Cache Control [11].
46
472. Memory Control
48
49Memory is a unique resource in the sense that it is present in a limited
50amount. If a task requires a lot of CPU processing, the task can spread
51its processing over a period of hours, days, months or years, but with
52memory, the same physical memory needs to be reused to accomplish the task.
53
54The memory controller implementation has been divided into phases. These
55are:
56
571. Memory controller
582. mlock(2) controller
593. Kernel user memory accounting and slab control
604. user mappings length controller
61
62The memory controller is the first controller developed.
63
642.1. Design
65
66The core of the design is a counter called the res_counter. The res_counter
67tracks the current memory usage and limit of the group of processes associated
68with the controller. Each cgroup has a memory controller specific data
69structure (mem_cgroup) associated with it.
70
712.2. Accounting
72
73 +--------------------+
74 | mem_cgroup |
75 | (res_counter) |
76 +--------------------+
77 / ^ \
78 / | \
79 +---------------+ | +---------------+
80 | mm_struct | |.... | mm_struct |
81 | | | | |
82 +---------------+ | +---------------+
83 |
84 + --------------+
85 |
86 +---------------+ +------+--------+
87 | page +----------> page_cgroup|
88 | | | |
89 +---------------+ +---------------+
90
91 (Figure 1: Hierarchy of Accounting)
92
93
94Figure 1 shows the important aspects of the controller
95
961. Accounting happens per cgroup
972. Each mm_struct knows about which cgroup it belongs to
983. Each page has a pointer to the page_cgroup, which in turn knows the
99 cgroup it belongs to
100
101The accounting is done as follows: mem_cgroup_charge() is invoked to setup
102the necessary data structures and check if the cgroup that is being charged
103is over its limit. If it is then reclaim is invoked on the cgroup.
104More details can be found in the reclaim section of this document.
105If everything goes well, a page meta-data-structure called page_cgroup is
106allocated and associated with the page. This routine also adds the page to
107the per cgroup LRU.
108
1092.2.1 Accounting details
110
111All mapped pages (RSS) and unmapped user pages (Page Cache) are accounted.
112RSS pages are accounted at the time of page_add_*_rmap() unless they've already
113been accounted for earlier. A file page will be accounted for as Page Cache;
114it's mapped into the page tables of a process, duplicate accounting is carefully
115avoided. Page Cache pages are accounted at the time of add_to_page_cache().
116The corresponding routines that remove a page from the page tables or removes
117a page from Page Cache is used to decrement the accounting counters of the
118cgroup.
119
1202.3 Shared Page Accounting
121
122Shared pages are accounted on the basis of the first touch approach. The
123cgroup that first touches a page is accounted for the page. The principle
124behind this approach is that a cgroup that aggressively uses a shared
125page will eventually get charged for it (once it is uncharged from
126the cgroup that brought it in -- this will happen on memory pressure).
127
1282.4 Reclaim
129
130Each cgroup maintains a per cgroup LRU that consists of an active
131and inactive list. When a cgroup goes over its limit, we first try
132to reclaim memory from the cgroup so as to make space for the new
133pages that the cgroup has touched. If the reclaim is unsuccessful,
134an OOM routine is invoked to select and kill the bulkiest task in the
135cgroup.
136
137The reclaim algorithm has not been modified for cgroups, except that
138pages that are selected for reclaiming come from the per cgroup LRU
139list.
140
1412. Locking
142
143The memory controller uses the following hierarchy
144
1451. zone->lru_lock is used for selecting pages to be isolated
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -08001462. mem->per_zone->lru_lock protects the per cgroup LRU (per zone)
Balbir Singh1b6df3a2008-02-07 00:13:46 -08001473. lock_page_cgroup() is used to protect page->page_cgroup
148
1493. User Interface
150
1510. Configuration
152
153a. Enable CONFIG_CGROUPS
154b. Enable CONFIG_RESOURCE_COUNTERS
155c. Enable CONFIG_CGROUP_MEM_CONT
156
1571. Prepare the cgroups
158# mkdir -p /cgroups
159# mount -t cgroup none /cgroups -o memory
160
1612. Make the new group and move bash into it
162# mkdir /cgroups/0
163# echo $$ > /cgroups/0/tasks
164
165Since now we're in the 0 cgroup,
166We can alter the memory limit:
Balbir Singh0eea1032008-02-07 00:13:57 -0800167# echo -n 4M > /cgroups/0/memory.limit_in_bytes
168
169NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
170mega or gigabytes.
171
172# cat /cgroups/0/memory.limit_in_bytes
1734194304 Bytes
174
175NOTE: The interface has now changed to display the usage in bytes
176instead of pages
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800177
178We can check the usage:
Balbir Singh0eea1032008-02-07 00:13:57 -0800179# cat /cgroups/0/memory.usage_in_bytes
1801216512 Bytes
181
182A successful write to this file does not guarantee a successful set of
183this limit to the value written into the file. This can be due to a
184number of factors, such as rounding up to page boundaries or the total
185availability of memory on the system. The user is required to re-read
186this file after a write to guarantee the value committed by the kernel.
187
188# echo -n 1 > memory.limit_in_bytes
189# cat memory.limit_in_bytes
1904096 Bytes
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800191
192The memory.failcnt field gives the number of times that the cgroup limit was
193exceeded.
194
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -0800195The memory.stat file gives accounting information. Now, the number of
196caches, RSS and Active pages/Inactive pages are shown.
197
198The memory.force_empty gives an interface to drop *all* charges by force.
199
200# echo -n 1 > memory.force_empty
201
202will drop all charges in cgroup. Currently, this is maintained for test.
203
Balbir Singh1b6df3a2008-02-07 00:13:46 -08002044. Testing
205
206Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
207Apart from that v6 has been tested with several applications and regular
208daily use. The controller has also been tested on the PPC64, x86_64 and
209UML platforms.
210
2114.1 Troubleshooting
212
213Sometimes a user might find that the application under a cgroup is
214terminated. There are several causes for this:
215
2161. The cgroup limit is too low (just too low to do anything useful)
2172. The user is using anonymous memory and swap is turned off or too low
218
219A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
220some of the pages cached in the cgroup (page cache pages).
221
2224.2 Task migration
223
224When a task migrates from one cgroup to another, it's charge is not
225carried forward. The pages allocated from the original cgroup still
226remain charged to it, the charge is dropped when the page is freed or
227reclaimed.
228
2294.3 Removing a cgroup
230
231A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
232cgroup might have some charge associated with it, even though all
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -0800233tasks have migrated away from it. Such charges are automatically dropped at
234rmdir() if there are no tasks.
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800235
2364.4 Choosing what to account -- Page Cache (unmapped) vs RSS (mapped)?
237
238The type of memory accounted by the cgroup can be limited to just
239mapped pages by writing "1" to memory.control_type field
240
241echo -n 1 > memory.control_type
242
2435. TODO
244
2451. Add support for accounting huge pages (as a separate controller)
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -08002462. Make per-cgroup scanner reclaim not-shared pages first
2473. Teach controller to account for shared-pages
2484. Start reclamation when the limit is lowered
2495. Start reclamation in the background when the limit is
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800250 not yet hit but the usage is getting closer
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800251
252Summary
253
254Overall, the memory controller has been a stable controller and has been
255commented and discussed quite extensively in the community.
256
257References
258
2591. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
2602. Singh, Balbir. Memory Controller (RSS Control),
261 http://lwn.net/Articles/222762/
2623. Emelianov, Pavel. Resource controllers based on process cgroups
263 http://lkml.org/lkml/2007/3/6/198
2644. Emelianov, Pavel. RSS controller based on process cgroups (v2)
265 http://lkml.org/lkml/2007/4/9/74
2665. Emelianov, Pavel. RSS controller based on process cgroups (v3)
267 http://lkml.org/lkml/2007/5/30/244
2686. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
2697. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
270 subsystem (v3), http://lwn.net/Articles/235534/
2718. Singh, Balbir. RSS controller V2 test results (lmbench),
272 http://lkml.org/lkml/2007/5/17/232
2739. Singh, Balbir. RSS controller V2 AIM9 results
274 http://lkml.org/lkml/2007/5/18/1
27510. Singh, Balbir. Memory controller v6 results,
276 http://lkml.org/lkml/2007/8/19/36
27711. Singh, Balbir. Memory controller v6, http://lkml.org/lkml/2007/8/17/69
27812. Corbet, Jonathan, Controlling memory use in cgroups,
279 http://lwn.net/Articles/243795/