1.\" $NetBSD: affinity.3,v 1.8 2011/12/05 10:27:40 wiz Exp $ 2.\" 3.\" Copyright (c) 2008 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Mindaugas Rasiukevicius <rmind at NetBSD org>. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd December 4, 2011 31.Dt AFFINITY 3 32.Os 33.Sh NAME 34.Nm pthread_setaffinity_np , 35.Nm pthread_getaffinity_np 36.Nd affinity of threads 37.Sh LIBRARY 38.Lb libpthread 39.Sh SYNOPSIS 40.In pthread.h 41.In sched.h 42.Ft int 43.Fn pthread_setaffinity_np "pthread_t thread" "size_t size" "cpuset_t *set" 44.Ft int 45.Fn pthread_getaffinity_np "pthread_t thread" "size_t size" "cpuset_t *set" 46.Sh DESCRIPTION 47Thread affinity allows to run the thread on specified CPU or CPUs only. 48.Pp 49The 50.Fn pthread_setaffinity_np 51function sets the affinity mask 52.Fa set 53for 54.Fa thread . 55At least one valid CPU must be set in the mask. 56.Pp 57The 58.Fn pthread_getaffinity_np 59function gets the affinity mask of 60.Fa thread 61into 62.Fa set . 63Note that 64.Fa set 65must be created and initialized using the 66.Xr cpuset 3 67functions. 68.Sh IMPLEMENTATION NOTES 69Setting CPU 70.Nm 71requires super-user privileges. 72Ordinary users can be allowed to control CPU affinity 73of their threads via the 74.Pa security.models.extensions.user_set_cpu_affinity 75.Xr sysctl 7 . 76See 77.Xr secmodel_extensions 9 . 78.Pp 79Portable applications should not use the 80.Fn pthread_setaffinity_np 81and 82.Fn pthread_getaffinity_np 83functions. 84.Sh RETURN VALUES 85The 86.Fn pthread_setaffinity_np 87and 88.Fn pthread_getaffinity_np 89functions return 0 on success. 90Otherwise, an error number is returned to indicate the error. 91.Sh EXAMPLES 92An example of code fragment, which sets the affinity for the current 93thread to the CPU whose ID is 0: 94.Bd -literal 95 cpuset_t *cset; 96 pthread_t pth; 97 cpuid_t ci; 98 99 cset = cpuset_create(); 100 if (cset == NULL) { 101 err(EXIT_FAILURE, "cpuset_create"); 102 } 103 ci = 0; 104 cpuset_set(ci, cset); 105 106 pth = pthread_self(); 107 error = pthread_setaffinity_np(pth, cpuset_size(cset), cset); 108 if (error) { 109 ... 110 } 111 cpuset_destroy(cset); 112.Ed 113.Sh COMPATIBILITY 114Both functions are non-standard extensions. 115.Sh ERRORS 116Both functions may fail if: 117.Bl -tag -width Er 118.It Bq Er EINVAL 119The specified 120.Fa set 121was invalid. 122.It Bq Er EPERM 123The calling process lacks the appropriate privileges to perform 124the operation. 125.It Bq Er ESRCH 126No thread could be found corresponding to the one specified by 127.Fa thread . 128.El 129.Sh NOTES 130There is an alternative processor sets interface, see 131.Xr pset 3 . 132However, thread affinity and processor sets are mutually exclusive, 133hence mixing of these interfaces is prohibited. 134.Sh SEE ALSO 135.Xr cpuset 3 , 136.Xr pset 3 , 137.Xr pthread_getschedparam 3 , 138.Xr pthread_setschedparam 3 , 139.Xr sched 3 , 140.Xr schedctl 8 141