e2d9afc30d2d68cc923979bf561a88ba79f119bf
[pandora-kernel.git] / Documentation / cpusets.txt
1                                 CPUSETS
2                                 -------
3
4 Copyright (C) 2004 BULL SA.
5 Written by Simon.Derr@bull.net
6
7 Portions Copyright (c) 2004 Silicon Graphics, Inc.
8 Modified by Paul Jackson <pj@sgi.com>
9
10 CONTENTS:
11 =========
12
13 1. Cpusets
14   1.1 What are cpusets ?
15   1.2 Why are cpusets needed ?
16   1.3 How are cpusets implemented ?
17   1.4 How do I use cpusets ?
18 2. Usage Examples and Syntax
19   2.1 Basic Usage
20   2.2 Adding/removing cpus
21   2.3 Setting flags
22   2.4 Attaching processes
23 3. Questions
24 4. Contact
25
26 1. Cpusets
27 ==========
28
29 1.1 What are cpusets ?
30 ----------------------
31
32 Cpusets provide a mechanism for assigning a set of CPUs and Memory
33 Nodes to a set of tasks.
34
35 Cpusets constrain the CPU and Memory placement of tasks to only
36 the resources within a tasks current cpuset.  They form a nested
37 hierarchy visible in a virtual file system.  These are the essential
38 hooks, beyond what is already present, required to manage dynamic
39 job placement on large systems.
40
41 Each task has a pointer to a cpuset.  Multiple tasks may reference
42 the same cpuset.  Requests by a task, using the sched_setaffinity(2)
43 system call to include CPUs in its CPU affinity mask, and using the
44 mbind(2) and set_mempolicy(2) system calls to include Memory Nodes
45 in its memory policy, are both filtered through that tasks cpuset,
46 filtering out any CPUs or Memory Nodes not in that cpuset.  The
47 scheduler will not schedule a task on a CPU that is not allowed in
48 its cpus_allowed vector, and the kernel page allocator will not
49 allocate a page on a node that is not allowed in the requesting tasks
50 mems_allowed vector.
51
52 If a cpuset is cpu or mem exclusive, no other cpuset, other than a direct
53 ancestor or descendent, may share any of the same CPUs or Memory Nodes.
54 A cpuset that is cpu exclusive has a sched domain associated with it.
55 The sched domain consists of all cpus in the current cpuset that are not
56 part of any exclusive child cpusets.
57 This ensures that the scheduler load balacing code only balances
58 against the cpus that are in the sched domain as defined above and not
59 all of the cpus in the system. This removes any overhead due to
60 load balancing code trying to pull tasks outside of the cpu exclusive
61 cpuset only to be prevented by the tasks' cpus_allowed mask.
62
63 A cpuset that is mem_exclusive restricts kernel allocations for
64 page, buffer and other data commonly shared by the kernel across
65 multiple users.  All cpusets, whether mem_exclusive or not, restrict
66 allocations of memory for user space.  This enables configuring a
67 system so that several independent jobs can share common kernel
68 data, such as file system pages, while isolating each jobs user
69 allocation in its own cpuset.  To do this, construct a large
70 mem_exclusive cpuset to hold all the jobs, and construct child,
71 non-mem_exclusive cpusets for each individual job.  Only a small
72 amount of typical kernel memory, such as requests from interrupt
73 handlers, is allowed to be taken outside even a mem_exclusive cpuset.
74
75 User level code may create and destroy cpusets by name in the cpuset
76 virtual file system, manage the attributes and permissions of these
77 cpusets and which CPUs and Memory Nodes are assigned to each cpuset,
78 specify and query to which cpuset a task is assigned, and list the
79 task pids assigned to a cpuset.
80
81
82 1.2 Why are cpusets needed ?
83 ----------------------------
84
85 The management of large computer systems, with many processors (CPUs),
86 complex memory cache hierarchies and multiple Memory Nodes having
87 non-uniform access times (NUMA) presents additional challenges for
88 the efficient scheduling and memory placement of processes.
89
90 Frequently more modest sized systems can be operated with adequate
91 efficiency just by letting the operating system automatically share
92 the available CPU and Memory resources amongst the requesting tasks.
93
94 But larger systems, which benefit more from careful processor and
95 memory placement to reduce memory access times and contention,
96 and which typically represent a larger investment for the customer,
97 can benefit from explicitly placing jobs on properly sized subsets of
98 the system.
99
100 This can be especially valuable on:
101
102     * Web Servers running multiple instances of the same web application,
103     * Servers running different applications (for instance, a web server
104       and a database), or
105     * NUMA systems running large HPC applications with demanding
106       performance characteristics.
107     * Also cpu_exclusive cpusets are useful for servers running orthogonal
108       workloads such as RT applications requiring low latency and HPC
109       applications that are throughput sensitive
110
111 These subsets, or "soft partitions" must be able to be dynamically
112 adjusted, as the job mix changes, without impacting other concurrently
113 executing jobs.
114
115 The kernel cpuset patch provides the minimum essential kernel
116 mechanisms required to efficiently implement such subsets.  It
117 leverages existing CPU and Memory Placement facilities in the Linux
118 kernel to avoid any additional impact on the critical scheduler or
119 memory allocator code.
120
121
122 1.3 How are cpusets implemented ?
123 ---------------------------------
124
125 Cpusets provide a Linux kernel (2.6.7 and above) mechanism to constrain
126 which CPUs and Memory Nodes are used by a process or set of processes.
127
128 The Linux kernel already has a pair of mechanisms to specify on which
129 CPUs a task may be scheduled (sched_setaffinity) and on which Memory
130 Nodes it may obtain memory (mbind, set_mempolicy).
131
132 Cpusets extends these two mechanisms as follows:
133
134  - Cpusets are sets of allowed CPUs and Memory Nodes, known to the
135    kernel.
136  - Each task in the system is attached to a cpuset, via a pointer
137    in the task structure to a reference counted cpuset structure.
138  - Calls to sched_setaffinity are filtered to just those CPUs
139    allowed in that tasks cpuset.
140  - Calls to mbind and set_mempolicy are filtered to just
141    those Memory Nodes allowed in that tasks cpuset.
142  - The root cpuset contains all the systems CPUs and Memory
143    Nodes.
144  - For any cpuset, one can define child cpusets containing a subset
145    of the parents CPU and Memory Node resources.
146  - The hierarchy of cpusets can be mounted at /dev/cpuset, for
147    browsing and manipulation from user space.
148  - A cpuset may be marked exclusive, which ensures that no other
149    cpuset (except direct ancestors and descendents) may contain
150    any overlapping CPUs or Memory Nodes.
151    Also a cpu_exclusive cpuset would be associated with a sched
152    domain.
153  - You can list all the tasks (by pid) attached to any cpuset.
154
155 The implementation of cpusets requires a few, simple hooks
156 into the rest of the kernel, none in performance critical paths:
157
158  - in main/init.c, to initialize the root cpuset at system boot.
159  - in fork and exit, to attach and detach a task from its cpuset.
160  - in sched_setaffinity, to mask the requested CPUs by what's
161    allowed in that tasks cpuset.
162  - in sched.c migrate_all_tasks(), to keep migrating tasks within
163    the CPUs allowed by their cpuset, if possible.
164  - in sched.c, a new API partition_sched_domains for handling
165    sched domain changes associated with cpu_exclusive cpusets
166    and related changes in both sched.c and arch/ia64/kernel/domain.c
167  - in the mbind and set_mempolicy system calls, to mask the requested
168    Memory Nodes by what's allowed in that tasks cpuset.
169  - in page_alloc, to restrict memory to allowed nodes.
170  - in vmscan.c, to restrict page recovery to the current cpuset.
171
172 In addition a new file system, of type "cpuset" may be mounted,
173 typically at /dev/cpuset, to enable browsing and modifying the cpusets
174 presently known to the kernel.  No new system calls are added for
175 cpusets - all support for querying and modifying cpusets is via
176 this cpuset file system.
177
178 Each task under /proc has an added file named 'cpuset', displaying
179 the cpuset name, as the path relative to the root of the cpuset file
180 system.
181
182 The /proc/<pid>/status file for each task has two added lines,
183 displaying the tasks cpus_allowed (on which CPUs it may be scheduled)
184 and mems_allowed (on which Memory Nodes it may obtain memory),
185 in the format seen in the following example:
186
187   Cpus_allowed:   ffffffff,ffffffff,ffffffff,ffffffff
188   Mems_allowed:   ffffffff,ffffffff
189
190 Each cpuset is represented by a directory in the cpuset file system
191 containing the following files describing that cpuset:
192
193  - cpus: list of CPUs in that cpuset
194  - mems: list of Memory Nodes in that cpuset
195  - memory_migrate flag: if set, move pages to cpusets nodes
196  - cpu_exclusive flag: is cpu placement exclusive?
197  - mem_exclusive flag: is memory placement exclusive?
198  - tasks: list of tasks (by pid) attached to that cpuset
199
200 New cpusets are created using the mkdir system call or shell
201 command.  The properties of a cpuset, such as its flags, allowed
202 CPUs and Memory Nodes, and attached tasks, are modified by writing
203 to the appropriate file in that cpusets directory, as listed above.
204
205 The named hierarchical structure of nested cpusets allows partitioning
206 a large system into nested, dynamically changeable, "soft-partitions".
207
208 The attachment of each task, automatically inherited at fork by any
209 children of that task, to a cpuset allows organizing the work load
210 on a system into related sets of tasks such that each set is constrained
211 to using the CPUs and Memory Nodes of a particular cpuset.  A task
212 may be re-attached to any other cpuset, if allowed by the permissions
213 on the necessary cpuset file system directories.
214
215 Such management of a system "in the large" integrates smoothly with
216 the detailed placement done on individual tasks and memory regions
217 using the sched_setaffinity, mbind and set_mempolicy system calls.
218
219 The following rules apply to each cpuset:
220
221  - Its CPUs and Memory Nodes must be a subset of its parents.
222  - It can only be marked exclusive if its parent is.
223  - If its cpu or memory is exclusive, they may not overlap any sibling.
224
225 These rules, and the natural hierarchy of cpusets, enable efficient
226 enforcement of the exclusive guarantee, without having to scan all
227 cpusets every time any of them change to ensure nothing overlaps a
228 exclusive cpuset.  Also, the use of a Linux virtual file system (vfs)
229 to represent the cpuset hierarchy provides for a familiar permission
230 and name space for cpusets, with a minimum of additional kernel code.
231
232 1.4 How do I use cpusets ?
233 --------------------------
234
235 In order to minimize the impact of cpusets on critical kernel
236 code, such as the scheduler, and due to the fact that the kernel
237 does not support one task updating the memory placement of another
238 task directly, the impact on a task of changing its cpuset CPU
239 or Memory Node placement, or of changing to which cpuset a task
240 is attached, is subtle.
241
242 If a cpuset has its Memory Nodes modified, then for each task attached
243 to that cpuset, the next time that the kernel attempts to allocate
244 a page of memory for that task, the kernel will notice the change
245 in the tasks cpuset, and update its per-task memory placement to
246 remain within the new cpusets memory placement.  If the task was using
247 mempolicy MPOL_BIND, and the nodes to which it was bound overlap with
248 its new cpuset, then the task will continue to use whatever subset
249 of MPOL_BIND nodes are still allowed in the new cpuset.  If the task
250 was using MPOL_BIND and now none of its MPOL_BIND nodes are allowed
251 in the new cpuset, then the task will be essentially treated as if it
252 was MPOL_BIND bound to the new cpuset (even though its numa placement,
253 as queried by get_mempolicy(), doesn't change).  If a task is moved
254 from one cpuset to another, then the kernel will adjust the tasks
255 memory placement, as above, the next time that the kernel attempts
256 to allocate a page of memory for that task.
257
258 If a cpuset has its CPUs modified, then each task using that
259 cpuset does _not_ change its behavior automatically.  In order to
260 minimize the impact on the critical scheduling code in the kernel,
261 tasks will continue to use their prior CPU placement until they
262 are rebound to their cpuset, by rewriting their pid to the 'tasks'
263 file of their cpuset.  If a task had been bound to some subset of its
264 cpuset using the sched_setaffinity() call, and if any of that subset
265 is still allowed in its new cpuset settings, then the task will be
266 restricted to the intersection of the CPUs it was allowed on before,
267 and its new cpuset CPU placement.  If, on the other hand, there is
268 no overlap between a tasks prior placement and its new cpuset CPU
269 placement, then the task will be allowed to run on any CPU allowed
270 in its new cpuset.  If a task is moved from one cpuset to another,
271 its CPU placement is updated in the same way as if the tasks pid is
272 rewritten to the 'tasks' file of its current cpuset.
273
274 In summary, the memory placement of a task whose cpuset is changed is
275 updated by the kernel, on the next allocation of a page for that task,
276 but the processor placement is not updated, until that tasks pid is
277 rewritten to the 'tasks' file of its cpuset.  This is done to avoid
278 impacting the scheduler code in the kernel with a check for changes
279 in a tasks processor placement.
280
281 Normally, once a page is allocated (given a physical page
282 of main memory) then that page stays on whatever node it
283 was allocated, so long as it remains allocated, even if the
284 cpusets memory placement policy 'mems' subsequently changes.
285 If the cpuset flag file 'memory_migrate' is set true, then when
286 tasks are attached to that cpuset, any pages that task had
287 allocated to it on nodes in its previous cpuset are migrated
288 to the tasks new cpuset.  Depending on the implementation,
289 this migration may either be done by swapping the page out,
290 so that the next time the page is referenced, it will be paged
291 into the tasks new cpuset, usually on the node where it was
292 referenced, or this migration may be done by directly copying
293 the pages from the tasks previous cpuset to the new cpuset,
294 where possible to the same node, relative to the new cpuset,
295 as the node that held the page, relative to the old cpuset.
296 Also if 'memory_migrate' is set true, then if that cpusets
297 'mems' file is modified, pages allocated to tasks in that
298 cpuset, that were on nodes in the previous setting of 'mems',
299 will be moved to nodes in the new setting of 'mems.'  Again,
300 depending on the implementation, this might be done by swapping,
301 or by direct copying.  In either case, pages that were not in
302 the tasks prior cpuset, or in the cpusets prior 'mems' setting,
303 will not be moved.
304
305 There is an exception to the above.  If hotplug functionality is used
306 to remove all the CPUs that are currently assigned to a cpuset,
307 then the kernel will automatically update the cpus_allowed of all
308 tasks attached to CPUs in that cpuset to allow all CPUs.  When memory
309 hotplug functionality for removing Memory Nodes is available, a
310 similar exception is expected to apply there as well.  In general,
311 the kernel prefers to violate cpuset placement, over starving a task
312 that has had all its allowed CPUs or Memory Nodes taken offline.  User
313 code should reconfigure cpusets to only refer to online CPUs and Memory
314 Nodes when using hotplug to add or remove such resources.
315
316 There is a second exception to the above.  GFP_ATOMIC requests are
317 kernel internal allocations that must be satisfied, immediately.
318 The kernel may drop some request, in rare cases even panic, if a
319 GFP_ATOMIC alloc fails.  If the request cannot be satisfied within
320 the current tasks cpuset, then we relax the cpuset, and look for
321 memory anywhere we can find it.  It's better to violate the cpuset
322 than stress the kernel.
323
324 To start a new job that is to be contained within a cpuset, the steps are:
325
326  1) mkdir /dev/cpuset
327  2) mount -t cpuset none /dev/cpuset
328  3) Create the new cpuset by doing mkdir's and write's (or echo's) in
329     the /dev/cpuset virtual file system.
330  4) Start a task that will be the "founding father" of the new job.
331  5) Attach that task to the new cpuset by writing its pid to the
332     /dev/cpuset tasks file for that cpuset.
333  6) fork, exec or clone the job tasks from this founding father task.
334
335 For example, the following sequence of commands will setup a cpuset
336 named "Charlie", containing just CPUs 2 and 3, and Memory Node 1,
337 and then start a subshell 'sh' in that cpuset:
338
339   mount -t cpuset none /dev/cpuset
340   cd /dev/cpuset
341   mkdir Charlie
342   cd Charlie
343   /bin/echo 2-3 > cpus
344   /bin/echo 1 > mems
345   /bin/echo $$ > tasks
346   sh
347   # The subshell 'sh' is now running in cpuset Charlie
348   # The next line should display '/Charlie'
349   cat /proc/self/cpuset
350
351 In the case that a change of cpuset includes wanting to move already
352 allocated memory pages, consider further the work of IWAMOTO
353 Toshihiro <iwamoto@valinux.co.jp> for page remapping and memory
354 hotremoval, which can be found at:
355
356   http://people.valinux.co.jp/~iwamoto/mh.html
357
358 The integration of cpusets with such memory migration is not yet
359 available.
360
361 In the future, a C library interface to cpusets will likely be
362 available.  For now, the only way to query or modify cpusets is
363 via the cpuset file system, using the various cd, mkdir, echo, cat,
364 rmdir commands from the shell, or their equivalent from C.
365
366 The sched_setaffinity calls can also be done at the shell prompt using
367 SGI's runon or Robert Love's taskset.  The mbind and set_mempolicy
368 calls can be done at the shell prompt using the numactl command
369 (part of Andi Kleen's numa package).
370
371 2. Usage Examples and Syntax
372 ============================
373
374 2.1 Basic Usage
375 ---------------
376
377 Creating, modifying, using the cpusets can be done through the cpuset
378 virtual filesystem.
379
380 To mount it, type:
381 # mount -t cpuset none /dev/cpuset
382
383 Then under /dev/cpuset you can find a tree that corresponds to the
384 tree of the cpusets in the system. For instance, /dev/cpuset
385 is the cpuset that holds the whole system.
386
387 If you want to create a new cpuset under /dev/cpuset:
388 # cd /dev/cpuset
389 # mkdir my_cpuset
390
391 Now you want to do something with this cpuset.
392 # cd my_cpuset
393
394 In this directory you can find several files:
395 # ls
396 cpus  cpu_exclusive  mems  mem_exclusive  tasks
397
398 Reading them will give you information about the state of this cpuset:
399 the CPUs and Memory Nodes it can use, the processes that are using
400 it, its properties.  By writing to these files you can manipulate
401 the cpuset.
402
403 Set some flags:
404 # /bin/echo 1 > cpu_exclusive
405
406 Add some cpus:
407 # /bin/echo 0-7 > cpus
408
409 Now attach your shell to this cpuset:
410 # /bin/echo $$ > tasks
411
412 You can also create cpusets inside your cpuset by using mkdir in this
413 directory.
414 # mkdir my_sub_cs
415
416 To remove a cpuset, just use rmdir:
417 # rmdir my_sub_cs
418 This will fail if the cpuset is in use (has cpusets inside, or has
419 processes attached).
420
421 2.2 Adding/removing cpus
422 ------------------------
423
424 This is the syntax to use when writing in the cpus or mems files
425 in cpuset directories:
426
427 # /bin/echo 1-4 > cpus          -> set cpus list to cpus 1,2,3,4
428 # /bin/echo 1,2,3,4 > cpus      -> set cpus list to cpus 1,2,3,4
429
430 2.3 Setting flags
431 -----------------
432
433 The syntax is very simple:
434
435 # /bin/echo 1 > cpu_exclusive   -> set flag 'cpu_exclusive'
436 # /bin/echo 0 > cpu_exclusive   -> unset flag 'cpu_exclusive'
437
438 2.4 Attaching processes
439 -----------------------
440
441 # /bin/echo PID > tasks
442
443 Note that it is PID, not PIDs. You can only attach ONE task at a time.
444 If you have several tasks to attach, you have to do it one after another:
445
446 # /bin/echo PID1 > tasks
447 # /bin/echo PID2 > tasks
448         ...
449 # /bin/echo PIDn > tasks
450
451
452 3. Questions
453 ============
454
455 Q: what's up with this '/bin/echo' ?
456 A: bash's builtin 'echo' command does not check calls to write() against
457    errors. If you use it in the cpuset file system, you won't be
458    able to tell whether a command succeeded or failed.
459
460 Q: When I attach processes, only the first of the line gets really attached !
461 A: We can only return one error code per call to write(). So you should also
462    put only ONE pid.
463
464 4. Contact
465 ==========
466
467 Web: http://www.bullopensource.org/cpuset