1777f8cebSXin LI.\"- 2777f8cebSXin LI.\" Copyright (c) 2010 Xin LI <delphij@FreeBSD.org> 3777f8cebSXin LI.\" 4777f8cebSXin LI.\" Redistribution and use in source and binary forms, with or without 5777f8cebSXin LI.\" modification, are permitted provided that the following conditions 6777f8cebSXin LI.\" are met: 7777f8cebSXin LI.\" 1. Redistributions of source code must retain the above copyright 8777f8cebSXin LI.\" notice, this list of conditions and the following disclaimer. 9777f8cebSXin LI.\" 2. Redistributions in binary form must reproduce the above copyright 10777f8cebSXin LI.\" notice, this list of conditions and the following disclaimer in the 11777f8cebSXin LI.\" documentation and/or other materials provided with the distribution. 12777f8cebSXin LI.\" 13777f8cebSXin LI.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14777f8cebSXin LI.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15777f8cebSXin LI.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16777f8cebSXin LI.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17777f8cebSXin LI.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18777f8cebSXin LI.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19777f8cebSXin LI.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20777f8cebSXin LI.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21777f8cebSXin LI.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22777f8cebSXin LI.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23777f8cebSXin LI.\" SUCH DAMAGE. 24777f8cebSXin LI.\" 2562801712SDmitry Chagin.Dd January 29, 2023 26777f8cebSXin LI.Dt PTHREAD_AFFINITY_NP 3 27777f8cebSXin LI.Os 28777f8cebSXin LI.Sh NAME 29777f8cebSXin LI.Nm pthread_getaffinity_np , 30777f8cebSXin LI.Nm pthread_setaffinity_np 31777f8cebSXin LI.Nd manage CPU affinity 32777f8cebSXin LI.Sh LIBRARY 33777f8cebSXin LI.Lb libpthread 34777f8cebSXin LI.Sh SYNOPSIS 35777f8cebSXin LI.In pthread_np.h 36777f8cebSXin LI.Ft int 37777f8cebSXin LI.Fn pthread_getaffinity_np "pthread_t td" "size_t cpusetsize" "cpuset_t *cpusetp" 38777f8cebSXin LI.Ft int 39777f8cebSXin LI.Fn pthread_setaffinity_np "pthread_t td" "size_t cpusetsize" "const cpuset_t *cpusetp" 40777f8cebSXin LI.Sh DESCRIPTION 41777f8cebSXin LI.Fn pthread_getaffinity_np 42777f8cebSXin LIand 43777f8cebSXin LI.Fn pthread_setaffinity_np 44d49bbd73SChristian Bruefferallow the manipulation of sets of CPUs available to the specified thread. 45777f8cebSXin LI.Pp 46777f8cebSXin LIMasks of type 47777f8cebSXin LI.Ft cpuset_t 48777f8cebSXin LIare composed using the 49cd088fc4SJoel Dahl.Dv CPU_SET 50777f8cebSXin LImacros. 5162801712SDmitry ChaginIf the user-supplied mask is not large enough to fit all of the matching CPUs, 52777f8cebSXin LI.Fn pthread_getaffinity_np 5362801712SDmitry Chaginfails with 5462801712SDmitry Chagin.Er ERANGE . 55777f8cebSXin LICalls to 56777f8cebSXin LI.Fn pthread_setaffinity_np 5762801712SDmitry Chagintolerate masks of any size with no restrictions. 5862801712SDmitry ChaginThe kernel uses the meaningful part of the mask, where the upper bound is 5962801712SDmitry Chaginthe maximum CPU id present in the system. 6062801712SDmitry ChaginIf bits for non-existing CPUs are set, calls to 6162801712SDmitry Chagin.Fn pthread_setaffinity_np 62*2c2a0498SJens Schweikhardtfail with 6362801712SDmitry Chagin.Er EINVAL . 64777f8cebSXin LI.Pp 65777f8cebSXin LIThe supplied mask should have a size of 66777f8cebSXin LI.Fa cpusetsize 67777f8cebSXin LIbytes. 68777f8cebSXin LIThis size is usually provided by calling 69777f8cebSXin LI.Li sizeof(cpuset_t) 70777f8cebSXin LIwhich is ultimately determined by the value of 71777f8cebSXin LI.Dv CPU_SETSIZE 72777f8cebSXin LIas defined in 73777f8cebSXin LI.In sys/cpuset.h . 74777f8cebSXin LI.Pp 75777f8cebSXin LI.Fn pthread_getaffinity_np 76777f8cebSXin LIretrieves the 77777f8cebSXin LImask from the thread specified by 78777f8cebSXin LI.Fa td , 79777f8cebSXin LIand stores it in the space provided by 80d49bbd73SChristian Brueffer.Fa cpusetp . 81777f8cebSXin LI.Pp 82777f8cebSXin LI.Fn pthread_setaffinity_np 83777f8cebSXin LIattempts to set the mask for the thread specified by 84777f8cebSXin LI.Fa td 85777f8cebSXin LIto the value in 86d49bbd73SChristian Brueffer.Fa cpusetp . 87777f8cebSXin LI.Sh RETURN VALUES 88777f8cebSXin LIIf successful, the 89777f8cebSXin LI.Fn pthread_getaffinity_np 90777f8cebSXin LIand 91777f8cebSXin LI.Fn pthread_setaffinity_np 92777f8cebSXin LIfunctions will return zero. 93777f8cebSXin LIOtherwise an error number will be returned 94777f8cebSXin LIto indicate the error. 95777f8cebSXin LI.Sh ERRORS 96777f8cebSXin LIThe 97777f8cebSXin LI.Fn pthread_getaffinity_np 98777f8cebSXin LIand 99777f8cebSXin LI.Fn pthread_setaffinity_np 100777f8cebSXin LIfunctions may fail if: 101777f8cebSXin LI.Bl -tag -width Er 10262801712SDmitry Chagin.It Bq Er EINVAL 10362801712SDmitry ChaginThe 10462801712SDmitry Chagin.Fa cpusetp 10562801712SDmitry Chaginargument specified when calling 10662801712SDmitry Chagin.Fn pthread_setaffinity_np 10762801712SDmitry Chaginwas not a valid value. 108777f8cebSXin LI.It Bq Er EDEADLK 109777f8cebSXin LIThe 110777f8cebSXin LI.Fn pthread_setaffinity_np 111777f8cebSXin LIcall would leave a thread without a valid CPU to run on because the set 112777f8cebSXin LIdoes not overlap with the thread's anonymous mask. 113777f8cebSXin LI.It Bq Er EFAULT 114777f8cebSXin LIThe 115d49bbd73SChristian Brueffer.Fa cpusetp 116777f8cebSXin LIpointer passed was invalid. 117777f8cebSXin LI.It Bq Er ESRCH 118777f8cebSXin LIThe thread specified by the 119777f8cebSXin LI.Fa td 120777f8cebSXin LIargument could not be found. 121777f8cebSXin LI.It Bq Er ERANGE 122777f8cebSXin LIThe 123777f8cebSXin LI.Fa cpusetsize 12462801712SDmitry Chaginwas smaller than needed to fit all of the matching CPUs. 125777f8cebSXin LI.It Bq Er EPERM 126777f8cebSXin LIThe calling thread did not have the credentials required to complete the 127777f8cebSXin LIoperation. 128777f8cebSXin LI.El 129777f8cebSXin LI.Sh SEE ALSO 130777f8cebSXin LI.Xr cpuset 1 , 131777f8cebSXin LI.Xr cpuset 2 , 132777f8cebSXin LI.Xr cpuset_getid 2 , 133777f8cebSXin LI.Xr cpuset_setid 2 , 134777f8cebSXin LI.Xr pthread 3 , 13567b9e255SXin LI.Xr pthread_attr_getaffinity_np 3 , 136df7d7638SFelix Johnson.Xr pthread_attr_setaffinity_np 3 , 137df7d7638SFelix Johnson.Xr pthread_np 3 138777f8cebSXin LI.Sh STANDARDS 139777f8cebSXin LIThe 140777f8cebSXin LI.Nm pthread_getaffinity_np 141777f8cebSXin LIand 142777f8cebSXin LI.Nm pthread_setaffinity_np 143777f8cebSXin LIfunctions are non-standard 144777f8cebSXin LI.Fx 145777f8cebSXin LIextensions and may be not available on other operating systems. 146777f8cebSXin LI.Sh HISTORY 147777f8cebSXin LIThe 148777f8cebSXin LI.Nm pthread_getaffinity_np 149777f8cebSXin LIand 150777f8cebSXin LI.Nm pthread_setaffinity_np 151777f8cebSXin LIfunction first appeared in 152777f8cebSXin LI.Fx 7.2 . 153777f8cebSXin LI.Sh AUTHORS 154777f8cebSXin LI.An -nosplit 155777f8cebSXin LIThe 156777f8cebSXin LI.Nm pthread_getaffinity_np 157777f8cebSXin LIand 158777f8cebSXin LI.Nm pthread_setaffinity_np 159777f8cebSXin LIfunctions were written by 160a63d6c94SBaptiste Daroussin.An David Xu Aq Mt davidxu@FreeBSD.org , 161777f8cebSXin LIand this manpage was written by 162a63d6c94SBaptiste Daroussin.An Xin LI Aq Mt delphij@FreeBSD.org . 163