blob: ad944c06031294cc35265d82ecf2a8fcc12586de [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 ?
17 1.4 How do I use cpusets ?
182. Usage Examples and Syntax
19 2.1 Basic Usage
20 2.2 Adding/removing cpus
21 2.3 Setting flags
22 2.4 Attaching processes
233. Questions
244. Contact
25
261. Cpusets
27==========
28
291.1 What are cpusets ?
30----------------------
31
32Cpusets provide a mechanism for assigning a set of CPUs and Memory
33Nodes to a set of tasks.
34
35Cpusets constrain the CPU and Memory placement of tasks to only
36the resources within a tasks current cpuset. They form a nested
37hierarchy visible in a virtual file system. These are the essential
38hooks, beyond what is already present, required to manage dynamic
39job placement on large systems.
40
41Each task has a pointer to a cpuset. Multiple tasks may reference
42the same cpuset. Requests by a task, using the sched_setaffinity(2)
43system call to include CPUs in its CPU affinity mask, and using the
44mbind(2) and set_mempolicy(2) system calls to include Memory Nodes
45in its memory policy, are both filtered through that tasks cpuset,
46filtering out any CPUs or Memory Nodes not in that cpuset. The
47scheduler will not schedule a task on a CPU that is not allowed in
48its cpus_allowed vector, and the kernel page allocator will not
49allocate a page on a node that is not allowed in the requesting tasks
50mems_allowed vector.
51
52If a cpuset is cpu or mem exclusive, no other cpuset, other than a direct
53ancestor or descendent, may share any of the same CPUs or Memory Nodes.
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -070054A cpuset that is cpu exclusive has a sched domain associated with it.
55The sched domain consists of all cpus in the current cpuset that are not
56part of any exclusive child cpusets.
57This ensures that the scheduler load balacing code only balances
58against the cpus that are in the sched domain as defined above and not
59all of the cpus in the system. This removes any overhead due to
60load balancing code trying to pull tasks outside of the cpu exclusive
61cpuset only to be prevented by the tasks' cpus_allowed mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63User level code may create and destroy cpusets by name in the cpuset
64virtual file system, manage the attributes and permissions of these
65cpusets and which CPUs and Memory Nodes are assigned to each cpuset,
66specify and query to which cpuset a task is assigned, and list the
67task pids assigned to a cpuset.
68
69
701.2 Why are cpusets needed ?
71----------------------------
72
73The management of large computer systems, with many processors (CPUs),
74complex memory cache hierarchies and multiple Memory Nodes having
75non-uniform access times (NUMA) presents additional challenges for
76the efficient scheduling and memory placement of processes.
77
78Frequently more modest sized systems can be operated with adequate
79efficiency just by letting the operating system automatically share
80the available CPU and Memory resources amongst the requesting tasks.
81
82But larger systems, which benefit more from careful processor and
83memory placement to reduce memory access times and contention,
84and which typically represent a larger investment for the customer,
85can benefit from explictly placing jobs on properly sized subsets of
86the system.
87
88This can be especially valuable on:
89
90 * Web Servers running multiple instances of the same web application,
91 * Servers running different applications (for instance, a web server
92 and a database), or
93 * NUMA systems running large HPC applications with demanding
94 performance characteristics.
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -070095 * Also cpu_exclusive cpusets are useful for servers running orthogonal
96 workloads such as RT applications requiring low latency and HPC
97 applications that are throughput sensitive
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99These subsets, or "soft partitions" must be able to be dynamically
100adjusted, as the job mix changes, without impacting other concurrently
101executing jobs.
102
103The kernel cpuset patch provides the minimum essential kernel
104mechanisms required to efficiently implement such subsets. It
105leverages existing CPU and Memory Placement facilities in the Linux
106kernel to avoid any additional impact on the critical scheduler or
107memory allocator code.
108
109
1101.3 How are cpusets implemented ?
111---------------------------------
112
113Cpusets provide a Linux kernel (2.6.7 and above) mechanism to constrain
114which CPUs and Memory Nodes are used by a process or set of processes.
115
116The Linux kernel already has a pair of mechanisms to specify on which
117CPUs a task may be scheduled (sched_setaffinity) and on which Memory
118Nodes it may obtain memory (mbind, set_mempolicy).
119
120Cpusets extends these two mechanisms as follows:
121
122 - Cpusets are sets of allowed CPUs and Memory Nodes, known to the
123 kernel.
124 - Each task in the system is attached to a cpuset, via a pointer
125 in the task structure to a reference counted cpuset structure.
126 - Calls to sched_setaffinity are filtered to just those CPUs
127 allowed in that tasks cpuset.
128 - Calls to mbind and set_mempolicy are filtered to just
129 those Memory Nodes allowed in that tasks cpuset.
130 - The root cpuset contains all the systems CPUs and Memory
131 Nodes.
132 - For any cpuset, one can define child cpusets containing a subset
133 of the parents CPU and Memory Node resources.
134 - The hierarchy of cpusets can be mounted at /dev/cpuset, for
135 browsing and manipulation from user space.
136 - A cpuset may be marked exclusive, which ensures that no other
137 cpuset (except direct ancestors and descendents) may contain
138 any overlapping CPUs or Memory Nodes.
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700139 Also a cpu_exclusive cpuset would be associated with a sched
140 domain.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 - You can list all the tasks (by pid) attached to any cpuset.
142
143The implementation of cpusets requires a few, simple hooks
144into the rest of the kernel, none in performance critical paths:
145
146 - in main/init.c, to initialize the root cpuset at system boot.
147 - in fork and exit, to attach and detach a task from its cpuset.
148 - in sched_setaffinity, to mask the requested CPUs by what's
149 allowed in that tasks cpuset.
150 - in sched.c migrate_all_tasks(), to keep migrating tasks within
151 the CPUs allowed by their cpuset, if possible.
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700152 - in sched.c, a new API partition_sched_domains for handling
153 sched domain changes associated with cpu_exclusive cpusets
154 and related changes in both sched.c and arch/ia64/kernel/domain.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 - in the mbind and set_mempolicy system calls, to mask the requested
156 Memory Nodes by what's allowed in that tasks cpuset.
157 - in page_alloc, to restrict memory to allowed nodes.
158 - in vmscan.c, to restrict page recovery to the current cpuset.
159
160In addition a new file system, of type "cpuset" may be mounted,
161typically at /dev/cpuset, to enable browsing and modifying the cpusets
162presently known to the kernel. No new system calls are added for
163cpusets - all support for querying and modifying cpusets is via
164this cpuset file system.
165
166Each task under /proc has an added file named 'cpuset', displaying
167the cpuset name, as the path relative to the root of the cpuset file
168system.
169
170The /proc/<pid>/status file for each task has two added lines,
171displaying the tasks cpus_allowed (on which CPUs it may be scheduled)
172and mems_allowed (on which Memory Nodes it may obtain memory),
173in the format seen in the following example:
174
175 Cpus_allowed: ffffffff,ffffffff,ffffffff,ffffffff
176 Mems_allowed: ffffffff,ffffffff
177
178Each cpuset is represented by a directory in the cpuset file system
179containing the following files describing that cpuset:
180
181 - cpus: list of CPUs in that cpuset
182 - mems: list of Memory Nodes in that cpuset
183 - cpu_exclusive flag: is cpu placement exclusive?
184 - mem_exclusive flag: is memory placement exclusive?
185 - tasks: list of tasks (by pid) attached to that cpuset
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
2191.4 How do I use cpusets ?
220--------------------------
221
222In order to minimize the impact of cpusets on critical kernel
223code, such as the scheduler, and due to the fact that the kernel
224does not support one task updating the memory placement of another
225task directly, the impact on a task of changing its cpuset CPU
226or Memory Node placement, or of changing to which cpuset a task
227is attached, is subtle.
228
229If a cpuset has its Memory Nodes modified, then for each task attached
230to that cpuset, the next time that the kernel attempts to allocate
231a page of memory for that task, the kernel will notice the change
232in the tasks cpuset, and update its per-task memory placement to
233remain within the new cpusets memory placement. If the task was using
234mempolicy MPOL_BIND, and the nodes to which it was bound overlap with
235its new cpuset, then the task will continue to use whatever subset
236of MPOL_BIND nodes are still allowed in the new cpuset. If the task
237was using MPOL_BIND and now none of its MPOL_BIND nodes are allowed
238in the new cpuset, then the task will be essentially treated as if it
239was MPOL_BIND bound to the new cpuset (even though its numa placement,
240as queried by get_mempolicy(), doesn't change). If a task is moved
241from one cpuset to another, then the kernel will adjust the tasks
242memory placement, as above, the next time that the kernel attempts
243to allocate a page of memory for that task.
244
245If a cpuset has its CPUs modified, then each task using that
246cpuset does _not_ change its behavior automatically. In order to
247minimize the impact on the critical scheduling code in the kernel,
248tasks will continue to use their prior CPU placement until they
249are rebound to their cpuset, by rewriting their pid to the 'tasks'
250file of their cpuset. If a task had been bound to some subset of its
251cpuset using the sched_setaffinity() call, and if any of that subset
252is still allowed in its new cpuset settings, then the task will be
253restricted to the intersection of the CPUs it was allowed on before,
254and its new cpuset CPU placement. If, on the other hand, there is
255no overlap between a tasks prior placement and its new cpuset CPU
256placement, then the task will be allowed to run on any CPU allowed
257in its new cpuset. If a task is moved from one cpuset to another,
258its CPU placement is updated in the same way as if the tasks pid is
259rewritten to the 'tasks' file of its current cpuset.
260
261In summary, the memory placement of a task whose cpuset is changed is
262updated by the kernel, on the next allocation of a page for that task,
263but the processor placement is not updated, until that tasks pid is
264rewritten to the 'tasks' file of its cpuset. This is done to avoid
265impacting the scheduler code in the kernel with a check for changes
266in a tasks processor placement.
267
268There is an exception to the above. If hotplug funtionality is used
269to remove all the CPUs that are currently assigned to a cpuset,
270then the kernel will automatically update the cpus_allowed of all
Paul Jacksonb39c4fa2005-05-20 13:59:15 -0700271tasks attached to CPUs in that cpuset to allow all CPUs. When memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272hotplug functionality for removing Memory Nodes is available, a
273similar exception is expected to apply there as well. In general,
274the kernel prefers to violate cpuset placement, over starving a task
275that has had all its allowed CPUs or Memory Nodes taken offline. User
276code should reconfigure cpusets to only refer to online CPUs and Memory
277Nodes when using hotplug to add or remove such resources.
278
279There is a second exception to the above. GFP_ATOMIC requests are
280kernel internal allocations that must be satisfied, immediately.
281The kernel may drop some request, in rare cases even panic, if a
282GFP_ATOMIC alloc fails. If the request cannot be satisfied within
283the current tasks cpuset, then we relax the cpuset, and look for
284memory anywhere we can find it. It's better to violate the cpuset
285than stress the kernel.
286
287To start a new job that is to be contained within a cpuset, the steps are:
288
289 1) mkdir /dev/cpuset
290 2) mount -t cpuset none /dev/cpuset
291 3) Create the new cpuset by doing mkdir's and write's (or echo's) in
292 the /dev/cpuset virtual file system.
293 4) Start a task that will be the "founding father" of the new job.
294 5) Attach that task to the new cpuset by writing its pid to the
295 /dev/cpuset tasks file for that cpuset.
296 6) fork, exec or clone the job tasks from this founding father task.
297
298For example, the following sequence of commands will setup a cpuset
299named "Charlie", containing just CPUs 2 and 3, and Memory Node 1,
300and then start a subshell 'sh' in that cpuset:
301
302 mount -t cpuset none /dev/cpuset
303 cd /dev/cpuset
304 mkdir Charlie
305 cd Charlie
306 /bin/echo 2-3 > cpus
307 /bin/echo 1 > mems
308 /bin/echo $$ > tasks
309 sh
310 # The subshell 'sh' is now running in cpuset Charlie
311 # The next line should display '/Charlie'
312 cat /proc/self/cpuset
313
314In the case that a change of cpuset includes wanting to move already
315allocated memory pages, consider further the work of IWAMOTO
316Toshihiro <iwamoto@valinux.co.jp> for page remapping and memory
317hotremoval, which can be found at:
318
319 http://people.valinux.co.jp/~iwamoto/mh.html
320
321The integration of cpusets with such memory migration is not yet
322available.
323
324In the future, a C library interface to cpusets will likely be
325available. For now, the only way to query or modify cpusets is
326via the cpuset file system, using the various cd, mkdir, echo, cat,
327rmdir commands from the shell, or their equivalent from C.
328
329The sched_setaffinity calls can also be done at the shell prompt using
330SGI's runon or Robert Love's taskset. The mbind and set_mempolicy
331calls can be done at the shell prompt using the numactl command
332(part of Andi Kleen's numa package).
333
3342. Usage Examples and Syntax
335============================
336
3372.1 Basic Usage
338---------------
339
340Creating, modifying, using the cpusets can be done through the cpuset
341virtual filesystem.
342
343To mount it, type:
344# mount -t cpuset none /dev/cpuset
345
346Then under /dev/cpuset you can find a tree that corresponds to the
347tree of the cpusets in the system. For instance, /dev/cpuset
348is the cpuset that holds the whole system.
349
350If you want to create a new cpuset under /dev/cpuset:
351# cd /dev/cpuset
352# mkdir my_cpuset
353
354Now you want to do something with this cpuset.
355# cd my_cpuset
356
357In this directory you can find several files:
358# ls
359cpus cpu_exclusive mems mem_exclusive tasks
360
361Reading them will give you information about the state of this cpuset:
362the CPUs and Memory Nodes it can use, the processes that are using
363it, its properties. By writing to these files you can manipulate
364the cpuset.
365
366Set some flags:
367# /bin/echo 1 > cpu_exclusive
368
369Add some cpus:
370# /bin/echo 0-7 > cpus
371
372Now attach your shell to this cpuset:
373# /bin/echo $$ > tasks
374
375You can also create cpusets inside your cpuset by using mkdir in this
376directory.
377# mkdir my_sub_cs
378
379To remove a cpuset, just use rmdir:
380# rmdir my_sub_cs
381This will fail if the cpuset is in use (has cpusets inside, or has
382processes attached).
383
3842.2 Adding/removing cpus
385------------------------
386
387This is the syntax to use when writing in the cpus or mems files
388in cpuset directories:
389
390# /bin/echo 1-4 > cpus -> set cpus list to cpus 1,2,3,4
391# /bin/echo 1,2,3,4 > cpus -> set cpus list to cpus 1,2,3,4
392
3932.3 Setting flags
394-----------------
395
396The syntax is very simple:
397
398# /bin/echo 1 > cpu_exclusive -> set flag 'cpu_exclusive'
399# /bin/echo 0 > cpu_exclusive -> unset flag 'cpu_exclusive'
400
4012.4 Attaching processes
402-----------------------
403
404# /bin/echo PID > tasks
405
406Note that it is PID, not PIDs. You can only attach ONE task at a time.
407If you have several tasks to attach, you have to do it one after another:
408
409# /bin/echo PID1 > tasks
410# /bin/echo PID2 > tasks
411 ...
412# /bin/echo PIDn > tasks
413
414
4153. Questions
416============
417
418Q: what's up with this '/bin/echo' ?
419A: bash's builtin 'echo' command does not check calls to write() against
420 errors. If you use it in the cpuset file system, you won't be
421 able to tell whether a command succeeded or failed.
422
423Q: When I attach processes, only the first of the line gets really attached !
424A: We can only return one error code per call to write(). So you should also
425 put only ONE pid.
426
4274. Contact
428==========
429
430Web: http://www.bullopensource.org/cpuset