1.\" Copyright (c) 2008 Christian Brueffer 2.\" Copyright (c) 2008 Jeffrey Roberson 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.\" $FreeBSD$ 27.\" 28.Dd April 27, 2022 29.Dt CPUSET_GETAFFINITY 2 30.Os 31.Sh NAME 32.Nm cpuset_getaffinity , 33.Nm cpuset_setaffinity 34.Nd manage CPU affinity 35.Sh LIBRARY 36.Lb libc 37.Sh SYNOPSIS 38.In sys/param.h 39.In sys/cpuset.h 40.Ft int 41.Fn cpuset_getaffinity "cpulevel_t level" "cpuwhich_t which" "id_t id" "size_t setsize" "cpuset_t *mask" 42.Ft int 43.Fn cpuset_setaffinity "cpulevel_t level" "cpuwhich_t which" "id_t id" "size_t setsize" "const cpuset_t *mask" 44.Sh DESCRIPTION 45.Fn cpuset_getaffinity 46and 47.Fn cpuset_setaffinity 48allow the manipulation of sets of CPUs available to processes, threads, 49interrupts, jails and other resources. 50These functions may manipulate sets of CPUs that contain many processes 51or per-object anonymous masks that effect only a single object. 52.Pp 53The valid values for the 54.Fa level 55and 56.Fa which 57arguments are documented in 58.Xr cpuset 2 . 59These arguments specify which object and which set of the object we are 60referring to. 61Not all possible combinations are valid. 62For example, only processes may belong to a numbered set accessed by a 63.Fa level 64argument of 65.Dv CPU_LEVEL_CPUSET . 66All resources, however, have a mask which may be manipulated with 67.Dv CPU_LEVEL_WHICH . 68.Pp 69Masks of type 70.Ft cpuset_t 71are composed using the 72.Dv CPU_SET 73macros. 74If the user-supplied mask is not large enough to fit all of the matching CPUs, 75.Fn cpuset_getaffinity 76fails with 77.Er ERANGE . 78Calls to 79.Fn cpuset_setaffinity 80tolerate masks of any size with no restrictions. 81The kernel uses the meaningful part of the mask, where the upper bound is 82the maximum CPU id present in the system. 83If bits for non-existing CPUs are set, calls to 84.Fn cpuset_setaffinity 85fail with 86.Er EINVAL . 87.Pp 88The supplied mask should have a size of 89.Fa setsize 90bytes. 91This size is usually provided by calling 92.Li sizeof(mask) 93which is ultimately determined by the value of 94.Dv CPU_SETSIZE 95as defined in 96.In sys/cpuset.h . 97.Pp 98.Fn cpuset_getaffinity 99retrieves the 100mask from the object specified by 101.Fa level , 102.Fa which 103and 104.Fa id 105and stores it in the space provided by 106.Fa mask . 107.Pp 108.Fn cpuset_setaffinity 109attempts to set the mask for the object specified by 110.Fa level , 111.Fa which 112and 113.Fa id 114to the value in 115.Fa mask . 116.Sh RETURN VALUES 117.Rv -std 118.Sh ERRORS 119The following error codes may be set in 120.Va errno : 121.Bl -tag -width Er 122.\" When changing this list, consider updating share/man/man3/pthread_create.3, 123.\" since that function can return any of these errors. 124.It Bq Er EINVAL 125The 126.Fa level 127or 128.Fa which 129argument was not a valid value. 130.It Bq Er EINVAL 131The 132.Fa mask 133argument specified when calling 134.Fn cpuset_setaffinity 135was not a valid value. 136.It Bq Er EDEADLK 137The 138.Fn cpuset_setaffinity 139call would leave a thread without a valid CPU to run on because the set 140does not overlap with the thread's anonymous mask. 141.It Bq Er EFAULT 142The mask pointer passed was invalid. 143.It Bq Er ESRCH 144The object specified by the 145.Fa id 146and 147.Fa which 148arguments could not be found. 149.It Bq Er ERANGE 150The 151.Fa cpusetsize 152was smaller than needed to fit all of the matching CPUs. 153.It Bq Er EPERM 154The calling process did not have the credentials required to complete the 155operation. 156.It Bq Er ECAPMODE 157The calling process attempted to act on a process other than itself, while 158in capability mode. 159See 160.Xr capsicum 4 . 161.El 162.Sh SEE ALSO 163.Xr cpuset 1 , 164.Xr cpuset 2 , 165.Xr cpuset_getdomain 2 , 166.Xr cpuset_getid 2 , 167.Xr cpuset_setdomain 2 , 168.Xr cpuset_setid 2 , 169.Xr pthread_affinity_np 3 , 170.Xr pthread_attr_affinity_np 3 , 171.Xr capsicum 4 , 172.Xr cpuset 9 173.Sh HISTORY 174The 175.Nm 176family of system calls first appeared in 177.Fx 7.1 . 178.Sh AUTHORS 179.An Jeffrey Roberson Aq Mt [email protected] 180