memcg_test.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. Memory Resource Controller(Memcg) Implementation Memo.
  2. Last Updated: 2010/2
  3. Base Kernel Version: based on 2.6.33-rc7-mm(candidate for 34).
  4. Because VM is getting complex (one of reasons is memcg...), memcg's behavior
  5. is complex. This is a document for memcg's internal behavior.
  6. Please note that implementation details can be changed.
  7. (*) Topics on API should be in Documentation/cgroups/memory.txt)
  8. 0. How to record usage ?
  9. 2 objects are used.
  10. page_cgroup ....an object per page.
  11. Allocated at boot or memory hotplug. Freed at memory hot removal.
  12. swap_cgroup ... an entry per swp_entry.
  13. Allocated at swapon(). Freed at swapoff().
  14. The page_cgroup has USED bit and double count against a page_cgroup never
  15. occurs. swap_cgroup is used only when a charged page is swapped-out.
  16. 1. Charge
  17. a page/swp_entry may be charged (usage += PAGE_SIZE) at
  18. mem_cgroup_try_charge()
  19. 2. Uncharge
  20. a page/swp_entry may be uncharged (usage -= PAGE_SIZE) by
  21. mem_cgroup_uncharge()
  22. Called when a page's refcount goes down to 0.
  23. mem_cgroup_uncharge_swap()
  24. Called when swp_entry's refcnt goes down to 0. A charge against swap
  25. disappears.
  26. 3. charge-commit-cancel
  27. Memcg pages are charged in two steps:
  28. mem_cgroup_try_charge()
  29. mem_cgroup_commit_charge() or mem_cgroup_cancel_charge()
  30. At try_charge(), there are no flags to say "this page is charged".
  31. at this point, usage += PAGE_SIZE.
  32. At commit(), the page is associated with the memcg.
  33. At cancel(), simply usage -= PAGE_SIZE.
  34. Under below explanation, we assume CONFIG_MEM_RES_CTRL_SWAP=y.
  35. 4. Anonymous
  36. Anonymous page is newly allocated at
  37. - page fault into MAP_ANONYMOUS mapping.
  38. - Copy-On-Write.
  39. 4.1 Swap-in.
  40. At swap-in, the page is taken from swap-cache. There are 2 cases.
  41. (a) If the SwapCache is newly allocated and read, it has no charges.
  42. (b) If the SwapCache has been mapped by processes, it has been
  43. charged already.
  44. 4.2 Swap-out.
  45. At swap-out, typical state transition is below.
  46. (a) add to swap cache. (marked as SwapCache)
  47. swp_entry's refcnt += 1.
  48. (b) fully unmapped.
  49. swp_entry's refcnt += # of ptes.
  50. (c) write back to swap.
  51. (d) delete from swap cache. (remove from SwapCache)
  52. swp_entry's refcnt -= 1.
  53. Finally, at task exit,
  54. (e) zap_pte() is called and swp_entry's refcnt -=1 -> 0.
  55. 5. Page Cache
  56. Page Cache is charged at
  57. - add_to_page_cache_locked().
  58. The logic is very clear. (About migration, see below)
  59. Note: __remove_from_page_cache() is called by remove_from_page_cache()
  60. and __remove_mapping().
  61. 6. Shmem(tmpfs) Page Cache
  62. The best way to understand shmem's page state transition is to read
  63. mm/shmem.c.
  64. But brief explanation of the behavior of memcg around shmem will be
  65. helpful to understand the logic.
  66. Shmem's page (just leaf page, not direct/indirect block) can be on
  67. - radix-tree of shmem's inode.
  68. - SwapCache.
  69. - Both on radix-tree and SwapCache. This happens at swap-in
  70. and swap-out,
  71. It's charged when...
  72. - A new page is added to shmem's radix-tree.
  73. - A swp page is read. (move a charge from swap_cgroup to page_cgroup)
  74. 7. Page Migration
  75. mem_cgroup_migrate()
  76. 8. LRU
  77. Each memcg has its own private LRU. Now, its handling is under global
  78. VM's control (means that it's handled under global zone->lru_lock).
  79. Almost all routines around memcg's LRU is called by global LRU's
  80. list management functions under zone->lru_lock().
  81. A special function is mem_cgroup_isolate_pages(). This scans
  82. memcg's private LRU and call __isolate_lru_page() to extract a page
  83. from LRU.
  84. (By __isolate_lru_page(), the page is removed from both of global and
  85. private LRU.)
  86. 9. Typical Tests.
  87. Tests for racy cases.
  88. 9.1 Small limit to memcg.
  89. When you do test to do racy case, it's good test to set memcg's limit
  90. to be very small rather than GB. Many races found in the test under
  91. xKB or xxMB limits.
  92. (Memory behavior under GB and Memory behavior under MB shows very
  93. different situation.)
  94. 9.2 Shmem
  95. Historically, memcg's shmem handling was poor and we saw some amount
  96. of troubles here. This is because shmem is page-cache but can be
  97. SwapCache. Test with shmem/tmpfs is always good test.
  98. 9.3 Migration
  99. For NUMA, migration is an another special case. To do easy test, cpuset
  100. is useful. Following is a sample script to do migration.
  101. mount -t cgroup -o cpuset none /opt/cpuset
  102. mkdir /opt/cpuset/01
  103. echo 1 > /opt/cpuset/01/cpuset.cpus
  104. echo 0 > /opt/cpuset/01/cpuset.mems
  105. echo 1 > /opt/cpuset/01/cpuset.memory_migrate
  106. mkdir /opt/cpuset/02
  107. echo 1 > /opt/cpuset/02/cpuset.cpus
  108. echo 1 > /opt/cpuset/02/cpuset.mems
  109. echo 1 > /opt/cpuset/02/cpuset.memory_migrate
  110. In above set, when you moves a task from 01 to 02, page migration to
  111. node 0 to node 1 will occur. Following is a script to migrate all
  112. under cpuset.
  113. --
  114. move_task()
  115. {
  116. for pid in $1
  117. do
  118. /bin/echo $pid >$2/tasks 2>/dev/null
  119. echo -n $pid
  120. echo -n " "
  121. done
  122. echo END
  123. }
  124. G1_TASK=`cat ${G1}/tasks`
  125. G2_TASK=`cat ${G2}/tasks`
  126. move_task "${G1_TASK}" ${G2} &
  127. --
  128. 9.4 Memory hotplug.
  129. memory hotplug test is one of good test.
  130. to offline memory, do following.
  131. # echo offline > /sys/devices/system/memory/memoryXXX/state
  132. (XXX is the place of memory)
  133. This is an easy way to test page migration, too.
  134. 9.5 mkdir/rmdir
  135. When using hierarchy, mkdir/rmdir test should be done.
  136. Use tests like the following.
  137. echo 1 >/opt/cgroup/01/memory/use_hierarchy
  138. mkdir /opt/cgroup/01/child_a
  139. mkdir /opt/cgroup/01/child_b
  140. set limit to 01.
  141. add limit to 01/child_b
  142. run jobs under child_a and child_b
  143. create/delete following groups at random while jobs are running.
  144. /opt/cgroup/01/child_a/child_aa
  145. /opt/cgroup/01/child_b/child_bb
  146. /opt/cgroup/01/child_c
  147. running new jobs in new group is also good.
  148. 9.6 Mount with other subsystems.
  149. Mounting with other subsystems is a good test because there is a
  150. race and lock dependency with other cgroup subsystems.
  151. example)
  152. # mount -t cgroup none /cgroup -o cpuset,memory,cpu,devices
  153. and do task move, mkdir, rmdir etc...under this.
  154. 9.7 swapoff.
  155. Besides management of swap is one of complicated parts of memcg,
  156. call path of swap-in at swapoff is not same as usual swap-in path..
  157. It's worth to be tested explicitly.
  158. For example, test like following is good.
  159. (Shell-A)
  160. # mount -t cgroup none /cgroup -o memory
  161. # mkdir /cgroup/test
  162. # echo 40M > /cgroup/test/memory.limit_in_bytes
  163. # echo 0 > /cgroup/test/tasks
  164. Run malloc(100M) program under this. You'll see 60M of swaps.
  165. (Shell-B)
  166. # move all tasks in /cgroup/test to /cgroup
  167. # /sbin/swapoff -a
  168. # rmdir /cgroup/test
  169. # kill malloc task.
  170. Of course, tmpfs v.s. swapoff test should be tested, too.
  171. 9.8 OOM-Killer
  172. Out-of-memory caused by memcg's limit will kill tasks under
  173. the memcg. When hierarchy is used, a task under hierarchy
  174. will be killed by the kernel.
  175. In this case, panic_on_oom shouldn't be invoked and tasks
  176. in other groups shouldn't be killed.
  177. It's not difficult to cause OOM under memcg as following.
  178. Case A) when you can swapoff
  179. #swapoff -a
  180. #echo 50M > /memory.limit_in_bytes
  181. run 51M of malloc
  182. Case B) when you use mem+swap limitation.
  183. #echo 50M > memory.limit_in_bytes
  184. #echo 50M > memory.memsw.limit_in_bytes
  185. run 51M of malloc
  186. 9.9 Move charges at task migration
  187. Charges associated with a task can be moved along with task migration.
  188. (Shell-A)
  189. #mkdir /cgroup/A
  190. #echo $$ >/cgroup/A/tasks
  191. run some programs which uses some amount of memory in /cgroup/A.
  192. (Shell-B)
  193. #mkdir /cgroup/B
  194. #echo 1 >/cgroup/B/memory.move_charge_at_immigrate
  195. #echo "pid of the program running in group A" >/cgroup/B/tasks
  196. You can see charges have been moved by reading *.usage_in_bytes or
  197. memory.stat of both A and B.
  198. See 8.2 of Documentation/cgroups/memory.txt to see what value should be
  199. written to move_charge_at_immigrate.
  200. 9.10 Memory thresholds
  201. Memory controller implements memory thresholds using cgroups notification
  202. API. You can use tools/cgroup/cgroup_event_listener.c to test it.
  203. (Shell-A) Create cgroup and run event listener
  204. # mkdir /cgroup/A
  205. # ./cgroup_event_listener /cgroup/A/memory.usage_in_bytes 5M
  206. (Shell-B) Add task to cgroup and try to allocate and free memory
  207. # echo $$ >/cgroup/A/tasks
  208. # a="$(dd if=/dev/zero bs=1M count=10)"
  209. # a=
  210. You will see message from cgroup_event_listener every time you cross
  211. the thresholds.
  212. Use /cgroup/A/memory.memsw.usage_in_bytes to test memsw thresholds.
  213. It's good idea to test root cgroup as well.