xref: /dflybsd-src/lib/libc/sys/sched_setaffinity.2 (revision 0d9899e72d0ab73653a47790ac79317a20a1601c)
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 sched_setaffinity,
38.Nm sched_getaffinity
39.Nd set and get a process's CPU affinity mask
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In sys/types.h
44.In sys/sched.h
45.Ft int
46.Fn sched_setaffinity "pid_t pid" "size_t cpusetsize" "const cpu_set_t *mask"
47.Ft int
48.Fn sched_getaffinity "pid_t pid" "size_t cpusetsize" "cpu_set_t *mask"
49.Fn CPU_ZERO &mask
50.Fn CPU_SET cpu &mask
51.Fn CPU_CLR cpu &mask
52.Fn CPU_ISSET cpu &mask
53.Fn CPU_COUNT &mask
54.Fn CPU_AND &dstmask &srcmask1 &srcmask2
55.Fn CPU_OR &dstmask &srcmask1 srcmask2
56.Fn CPU_XOR &dstmask &srcmask1 &srcmask2
57.Fn CPU_EQUAL &mask1 &mask2
58.Sh DESCRIPTION
59.Pp
60The
61.Fn sched_setaffinity
62sets the CPU affinity mask of the process,
63which is identified by the
64.Fa pid
65to the value specified by the
66.Fa mask .
67The
68.Fa cpusetsize
69is the length (in bytes) of the data pointed to by
70.Fa mask .
71Normally this argument would be specified as sizeof(cpu_set_t).
72If the
73.Fa pid
74is zero,
75then the pid of the calling process is used.
76All threads of the process identified by
77.Fa pid
78are affected.
79If the calling thread is one of the target process's threads,
80then the calling thread will be migrated immediately,
81if necessary,
82while other threads of the targt process will be migrated
83by the scheduler later.
84.Pp
85A process created by
86.Xr fork 2 ,
87.Xr vfork 2 ,
88and
89.Xr rfork 2
90inherits the calling thread's CPU affinity mask.
91Same applies to the threads created by
92.Xr lwp_create 2 ,
93and the threads created by
94.Xr pthread_create 3 .
95.Pp
96.Fn sched_getaffinity
97writes the affinity mask of the process
98identified by the
99.Fa pid
100to the cpu_set_t structure pointed to by
101.Fa mask .
102If the
103.Fa pid
104is zero,
105then the pid of the calling process is used.
106If the process is multithreaded,
107then the CPU affinity mask of the first thread will be returned.
108.Pp
109The cpu_set_t data structure represents a set of CPUs.
110The first available CPU on the system corresponds to a cpu value of 0,
111the next CPU corresponds to a cpu value of 1,
112and so on and so forth.
113The constant
114.Va CPU_SETSIZE
115specifies a value one greater than the maximum
116CPU number that can be stored in cpu_set_t.
117The cpu_set_t should be treated as opaque data structure;
118all manipulation of cpu_set_t should be done via the following macros:
119.Bl -tag -width "CPU_COUNTxxx"
120.It Fn CPU_ZERO
121Reset the
122.Fa mask ,
123so that it contains no CPUs.
124.It Fn CPU_SET
125Add the
126.Fa cpu
127to the
128.Fa mask .
129.It Fn CPU_CLR
130Remove the
131.Fa cpu
132from the
133.Fa mask .
134.It Fn CPU_ISSET
135Test to see whether the
136.Fa cpu
137is a member of the
138.Fa mask .
139.It Fn CPU_COUNT
140Return the number of CPUs in the
141.Fa mask .
142.It Fn CPU_AND
143Store the intersection of the
144.Fa srcmask1
145and the
146.Fa srcmask2
147in the
148.Fa dstmask .
149The
150.Fa dstmask
151could be one of the source masks.
152.It Fn CPU_OR
153Store the union of the
154.Fa srcmask1
155and the
156.Fa srcmask2
157in the
158.Fa dstmask .
159The
160.Fa dstmask
161could be one of the source masks.
162.It Fn CPU_XOR
163Store the XOR of the
164.Fa srcmask1
165and the
166.Fa srcmask2
167in the
168.Fa dstmask .
169The
170.Fa dstmask
171could be one of the source masks.
172.It Fn CPU_EQUAL
173Test whether the
174.Fa mask1
175and the
176.Fa mask2
177contain the same CPUs.
178.El
179.Sh RETURN VALUES
180These system calls return \-1 on error and
1810 upon successful completion.
182.Sh ERRORS
183The
184.Fn sched_setaffinity
185and
186.Fn sched_getaffinity
187will fail if:
188.Bl -tag -width Er
189.It Bq Er EFAULT
190The
191.Fa mask
192parameter is outside the process's allocated address space.
193.It Bq Er EINVAL
194The
195.Fa pid
196parameter is negative.
197.It Bq Er ESRCH
198The process identified by the
199.Fa pid
200does not exist.
201.It Bq Er EPERM
202The
203.Fa mask
204does not contain a valid CPU.
205.El
206.Sh SEE ALSO
207.Xr fork 2
208.Xr lwp_create 2
209.Xr lwp_getaffinity 2
210.Xr lwp_setaffinity 2
211.Xr pthread_create 3
212.Xr pthread_getaffinity_np 3
213.Xr pthread_setaffinity_np 3
214.Xr rfork 2
215.Xr vfork 2
216.Sh HISTORY
217The
218.Fn sched_setaffinity
219and
220.Fn sched_getaffinity
221functions first appeared in
222.Dx 4.7 .
223