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