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