memory.txt 11 KB

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