1.\" $NetBSD: affinity.3,v 1.1 2008/06/16 14:25:49 rmind 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 June 15, 2008 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 49.Fn pthread_setaffinity_np 50function sets the affinity mask 51.Fa set 52for 53.Fa thread . 54At least one valid CPU must be set in the mask. 55.Pp 56The 57.Fn pthread_getaffinity_np 58function gets the affinity mask of 59.Fa thread 60into 61.Fa set . 62.Pp 63.Fa set 64must be created and initialized using the 65.Xr cpuset 3 66functions. 67.Sh RETURN VALUES 68The 69.Fn pthread_setaffinity_np 70and 71.Fn pthread_getaffinity_np 72functions return 0 on success. 73Otherwise, an error number is returned to indicate the error. 74.Sh EXAMPLES 75An example of code fragment, which sets the affinity for the current 76thread to the CPU whose ID is 0: 77.Bd -literal 78 cpuset_t *cset; 79 pthread_t pth; 80 cpuid_t ci; 81 82 cset = cpuset_create(); 83 if (cset == NULL) { 84 err(EXIT_FAILURE, "cpuset_create"); 85 } 86 ci = 0; 87 cpuset_set(ci); 88 89 pth = pthread_self(); 90 error = pthread_setaffinity_np(pth, cpuset_size(cset), cset); 91 if (error) { 92 ... 93 } 94 cpuset_destroy(cset); 95.Ed 96.Sh ERRORS 97The 98.Fn pthread_setaffinity_np 99and 100.Fn pthread_getaffinity_np 101functions fail if: 102.Bl -tag -width Er 103.It Bq Er EINVAL 104The specified 105.Fa set 106was invalid. 107.It Bq Er EPERM 108The calling process lacks the appropriate privileges to perform 109the operation. 110.It Bq Er ESRCH 111No thread could be found corresponding to the one specified by 112.Fa thread . 113.El 114.Sh NOTES 115Thread affinity might be used together with the processor sets, see 116.Xr pset 3 . 117In such case, the affinity mask takes precedence over the assignment 118to processor sets. 119.Sh SEE ALSO 120.Xr cpuset 3 , 121.Xr pset 3 , 122.Xr pthread_getschedparam 3 , 123.Xr pthread_setschedparam 3 , 124.Xr sched 3 , 125.Xr schedctl 8 126