blob: 23d1262c0775755efa8363d049d008e875776f89 [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
Bharata B Raoc863d832009-04-13 14:40:15 -07009a. Enable control of Anonymous, Page Cache (mapped and unmapped) and
10 Swap Cache memory pages.
Balbir Singh1b6df3a2008-02-07 00:13:46 -080011b. The infrastructure allows easy addition of other types of memory to control
12c. Provides *zero overhead* for non memory controller users
13d. Provides a double LRU: global memory pressure causes reclaim from the
14 global LRU; a cgroup on hitting a limit, reclaims from the per
15 cgroup LRU
16
Balbir Singh1b6df3a2008-02-07 00:13:46 -080017Benefits and Purpose of the memory controller
18
19The memory controller isolates the memory behaviour of a group of tasks
20from the rest of the system. The article on LWN [12] mentions some probable
21uses of the memory controller. The memory controller can be used to
22
23a. Isolate an application or a group of applications
24 Memory hungry applications can be isolated and limited to a smaller
25 amount of memory.
26b. Create a cgroup with limited amount of memory, this can be used
27 as a good alternative to booting with mem=XXXX.
28c. Virtualization solutions can control the amount of memory they want
29 to assign to a virtual machine instance.
30d. A CD/DVD burner could control the amount of memory used by the
31 rest of the system to ensure that burning does not fail due to lack
32 of available memory.
33e. There are several other use cases, find one or use the controller just
34 for fun (to learn and hack on the VM subsystem).
35
361. History
37
38The memory controller has a long history. A request for comments for the memory
39controller was posted by Balbir Singh [1]. At the time the RFC was posted
40there were several implementations for memory control. The goal of the
41RFC was to build consensus and agreement for the minimal features required
42for memory control. The first RSS controller was posted by Balbir Singh[2]
43in Feb 2007. Pavel Emelianov [3][4][5] has since posted three versions of the
44RSS controller. At OLS, at the resource management BoF, everyone suggested
45that we handle both page cache and RSS together. Another request was raised
46to allow user space handling of OOM. The current memory controller is
47at version 6; it combines both mapped (RSS) and unmapped Page
48Cache Control [11].
49
502. Memory Control
51
52Memory is a unique resource in the sense that it is present in a limited
53amount. If a task requires a lot of CPU processing, the task can spread
54its processing over a period of hours, days, months or years, but with
55memory, the same physical memory needs to be reused to accomplish the task.
56
57The memory controller implementation has been divided into phases. These
58are:
59
601. Memory controller
612. mlock(2) controller
623. Kernel user memory accounting and slab control
634. user mappings length controller
64
65The memory controller is the first controller developed.
66
672.1. Design
68
69The core of the design is a counter called the res_counter. The res_counter
70tracks the current memory usage and limit of the group of processes associated
71with the controller. Each cgroup has a memory controller specific data
72structure (mem_cgroup) associated with it.
73
742.2. Accounting
75
76 +--------------------+
77 | mem_cgroup |
78 | (res_counter) |
79 +--------------------+
80 / ^ \
81 / | \
82 +---------------+ | +---------------+
83 | mm_struct | |.... | mm_struct |
84 | | | | |
85 +---------------+ | +---------------+
86 |
87 + --------------+
88 |
89 +---------------+ +------+--------+
90 | page +----------> page_cgroup|
91 | | | |
92 +---------------+ +---------------+
93
94 (Figure 1: Hierarchy of Accounting)
95
96
97Figure 1 shows the important aspects of the controller
98
991. Accounting happens per cgroup
1002. Each mm_struct knows about which cgroup it belongs to
1013. Each page has a pointer to the page_cgroup, which in turn knows the
102 cgroup it belongs to
103
104The accounting is done as follows: mem_cgroup_charge() is invoked to setup
105the necessary data structures and check if the cgroup that is being charged
106is over its limit. If it is then reclaim is invoked on the cgroup.
107More details can be found in the reclaim section of this document.
108If everything goes well, a page meta-data-structure called page_cgroup is
109allocated and associated with the page. This routine also adds the page to
110the per cgroup LRU.
111
1122.2.1 Accounting details
113
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -0700114All mapped anon pages (RSS) and cache pages (Page Cache) are accounted.
115(some pages which never be reclaimable and will not be on global LRU
116 are not accounted. we just accounts pages under usual vm management.)
117
118RSS pages are accounted at page_fault unless they've already been accounted
119for earlier. A file page will be accounted for as Page Cache when it's
120inserted into inode (radix-tree). While it's mapped into the page tables of
121processes, duplicate accounting is carefully avoided.
122
123A RSS page is unaccounted when it's fully unmapped. A PageCache page is
124unaccounted when it's removed from radix-tree.
125
126At page migration, accounting information is kept.
127
128Note: we just account pages-on-lru because our purpose is to control amount
129of used pages. not-on-lru pages are tend to be out-of-control from vm view.
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800130
1312.3 Shared Page Accounting
132
133Shared pages are accounted on the basis of the first touch approach. The
134cgroup that first touches a page is accounted for the page. The principle
135behind this approach is that a cgroup that aggressively uses a shared
136page will eventually get charged for it (once it is uncharged from
137the cgroup that brought it in -- this will happen on memory pressure).
138
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800139Exception: If CONFIG_CGROUP_CGROUP_MEM_RES_CTLR_SWAP is not used..
140When you do swapoff and make swapped-out pages of shmem(tmpfs) to
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800141be backed into memory in force, charges for pages are accounted against the
142caller of swapoff rather than the users of shmem.
143
144
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001452.4 Swap Extension (CONFIG_CGROUP_MEM_RES_CTLR_SWAP)
146Swap Extension allows you to record charge for swap. A swapped-in page is
147charged back to original page allocator if possible.
148
149When swap is accounted, following files are added.
150 - memory.memsw.usage_in_bytes.
151 - memory.memsw.limit_in_bytes.
152
153usage of mem+swap is limited by memsw.limit_in_bytes.
154
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700155* why 'mem+swap' rather than swap.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800156The global LRU(kswapd) can swap out arbitrary pages. Swap-out means
157to move account from memory to swap...there is no change in usage of
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700158mem+swap. In other words, when we want to limit the usage of swap without
159affecting global LRU, mem+swap limit is better than just limiting swap from
160OS point of view.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800161
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700162* What happens when a cgroup hits memory.memsw.limit_in_bytes
163When a cgroup his memory.memsw.limit_in_bytes, it's useless to do swap-out
164in this cgroup. Then, swap-out will not be done by cgroup routine and file
165caches are dropped. But as mentioned above, global LRU can do swapout memory
166from it for sanity of the system's memory management state. You can't forbid
167it by cgroup.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800168
1692.5 Reclaim
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800170
171Each cgroup maintains a per cgroup LRU that consists of an active
172and inactive list. When a cgroup goes over its limit, we first try
173to reclaim memory from the cgroup so as to make space for the new
174pages that the cgroup has touched. If the reclaim is unsuccessful,
175an OOM routine is invoked to select and kill the bulkiest task in the
176cgroup.
177
178The reclaim algorithm has not been modified for cgroups, except that
179pages that are selected for reclaiming come from the per cgroup LRU
180list.
181
1822. Locking
183
184The memory controller uses the following hierarchy
185
1861. zone->lru_lock is used for selecting pages to be isolated
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -08001872. mem->per_zone->lru_lock protects the per cgroup LRU (per zone)
Balbir Singh1b6df3a2008-02-07 00:13:46 -08001883. lock_page_cgroup() is used to protect page->page_cgroup
189
1903. User Interface
191
1920. Configuration
193
194a. Enable CONFIG_CGROUPS
195b. Enable CONFIG_RESOURCE_COUNTERS
Balbir Singh00f0b822008-03-04 14:28:39 -0800196c. Enable CONFIG_CGROUP_MEM_RES_CTLR
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800197
1981. Prepare the cgroups
199# mkdir -p /cgroups
200# mount -t cgroup none /cgroups -o memory
201
2022. Make the new group and move bash into it
203# mkdir /cgroups/0
204# echo $$ > /cgroups/0/tasks
205
206Since now we're in the 0 cgroup,
207We can alter the memory limit:
Balbir Singhfb789222008-03-04 14:28:24 -0800208# echo 4M > /cgroups/0/memory.limit_in_bytes
Balbir Singh0eea1032008-02-07 00:13:57 -0800209
210NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
211mega or gigabytes.
Daisuke Nishimurac5b947b2009-06-17 16:27:20 -0700212NOTE: We can write "-1" to reset the *.limit_in_bytes(unlimited).
Balbir Singh0eea1032008-02-07 00:13:57 -0800213
214# cat /cgroups/0/memory.limit_in_bytes
Li Zefan2324c5d2008-02-23 15:24:12 -08002154194304
Balbir Singh0eea1032008-02-07 00:13:57 -0800216
217NOTE: The interface has now changed to display the usage in bytes
218instead of pages
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800219
220We can check the usage:
Balbir Singh0eea1032008-02-07 00:13:57 -0800221# cat /cgroups/0/memory.usage_in_bytes
Li Zefan2324c5d2008-02-23 15:24:12 -08002221216512
Balbir Singh0eea1032008-02-07 00:13:57 -0800223
224A successful write to this file does not guarantee a successful set of
225this limit to the value written into the file. This can be due to a
226number of factors, such as rounding up to page boundaries or the total
227availability of memory on the system. The user is required to re-read
228this file after a write to guarantee the value committed by the kernel.
229
Balbir Singhfb789222008-03-04 14:28:24 -0800230# echo 1 > memory.limit_in_bytes
Balbir Singh0eea1032008-02-07 00:13:57 -0800231# cat memory.limit_in_bytes
Li Zefan2324c5d2008-02-23 15:24:12 -08002324096
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800233
234The memory.failcnt field gives the number of times that the cgroup limit was
235exceeded.
236
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -0800237The memory.stat file gives accounting information. Now, the number of
238caches, RSS and Active pages/Inactive pages are shown.
239
Balbir Singh1b6df3a2008-02-07 00:13:46 -08002404. Testing
241
242Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
243Apart from that v6 has been tested with several applications and regular
244daily use. The controller has also been tested on the PPC64, x86_64 and
245UML platforms.
246
2474.1 Troubleshooting
248
249Sometimes a user might find that the application under a cgroup is
250terminated. There are several causes for this:
251
2521. The cgroup limit is too low (just too low to do anything useful)
2532. The user is using anonymous memory and swap is turned off or too low
254
255A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
256some of the pages cached in the cgroup (page cache pages).
257
2584.2 Task migration
259
260When a task migrates from one cgroup to another, it's charge is not
261carried forward. The pages allocated from the original cgroup still
262remain charged to it, the charge is dropped when the page is freed or
263reclaimed.
264
2654.3 Removing a cgroup
266
267A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
268cgroup might have some charge associated with it, even though all
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800269tasks have migrated away from it.
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -0800270Such charges are freed(at default) or moved to its parent. When moved,
271both of RSS and CACHES are moved to parent.
272If both of them are busy, rmdir() returns -EBUSY. See 5.1 Also.
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800273
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800274Charges recorded in swap information is not updated at removal of cgroup.
275Recorded information is discarded and a cgroup which uses swap (swapcache)
276will be charged as a new owner of it.
277
278
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002795. Misc. interfaces.
280
2815.1 force_empty
282 memory.force_empty interface is provided to make cgroup's memory usage empty.
283 You can use this interface only when the cgroup has no tasks.
284 When writing anything to this
285
286 # echo 0 > memory.force_empty
287
288 Almost all pages tracked by this memcg will be unmapped and freed. Some of
289 pages cannot be freed because it's locked or in-use. Such pages are moved
290 to parent and this cgroup will be empty. But this may return -EBUSY in
291 some too busy case.
292
293 Typical use case of this interface is that calling this before rmdir().
294 Because rmdir() moves all pages to parent, some out-of-use page caches can be
295 moved to the parent. If you want to avoid that, force_empty will be useful.
296
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002975.2 stat file
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -0800298
Bharata B Raoc863d832009-04-13 14:40:15 -0700299memory.stat file includes following statistics
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -0800300
Bharata B Raoc863d832009-04-13 14:40:15 -0700301cache - # of bytes of page cache memory.
302rss - # of bytes of anonymous and swap cache memory.
303pgpgin - # of pages paged in (equivalent to # of charging events).
304pgpgout - # of pages paged out (equivalent to # of uncharging events).
305active_anon - # of bytes of anonymous and swap cache memory on active
306 lru list.
307inactive_anon - # of bytes of anonymous memory and swap cache memory on
308 inactive lru list.
309active_file - # of bytes of file-backed memory on active lru list.
310inactive_file - # of bytes of file-backed memory on inactive lru list.
311unevictable - # of bytes of memory that cannot be reclaimed (mlocked etc).
312
313The following additional stats are dependent on CONFIG_DEBUG_VM.
314
315inactive_ratio - VM internal parameter. (see mm/page_alloc.c)
316recent_rotated_anon - VM internal parameter. (see mm/vmscan.c)
317recent_rotated_file - VM internal parameter. (see mm/vmscan.c)
318recent_scanned_anon - VM internal parameter. (see mm/vmscan.c)
319recent_scanned_file - VM internal parameter. (see mm/vmscan.c)
320
321Memo:
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -0800322 recent_rotated means recent frequency of lru rotation.
323 recent_scanned means recent # of scans to lru.
324 showing for better debug please see the code for meanings.
325
Bharata B Raoc863d832009-04-13 14:40:15 -0700326Note:
327 Only anonymous and swap cache memory is listed as part of 'rss' stat.
328 This should not be confused with the true 'resident set size' or the
329 amount of physical memory used by the cgroup. Per-cgroup rss
330 accounting is not done yet.
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -0800331
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003325.3 swappiness
333 Similar to /proc/sys/vm/swappiness, but affecting a hierarchy of groups only.
334
Bharata B Raoc863d832009-04-13 14:40:15 -0700335 Following cgroups' swapiness can't be changed.
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800336 - root cgroup (uses /proc/sys/vm/swappiness).
337 - a cgroup which uses hierarchy and it has child cgroup.
338 - a cgroup which uses hierarchy and not the root of hierarchy.
339
340
Balbir Singh52bc0d82009-01-07 18:08:03 -08003416. Hierarchy support
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -0800342
Balbir Singh52bc0d82009-01-07 18:08:03 -0800343The memory controller supports a deep hierarchy and hierarchical accounting.
344The hierarchy is created by creating the appropriate cgroups in the
345cgroup filesystem. Consider for example, the following cgroup filesystem
346hierarchy
347
348 root
349 / | \
350 / | \
351 a b c
352 | \
353 | \
354 d e
355
356In the diagram above, with hierarchical accounting enabled, all memory
357usage of e, is accounted to its ancestors up until the root (i.e, c and root),
358that has memory.use_hierarchy enabled. If one of the ancestors goes over its
359limit, the reclaim algorithm reclaims from the tasks in the ancestor and the
360children of the ancestor.
361
3626.1 Enabling hierarchical accounting and reclaim
363
364The memory controller by default disables the hierarchy feature. Support
365can be enabled by writing 1 to memory.use_hierarchy file of the root cgroup
366
367# echo 1 > memory.use_hierarchy
368
369The feature can be disabled by
370
371# echo 0 > memory.use_hierarchy
372
373NOTE1: Enabling/disabling will fail if the cgroup already has other
374cgroups created below it.
375
376NOTE2: This feature can be enabled/disabled per subtree.
377
3787. TODO
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800379
3801. Add support for accounting huge pages (as a separate controller)
KAMEZAWA Hiroyukidfc05c22008-02-07 00:14:41 -08003812. Make per-cgroup scanner reclaim not-shared pages first
3823. Teach controller to account for shared-pages
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003834. Start reclamation in the background when the limit is
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800384 not yet hit but the usage is getting closer
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800385
386Summary
387
388Overall, the memory controller has been a stable controller and has been
389commented and discussed quite extensively in the community.
390
391References
392
3931. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
3942. Singh, Balbir. Memory Controller (RSS Control),
395 http://lwn.net/Articles/222762/
3963. Emelianov, Pavel. Resource controllers based on process cgroups
397 http://lkml.org/lkml/2007/3/6/198
3984. Emelianov, Pavel. RSS controller based on process cgroups (v2)
Li Zefan2324c5d2008-02-23 15:24:12 -0800399 http://lkml.org/lkml/2007/4/9/78
Balbir Singh1b6df3a2008-02-07 00:13:46 -08004005. Emelianov, Pavel. RSS controller based on process cgroups (v3)
401 http://lkml.org/lkml/2007/5/30/244
4026. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
4037. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
404 subsystem (v3), http://lwn.net/Articles/235534/
Li Zefan2324c5d2008-02-23 15:24:12 -08004058. Singh, Balbir. RSS controller v2 test results (lmbench),
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800406 http://lkml.org/lkml/2007/5/17/232
Li Zefan2324c5d2008-02-23 15:24:12 -08004079. Singh, Balbir. RSS controller v2 AIM9 results
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800408 http://lkml.org/lkml/2007/5/18/1
Li Zefan2324c5d2008-02-23 15:24:12 -080040910. Singh, Balbir. Memory controller v6 test results,
Balbir Singh1b6df3a2008-02-07 00:13:46 -0800410 http://lkml.org/lkml/2007/8/19/36
Li Zefan2324c5d2008-02-23 15:24:12 -080041111. Singh, Balbir. Memory controller introduction (v6),
412 http://lkml.org/lkml/2007/8/17/69
Balbir Singh1b6df3a2008-02-07 00:13:46 -080041312. Corbet, Jonathan, Controlling memory use in cgroups,
414 http://lwn.net/Articles/243795/