]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - Documentation/cpusets.txt
Merge branch 'origin'
[mirror_ubuntu-artful-kernel.git] / Documentation / cpusets.txt
CommitLineData
1da177e4
LT
1 CPUSETS
2 -------
3
4Copyright (C) 2004 BULL SA.
5Written by Simon.Derr@bull.net
6
b4fb3766 7Portions Copyright (c) 2004-2006 Silicon Graphics, Inc.
1da177e4 8Modified by Paul Jackson <pj@sgi.com>
b4fb3766 9Modified by Christoph Lameter <clameter@sgi.com>
1da177e4
LT
10
11CONTENTS:
12=========
13
141. Cpusets
15 1.1 What are cpusets ?
16 1.2 Why are cpusets needed ?
17 1.3 How are cpusets implemented ?
bd5e09cf
PJ
18 1.4 What are exclusive cpusets ?
19 1.5 What does notify_on_release do ?
90c9cc40
PJ
20 1.6 What is memory_pressure ?
21 1.7 How do I use cpusets ?
1da177e4
LT
222. 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
273. Questions
284. Contact
29
301. Cpusets
31==========
32
331.1 What are cpusets ?
34----------------------
35
36Cpusets provide a mechanism for assigning a set of CPUs and Memory
37Nodes to a set of tasks.
38
39Cpusets constrain the CPU and Memory placement of tasks to only
40the resources within a tasks current cpuset. They form a nested
41hierarchy visible in a virtual file system. These are the essential
42hooks, beyond what is already present, required to manage dynamic
43job placement on large systems.
44
45Each task has a pointer to a cpuset. Multiple tasks may reference
46the same cpuset. Requests by a task, using the sched_setaffinity(2)
47system call to include CPUs in its CPU affinity mask, and using the
48mbind(2) and set_mempolicy(2) system calls to include Memory Nodes
49in its memory policy, are both filtered through that tasks cpuset,
50filtering out any CPUs or Memory Nodes not in that cpuset. The
51scheduler will not schedule a task on a CPU that is not allowed in
52its cpus_allowed vector, and the kernel page allocator will not
53allocate a page on a node that is not allowed in the requesting tasks
54mems_allowed vector.
55
1da177e4
LT
56User level code may create and destroy cpusets by name in the cpuset
57virtual file system, manage the attributes and permissions of these
58cpusets and which CPUs and Memory Nodes are assigned to each cpuset,
59specify and query to which cpuset a task is assigned, and list the
60task pids assigned to a cpuset.
61
62
631.2 Why are cpusets needed ?
64----------------------------
65
66The management of large computer systems, with many processors (CPUs),
67complex memory cache hierarchies and multiple Memory Nodes having
68non-uniform access times (NUMA) presents additional challenges for
69the efficient scheduling and memory placement of processes.
70
71Frequently more modest sized systems can be operated with adequate
72efficiency just by letting the operating system automatically share
73the available CPU and Memory resources amongst the requesting tasks.
74
75But larger systems, which benefit more from careful processor and
76memory placement to reduce memory access times and contention,
77and which typically represent a larger investment for the customer,
33430dc5 78can benefit from explicitly placing jobs on properly sized subsets of
1da177e4
LT
79the system.
80
81This 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.
85d7b949
DG
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
1da177e4
LT
91
92These subsets, or "soft partitions" must be able to be dynamically
93adjusted, as the job mix changes, without impacting other concurrently
b4fb3766
CL
94executing jobs. The location of the running jobs pages may also be moved
95when the memory locations are changed.
1da177e4
LT
96
97The kernel cpuset patch provides the minimum essential kernel
98mechanisms required to efficiently implement such subsets. It
99leverages existing CPU and Memory Placement facilities in the Linux
100kernel to avoid any additional impact on the critical scheduler or
101memory allocator code.
102
103
1041.3 How are cpusets implemented ?
105---------------------------------
106
b4fb3766
CL
107Cpusets provide a Linux kernel mechanism to constrain which CPUs and
108Memory Nodes are used by a process or set of processes.
1da177e4
LT
109
110The Linux kernel already has a pair of mechanisms to specify on which
111CPUs a task may be scheduled (sched_setaffinity) and on which Memory
112Nodes it may obtain memory (mbind, set_mempolicy).
113
114Cpusets 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.
85d7b949
DG
133 Also a cpu_exclusive cpuset would be associated with a sched
134 domain.
1da177e4
LT
135 - You can list all the tasks (by pid) attached to any cpuset.
136
137The implementation of cpusets requires a few, simple hooks
138into the rest of the kernel, none in performance critical paths:
139
864913f3 140 - in init/main.c, to initialize the root cpuset at system boot.
1da177e4
LT
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.
85d7b949
DG
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
1da177e4
LT
149 - in the mbind and set_mempolicy system calls, to mask the requested
150 Memory Nodes by what's allowed in that tasks cpuset.
864913f3 151 - in page_alloc.c, to restrict memory to allowed nodes.
1da177e4
LT
152 - in vmscan.c, to restrict page recovery to the current cpuset.
153
154In addition a new file system, of type "cpuset" may be mounted,
155typically at /dev/cpuset, to enable browsing and modifying the cpusets
156presently known to the kernel. No new system calls are added for
157cpusets - all support for querying and modifying cpusets is via
158this cpuset file system.
159
160Each task under /proc has an added file named 'cpuset', displaying
161the cpuset name, as the path relative to the root of the cpuset file
162system.
163
164The /proc/<pid>/status file for each task has two added lines,
165displaying the tasks cpus_allowed (on which CPUs it may be scheduled)
166and mems_allowed (on which Memory Nodes it may obtain memory),
167in the format seen in the following example:
168
169 Cpus_allowed: ffffffff,ffffffff,ffffffff,ffffffff
170 Mems_allowed: ffffffff,ffffffff
171
172Each cpuset is represented by a directory in the cpuset file system
173containing the following files describing that cpuset:
174
175 - cpus: list of CPUs in that cpuset
176 - mems: list of Memory Nodes in that cpuset
45b07ef3 177 - memory_migrate flag: if set, move pages to cpusets nodes
1da177e4
LT
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
bd5e09cf 181 - notify_on_release flag: run /sbin/cpuset_release_agent on exit?
bd5e09cf
PJ
182 - memory_pressure: measure of how much paging pressure in cpuset
183
184In addition, the root cpuset only has the following file:
185 - memory_pressure_enabled flag: compute memory_pressure?
1da177e4
LT
186
187New cpusets are created using the mkdir system call or shell
188command. The properties of a cpuset, such as its flags, allowed
189CPUs and Memory Nodes, and attached tasks, are modified by writing
190to the appropriate file in that cpusets directory, as listed above.
191
192The named hierarchical structure of nested cpusets allows partitioning
193a large system into nested, dynamically changeable, "soft-partitions".
194
195The attachment of each task, automatically inherited at fork by any
196children of that task, to a cpuset allows organizing the work load
197on a system into related sets of tasks such that each set is constrained
198to using the CPUs and Memory Nodes of a particular cpuset. A task
199may be re-attached to any other cpuset, if allowed by the permissions
200on the necessary cpuset file system directories.
201
202Such management of a system "in the large" integrates smoothly with
203the detailed placement done on individual tasks and memory regions
204using the sched_setaffinity, mbind and set_mempolicy system calls.
205
206The 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
212These rules, and the natural hierarchy of cpusets, enable efficient
213enforcement of the exclusive guarantee, without having to scan all
214cpusets every time any of them change to ensure nothing overlaps a
215exclusive cpuset. Also, the use of a Linux virtual file system (vfs)
216to represent the cpuset hierarchy provides for a familiar permission
217and name space for cpusets, with a minimum of additional kernel code.
218
bd5e09cf
PJ
219
2201.4 What are exclusive cpusets ?
221--------------------------------
222
223If a cpuset is cpu or mem exclusive, no other cpuset, other than
224a direct ancestor or descendent, may share any of the same CPUs or
225Memory Nodes.
226
227A cpuset that is cpu_exclusive has a scheduler (sched) domain
228associated with it. The sched domain consists of all CPUs in the
229current cpuset that are not part of any exclusive child cpusets.
230This ensures that the scheduler load balancing code only balances
231against the CPUs that are in the sched domain as defined above and
232not all of the CPUs in the system. This removes any overhead due to
233load balancing code trying to pull tasks outside of the cpu_exclusive
234cpuset only to be prevented by the tasks' cpus_allowed mask.
235
236A cpuset that is mem_exclusive restricts kernel allocations for
237page, buffer and other data commonly shared by the kernel across
238multiple users. All cpusets, whether mem_exclusive or not, restrict
239allocations of memory for user space. This enables configuring a
240system so that several independent jobs can share common kernel data,
241such as file system pages, while isolating each jobs user allocation in
242its own cpuset. To do this, construct a large mem_exclusive cpuset to
243hold all the jobs, and construct child, non-mem_exclusive cpusets for
244each individual job. Only a small amount of typical kernel memory,
245such as requests from interrupt handlers, is allowed to be taken
246outside even a mem_exclusive cpuset.
247
248
2491.5 What does notify_on_release do ?
250------------------------------------
251
252If the notify_on_release flag is enabled (1) in a cpuset, then whenever
253the last task in the cpuset leaves (exits or attaches to some other
254cpuset) and the last child cpuset of that cpuset is removed, then
255the kernel runs the command /sbin/cpuset_release_agent, supplying the
256pathname (relative to the mount point of the cpuset file system) of the
257abandoned cpuset. This enables automatic removal of abandoned cpusets.
258The default value of notify_on_release in the root cpuset at system
259boot is disabled (0). The default value of other cpusets at creation
260is the current value of their parents notify_on_release setting.
261
262
90c9cc40 2631.6 What is memory_pressure ?
bd5e09cf
PJ
264-----------------------------
265The memory_pressure of a cpuset provides a simple per-cpuset metric
266of the rate that the tasks in a cpuset are attempting to free up in
267use memory on the nodes of the cpuset to satisfy additional memory
268requests.
269
270This enables batch managers monitoring jobs running in dedicated
271cpusets to efficiently detect what level of memory pressure that job
272is causing.
273
274This is useful both on tightly managed systems running a wide mix of
275submitted jobs, which may choose to terminate or re-prioritize jobs that
276are trying to use more memory than allowed on the nodes assigned them,
277and with tightly coupled, long running, massively parallel scientific
278computing jobs that will dramatically fail to meet required performance
279goals if they start to use more memory than allowed to them.
280
281This mechanism provides a very economical way for the batch manager
282to monitor a cpuset for signs of memory pressure. It's up to the
283batch manager or other user code to decide what to do about it and
284take 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
292Why 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
310A per-cpuset simple digital filter (requires a spinlock and 3 words
311of data per-cpuset) is kept, and updated by any task attached to that
312cpuset, if it enters the synchronous (direct) page reclaim code.
313
314A per-cpuset file provides an integer number representing the recent
315(half-life of 10 seconds) rate of direct page reclaims caused by
316the tasks in the cpuset, in units of reclaims attempted per second,
317times 1000.
318
319
90c9cc40 3201.7 How do I use cpusets ?
1da177e4
LT
321--------------------------
322
323In order to minimize the impact of cpusets on critical kernel
324code, such as the scheduler, and due to the fact that the kernel
325does not support one task updating the memory placement of another
326task directly, the impact on a task of changing its cpuset CPU
327or Memory Node placement, or of changing to which cpuset a task
328is attached, is subtle.
329
330If a cpuset has its Memory Nodes modified, then for each task attached
331to that cpuset, the next time that the kernel attempts to allocate
332a page of memory for that task, the kernel will notice the change
333in the tasks cpuset, and update its per-task memory placement to
334remain within the new cpusets memory placement. If the task was using
335mempolicy MPOL_BIND, and the nodes to which it was bound overlap with
336its new cpuset, then the task will continue to use whatever subset
337of MPOL_BIND nodes are still allowed in the new cpuset. If the task
338was using MPOL_BIND and now none of its MPOL_BIND nodes are allowed
339in the new cpuset, then the task will be essentially treated as if it
340was MPOL_BIND bound to the new cpuset (even though its numa placement,
341as queried by get_mempolicy(), doesn't change). If a task is moved
342from one cpuset to another, then the kernel will adjust the tasks
343memory placement, as above, the next time that the kernel attempts
344to allocate a page of memory for that task.
345
346If a cpuset has its CPUs modified, then each task using that
347cpuset does _not_ change its behavior automatically. In order to
348minimize the impact on the critical scheduling code in the kernel,
349tasks will continue to use their prior CPU placement until they
350are rebound to their cpuset, by rewriting their pid to the 'tasks'
351file of their cpuset. If a task had been bound to some subset of its
352cpuset using the sched_setaffinity() call, and if any of that subset
353is still allowed in its new cpuset settings, then the task will be
354restricted to the intersection of the CPUs it was allowed on before,
355and its new cpuset CPU placement. If, on the other hand, there is
356no overlap between a tasks prior placement and its new cpuset CPU
357placement, then the task will be allowed to run on any CPU allowed
358in its new cpuset. If a task is moved from one cpuset to another,
359its CPU placement is updated in the same way as if the tasks pid is
360rewritten to the 'tasks' file of its current cpuset.
361
362In summary, the memory placement of a task whose cpuset is changed is
363updated by the kernel, on the next allocation of a page for that task,
364but the processor placement is not updated, until that tasks pid is
365rewritten to the 'tasks' file of its cpuset. This is done to avoid
366impacting the scheduler code in the kernel with a check for changes
367in a tasks processor placement.
368
45b07ef3
PJ
369Normally, once a page is allocated (given a physical page
370of main memory) then that page stays on whatever node it
371was allocated, so long as it remains allocated, even if the
372cpusets memory placement policy 'mems' subsequently changes.
373If the cpuset flag file 'memory_migrate' is set true, then when
374tasks are attached to that cpuset, any pages that task had
375allocated to it on nodes in its previous cpuset are migrated
b4fb3766
CL
376to the tasks new cpuset. The relative placement of the page within
377the cpuset is preserved during these migration operations if possible.
378For example if the page was on the second valid node of the prior cpuset
379then the page will be placed on the second valid node of the new cpuset.
380
45b07ef3
PJ
381Also if 'memory_migrate' is set true, then if that cpusets
382'mems' file is modified, pages allocated to tasks in that
383cpuset, that were on nodes in the previous setting of 'mems',
b4fb3766
CL
384will be moved to nodes in the new setting of 'mems.'
385Pages that were not in the tasks prior cpuset, or in the cpusets
386prior 'mems' setting, will not be moved.
45b07ef3 387
d533f671 388There is an exception to the above. If hotplug functionality is used
1da177e4
LT
389to remove all the CPUs that are currently assigned to a cpuset,
390then the kernel will automatically update the cpus_allowed of all
b39c4fab 391tasks attached to CPUs in that cpuset to allow all CPUs. When memory
1da177e4
LT
392hotplug functionality for removing Memory Nodes is available, a
393similar exception is expected to apply there as well. In general,
394the kernel prefers to violate cpuset placement, over starving a task
395that has had all its allowed CPUs or Memory Nodes taken offline. User
396code should reconfigure cpusets to only refer to online CPUs and Memory
397Nodes when using hotplug to add or remove such resources.
398
399There is a second exception to the above. GFP_ATOMIC requests are
400kernel internal allocations that must be satisfied, immediately.
401The kernel may drop some request, in rare cases even panic, if a
402GFP_ATOMIC alloc fails. If the request cannot be satisfied within
403the current tasks cpuset, then we relax the cpuset, and look for
404memory anywhere we can find it. It's better to violate the cpuset
405than stress the kernel.
406
407To 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
418For example, the following sequence of commands will setup a cpuset
419named "Charlie", containing just CPUs 2 and 3, and Memory Node 1,
420and 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
1da177e4
LT
434In the future, a C library interface to cpusets will likely be
435available. For now, the only way to query or modify cpusets is
436via the cpuset file system, using the various cd, mkdir, echo, cat,
437rmdir commands from the shell, or their equivalent from C.
438
439The sched_setaffinity calls can also be done at the shell prompt using
440SGI's runon or Robert Love's taskset. The mbind and set_mempolicy
441calls can be done at the shell prompt using the numactl command
442(part of Andi Kleen's numa package).
443
4442. Usage Examples and Syntax
445============================
446
4472.1 Basic Usage
448---------------
449
450Creating, modifying, using the cpusets can be done through the cpuset
451virtual filesystem.
452
453To mount it, type:
454# mount -t cpuset none /dev/cpuset
455
456Then under /dev/cpuset you can find a tree that corresponds to the
457tree of the cpusets in the system. For instance, /dev/cpuset
458is the cpuset that holds the whole system.
459
460If you want to create a new cpuset under /dev/cpuset:
461# cd /dev/cpuset
462# mkdir my_cpuset
463
464Now you want to do something with this cpuset.
465# cd my_cpuset
466
467In this directory you can find several files:
468# ls
469cpus cpu_exclusive mems mem_exclusive tasks
470
471Reading them will give you information about the state of this cpuset:
472the CPUs and Memory Nodes it can use, the processes that are using
473it, its properties. By writing to these files you can manipulate
474the cpuset.
475
476Set some flags:
477# /bin/echo 1 > cpu_exclusive
478
479Add some cpus:
480# /bin/echo 0-7 > cpus
481
482Now attach your shell to this cpuset:
483# /bin/echo $$ > tasks
484
485You can also create cpusets inside your cpuset by using mkdir in this
486directory.
487# mkdir my_sub_cs
488
489To remove a cpuset, just use rmdir:
490# rmdir my_sub_cs
491This will fail if the cpuset is in use (has cpusets inside, or has
492processes attached).
493
4942.2 Adding/removing cpus
495------------------------
496
497This is the syntax to use when writing in the cpus or mems files
498in 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
5032.3 Setting flags
504-----------------
505
506The 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
5112.4 Attaching processes
512-----------------------
513
514# /bin/echo PID > tasks
515
516Note that it is PID, not PIDs. You can only attach ONE task at a time.
517If 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
5253. Questions
526============
527
528Q: what's up with this '/bin/echo' ?
529A: 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
533Q: When I attach processes, only the first of the line gets really attached !
534A: We can only return one error code per call to write(). So you should also
535 put only ONE pid.
536
5374. Contact
538==========
539
540Web: http://www.bullopensource.org/cpuset