memcg_test.rst 8.5 KB

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