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