blob: 866b9cd9a9590d6b6b8c3d577038e8d51234082b [file] [log] [blame]
Balbir Singh00f0b822008-03-04 14:28:39 -08001Memory 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.
Balbir Singh1b6df3a2008-02-07 00:13:46 -08006
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
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -080016NOTE: Swap Cache (unmapped) is not accounted now.
Balbir Singh1b6df3a2008-02-07 00:13:46 -080017
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
115All mapped pages (RSS) and unmapped user pages (Page Cache) are accounted.
116RSS pages are accounted at the time of page_add_*_rmap() unless they've already
117been accounted for earlier. A file page will be accounted for as Page Cache;
118it's mapped into the page tables of a process, duplicate accounting is carefully
119avoided. Page Cache pages are accounted at the time of add_to_page_cache().
120The corresponding routines that remove a page from the page tables or removes
121a page from Page Cache is used to decrement the accounting counters of the
122cgroup.
123
1242.3 Shared Page Accounting
125
126Shared pages are accounted on the basis of the first touch approach. The
127cgroup that first touches a page is accounted for the page. The principle
128behind this approach is that a cgroup that aggressively uses a shared
129page will eventually get charged for it (once it is uncharged from
130the cgroup that brought it in -- this will happen on memory pressure).
131
1322.4 Reclaim
133
134Each cgroup maintains a per cgroup LRU that consists of an active
135and inactive list. When a cgroup goes over its limit, we first try
136to reclaim memory from the cgroup so as to make space for the new
137pages that the cgroup has touched. If the reclaim is unsuccessful,
138an OOM routine is invoked to select and kill the bulkiest task in the
139cgroup.
140
141The reclaim algorithm has not been modified for cgroups, except that
142pages that are selected for reclaiming come from the per cgroup LRU
143list.
144
1452. Locking
146
147The memory controller uses the following hierarchy
148
1491. zone->lru_lock is used for selecting pages to be isolated
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -08001502. mem->per_zone->lru_lock protects the per cgroup LRU (per zone)
Balbir Singh1b6df3a2008-02-07 00:13:46 -08001513. lock_page_cgroup() is used to protect page->page_cgroup
152
1533. User Interface
154
1550. Configuration
156
157a. Enable CONFIG_CGROUPS
158b. Enable CONFIG_RESOURCE_COUNTERS
Balbir Singh00f0b822008-03-04 14:28:39 -0800159c. Enable CONFIG_CGROUP_MEM_RES_CTLR
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800160
1611. Prepare the cgroups
162# mkdir -p /cgroups
163# mount -t cgroup none /cgroups -o memory
164
1652. Make the new group and move bash into it
166# mkdir /cgroups/0
167# echo $$ > /cgroups/0/tasks
168
169Since now we're in the 0 cgroup,
170We can alter the memory limit:
Balbir Singhfb789222008-03-04 14:28:24 -0800171# echo 4M > /cgroups/0/memory.limit_in_bytes
Balbir Singh0eea1032008-02-07 00:13:57 -0800172
173NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
174mega or gigabytes.
175
176# cat /cgroups/0/memory.limit_in_bytes
Li Zefan2324c5d2008-02-23 15:24:12 -08001774194304
Balbir Singh0eea1032008-02-07 00:13:57 -0800178
179NOTE: The interface has now changed to display the usage in bytes
180instead of pages
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800181
182We can check the usage:
Balbir Singh0eea1032008-02-07 00:13:57 -0800183# cat /cgroups/0/memory.usage_in_bytes
Li Zefan2324c5d2008-02-23 15:24:12 -08001841216512
Balbir Singh0eea1032008-02-07 00:13:57 -0800185
186A successful write to this file does not guarantee a successful set of
187this limit to the value written into the file. This can be due to a
188number of factors, such as rounding up to page boundaries or the total
189availability of memory on the system. The user is required to re-read
190this file after a write to guarantee the value committed by the kernel.
191
Balbir Singhfb789222008-03-04 14:28:24 -0800192# echo 1 > memory.limit_in_bytes
Balbir Singh0eea1032008-02-07 00:13:57 -0800193# cat memory.limit_in_bytes
Li Zefan2324c5d2008-02-23 15:24:12 -08001944096
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800195
196The memory.failcnt field gives the number of times that the cgroup limit was
197exceeded.
198
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -0800199The memory.stat file gives accounting information. Now, the number of
200caches, RSS and Active pages/Inactive pages are shown.
201
202The memory.force_empty gives an interface to drop *all* charges by force.
203
Balbir Singhfb789222008-03-04 14:28:24 -0800204# echo 1 > memory.force_empty
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -0800205
206will drop all charges in cgroup. Currently, this is maintained for test.
207
Balbir Singh1b6df3a2008-02-07 00:13:46 -08002084. Testing
209
210Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
211Apart from that v6 has been tested with several applications and regular
212daily use. The controller has also been tested on the PPC64, x86_64 and
213UML platforms.
214
2154.1 Troubleshooting
216
217Sometimes a user might find that the application under a cgroup is
218terminated. There are several causes for this:
219
2201. The cgroup limit is too low (just too low to do anything useful)
2212. The user is using anonymous memory and swap is turned off or too low
222
223A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
224some of the pages cached in the cgroup (page cache pages).
225
2264.2 Task migration
227
228When a task migrates from one cgroup to another, it's charge is not
229carried forward. The pages allocated from the original cgroup still
230remain charged to it, the charge is dropped when the page is freed or
231reclaimed.
232
2334.3 Removing a cgroup
234
235A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
236cgroup might have some charge associated with it, even though all
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -0800237tasks have migrated away from it. Such charges are automatically dropped at
238rmdir() if there are no tasks.
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800239
Balbir Singh1b6df3a2008-02-07 00:13:46 -08002405. TODO
241
2421. Add support for accounting huge pages (as a separate controller)
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -08002432. Make per-cgroup scanner reclaim not-shared pages first
2443. Teach controller to account for shared-pages
2454. Start reclamation when the limit is lowered
2465. Start reclamation in the background when the limit is
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800247 not yet hit but the usage is getting closer
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800248
249Summary
250
251Overall, the memory controller has been a stable controller and has been
252commented and discussed quite extensively in the community.
253
254References
255
2561. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
2572. Singh, Balbir. Memory Controller (RSS Control),
258 http://lwn.net/Articles/222762/
2593. Emelianov, Pavel. Resource controllers based on process cgroups
260 http://lkml.org/lkml/2007/3/6/198
2614. Emelianov, Pavel. RSS controller based on process cgroups (v2)
Li Zefan2324c5d2008-02-23 15:24:12 -0800262 http://lkml.org/lkml/2007/4/9/78
Balbir Singh1b6df3a2008-02-07 00:13:46 -08002635. Emelianov, Pavel. RSS controller based on process cgroups (v3)
264 http://lkml.org/lkml/2007/5/30/244
2656. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
2667. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
267 subsystem (v3), http://lwn.net/Articles/235534/
Li Zefan2324c5d2008-02-23 15:24:12 -08002688. Singh, Balbir. RSS controller v2 test results (lmbench),
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800269 http://lkml.org/lkml/2007/5/17/232
Li Zefan2324c5d2008-02-23 15:24:12 -08002709. Singh, Balbir. RSS controller v2 AIM9 results
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800271 http://lkml.org/lkml/2007/5/18/1
Li Zefan2324c5d2008-02-23 15:24:12 -080027210. Singh, Balbir. Memory controller v6 test results,
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800273 http://lkml.org/lkml/2007/8/19/36
Li Zefan2324c5d2008-02-23 15:24:12 -080027411. Singh, Balbir. Memory controller introduction (v6),
275 http://lkml.org/lkml/2007/8/17/69
Balbir Singh1b6df3a2008-02-07 00:13:46 -080027612. Corbet, Jonathan, Controlling memory use in cgroups,
277 http://lwn.net/Articles/243795/