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