xref: /dragonfly/lib/libc/sys/sched_setaffinity.2 (revision b8c93cad)
1.\" Copyright (c) 2017 The DragonFly Project.  All rights reserved.
2.\"
3.\" This code is derived from software contributed to The DragonFly Project
4.\" by Sepherosa Ziehau <sepherosa@gmail.com>.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\"
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in
14.\"    the documentation and/or other materials provided with the
15.\"    distribution.
16.\" 3. Neither the name of The DragonFly Project nor the names of its
17.\"    contributors may be used to endorse or promote products derived
18.\"    from this software without specific, prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.Dd January 14, 2017
34.Dt SCHED_SETAFFINITY 2
35.Os
36.Sh NAME
37.Nm CPU_AND ,
38.Nm CPU_CLR ,
39.Nm CPU_COUNT ,
40.Nm CPU_EQUAL ,
41.Nm CPU_ISSET ,
42.Nm CPU_OR ,
43.Nm CPU_SET ,
44.Nm CPU_XOR ,
45.Nm CPU_ZERO ,
46.Nm sched_setaffinity ,
47.Nm sched_getaffinity
48.Nd set and get a process's CPU affinity mask
49.Sh LIBRARY
50.Lb libc
51.Sh SYNOPSIS
52.In sys/types.h
53.In sys/sched.h
54.Ft int
55.Fn sched_setaffinity "pid_t pid" "size_t cpusetsize" "const cpu_set_t *mask"
56.Ft int
57.Fn sched_getaffinity "pid_t pid" "size_t cpusetsize" "cpu_set_t *mask"
58.Fn CPU_ZERO &mask
59.Fn CPU_SET cpu &mask
60.Fn CPU_CLR cpu &mask
61.Fn CPU_ISSET cpu &mask
62.Fn CPU_COUNT &mask
63.Fn CPU_AND &dstmask &srcmask1 &srcmask2
64.Fn CPU_OR &dstmask &srcmask1 srcmask2
65.Fn CPU_XOR &dstmask &srcmask1 &srcmask2
66.Fn CPU_EQUAL &mask1 &mask2
67.Sh DESCRIPTION
68.Pp
69The
70.Fn sched_setaffinity
71sets the CPU affinity mask of the process,
72which is identified by the
73.Fa pid
74to the value specified by the
75.Fa mask .
76The
77.Fa cpusetsize
78is the length (in bytes) of the data pointed to by
79.Fa mask .
80Normally this argument would be specified as sizeof(cpu_set_t).
81If the
82.Fa pid
83is zero,
84then the pid of the calling process is used.
85All threads of the process identified by
86.Fa pid
87are affected.
88If the calling thread is one of the target process's threads,
89then the calling thread will be migrated immediately,
90if necessary,
91while other threads of the targt process will be migrated
92by the scheduler later.
93.Pp
94A process created by
95.Xr fork 2 ,
96.Xr vfork 2 ,
97and
98.Xr rfork 2
99inherits the calling thread's CPU affinity mask.
100Same applies to the threads created by
101.Xr lwp_create 2 ,
102and the threads created by
103.Xr pthread_create 3 .
104.Pp
105.Fn sched_getaffinity
106writes the affinity mask of the process
107identified by the
108.Fa pid
109to the cpu_set_t structure pointed to by
110.Fa mask .
111If the
112.Fa pid
113is zero,
114then the pid of the calling process is used.
115If the process is multithreaded,
116then the CPU affinity mask of the first thread will be returned.
117.Pp
118The cpu_set_t data structure represents a set of CPUs.
119The first available CPU on the system corresponds to a cpu value of 0,
120the next CPU corresponds to a cpu value of 1,
121and so on and so forth.
122The constant
123.Dv CPU_SETSIZE
124specifies a value one greater than the maximum
125CPU number that can be stored in cpu_set_t.
126The cpu_set_t should be treated as opaque data structure;
127all manipulation of cpu_set_t should be done via the following macros:
128.Bl -tag -width "CPU_COUNTxxx"
129.It Fn CPU_ZERO
130Reset the
131.Fa mask ,
132so that it contains no CPUs.
133.It Fn CPU_SET
134Add the
135.Fa cpu
136to the
137.Fa mask .
138.It Fn CPU_CLR
139Remove the
140.Fa cpu
141from the
142.Fa mask .
143.It Fn CPU_ISSET
144Test to see whether the
145.Fa cpu
146is a member of the
147.Fa mask .
148.It Fn CPU_COUNT
149Return the number of CPUs in the
150.Fa mask .
151.It Fn CPU_AND
152Store the intersection of the
153.Fa srcmask1
154and the
155.Fa srcmask2
156in the
157.Fa dstmask .
158The
159.Fa dstmask
160could be one of the source masks.
161.It Fn CPU_OR
162Store the union of the
163.Fa srcmask1
164and the
165.Fa srcmask2
166in the
167.Fa dstmask .
168The
169.Fa dstmask
170could be one of the source masks.
171.It Fn CPU_XOR
172Store the XOR of the
173.Fa srcmask1
174and the
175.Fa srcmask2
176in the
177.Fa dstmask .
178The
179.Fa dstmask
180could be one of the source masks.
181.It Fn CPU_EQUAL
182Test whether the
183.Fa mask1
184and the
185.Fa mask2
186contain the same CPUs.
187.El
188.Sh RETURN VALUES
189These system calls return \-1 on error and
1900 upon successful completion.
191.Sh ERRORS
192The
193.Fn sched_setaffinity
194and
195.Fn sched_getaffinity
196will fail if:
197.Bl -tag -width Er
198.It Bq Er EFAULT
199The
200.Fa mask
201parameter is outside the process's allocated address space.
202.It Bq Er EINVAL
203The
204.Fa pid
205parameter is negative.
206.It Bq Er ESRCH
207The process identified by the
208.Fa pid
209does not exist.
210.It Bq Er EPERM
211The
212.Fa mask
213does not contain a valid CPU.
214.El
215.Sh SEE ALSO
216.Xr fork 2
217.Xr lwp_create 2
218.Xr lwp_getaffinity 2
219.Xr lwp_setaffinity 2
220.Xr pthread_create 3
221.Xr pthread_getaffinity_np 3
222.Xr pthread_setaffinity_np 3
223.Xr rfork 2
224.Xr vfork 2
225.Sh HISTORY
226The
227.Fn sched_setaffinity
228and
229.Fn sched_getaffinity
230functions first appeared in
231.Dx 4.7 .
232