]> bbs.cooldavid.org Git - net-next-2.6.git/blame - Documentation/cgroups/memory.txt
memcg: oom notifier
[net-next-2.6.git] / Documentation / cgroups / 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
c863d835
BR
9a. Enable control of Anonymous, Page Cache (mapped and unmapped) and
10 Swap Cache memory pages.
1b6df3aa
BS
11b. 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
1b6df3aa
BS
17Benefits 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
5b4e655e
KH
114All 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.
1b6df3aa
BS
130
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
8c7c6e34
KH
139Exception: If CONFIG_CGROUP_CGROUP_MEM_RES_CTLR_SWAP is not used..
140When you do swapoff and make swapped-out pages of shmem(tmpfs) to
d13d1443
KH
141be backed into memory in force, charges for pages are accounted against the
142caller of swapoff rather than the users of shmem.
143
144
8c7c6e34
KH
1452.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
22a668d7 155* why 'mem+swap' rather than swap.
8c7c6e34
KH
156The 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
22a668d7
KH
158mem+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.
161
162* 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.
8c7c6e34
KH
168
1692.5 Reclaim
1b6df3aa
BS
170
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
4b3bde4c
BS
182NOTE: Reclaim does not work for the root cgroup, since we cannot set any
183limits on the root cgroup.
184
daaf1e68
KH
185Note2: When panic_on_oom is set to "2", the whole system will panic.
186
9490ff27
KH
187When oom event notifier is registered, event will be delivered.
188(See oom_control section)
189
1b6df3aa
BS
1902. Locking
191
192The memory controller uses the following hierarchy
193
1941. zone->lru_lock is used for selecting pages to be isolated
dfc05c25 1952. mem->per_zone->lru_lock protects the per cgroup LRU (per zone)
1b6df3aa
BS
1963. lock_page_cgroup() is used to protect page->page_cgroup
197
1983. User Interface
199
2000. Configuration
201
202a. Enable CONFIG_CGROUPS
203b. Enable CONFIG_RESOURCE_COUNTERS
00f0b825 204c. Enable CONFIG_CGROUP_MEM_RES_CTLR
1b6df3aa
BS
205
2061. Prepare the cgroups
207# mkdir -p /cgroups
208# mount -t cgroup none /cgroups -o memory
209
2102. Make the new group and move bash into it
211# mkdir /cgroups/0
212# echo $$ > /cgroups/0/tasks
213
214Since now we're in the 0 cgroup,
215We can alter the memory limit:
fb78922c 216# echo 4M > /cgroups/0/memory.limit_in_bytes
0eea1030
BS
217
218NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
219mega or gigabytes.
c5b947b2 220NOTE: We can write "-1" to reset the *.limit_in_bytes(unlimited).
4b3bde4c 221NOTE: We cannot set limits on the root cgroup any more.
0eea1030
BS
222
223# cat /cgroups/0/memory.limit_in_bytes
2324c5dd 2244194304
0eea1030
BS
225
226NOTE: The interface has now changed to display the usage in bytes
227instead of pages
1b6df3aa
BS
228
229We can check the usage:
0eea1030 230# cat /cgroups/0/memory.usage_in_bytes
2324c5dd 2311216512
0eea1030
BS
232
233A successful write to this file does not guarantee a successful set of
234this limit to the value written into the file. This can be due to a
235number of factors, such as rounding up to page boundaries or the total
236availability of memory on the system. The user is required to re-read
237this file after a write to guarantee the value committed by the kernel.
238
fb78922c 239# echo 1 > memory.limit_in_bytes
0eea1030 240# cat memory.limit_in_bytes
2324c5dd 2414096
1b6df3aa
BS
242
243The memory.failcnt field gives the number of times that the cgroup limit was
244exceeded.
245
dfc05c25
KH
246The memory.stat file gives accounting information. Now, the number of
247caches, RSS and Active pages/Inactive pages are shown.
248
1b6df3aa
BS
2494. Testing
250
251Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
252Apart from that v6 has been tested with several applications and regular
253daily use. The controller has also been tested on the PPC64, x86_64 and
254UML platforms.
255
2564.1 Troubleshooting
257
258Sometimes a user might find that the application under a cgroup is
259terminated. There are several causes for this:
260
2611. The cgroup limit is too low (just too low to do anything useful)
2622. The user is using anonymous memory and swap is turned off or too low
263
264A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
265some of the pages cached in the cgroup (page cache pages).
266
2674.2 Task migration
268
a33f3224 269When a task migrates from one cgroup to another, its charge is not
7dc74be0 270carried forward by default. The pages allocated from the original cgroup still
1b6df3aa
BS
271remain charged to it, the charge is dropped when the page is freed or
272reclaimed.
273
7dc74be0
DN
274Note: You can move charges of a task along with task migration. See 8.
275
1b6df3aa
BS
2764.3 Removing a cgroup
277
278A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
279cgroup might have some charge associated with it, even though all
f817ed48 280tasks have migrated away from it.
c1e862c1
KH
281Such charges are freed(at default) or moved to its parent. When moved,
282both of RSS and CACHES are moved to parent.
283If both of them are busy, rmdir() returns -EBUSY. See 5.1 Also.
1b6df3aa 284
8c7c6e34
KH
285Charges recorded in swap information is not updated at removal of cgroup.
286Recorded information is discarded and a cgroup which uses swap (swapcache)
287will be charged as a new owner of it.
288
289
c1e862c1
KH
2905. Misc. interfaces.
291
2925.1 force_empty
293 memory.force_empty interface is provided to make cgroup's memory usage empty.
294 You can use this interface only when the cgroup has no tasks.
295 When writing anything to this
296
297 # echo 0 > memory.force_empty
298
299 Almost all pages tracked by this memcg will be unmapped and freed. Some of
300 pages cannot be freed because it's locked or in-use. Such pages are moved
301 to parent and this cgroup will be empty. But this may return -EBUSY in
302 some too busy case.
303
304 Typical use case of this interface is that calling this before rmdir().
305 Because rmdir() moves all pages to parent, some out-of-use page caches can be
306 moved to the parent. If you want to avoid that, force_empty will be useful.
307
7f016ee8 3085.2 stat file
c863d835
BR
309
310memory.stat file includes following statistics
311
312cache - # of bytes of page cache memory.
313rss - # of bytes of anonymous and swap cache memory.
314pgpgin - # of pages paged in (equivalent to # of charging events).
315pgpgout - # of pages paged out (equivalent to # of uncharging events).
316active_anon - # of bytes of anonymous and swap cache memory on active
317 lru list.
318inactive_anon - # of bytes of anonymous memory and swap cache memory on
319 inactive lru list.
320active_file - # of bytes of file-backed memory on active lru list.
321inactive_file - # of bytes of file-backed memory on inactive lru list.
322unevictable - # of bytes of memory that cannot be reclaimed (mlocked etc).
323
324The following additional stats are dependent on CONFIG_DEBUG_VM.
325
326inactive_ratio - VM internal parameter. (see mm/page_alloc.c)
327recent_rotated_anon - VM internal parameter. (see mm/vmscan.c)
328recent_rotated_file - VM internal parameter. (see mm/vmscan.c)
329recent_scanned_anon - VM internal parameter. (see mm/vmscan.c)
330recent_scanned_file - VM internal parameter. (see mm/vmscan.c)
331
332Memo:
7f016ee8
KM
333 recent_rotated means recent frequency of lru rotation.
334 recent_scanned means recent # of scans to lru.
335 showing for better debug please see the code for meanings.
336
c863d835
BR
337Note:
338 Only anonymous and swap cache memory is listed as part of 'rss' stat.
339 This should not be confused with the true 'resident set size' or the
340 amount of physical memory used by the cgroup. Per-cgroup rss
341 accounting is not done yet.
7f016ee8 342
a7885eb8
KM
3435.3 swappiness
344 Similar to /proc/sys/vm/swappiness, but affecting a hierarchy of groups only.
345
ab5097b1 346 Following cgroups' swappiness can't be changed.
a7885eb8
KM
347 - root cgroup (uses /proc/sys/vm/swappiness).
348 - a cgroup which uses hierarchy and it has child cgroup.
349 - a cgroup which uses hierarchy and not the root of hierarchy.
350
351
52bc0d82 3526. Hierarchy support
c1e862c1 353
52bc0d82
BS
354The memory controller supports a deep hierarchy and hierarchical accounting.
355The hierarchy is created by creating the appropriate cgroups in the
356cgroup filesystem. Consider for example, the following cgroup filesystem
357hierarchy
358
359 root
360 / | \
361 / | \
362 a b c
363 | \
364 | \
365 d e
366
367In the diagram above, with hierarchical accounting enabled, all memory
368usage of e, is accounted to its ancestors up until the root (i.e, c and root),
369that has memory.use_hierarchy enabled. If one of the ancestors goes over its
370limit, the reclaim algorithm reclaims from the tasks in the ancestor and the
371children of the ancestor.
372
3736.1 Enabling hierarchical accounting and reclaim
374
375The memory controller by default disables the hierarchy feature. Support
376can be enabled by writing 1 to memory.use_hierarchy file of the root cgroup
377
378# echo 1 > memory.use_hierarchy
379
380The feature can be disabled by
381
382# echo 0 > memory.use_hierarchy
383
384NOTE1: Enabling/disabling will fail if the cgroup already has other
385cgroups created below it.
386
daaf1e68
KH
387NOTE2: When panic_on_oom is set to "2", the whole system will panic in
388case of an oom event in any cgroup.
52bc0d82 389
a6df6361
BS
3907. Soft limits
391
392Soft limits allow for greater sharing of memory. The idea behind soft limits
393is to allow control groups to use as much of the memory as needed, provided
394
395a. There is no memory contention
396b. They do not exceed their hard limit
397
398When the system detects memory contention or low memory control groups
399are pushed back to their soft limits. If the soft limit of each control
400group is very high, they are pushed back as much as possible to make
401sure that one control group does not starve the others of memory.
402
403Please note that soft limits is a best effort feature, it comes with
404no guarantees, but it does its best to make sure that when memory is
405heavily contended for, memory is allocated based on the soft limit
406hints/setup. Currently soft limit based reclaim is setup such that
407it gets invoked from balance_pgdat (kswapd).
408
4097.1 Interface
410
411Soft limits can be setup by using the following commands (in this example we
412assume a soft limit of 256 megabytes)
413
414# echo 256M > memory.soft_limit_in_bytes
415
416If we want to change this to 1G, we can at any time use
417
418# echo 1G > memory.soft_limit_in_bytes
419
420NOTE1: Soft limits take effect over a long period of time, since they involve
421 reclaiming memory for balancing between memory cgroups
422NOTE2: It is recommended to set the soft limit always below the hard limit,
423 otherwise the hard limit will take precedence.
424
7dc74be0
DN
4258. Move charges at task migration
426
427Users can move charges associated with a task along with task migration, that
428is, uncharge task's pages from the old cgroup and charge them to the new cgroup.
02491447
DN
429This feature is not supported in !CONFIG_MMU environments because of lack of
430page tables.
7dc74be0
DN
431
4328.1 Interface
433
434This feature is disabled by default. It can be enabled(and disabled again) by
435writing to memory.move_charge_at_immigrate of the destination cgroup.
436
437If you want to enable it:
438
439# echo (some positive value) > memory.move_charge_at_immigrate
440
441Note: Each bits of move_charge_at_immigrate has its own meaning about what type
442 of charges should be moved. See 8.2 for details.
443Note: Charges are moved only when you move mm->owner, IOW, a leader of a thread
444 group.
445Note: If we cannot find enough space for the task in the destination cgroup, we
446 try to make space by reclaiming memory. Task migration may fail if we
447 cannot make enough space.
448Note: It can take several seconds if you move charges in giga bytes order.
449
450And if you want disable it again:
451
452# echo 0 > memory.move_charge_at_immigrate
453
4548.2 Type of charges which can be move
455
456Each bits of move_charge_at_immigrate has its own meaning about what type of
457charges should be moved.
458
459 bit | what type of charges would be moved ?
460 -----+------------------------------------------------------------------------
461 0 | A charge of an anonymous page(or swap of it) used by the target task.
462 | Those pages and swaps must be used only by the target task. You must
463 | enable Swap Extension(see 2.4) to enable move of swap charges.
464
465Note: Those pages and swaps must be charged to the old cgroup.
466Note: More type of pages(e.g. file cache, shmem,) will be supported by other
467 bits in future.
468
4698.3 TODO
470
471- Add support for other types of pages(e.g. file cache, shmem, etc.).
472- Implement madvise(2) to let users decide the vma to be moved or not to be
473 moved.
474- All of moving charge operations are done under cgroup_mutex. It's not good
475 behavior to hold the mutex too long, so we may need some trick.
476
2e72b634
KS
4779. Memory thresholds
478
479Memory controler implements memory thresholds using cgroups notification
480API (see cgroups.txt). It allows to register multiple memory and memsw
481thresholds and gets notifications when it crosses.
482
483To register a threshold application need:
484 - create an eventfd using eventfd(2);
485 - open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
486 - write string like "<event_fd> <memory.usage_in_bytes> <threshold>" to
487 cgroup.event_control.
488
489Application will be notified through eventfd when memory usage crosses
490threshold in any direction.
491
492It's applicable for root and non-root cgroup.
493
9490ff27
KH
49410. OOM Control
495
496Memory controler implements oom notifier using cgroup notification
497API (See cgroups.txt). It allows to register multiple oom notification
498delivery and gets notification when oom happens.
499
500To register a notifier, application need:
501 - create an eventfd using eventfd(2)
502 - open memory.oom_control file
503 - write string like "<event_fd> <memory.oom_control>" to cgroup.event_control
504
505Application will be notifier through eventfd when oom happens.
506OOM notification doesn't work for root cgroup.
507
508
50911. TODO
1b6df3aa
BS
510
5111. Add support for accounting huge pages (as a separate controller)
dfc05c25
KH
5122. Make per-cgroup scanner reclaim not-shared pages first
5133. Teach controller to account for shared-pages
628f4235 5144. Start reclamation in the background when the limit is
1b6df3aa 515 not yet hit but the usage is getting closer
1b6df3aa
BS
516
517Summary
518
519Overall, the memory controller has been a stable controller and has been
520commented and discussed quite extensively in the community.
521
522References
523
5241. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
5252. Singh, Balbir. Memory Controller (RSS Control),
526 http://lwn.net/Articles/222762/
5273. Emelianov, Pavel. Resource controllers based on process cgroups
528 http://lkml.org/lkml/2007/3/6/198
5294. Emelianov, Pavel. RSS controller based on process cgroups (v2)
2324c5dd 530 http://lkml.org/lkml/2007/4/9/78
1b6df3aa
BS
5315. Emelianov, Pavel. RSS controller based on process cgroups (v3)
532 http://lkml.org/lkml/2007/5/30/244
5336. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
5347. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
535 subsystem (v3), http://lwn.net/Articles/235534/
2324c5dd 5368. Singh, Balbir. RSS controller v2 test results (lmbench),
1b6df3aa 537 http://lkml.org/lkml/2007/5/17/232
2324c5dd 5389. Singh, Balbir. RSS controller v2 AIM9 results
1b6df3aa 539 http://lkml.org/lkml/2007/5/18/1
2324c5dd 54010. Singh, Balbir. Memory controller v6 test results,
1b6df3aa 541 http://lkml.org/lkml/2007/8/19/36
2324c5dd
LZ
54211. Singh, Balbir. Memory controller introduction (v6),
543 http://lkml.org/lkml/2007/8/17/69
1b6df3aa
BS
54412. Corbet, Jonathan, Controlling memory use in cgroups,
545 http://lwn.net/Articles/243795/