1 .\" Copyright (c) 2008 Christian Brueffer
2 .\" Copyright (c) 2008 Jeffrey Roberson
3 .\" Copyright (c) 2021 Robert N. M. Watson
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .Nd manage CPU affinity sets
41 .Fn cpuset "cpusetid_t *setid"
43 .Fn cpuset_setid "cpuwhich_t which" "id_t id" "cpusetid_t setid"
45 .Fn cpuset_getid "cpulevel_t level" "cpuwhich_t which" "id_t id" "cpusetid_t *setid"
49 family of system calls allow applications to control sets of processors and
50 memory domains and assign processes and threads to these sets.
51 Processor sets contain lists of CPUs and domains that members may run on
52 and exist only as long as some process is a member of the set.
53 All processes in the system have an assigned set.
54 The default set for all processes in the system is the set numbered 1.
55 Threads belong to the same set as the process which contains them,
56 however, they may further restrict their set with the anonymous
57 per-thread mask to bind to a specific CPU or subset of CPUs and memory domains.
59 Sets are referenced by a number of type
61 Each thread has a root set, an assigned set, and an anonymous mask.
62 Only the root and assigned sets are numbered.
63 The root set is the set of all CPUs and memory domains available in the system
64 or in the system partition the thread is running in.
65 The assigned set is a subset of the root set and is administratively
66 assignable on a per-process basis.
67 Many processes and threads may be members of a numbered set.
69 The anonymous set is a further thread-specific refinement on the assigned
71 It is intended that administrators will manipulate numbered sets using
73 while application developers will manipulate anonymous sets using
74 .Xr cpuset_setaffinity 2 and
75 .Xr cpuset_setdomain 2 .
77 To select the correct set a value of type
80 The following values for
83 .Bl -column CPU_LEVEL_CPUSET -offset indent
84 .It Dv CPU_LEVEL_ROOT Ta "Root set"
85 .It Dv CPU_LEVEL_CPUSET Ta "Assigned set"
86 .It Dv CPU_LEVEL_WHICH Ta "Set specified by which argument"
91 argument determines how the value of
93 is interpreted and is of type
97 argument may have the following values:
98 .Bl -column CPU_WHICH_INTRHANDLER -offset indent
99 .It Dv CPU_WHICH_TID Ta "id is lwpid_t (thread id)"
100 .It Dv CPU_WHICH_PID Ta "id is pid_t (process id)"
101 .It Dv CPU_WHICH_TIDPID Ta "id is either a thread or process id"
102 .It Dv CPU_WHICH_JAIL Ta "id is jid (jail id)"
103 .It Dv CPU_WHICH_CPUSET Ta "id is a cpusetid_t (cpuset id)"
104 .It Dv CPU_WHICH_IRQ Ta "id is an irq number"
105 .It Dv CPU_WHICH_INTRHANDLER Ta "id is an irq number for an interrupt handler"
106 .It Dv CPU_WHICH_ITHREAD Ta "id is an irq number for an ithread"
107 .It Dv CPU_WHICH_DOMAIN Ta "id is a NUMA domain"
112 of '-1' may be used with a
117 .Dv CPU_WHICH_TIDPID ,
120 to mean the current thread, process, or current thread's
122 All cpuset syscalls allow this usage.
132 refers to the anonymous mask of the object.
133 This mask does not have an id and may only be manipulated with
134 .Xr cpuset_setaffinity 2 .
137 creates a new set containing the same CPUs as the root set of the current
138 process and stores its id in the space provided by
140 On successful completion the calling process joins the set and is the
142 Children inherit this set after a call to
146 attempts to set the id of the object specified by the
151 is the only acceptable value for which as
152 threads do not have an id distinct from their process and the API does
153 not permit changing the id of an existing set.
154 Upon successful completion all of the threads in the target process will
155 be running on CPUs permitted by the set.
158 retrieves a set id from the object indicated by
160 and stores it in the space pointed to by
162 The retrieved id may be that of either the root or assigned set
163 depending on the value of
170 to get the set id from
171 the process or thread specified by the
176 with a process or thread is unsupported since
177 this references the unnumbered anonymous mask.
179 The actual contents of the sets may be retrieved or manipulated using
180 .Xr cpuset_getaffinity 2 ,
181 .Xr cpuset_setaffinity 2 ,
182 .Xr cpuset_getdomain 2 , and
183 .Xr cpuset_setdomain 2 .
186 macros may be used to manipulate masks of type
188 get and set using those APIs.
189 See those manual pages for more detail.
193 In this example, a CPU set mask is configured to limit execution to the first
200 programming interface.
201 Then, the mask is applied to a new anonymous CPU set associated with the
202 current process using
203 .Xr cpuset_setaffinity 2 .
204 This mask will be used by the current process, and inherited by any new
206 .Bd -literal -offset indent
207 #include <sys/param.h>
208 #include <sys/cpuset.h>
210 #include <sysexits.h>
212 cpuset_t cpuset_mask;
214 /* Initialize a CPU mask and enable CPU 0. */
215 CPU_ZERO(&cpuset_mask);
216 CPU_SET(0, &cpuset_mask);
218 /* Set affinity for the CPU set for the current process. */
219 if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
220 sizeof(cpuset_mask), &cpuset_mask) < 0)
221 err(EX_OSERR, "cpuset_setaffinity");
224 In the next example, a named CPU set is created containing the current
225 process, and its affinity similarly configured.
226 The resulting CPU set ID can then be used for further external management of
227 the affinity of the set.
228 .Bd -literal -offset indent
229 #include <sys/param.h>
230 #include <sys/cpuset.h>
232 #include <sysexits.h>
234 cpusetid_t cpuset_id;
235 cpuset_t cpuset_mask;
237 /* Create new cpuset for the current process. */
238 if (cpuset(&cpuset_id) < 0)
239 err(EX_OSERR, "cpuset");
241 /* Initialize a CPU mask and enable CPU 0. */
242 CPU_ZERO(&cpuset_mask);
243 CPU_SET(0, &cpuset_mask);
245 /* Set affinity for the CPU set for the current process. */
246 if (cpuset_setaffinity(CPU_LEVEL_SET, CPU_WHICH_CPUSET, cpuset_id,
247 sizeof(cpuset_mask), &cpuset_mask) < 0)
248 err(EX_OSERR, "cpuset_setaffinity");
251 The following error codes may be set in
259 argument was not a valid value.
263 call would leave a thread without a valid CPU to run on because the set
264 does not overlap with the thread's anonymous mask.
266 The setid pointer passed to
272 The object specified by the
276 arguments could not be found.
278 The calling process did not have the credentials required to complete the
287 .Xr cpuset_getaffinity 2 ,
288 .Xr cpuset_getdomain 2 ,
289 .Xr cpuset_setaffinity 2 ,
290 .Xr cpuset_setdomain 2 ,
291 .Xr pthread_affinity_np 3 ,
292 .Xr pthread_attr_affinity_np 3 ,
299 family of system calls first appeared in
302 .An Jeffrey Roberson Aq Mt jeff@FreeBSD.org