1*8269e767SBrooks Davis.\" Copyright (c) 2008 Christian Brueffer 2*8269e767SBrooks Davis.\" Copyright (c) 2008 Jeffrey Roberson 3*8269e767SBrooks Davis.\" Copyright (c) 2021 Robert N. M. Watson 4*8269e767SBrooks Davis.\" All rights reserved. 5*8269e767SBrooks Davis.\" 6*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 7*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 8*8269e767SBrooks Davis.\" are met: 9*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 10*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 11*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 12*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 13*8269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 14*8269e767SBrooks Davis.\" 15*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18*8269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25*8269e767SBrooks Davis.\" SUCH DAMAGE. 26*8269e767SBrooks Davis.\" 27*8269e767SBrooks Davis.Dd January 29, 2023 28*8269e767SBrooks Davis.Dt CPUSET 2 29*8269e767SBrooks Davis.Os 30*8269e767SBrooks Davis.Sh NAME 31*8269e767SBrooks Davis.Nm cpuset , 32*8269e767SBrooks Davis.Nm cpuset_getid , 33*8269e767SBrooks Davis.Nm cpuset_setid 34*8269e767SBrooks Davis.Nd manage CPU affinity sets 35*8269e767SBrooks Davis.Sh LIBRARY 36*8269e767SBrooks Davis.Lb libc 37*8269e767SBrooks Davis.Sh SYNOPSIS 38*8269e767SBrooks Davis.In sys/param.h 39*8269e767SBrooks Davis.In sys/cpuset.h 40*8269e767SBrooks Davis.Ft int 41*8269e767SBrooks Davis.Fn cpuset "cpusetid_t *setid" 42*8269e767SBrooks Davis.Ft int 43*8269e767SBrooks Davis.Fn cpuset_setid "cpuwhich_t which" "id_t id" "cpusetid_t setid" 44*8269e767SBrooks Davis.Ft int 45*8269e767SBrooks Davis.Fn cpuset_getid "cpulevel_t level" "cpuwhich_t which" "id_t id" "cpusetid_t *setid" 46*8269e767SBrooks Davis.Sh DESCRIPTION 47*8269e767SBrooks DavisThe 48*8269e767SBrooks Davis.Nm 49*8269e767SBrooks Davisfamily of system calls allow applications to control sets of processors and 50*8269e767SBrooks Davismemory domains and assign processes and threads to these sets. 51*8269e767SBrooks DavisProcessor sets contain lists of CPUs and domains that members may run on 52*8269e767SBrooks Davisand exist only as long as some process is a member of the set. 53*8269e767SBrooks DavisAll processes in the system have an assigned set. 54*8269e767SBrooks DavisThe default set for all processes in the system is the set numbered 1. 55*8269e767SBrooks DavisThreads belong to the same set as the process which contains them, 56*8269e767SBrooks Davishowever, they may further restrict their set with the anonymous 57*8269e767SBrooks Davisper-thread mask to bind to a specific CPU or subset of CPUs and memory domains. 58*8269e767SBrooks Davis.Pp 59*8269e767SBrooks DavisSets are referenced by a number of type 60*8269e767SBrooks Davis.Ft cpuset_id_t . 61*8269e767SBrooks DavisEach thread has a root set, an assigned set, and an anonymous mask. 62*8269e767SBrooks DavisOnly the root and assigned sets are numbered. 63*8269e767SBrooks DavisThe root set is the set of all CPUs and memory domains available in the system 64*8269e767SBrooks Davisor in the system partition the thread is running in. 65*8269e767SBrooks DavisThe assigned set is a subset of the root set and is administratively 66*8269e767SBrooks Davisassignable on a per-process basis. 67*8269e767SBrooks DavisMany processes and threads may be members of a numbered set. 68*8269e767SBrooks Davis.Pp 69*8269e767SBrooks DavisThe anonymous set is a further thread-specific refinement on the assigned 70*8269e767SBrooks Davisset. 71*8269e767SBrooks DavisIt is intended that administrators will manipulate numbered sets using 72*8269e767SBrooks Davis.Xr cpuset 1 73*8269e767SBrooks Daviswhile application developers will manipulate anonymous sets using 74*8269e767SBrooks Davis.Xr cpuset_setaffinity 2 and 75*8269e767SBrooks Davis.Xr cpuset_setdomain 2 . 76*8269e767SBrooks Davis.Pp 77*8269e767SBrooks DavisTo select the correct set a value of type 78*8269e767SBrooks Davis.Ft cpulevel_t 79*8269e767SBrooks Davisis used. 80*8269e767SBrooks DavisThe following values for 81*8269e767SBrooks Davis.Fa level 82*8269e767SBrooks Davisare supported: 83*8269e767SBrooks Davis.Bl -column CPU_LEVEL_CPUSET -offset indent 84*8269e767SBrooks Davis.It Dv CPU_LEVEL_ROOT Ta "Root set" 85*8269e767SBrooks Davis.It Dv CPU_LEVEL_CPUSET Ta "Assigned set" 86*8269e767SBrooks Davis.It Dv CPU_LEVEL_WHICH Ta "Set specified by which argument" 87*8269e767SBrooks Davis.El 88*8269e767SBrooks Davis.Pp 89*8269e767SBrooks DavisThe 90*8269e767SBrooks Davis.Fa which 91*8269e767SBrooks Davisargument determines how the value of 92*8269e767SBrooks Davis.Fa id 93*8269e767SBrooks Davisis interpreted and is of type 94*8269e767SBrooks Davis.Ft cpuwhich_t . 95*8269e767SBrooks DavisThe 96*8269e767SBrooks Davis.Fa which 97*8269e767SBrooks Davisargument may have the following values: 98*8269e767SBrooks Davis.Bl -column CPU_WHICH_INTRHANDLER -offset indent 99*8269e767SBrooks Davis.It Dv CPU_WHICH_TID Ta "id is lwpid_t (thread id)" 100*8269e767SBrooks Davis.It Dv CPU_WHICH_PID Ta "id is pid_t (process id)" 101*8269e767SBrooks Davis.It Dv CPU_WHICH_TIDPID Ta "id is either a thread or process id" 102*8269e767SBrooks Davis.It Dv CPU_WHICH_JAIL Ta "id is jid (jail id)" 103*8269e767SBrooks Davis.It Dv CPU_WHICH_CPUSET Ta "id is a cpusetid_t (cpuset id)" 104*8269e767SBrooks Davis.It Dv CPU_WHICH_IRQ Ta "id is an irq number" 105*8269e767SBrooks Davis.It Dv CPU_WHICH_INTRHANDLER Ta "id is an irq number for an interrupt handler" 106*8269e767SBrooks Davis.It Dv CPU_WHICH_ITHREAD Ta "id is an irq number for an ithread" 107*8269e767SBrooks Davis.It Dv CPU_WHICH_DOMAIN Ta "id is a NUMA domain" 108*8269e767SBrooks Davis.El 109*8269e767SBrooks Davis.Pp 110*8269e767SBrooks DavisAn 111*8269e767SBrooks Davis.Fa id 112*8269e767SBrooks Davisof '-1' may be used with a 113*8269e767SBrooks Davis.Fa which 114*8269e767SBrooks Davisof 115*8269e767SBrooks Davis.Dv CPU_WHICH_TID , 116*8269e767SBrooks Davis.Dv CPU_WHICH_PID , 117*8269e767SBrooks Davis.Dv CPU_WHICH_TIDPID , 118*8269e767SBrooks Davisor 119*8269e767SBrooks Davis.Dv CPU_WHICH_CPUSET 120*8269e767SBrooks Davisto mean the current thread, process, or current thread's 121*8269e767SBrooks Daviscpuset. 122*8269e767SBrooks DavisAll cpuset syscalls allow this usage. 123*8269e767SBrooks Davis.Pp 124*8269e767SBrooks DavisA 125*8269e767SBrooks Davis.Fa level 126*8269e767SBrooks Davisargument of 127*8269e767SBrooks Davis.Dv CPU_LEVEL_WHICH 128*8269e767SBrooks Daviscombined with a 129*8269e767SBrooks Davis.Fa which 130*8269e767SBrooks Davisargument other than 131*8269e767SBrooks Davis.Dv CPU_WHICH_CPUSET 132*8269e767SBrooks Davisrefers to the anonymous mask of the object. 133*8269e767SBrooks DavisThis mask does not have an id and may only be manipulated with 134*8269e767SBrooks Davis.Xr cpuset_setaffinity 2 . 135*8269e767SBrooks Davis.Pp 136*8269e767SBrooks Davis.Fn cpuset 137*8269e767SBrooks Daviscreates a new set containing the same CPUs as the root set of the current 138*8269e767SBrooks Davisprocess and stores its id in the space provided by 139*8269e767SBrooks Davis.Fa setid . 140*8269e767SBrooks DavisOn successful completion the calling process joins the set and is the 141*8269e767SBrooks Davisonly member. 142*8269e767SBrooks DavisChildren inherit this set after a call to 143*8269e767SBrooks Davis.Xr fork 2 . 144*8269e767SBrooks Davis.Pp 145*8269e767SBrooks Davis.Fn cpuset_setid 146*8269e767SBrooks Davisattempts to set the id of the object specified by the 147*8269e767SBrooks Davis.Fa which 148*8269e767SBrooks Davisargument. 149*8269e767SBrooks DavisCurrently 150*8269e767SBrooks Davis.Dv CPU_WHICH_PID 151*8269e767SBrooks Davisis the only acceptable value for which as 152*8269e767SBrooks Davisthreads do not have an id distinct from their process and the API does 153*8269e767SBrooks Davisnot permit changing the id of an existing set. 154*8269e767SBrooks DavisUpon successful completion all of the threads in the target process will 155*8269e767SBrooks Davisbe running on CPUs permitted by the set. 156*8269e767SBrooks Davis.Pp 157*8269e767SBrooks Davis.Fn cpuset_getid 158*8269e767SBrooks Davisretrieves a set id from the object indicated by 159*8269e767SBrooks Davis.Fa which 160*8269e767SBrooks Davisand stores it in the space pointed to by 161*8269e767SBrooks Davis.Fa setid . 162*8269e767SBrooks DavisThe retrieved id may be that of either the root or assigned set 163*8269e767SBrooks Davisdepending on the value of 164*8269e767SBrooks Davis.Fa level . 165*8269e767SBrooks Davis.Fa level 166*8269e767SBrooks Davisshould be 167*8269e767SBrooks Davis.Dv CPU_LEVEL_CPUSET 168*8269e767SBrooks Davisor 169*8269e767SBrooks Davis.Dv CPU_LEVEL_ROOT 170*8269e767SBrooks Davisto get the set id from 171*8269e767SBrooks Davisthe process or thread specified by the 172*8269e767SBrooks Davis.Fa id 173*8269e767SBrooks Davisargument. 174*8269e767SBrooks DavisSpecifying 175*8269e767SBrooks Davis.Dv CPU_LEVEL_WHICH 176*8269e767SBrooks Daviswith a process or thread is unsupported since 177*8269e767SBrooks Davisthis references the unnumbered anonymous mask. 178*8269e767SBrooks Davis.Pp 179*8269e767SBrooks DavisThe actual contents of the sets may be retrieved or manipulated using 180*8269e767SBrooks Davis.Xr cpuset_getaffinity 2 , 181*8269e767SBrooks Davis.Xr cpuset_setaffinity 2 , 182*8269e767SBrooks Davis.Xr cpuset_getdomain 2 , and 183*8269e767SBrooks Davis.Xr cpuset_setdomain 2 . 184*8269e767SBrooks DavisThe 185*8269e767SBrooks Davis.Xr cpuset 9 186*8269e767SBrooks Davismacros may be used to manipulate masks of type 187*8269e767SBrooks Davis.Ft cpuset_t 188*8269e767SBrooks Davisget and set using those APIs. 189*8269e767SBrooks DavisSee those manual pages for more detail. 190*8269e767SBrooks Davis.Sh RETURN VALUES 191*8269e767SBrooks Davis.Rv -std 192*8269e767SBrooks Davis.Sh EXAMPLES 193*8269e767SBrooks DavisIn this example, a CPU set mask is configured to limit execution to the first 194*8269e767SBrooks DavisCPU using 195*8269e767SBrooks Davis.Xr CPU_ZERO 9 196*8269e767SBrooks Davisand 197*8269e767SBrooks Davis.Xr CPU_SET 9 , 198*8269e767SBrooks Davismembers of the 199*8269e767SBrooks Davis.Xr cpuset 9 200*8269e767SBrooks Davisprogramming interface. 201*8269e767SBrooks DavisThen, the mask is applied to a new anonymous CPU set associated with the 202*8269e767SBrooks Daviscurrent process using 203*8269e767SBrooks Davis.Xr cpuset_setaffinity 2 . 204*8269e767SBrooks DavisThis mask will be used by the current process, and inherited by any new 205*8269e767SBrooks Davischild processes. 206*8269e767SBrooks Davis.Bd -literal -offset indent 207*8269e767SBrooks Davis#include <sys/param.h> 208*8269e767SBrooks Davis#include <sys/cpuset.h> 209*8269e767SBrooks Davis 210*8269e767SBrooks Davis#include <sysexits.h> 211*8269e767SBrooks Davis 212*8269e767SBrooks Daviscpuset_t cpuset_mask; 213*8269e767SBrooks Davis 214*8269e767SBrooks Davis/* Initialize a CPU mask and enable CPU 0. */ 215*8269e767SBrooks DavisCPU_ZERO(&cpuset_mask); 216*8269e767SBrooks DavisCPU_SET(0, &cpuset_mask); 217*8269e767SBrooks Davis 218*8269e767SBrooks Davis/* Set affinity for the CPU set for the current process. */ 219*8269e767SBrooks Davisif (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, 220*8269e767SBrooks Davis sizeof(cpuset_mask), &cpuset_mask) < 0) 221*8269e767SBrooks Davis err(EX_OSERR, "cpuset_setaffinity"); 222*8269e767SBrooks Davis.Ed 223*8269e767SBrooks Davis.Pp 224*8269e767SBrooks DavisIn the next example, a named CPU set is created containing the current 225*8269e767SBrooks Davisprocess, and its affinity similarly configured. 226*8269e767SBrooks DavisThe resulting CPU set ID can then be used for further external management of 227*8269e767SBrooks Davisthe affinity of the set. 228*8269e767SBrooks Davis.Bd -literal -offset indent 229*8269e767SBrooks Davis#include <sys/param.h> 230*8269e767SBrooks Davis#include <sys/cpuset.h> 231*8269e767SBrooks Davis 232*8269e767SBrooks Davis#include <sysexits.h> 233*8269e767SBrooks Davis 234*8269e767SBrooks Daviscpusetid_t cpuset_id; 235*8269e767SBrooks Daviscpuset_t cpuset_mask; 236*8269e767SBrooks Davis 237*8269e767SBrooks Davis/* Create new cpuset for the current process. */ 238*8269e767SBrooks Davisif (cpuset(&cpuset_id) < 0) 239*8269e767SBrooks Davis err(EX_OSERR, "cpuset"); 240*8269e767SBrooks Davis 241*8269e767SBrooks Davis/* Initialize a CPU mask and enable CPU 0. */ 242*8269e767SBrooks DavisCPU_ZERO(&cpuset_mask); 243*8269e767SBrooks DavisCPU_SET(0, &cpuset_mask); 244*8269e767SBrooks Davis 245*8269e767SBrooks Davis/* Set affinity for the CPU set for the current process. */ 246*8269e767SBrooks Davisif (cpuset_setaffinity(CPU_LEVEL_SET, CPU_WHICH_CPUSET, cpuset_id, 247*8269e767SBrooks Davis sizeof(cpuset_mask), &cpuset_mask) < 0) 248*8269e767SBrooks Davis err(EX_OSERR, "cpuset_setaffinity"); 249*8269e767SBrooks Davis.Ed 250*8269e767SBrooks Davis.Sh ERRORS 251*8269e767SBrooks DavisThe following error codes may be set in 252*8269e767SBrooks Davis.Va errno : 253*8269e767SBrooks Davis.Bl -tag -width Er 254*8269e767SBrooks Davis.It Bq Er EINVAL 255*8269e767SBrooks DavisThe 256*8269e767SBrooks Davis.Fa which 257*8269e767SBrooks Davisor 258*8269e767SBrooks Davis.Fa level 259*8269e767SBrooks Davisargument was not a valid value. 260*8269e767SBrooks Davis.It Bq Er EDEADLK 261*8269e767SBrooks DavisThe 262*8269e767SBrooks Davis.Fn cpuset_setid 263*8269e767SBrooks Daviscall would leave a thread without a valid CPU to run on because the set 264*8269e767SBrooks Davisdoes not overlap with the thread's anonymous mask. 265*8269e767SBrooks Davis.It Bq Er EFAULT 266*8269e767SBrooks DavisThe setid pointer passed to 267*8269e767SBrooks Davis.Fn cpuset_getid 268*8269e767SBrooks Davisor 269*8269e767SBrooks Davis.Fn cpuset 270*8269e767SBrooks Daviswas invalid. 271*8269e767SBrooks Davis.It Bq Er ESRCH 272*8269e767SBrooks DavisThe object specified by the 273*8269e767SBrooks Davis.Fa id 274*8269e767SBrooks Davisand 275*8269e767SBrooks Davis.Fa which 276*8269e767SBrooks Davisarguments could not be found. 277*8269e767SBrooks Davis.It Bq Er EPERM 278*8269e767SBrooks DavisThe calling process did not have the credentials required to complete the 279*8269e767SBrooks Davisoperation. 280*8269e767SBrooks Davis.It Bq Er ENFILE 281*8269e767SBrooks DavisThere was no free 282*8269e767SBrooks Davis.Ft cpusetid_t 283*8269e767SBrooks Davisfor allocation. 284*8269e767SBrooks Davis.El 285*8269e767SBrooks Davis.Sh SEE ALSO 286*8269e767SBrooks Davis.Xr cpuset 1 , 287*8269e767SBrooks Davis.Xr cpuset_getaffinity 2 , 288*8269e767SBrooks Davis.Xr cpuset_getdomain 2 , 289*8269e767SBrooks Davis.Xr cpuset_setaffinity 2 , 290*8269e767SBrooks Davis.Xr cpuset_setdomain 2 , 291*8269e767SBrooks Davis.Xr pthread_affinity_np 3 , 292*8269e767SBrooks Davis.Xr pthread_attr_affinity_np 3 , 293*8269e767SBrooks Davis.Xr CPU_SET 9 , 294*8269e767SBrooks Davis.Xr CPU_ZERO 9 , 295*8269e767SBrooks Davis.Xr cpuset 9 296*8269e767SBrooks Davis.Sh HISTORY 297*8269e767SBrooks DavisThe 298*8269e767SBrooks Davis.Nm 299*8269e767SBrooks Davisfamily of system calls first appeared in 300*8269e767SBrooks Davis.Fx 7.1 . 301*8269e767SBrooks Davis.Sh AUTHORS 302*8269e767SBrooks Davis.An Jeffrey Roberson Aq Mt jeff@FreeBSD.org 303