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