4 Copyright (C) 2004 BULL SA.
5 Written by Simon.Derr@bull.net
7 Portions Copyright (c) 2004 Silicon Graphics, Inc.
8 Modified by Paul Jackson <pj@sgi.com>
14 1.1 What are cpusets ?
15 1.2 Why are cpusets needed ?
16 1.3 How are cpusets implemented ?
17 1.4 What are exclusive cpusets ?
18 1.5 What does notify_on_release do ?
19 1.6 What is memory_pressure ?
20 1.7 How do I use cpusets ?
21 2. Usage Examples and Syntax
23 2.2 Adding/removing cpus
25 2.4 Attaching processes
32 1.1 What are cpusets ?
33 ----------------------
35 Cpusets provide a mechanism for assigning a set of CPUs and Memory
36 Nodes to a set of tasks.
38 Cpusets constrain the CPU and Memory placement of tasks to only
39 the resources within a tasks current cpuset. They form a nested
40 hierarchy visible in a virtual file system. These are the essential
41 hooks, beyond what is already present, required to manage dynamic
42 job placement on large systems.
44 Each task has a pointer to a cpuset. Multiple tasks may reference
45 the same cpuset. Requests by a task, using the sched_setaffinity(2)
46 system call to include CPUs in its CPU affinity mask, and using the
47 mbind(2) and set_mempolicy(2) system calls to include Memory Nodes
48 in its memory policy, are both filtered through that tasks cpuset,
49 filtering out any CPUs or Memory Nodes not in that cpuset. The
50 scheduler will not schedule a task on a CPU that is not allowed in
51 its cpus_allowed vector, and the kernel page allocator will not
52 allocate a page on a node that is not allowed in the requesting tasks
55 User level code may create and destroy cpusets by name in the cpuset
56 virtual file system, manage the attributes and permissions of these
57 cpusets and which CPUs and Memory Nodes are assigned to each cpuset,
58 specify and query to which cpuset a task is assigned, and list the
59 task pids assigned to a cpuset.
62 1.2 Why are cpusets needed ?
63 ----------------------------
65 The management of large computer systems, with many processors (CPUs),
66 complex memory cache hierarchies and multiple Memory Nodes having
67 non-uniform access times (NUMA) presents additional challenges for
68 the efficient scheduling and memory placement of processes.
70 Frequently more modest sized systems can be operated with adequate
71 efficiency just by letting the operating system automatically share
72 the available CPU and Memory resources amongst the requesting tasks.
74 But larger systems, which benefit more from careful processor and
75 memory placement to reduce memory access times and contention,
76 and which typically represent a larger investment for the customer,
77 can benefit from explicitly placing jobs on properly sized subsets of
80 This can be especially valuable on:
82 * Web Servers running multiple instances of the same web application,
83 * Servers running different applications (for instance, a web server
85 * NUMA systems running large HPC applications with demanding
86 performance characteristics.
87 * 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
91 These subsets, or "soft partitions" must be able to be dynamically
92 adjusted, as the job mix changes, without impacting other concurrently
95 The kernel cpuset patch provides the minimum essential kernel
96 mechanisms required to efficiently implement such subsets. It
97 leverages existing CPU and Memory Placement facilities in the Linux
98 kernel to avoid any additional impact on the critical scheduler or
99 memory allocator code.
102 1.3 How are cpusets implemented ?
103 ---------------------------------
105 Cpusets provide a Linux kernel (2.6.7 and above) mechanism to constrain
106 which CPUs and Memory Nodes are used by a process or set of processes.
108 The Linux kernel already has a pair of mechanisms to specify on which
109 CPUs a task may be scheduled (sched_setaffinity) and on which Memory
110 Nodes it may obtain memory (mbind, set_mempolicy).
112 Cpusets extends these two mechanisms as follows:
114 - Cpusets are sets of allowed CPUs and Memory Nodes, known to the
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
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.
131 Also a cpu_exclusive cpuset would be associated with a sched
133 - You can list all the tasks (by pid) attached to any cpuset.
135 The implementation of cpusets requires a few, simple hooks
136 into the rest of the kernel, none in performance critical paths:
138 - in init/main.c, to initialize the root cpuset at system boot.
139 - 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.
144 - 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
147 - in the mbind and set_mempolicy system calls, to mask the requested
148 Memory Nodes by what's allowed in that tasks cpuset.
149 - in page_alloc.c, to restrict memory to allowed nodes.
150 - in vmscan.c, to restrict page recovery to the current cpuset.
152 In addition a new file system, of type "cpuset" may be mounted,
153 typically at /dev/cpuset, to enable browsing and modifying the cpusets
154 presently known to the kernel. No new system calls are added for
155 cpusets - all support for querying and modifying cpusets is via
156 this cpuset file system.
158 Each task under /proc has an added file named 'cpuset', displaying
159 the cpuset name, as the path relative to the root of the cpuset file
162 The /proc/<pid>/status file for each task has two added lines,
163 displaying the tasks cpus_allowed (on which CPUs it may be scheduled)
164 and mems_allowed (on which Memory Nodes it may obtain memory),
165 in the format seen in the following example:
167 Cpus_allowed: ffffffff,ffffffff,ffffffff,ffffffff
168 Mems_allowed: ffffffff,ffffffff
170 Each cpuset is represented by a directory in the cpuset file system
171 containing the following files describing that cpuset:
173 - cpus: list of CPUs in that cpuset
174 - mems: list of Memory Nodes in that cpuset
175 - memory_migrate flag: if set, move pages to cpusets nodes
176 - 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
179 - notify_on_release flag: run /sbin/cpuset_release_agent on exit?
180 - memory_pressure: measure of how much paging pressure in cpuset
182 In addition, the root cpuset only has the following file:
183 - memory_pressure_enabled flag: compute memory_pressure?
185 New cpusets are created using the mkdir system call or shell
186 command. The properties of a cpuset, such as its flags, allowed
187 CPUs and Memory Nodes, and attached tasks, are modified by writing
188 to the appropriate file in that cpusets directory, as listed above.
190 The named hierarchical structure of nested cpusets allows partitioning
191 a large system into nested, dynamically changeable, "soft-partitions".
193 The attachment of each task, automatically inherited at fork by any
194 children of that task, to a cpuset allows organizing the work load
195 on a system into related sets of tasks such that each set is constrained
196 to using the CPUs and Memory Nodes of a particular cpuset. A task
197 may be re-attached to any other cpuset, if allowed by the permissions
198 on the necessary cpuset file system directories.
200 Such management of a system "in the large" integrates smoothly with
201 the detailed placement done on individual tasks and memory regions
202 using the sched_setaffinity, mbind and set_mempolicy system calls.
204 The following rules apply to each cpuset:
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.
210 These rules, and the natural hierarchy of cpusets, enable efficient
211 enforcement of the exclusive guarantee, without having to scan all
212 cpusets every time any of them change to ensure nothing overlaps a
213 exclusive cpuset. Also, the use of a Linux virtual file system (vfs)
214 to represent the cpuset hierarchy provides for a familiar permission
215 and name space for cpusets, with a minimum of additional kernel code.
218 1.4 What are exclusive cpusets ?
219 --------------------------------
221 If a cpuset is cpu or mem exclusive, no other cpuset, other than
222 a direct ancestor or descendent, may share any of the same CPUs or
225 A cpuset that is cpu_exclusive has a scheduler (sched) domain
226 associated with it. The sched domain consists of all CPUs in the
227 current cpuset that are not part of any exclusive child cpusets.
228 This ensures that the scheduler load balancing code only balances
229 against the CPUs that are in the sched domain as defined above and
230 not all of the CPUs in the system. This removes any overhead due to
231 load balancing code trying to pull tasks outside of the cpu_exclusive
232 cpuset only to be prevented by the tasks' cpus_allowed mask.
234 A cpuset that is mem_exclusive restricts kernel allocations for
235 page, buffer and other data commonly shared by the kernel across
236 multiple users. All cpusets, whether mem_exclusive or not, restrict
237 allocations of memory for user space. This enables configuring a
238 system so that several independent jobs can share common kernel data,
239 such as file system pages, while isolating each jobs user allocation in
240 its own cpuset. To do this, construct a large mem_exclusive cpuset to
241 hold all the jobs, and construct child, non-mem_exclusive cpusets for
242 each individual job. Only a small amount of typical kernel memory,
243 such as requests from interrupt handlers, is allowed to be taken
244 outside even a mem_exclusive cpuset.
247 1.5 What does notify_on_release do ?
248 ------------------------------------
250 If the notify_on_release flag is enabled (1) in a cpuset, then whenever
251 the last task in the cpuset leaves (exits or attaches to some other
252 cpuset) and the last child cpuset of that cpuset is removed, then
253 the kernel runs the command /sbin/cpuset_release_agent, supplying the
254 pathname (relative to the mount point of the cpuset file system) of the
255 abandoned cpuset. This enables automatic removal of abandoned cpusets.
256 The default value of notify_on_release in the root cpuset at system
257 boot is disabled (0). The default value of other cpusets at creation
258 is the current value of their parents notify_on_release setting.
261 1.6 What is memory_pressure ?
262 -----------------------------
263 The memory_pressure of a cpuset provides a simple per-cpuset metric
264 of the rate that the tasks in a cpuset are attempting to free up in
265 use memory on the nodes of the cpuset to satisfy additional memory
268 This enables batch managers monitoring jobs running in dedicated
269 cpusets to efficiently detect what level of memory pressure that job
272 This is useful both on tightly managed systems running a wide mix of
273 submitted jobs, which may choose to terminate or re-prioritize jobs that
274 are trying to use more memory than allowed on the nodes assigned them,
275 and with tightly coupled, long running, massively parallel scientific
276 computing jobs that will dramatically fail to meet required performance
277 goals if they start to use more memory than allowed to them.
279 This mechanism provides a very economical way for the batch manager
280 to monitor a cpuset for signs of memory pressure. It's up to the
281 batch manager or other user code to decide what to do about it and
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.
290 Why a per-cpuset, running average:
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.
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.
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.
308 A per-cpuset simple digital filter (requires a spinlock and 3 words
309 of data per-cpuset) is kept, and updated by any task attached to that
310 cpuset, if it enters the synchronous (direct) page reclaim code.
312 A per-cpuset file provides an integer number representing the recent
313 (half-life of 10 seconds) rate of direct page reclaims caused by
314 the tasks in the cpuset, in units of reclaims attempted per second,
318 1.7 How do I use cpusets ?
319 --------------------------
321 In order to minimize the impact of cpusets on critical kernel
322 code, such as the scheduler, and due to the fact that the kernel
323 does not support one task updating the memory placement of another
324 task directly, the impact on a task of changing its cpuset CPU
325 or Memory Node placement, or of changing to which cpuset a task
326 is attached, is subtle.
328 If a cpuset has its Memory Nodes modified, then for each task attached
329 to that cpuset, the next time that the kernel attempts to allocate
330 a page of memory for that task, the kernel will notice the change
331 in the tasks cpuset, and update its per-task memory placement to
332 remain within the new cpusets memory placement. If the task was using
333 mempolicy MPOL_BIND, and the nodes to which it was bound overlap with
334 its new cpuset, then the task will continue to use whatever subset
335 of MPOL_BIND nodes are still allowed in the new cpuset. If the task
336 was using MPOL_BIND and now none of its MPOL_BIND nodes are allowed
337 in the new cpuset, then the task will be essentially treated as if it
338 was MPOL_BIND bound to the new cpuset (even though its numa placement,
339 as queried by get_mempolicy(), doesn't change). If a task is moved
340 from one cpuset to another, then the kernel will adjust the tasks
341 memory placement, as above, the next time that the kernel attempts
342 to allocate a page of memory for that task.
344 If a cpuset has its CPUs modified, then each task using that
345 cpuset does _not_ change its behavior automatically. In order to
346 minimize the impact on the critical scheduling code in the kernel,
347 tasks will continue to use their prior CPU placement until they
348 are rebound to their cpuset, by rewriting their pid to the 'tasks'
349 file of their cpuset. If a task had been bound to some subset of its
350 cpuset using the sched_setaffinity() call, and if any of that subset
351 is still allowed in its new cpuset settings, then the task will be
352 restricted to the intersection of the CPUs it was allowed on before,
353 and its new cpuset CPU placement. If, on the other hand, there is
354 no overlap between a tasks prior placement and its new cpuset CPU
355 placement, then the task will be allowed to run on any CPU allowed
356 in its new cpuset. If a task is moved from one cpuset to another,
357 its CPU placement is updated in the same way as if the tasks pid is
358 rewritten to the 'tasks' file of its current cpuset.
360 In summary, the memory placement of a task whose cpuset is changed is
361 updated by the kernel, on the next allocation of a page for that task,
362 but the processor placement is not updated, until that tasks pid is
363 rewritten to the 'tasks' file of its cpuset. This is done to avoid
364 impacting the scheduler code in the kernel with a check for changes
365 in a tasks processor placement.
367 Normally, once a page is allocated (given a physical page
368 of main memory) then that page stays on whatever node it
369 was allocated, so long as it remains allocated, even if the
370 cpusets memory placement policy 'mems' subsequently changes.
371 If the cpuset flag file 'memory_migrate' is set true, then when
372 tasks are attached to that cpuset, any pages that task had
373 allocated to it on nodes in its previous cpuset are migrated
374 to the tasks new cpuset. Depending on the implementation,
375 this migration may either be done by swapping the page out,
376 so that the next time the page is referenced, it will be paged
377 into the tasks new cpuset, usually on the node where it was
378 referenced, or this migration may be done by directly copying
379 the pages from the tasks previous cpuset to the new cpuset,
380 where possible to the same node, relative to the new cpuset,
381 as the node that held the page, relative to the old cpuset.
382 Also if 'memory_migrate' is set true, then if that cpusets
383 'mems' file is modified, pages allocated to tasks in that
384 cpuset, that were on nodes in the previous setting of 'mems',
385 will be moved to nodes in the new setting of 'mems.' Again,
386 depending on the implementation, this might be done by swapping,
387 or by direct copying. In either case, pages that were not in
388 the tasks prior cpuset, or in the cpusets prior 'mems' setting,
391 There is an exception to the above. If hotplug functionality is used
392 to remove all the CPUs that are currently assigned to a cpuset,
393 then the kernel will automatically update the cpus_allowed of all
394 tasks attached to CPUs in that cpuset to allow all CPUs. When memory
395 hotplug functionality for removing Memory Nodes is available, a
396 similar exception is expected to apply there as well. In general,
397 the kernel prefers to violate cpuset placement, over starving a task
398 that has had all its allowed CPUs or Memory Nodes taken offline. User
399 code should reconfigure cpusets to only refer to online CPUs and Memory
400 Nodes when using hotplug to add or remove such resources.
402 There is a second exception to the above. GFP_ATOMIC requests are
403 kernel internal allocations that must be satisfied, immediately.
404 The kernel may drop some request, in rare cases even panic, if a
405 GFP_ATOMIC alloc fails. If the request cannot be satisfied within
406 the current tasks cpuset, then we relax the cpuset, and look for
407 memory anywhere we can find it. It's better to violate the cpuset
408 than stress the kernel.
410 To start a new job that is to be contained within a cpuset, the steps are:
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.
421 For example, the following sequence of commands will setup a cpuset
422 named "Charlie", containing just CPUs 2 and 3, and Memory Node 1,
423 and then start a subshell 'sh' in that cpuset:
425 mount -t cpuset none /dev/cpuset
433 # The subshell 'sh' is now running in cpuset Charlie
434 # The next line should display '/Charlie'
435 cat /proc/self/cpuset
437 In the case that a change of cpuset includes wanting to move already
438 allocated memory pages, consider further the work of IWAMOTO
439 Toshihiro <iwamoto@valinux.co.jp> for page remapping and memory
440 hotremoval, which can be found at:
442 http://people.valinux.co.jp/~iwamoto/mh.html
444 The integration of cpusets with such memory migration is not yet
447 In the future, a C library interface to cpusets will likely be
448 available. For now, the only way to query or modify cpusets is
449 via the cpuset file system, using the various cd, mkdir, echo, cat,
450 rmdir commands from the shell, or their equivalent from C.
452 The sched_setaffinity calls can also be done at the shell prompt using
453 SGI's runon or Robert Love's taskset. The mbind and set_mempolicy
454 calls can be done at the shell prompt using the numactl command
455 (part of Andi Kleen's numa package).
457 2. Usage Examples and Syntax
458 ============================
463 Creating, modifying, using the cpusets can be done through the cpuset
467 # mount -t cpuset none /dev/cpuset
469 Then under /dev/cpuset you can find a tree that corresponds to the
470 tree of the cpusets in the system. For instance, /dev/cpuset
471 is the cpuset that holds the whole system.
473 If you want to create a new cpuset under /dev/cpuset:
477 Now you want to do something with this cpuset.
480 In this directory you can find several files:
482 cpus cpu_exclusive mems mem_exclusive tasks
484 Reading them will give you information about the state of this cpuset:
485 the CPUs and Memory Nodes it can use, the processes that are using
486 it, its properties. By writing to these files you can manipulate
490 # /bin/echo 1 > cpu_exclusive
493 # /bin/echo 0-7 > cpus
495 Now attach your shell to this cpuset:
496 # /bin/echo $$ > tasks
498 You can also create cpusets inside your cpuset by using mkdir in this
502 To remove a cpuset, just use rmdir:
504 This will fail if the cpuset is in use (has cpusets inside, or has
507 2.2 Adding/removing cpus
508 ------------------------
510 This is the syntax to use when writing in the cpus or mems files
511 in cpuset directories:
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
519 The syntax is very simple:
521 # /bin/echo 1 > cpu_exclusive -> set flag 'cpu_exclusive'
522 # /bin/echo 0 > cpu_exclusive -> unset flag 'cpu_exclusive'
524 2.4 Attaching processes
525 -----------------------
527 # /bin/echo PID > tasks
529 Note that it is PID, not PIDs. You can only attach ONE task at a time.
530 If you have several tasks to attach, you have to do it one after another:
532 # /bin/echo PID1 > tasks
533 # /bin/echo PID2 > tasks
535 # /bin/echo PIDn > tasks
541 Q: what's up with this '/bin/echo' ?
542 A: 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.
546 Q: When I attach processes, only the first of the line gets really attached !
547 A: We can only return one error code per call to write(). So you should also
553 Web: http://www.bullopensource.org/cpuset