This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / Documentation / ckrm / mem_rc.design
1 0. Lifecycle of a LRU Page:
2 ----------------------------
3 These are the events in a page's lifecycle:
4    - allocation of the page
5      there are multiple high level page alloc functions; __alloc_pages()
6          is the lowest level function that does the real allocation.
7    - get into LRU list (active list or inactive list)
8    - get out of LRU list
9    - freeing the page
10      there are multiple high level page free functions; free_pages_bulk()
11          is the lowest level function that does the real free.
12
13 When the memory subsystem runs low on LRU pages, pages are reclaimed by
14     - moving pages from active list to inactive list (refill_inactive_zone())
15     - freeing pages from the inactive list (shrink_zone)
16 depending on the recent usage of the page(approximately).
17
18 1. Introduction
19 ---------------
20 Memory resource controller controls the number of lru physical pages
21 (active and inactive list) a class uses. It does not restrict any
22 other physical pages (slabs etc.,)
23
24 For simplicity, this document will always refer lru physical pages as
25 physical pages or simply pages.
26
27 There are two parameters(that are set by the user) that affect the number
28 of pages a class is allowed to have in active/inactive list.
29 They are
30   - guarantee - specifies the number of pages a class is
31         guaranteed to get. In other words, if a class is using less than
32         'guarantee' number of pages, its pages will not be freed when the
33         memory subsystem tries to free some pages.
34   - limit - specifies the maximum number of pages a class can get;
35     'limit' in essence can be considered as the 'hard limit'
36
37 Rest of this document details how these two parameters are used in the
38 memory allocation logic.
39
40 Note that the numbers that are specified in the shares file, doesn't
41 directly correspond to the number of pages. But, the user can make
42 it so by making the total_guarantee and max_limit of the default class
43 (/rcfs/taskclass) to be the total number of pages(given in stats file)
44 available in the system.
45
46   for example: 
47    # cd /rcfs/taskclass
48    # grep System stats
49    System: tot_pages=257512,active=5897,inactive=2931,free=243991
50    # cat shares
51    res=mem,guarantee=-2,limit=-2,total_guarantee=100,max_limit=100
52
53   "tot_pages=257512" above mean there are 257512 lru pages in
54   the system.
55   
56   By making total_guarantee and max_limit to be same as this number at 
57   this level (/rcfs/taskclass), one can make guarantee and limit in all 
58   classes refer to the number of pages.
59
60   # echo 'res=mem,total_guarantee=257512,max_limit=257512' > shares
61   # cat shares
62   res=mem,guarantee=-2,limit=-2,total_guarantee=257512,max_limit=257512
63
64
65 The number of pages a class can use be anywhere between its guarantee and
66 limit. CKRM memory controller springs into action when the system needs
67 to choose a victim page to swap out. While the number of pages a class can
68 have allocated may be anywhere between its guarantee and limit, victim
69 pages will be choosen from classes that are above their guarantee.
70
71 Victim class will be chosen by the number pages a class is using over its
72 guarantee. i.e a class that is using 10000 pages over its guarantee will be
73 chosen against a class that is using 1000 pages over its guarantee.
74 Pages belonging to classes that are below their guarantee will not be
75 chosen as a victim.
76
77 2. Configuaration parameters
78 ---------------------------
79
80 Memory controller provides the following configuration parameters. Usage of
81 these parameters will be made clear in the following section.
82
83 fail_over: When pages are being allocated, if the class is over fail_over % of
84     its limit, then fail the memory allocation. Default is 110.
85     ex: If limit of a class is 30000 and fail_over is 110, then memory
86     allocations would start failing once the class is using more than 33000
87     pages.
88
89 shrink_at: When a class is using shrink_at % of its limit, then start
90     shrinking the class, i.e start freeing the page to make more free pages
91     available for this class. Default is 90.
92     ex: If limit of a class is 30000 and shrink_at is 90, then pages from this
93     class will start to get freed when the class's usage is above 27000
94
95 shrink_to: When a class reached shrink_at % of its limit, ckrm will try to
96     shrink the class's usage to shrink_to %. Defalut is 80.
97     ex: If limit of a class is 30000 with shrink_at being 90 and shrink_to
98     being 80, then ckrm will try to free pages from the class when its
99     usage reaches 27000 and will try to bring it down to 24000.
100
101 num_shrinks: Number of shrink attempts ckrm will do within shrink_interval
102     seconds. After this many attempts in a period, ckrm will not attempt a
103     shrink even if the class's usage goes over shrink_at %. Default is 10.
104
105 shrink_interval: Number of seconds in a shrink period. Default is 10.
106
107 3. Design
108 --------------------------
109
110 CKRM memory resource controller taps at appropriate low level memory 
111 management functions to associate a page with a class and to charge
112 a class that brings the page to the LRU list.
113
114 CKRM maintains lru lists per-class instead of keeping it system-wide, so
115 that reducing a class's usage doesn't involve going through the system-wide
116 lru lists.
117
118 3.1 Changes in page allocation function(__alloc_pages())
119 --------------------------------------------------------
120 - If the class that the current task belong to is over 'fail_over' % of its
121   'limit', allocation of page(s) fail. Otherwise, the page allocation will
122   proceed as before.
123 - Note that the class is _not_ charged for the page(s) here.
124
125 3.2 Changes in page free(free_pages_bulk())
126 -------------------------------------------
127 - If the page still belong to a class, the class will be credited for this
128   page.
129
130 3.3 Adding/Deleting page to active/inactive list
131 -------------------------------------------------
132 When a page is added to the active or inactive list, the class that the
133 task belongs to is charged for the page usage.
134
135 When a page is deleted from the active or inactive list, the class that the
136 page belongs to is credited back.
137
138 If a class uses 'shrink_at' % of its limit, attempt is made to shrink
139 the class's usage to 'shrink_to' % of its limit, in order to help the class
140 stay within its limit.
141 But, if the class is aggressive, and keep getting over the class's limit
142 often(more than such 'num_shrinks' events in 'shrink_interval' seconds),
143 then the memory resource controller gives up on the class and doesn't try
144 to shrink the class, which will eventually lead the class to reach
145 fail_over % and then the page allocations will start failing.
146
147 3.4 Changes in the page reclaimation path (refill_inactive_zone and shrink_zone)
148 -------------------------------------------------------------------------------
149 Pages will be moved from active to inactive list(refill_inactive_zone) and
150 pages from inactive list by choosing victim classes. Victim classes are
151 chosen depending on their usage over their guarantee.
152
153 Classes with DONT_CARE guarantee are assumed an implicit guarantee which is
154 based on the number of children(with DONT_CARE guarantee) its parent has
155 (including the default class) and the unused pages its parent still has.
156 ex1: If a default root class /rcfs/taskclass has 3 children c1, c2 and c3
157 and has 200000 pages, and all the classes have DONT_CARE guarantees, then
158 all the classes (c1, c2, c3 and the default class of /rcfs/taskclass) will 
159 get 50000 (200000 / 4) pages each.
160 ex2: If, in the above example c1 is set with a guarantee of 80000 pages,
161 then the other classes (c2, c3 and the default class of /rcfs/taskclass)
162 will get 40000 ((200000 - 80000) / 3) pages each.
163
164 3.5 Handling of Shared pages
165 ----------------------------
166 Even if a mm is shared by tasks, the pages that belong to the mm will be
167 charged against the individual tasks that bring the page into LRU. 
168
169 But, when any task that is using a mm moves to a different class or exits,
170 then all pages that belong to the mm will be charged against the richest
171 class among the tasks that are using the mm.
172
173 Note: Shared page handling need to be improved with a better policy.
174