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