memory.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. Memory Resource Controller
  2. NOTE: The Memory Resource Controller has been generically been referred
  3. to as the memory controller in this document. Do not confuse memory controller
  4. used here with the memory controller that is used in hardware.
  5. Salient features
  6. a. Enable control of both RSS (mapped) and Page Cache (unmapped) pages
  7. b. The infrastructure allows easy addition of other types of memory to control
  8. c. Provides *zero overhead* for non memory controller users
  9. d. Provides a double LRU: global memory pressure causes reclaim from the
  10. global LRU; a cgroup on hitting a limit, reclaims from the per
  11. cgroup LRU
  12. NOTE: Swap Cache (unmapped) is not accounted now.
  13. Benefits and Purpose of the memory controller
  14. The memory controller isolates the memory behaviour of a group of tasks
  15. from the rest of the system. The article on LWN [12] mentions some probable
  16. uses of the memory controller. The memory controller can be used to
  17. a. Isolate an application or a group of applications
  18. Memory hungry applications can be isolated and limited to a smaller
  19. amount of memory.
  20. b. Create a cgroup with limited amount of memory, this can be used
  21. as a good alternative to booting with mem=XXXX.
  22. c. Virtualization solutions can control the amount of memory they want
  23. to assign to a virtual machine instance.
  24. d. A CD/DVD burner could control the amount of memory used by the
  25. rest of the system to ensure that burning does not fail due to lack
  26. of available memory.
  27. e. There are several other use cases, find one or use the controller just
  28. for fun (to learn and hack on the VM subsystem).
  29. 1. History
  30. The memory controller has a long history. A request for comments for the memory
  31. controller was posted by Balbir Singh [1]. At the time the RFC was posted
  32. there were several implementations for memory control. The goal of the
  33. RFC was to build consensus and agreement for the minimal features required
  34. for memory control. The first RSS controller was posted by Balbir Singh[2]
  35. in Feb 2007. Pavel Emelianov [3][4][5] has since posted three versions of the
  36. RSS controller. At OLS, at the resource management BoF, everyone suggested
  37. that we handle both page cache and RSS together. Another request was raised
  38. to allow user space handling of OOM. The current memory controller is
  39. at version 6; it combines both mapped (RSS) and unmapped Page
  40. Cache Control [11].
  41. 2. Memory Control
  42. Memory is a unique resource in the sense that it is present in a limited
  43. amount. If a task requires a lot of CPU processing, the task can spread
  44. its processing over a period of hours, days, months or years, but with
  45. memory, the same physical memory needs to be reused to accomplish the task.
  46. The memory controller implementation has been divided into phases. These
  47. are:
  48. 1. Memory controller
  49. 2. mlock(2) controller
  50. 3. Kernel user memory accounting and slab control
  51. 4. user mappings length controller
  52. The memory controller is the first controller developed.
  53. 2.1. Design
  54. The core of the design is a counter called the res_counter. The res_counter
  55. tracks the current memory usage and limit of the group of processes associated
  56. with the controller. Each cgroup has a memory controller specific data
  57. structure (mem_cgroup) associated with it.
  58. 2.2. Accounting
  59. +--------------------+
  60. | mem_cgroup |
  61. | (res_counter) |
  62. +--------------------+
  63. / ^ \
  64. / | \
  65. +---------------+ | +---------------+
  66. | mm_struct | |.... | mm_struct |
  67. | | | | |
  68. +---------------+ | +---------------+
  69. |
  70. + --------------+
  71. |
  72. +---------------+ +------+--------+
  73. | page +----------> page_cgroup|
  74. | | | |
  75. +---------------+ +---------------+
  76. (Figure 1: Hierarchy of Accounting)
  77. Figure 1 shows the important aspects of the controller
  78. 1. Accounting happens per cgroup
  79. 2. Each mm_struct knows about which cgroup it belongs to
  80. 3. Each page has a pointer to the page_cgroup, which in turn knows the
  81. cgroup it belongs to
  82. The accounting is done as follows: mem_cgroup_charge() is invoked to setup
  83. the necessary data structures and check if the cgroup that is being charged
  84. is over its limit. If it is then reclaim is invoked on the cgroup.
  85. More details can be found in the reclaim section of this document.
  86. If everything goes well, a page meta-data-structure called page_cgroup is
  87. allocated and associated with the page. This routine also adds the page to
  88. the per cgroup LRU.
  89. 2.2.1 Accounting details
  90. All mapped anon pages (RSS) and cache pages (Page Cache) are accounted.
  91. (some pages which never be reclaimable and will not be on global LRU
  92. are not accounted. we just accounts pages under usual vm management.)
  93. RSS pages are accounted at page_fault unless they've already been accounted
  94. for earlier. A file page will be accounted for as Page Cache when it's
  95. inserted into inode (radix-tree). While it's mapped into the page tables of
  96. processes, duplicate accounting is carefully avoided.
  97. A RSS page is unaccounted when it's fully unmapped. A PageCache page is
  98. unaccounted when it's removed from radix-tree.
  99. At page migration, accounting information is kept.
  100. Note: we just account pages-on-lru because our purpose is to control amount
  101. of used pages. not-on-lru pages are tend to be out-of-control from vm view.
  102. 2.3 Shared Page Accounting
  103. Shared pages are accounted on the basis of the first touch approach. The
  104. cgroup that first touches a page is accounted for the page. The principle
  105. behind this approach is that a cgroup that aggressively uses a shared
  106. page will eventually get charged for it (once it is uncharged from
  107. the cgroup that brought it in -- this will happen on memory pressure).
  108. Exception: When you do swapoff and make swapped-out pages of shmem(tmpfs) to
  109. be backed into memory in force, charges for pages are accounted against the
  110. caller of swapoff rather than the users of shmem.
  111. 2.4 Reclaim
  112. Each cgroup maintains a per cgroup LRU that consists of an active
  113. and inactive list. When a cgroup goes over its limit, we first try
  114. to reclaim memory from the cgroup so as to make space for the new
  115. pages that the cgroup has touched. If the reclaim is unsuccessful,
  116. an OOM routine is invoked to select and kill the bulkiest task in the
  117. cgroup.
  118. The reclaim algorithm has not been modified for cgroups, except that
  119. pages that are selected for reclaiming come from the per cgroup LRU
  120. list.
  121. 2. Locking
  122. The memory controller uses the following hierarchy
  123. 1. zone->lru_lock is used for selecting pages to be isolated
  124. 2. mem->per_zone->lru_lock protects the per cgroup LRU (per zone)
  125. 3. lock_page_cgroup() is used to protect page->page_cgroup
  126. 3. User Interface
  127. 0. Configuration
  128. a. Enable CONFIG_CGROUPS
  129. b. Enable CONFIG_RESOURCE_COUNTERS
  130. c. Enable CONFIG_CGROUP_MEM_RES_CTLR
  131. 1. Prepare the cgroups
  132. # mkdir -p /cgroups
  133. # mount -t cgroup none /cgroups -o memory
  134. 2. Make the new group and move bash into it
  135. # mkdir /cgroups/0
  136. # echo $$ > /cgroups/0/tasks
  137. Since now we're in the 0 cgroup,
  138. We can alter the memory limit:
  139. # echo 4M > /cgroups/0/memory.limit_in_bytes
  140. NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
  141. mega or gigabytes.
  142. # cat /cgroups/0/memory.limit_in_bytes
  143. 4194304
  144. NOTE: The interface has now changed to display the usage in bytes
  145. instead of pages
  146. We can check the usage:
  147. # cat /cgroups/0/memory.usage_in_bytes
  148. 1216512
  149. A successful write to this file does not guarantee a successful set of
  150. this limit to the value written into the file. This can be due to a
  151. number of factors, such as rounding up to page boundaries or the total
  152. availability of memory on the system. The user is required to re-read
  153. this file after a write to guarantee the value committed by the kernel.
  154. # echo 1 > memory.limit_in_bytes
  155. # cat memory.limit_in_bytes
  156. 4096
  157. The memory.failcnt field gives the number of times that the cgroup limit was
  158. exceeded.
  159. The memory.stat file gives accounting information. Now, the number of
  160. caches, RSS and Active pages/Inactive pages are shown.
  161. 4. Testing
  162. Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
  163. Apart from that v6 has been tested with several applications and regular
  164. daily use. The controller has also been tested on the PPC64, x86_64 and
  165. UML platforms.
  166. 4.1 Troubleshooting
  167. Sometimes a user might find that the application under a cgroup is
  168. terminated. There are several causes for this:
  169. 1. The cgroup limit is too low (just too low to do anything useful)
  170. 2. The user is using anonymous memory and swap is turned off or too low
  171. A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
  172. some of the pages cached in the cgroup (page cache pages).
  173. 4.2 Task migration
  174. When a task migrates from one cgroup to another, it's charge is not
  175. carried forward. The pages allocated from the original cgroup still
  176. remain charged to it, the charge is dropped when the page is freed or
  177. reclaimed.
  178. 4.3 Removing a cgroup
  179. A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
  180. cgroup might have some charge associated with it, even though all
  181. tasks have migrated away from it.
  182. Such charges are freed(at default) or moved to its parent. When moved,
  183. both of RSS and CACHES are moved to parent.
  184. If both of them are busy, rmdir() returns -EBUSY. See 5.1 Also.
  185. 5. Misc. interfaces.
  186. 5.1 force_empty
  187. memory.force_empty interface is provided to make cgroup's memory usage empty.
  188. You can use this interface only when the cgroup has no tasks.
  189. When writing anything to this
  190. # echo 0 > memory.force_empty
  191. Almost all pages tracked by this memcg will be unmapped and freed. Some of
  192. pages cannot be freed because it's locked or in-use. Such pages are moved
  193. to parent and this cgroup will be empty. But this may return -EBUSY in
  194. some too busy case.
  195. Typical use case of this interface is that calling this before rmdir().
  196. Because rmdir() moves all pages to parent, some out-of-use page caches can be
  197. moved to the parent. If you want to avoid that, force_empty will be useful.
  198. 6. TODO
  199. 1. Add support for accounting huge pages (as a separate controller)
  200. 2. Make per-cgroup scanner reclaim not-shared pages first
  201. 3. Teach controller to account for shared-pages
  202. 4. Start reclamation in the background when the limit is
  203. not yet hit but the usage is getting closer
  204. Summary
  205. Overall, the memory controller has been a stable controller and has been
  206. commented and discussed quite extensively in the community.
  207. References
  208. 1. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
  209. 2. Singh, Balbir. Memory Controller (RSS Control),
  210. http://lwn.net/Articles/222762/
  211. 3. Emelianov, Pavel. Resource controllers based on process cgroups
  212. http://lkml.org/lkml/2007/3/6/198
  213. 4. Emelianov, Pavel. RSS controller based on process cgroups (v2)
  214. http://lkml.org/lkml/2007/4/9/78
  215. 5. Emelianov, Pavel. RSS controller based on process cgroups (v3)
  216. http://lkml.org/lkml/2007/5/30/244
  217. 6. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
  218. 7. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
  219. subsystem (v3), http://lwn.net/Articles/235534/
  220. 8. Singh, Balbir. RSS controller v2 test results (lmbench),
  221. http://lkml.org/lkml/2007/5/17/232
  222. 9. Singh, Balbir. RSS controller v2 AIM9 results
  223. http://lkml.org/lkml/2007/5/18/1
  224. 10. Singh, Balbir. Memory controller v6 test results,
  225. http://lkml.org/lkml/2007/8/19/36
  226. 11. Singh, Balbir. Memory controller introduction (v6),
  227. http://lkml.org/lkml/2007/8/17/69
  228. 12. Corbet, Jonathan, Controlling memory use in cgroups,
  229. http://lwn.net/Articles/243795/