- Add affinity(3) manual page, which describes thread affinity,

pthread_setaffinity_np(3) and pthread_getaffinity_np(3) functions,
  provides simple code example.
- Add cpuset(3) manual page, which describes API of CPU-sets.

Thanks <wiz> for many improvements!
This commit is contained in:
rmind 2008-06-16 14:25:49 +00:00
parent ddbb9c58dd
commit e6acd90e07
5 changed files with 274 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.1157 2008/06/04 14:31:53 ad Exp $
# $NetBSD: mi,v 1.1158 2008/06/16 14:25:49 rmind Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@ -4251,6 +4251,7 @@
./usr/share/man/cat3/addnstr.0 comp-c-catman .cat
./usr/share/man/cat3/addr.0 comp-c-catman .cat
./usr/share/man/cat3/addstr.0 comp-c-catman .cat
./usr/share/man/cat3/affinity.0 comp-c-catman .cat
./usr/share/man/cat3/aio_cancel.0 comp-c-catman .cat
./usr/share/man/cat3/aio_error.0 comp-c-catman .cat
./usr/share/man/cat3/aio_fsync.0 comp-c-catman .cat
@ -4681,6 +4682,14 @@
./usr/share/man/cat3/coshf.0 comp-c-catman .cat
./usr/share/man/cat3/cpow.0 comp-c-catman .cat
./usr/share/man/cat3/cpowf.0 comp-c-catman .cat
./usr/share/man/cat3/cpuset.0 comp-c-catman .cat
./usr/share/man/cat3/cpuset_create.0 comp-c-catman .cat
./usr/share/man/cat3/cpuset_destroy.0 comp-c-catman .cat
./usr/share/man/cat3/cpuset_zero.0 comp-c-catman .cat
./usr/share/man/cat3/cpuset_set.0 comp-c-catman .cat
./usr/share/man/cat3/cpuset_clr.0 comp-c-catman .cat
./usr/share/man/cat3/cpuset_isset.0 comp-c-catman .cat
./usr/share/man/cat3/cpuset_size.0 comp-c-catman .cat
./usr/share/man/cat3/creal.0 comp-c-catman .cat
./usr/share/man/cat3/crealf.0 comp-c-catman .cat
./usr/share/man/cat3/creat.0 comp-c-catman .cat
@ -6699,6 +6708,7 @@
./usr/share/man/cat3/pthread_detach.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_equal.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_exit.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_getaffinity.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_getname_np.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_getschedparam.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_getspecific.0 comp-c-catman .cat
@ -6732,6 +6742,7 @@
./usr/share/man/cat3/pthread_rwlockattr_init.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_schedparam.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_self.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_setaffinity.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_setcancelstate.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_setcanceltype.0 comp-c-catman .cat
./usr/share/man/cat3/pthread_setname_np.0 comp-c-catman .cat
@ -9524,6 +9535,7 @@
./usr/share/man/man3/addnstr.3 comp-c-man .man
./usr/share/man/man3/addr.3 comp-c-man .man
./usr/share/man/man3/addstr.3 comp-c-man .man
./usr/share/man/man3/affinity.3 comp-c-man .man
./usr/share/man/man3/aio_cancel.3 comp-c-man .man
./usr/share/man/man3/aio_error.3 comp-c-man .man
./usr/share/man/man3/aio_fsync.3 comp-c-man .man
@ -9954,6 +9966,14 @@
./usr/share/man/man3/coshf.3 comp-c-man .man
./usr/share/man/man3/cpow.3 comp-c-man .man
./usr/share/man/man3/cpowf.3 comp-c-man .man
./usr/share/man/man3/cpuset.3 comp-c-man .man
./usr/share/man/man3/cpuset_create.3 comp-c-man .man
./usr/share/man/man3/cpuset_destroy.3 comp-c-man .man
./usr/share/man/man3/cpuset_zero.3 comp-c-man .man
./usr/share/man/man3/cpuset_set.3 comp-c-man .man
./usr/share/man/man3/cpuset_clr.3 comp-c-man .man
./usr/share/man/man3/cpuset_isset.3 comp-c-man .man
./usr/share/man/man3/cpuset_size.3 comp-c-man .man
./usr/share/man/man3/creal.3 comp-c-man .man
./usr/share/man/man3/crealf.3 comp-c-man .man
./usr/share/man/man3/creat.3 comp-c-man .man
@ -11972,6 +11992,7 @@
./usr/share/man/man3/pthread_detach.3 comp-c-man .man
./usr/share/man/man3/pthread_equal.3 comp-c-man .man
./usr/share/man/man3/pthread_exit.3 comp-c-man .man
./usr/share/man/man3/pthread_getaffinity.3 comp-c-man .man
./usr/share/man/man3/pthread_getname_np.3 comp-c-man .man
./usr/share/man/man3/pthread_getschedparam.3 comp-c-man .man
./usr/share/man/man3/pthread_getspecific.3 comp-c-man .man
@ -12005,6 +12026,7 @@
./usr/share/man/man3/pthread_rwlockattr_init.3 comp-c-man .man
./usr/share/man/man3/pthread_schedparam.3 comp-c-man .man
./usr/share/man/man3/pthread_self.3 comp-c-man .man
./usr/share/man/man3/pthread_setaffinity.3 comp-c-man .man
./usr/share/man/man3/pthread_setcancelstate.3 comp-c-man .man
./usr/share/man/man3/pthread_setcanceltype.3 comp-c-man .man
./usr/share/man/man3/pthread_setname_np.3 comp-c-man .man

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile.inc,v 1.159 2007/12/14 17:26:19 christos Exp $
# $NetBSD: Makefile.inc,v 1.160 2008/06/16 14:25:49 rmind Exp $
# from: @(#)Makefile.inc 8.6 (Berkeley) 5/4/95
# gen sources
@ -51,7 +51,7 @@ COPTS.execlp.c = -Wno-stack-protector
.include "${ARCHDIR}/gen/Makefile.inc"
MAN+= alarm.3 arc4random.3 basename.3 bswap.3 clock.3 closefrom.3 confstr.3 \
ctermid.3 ctype.3 daemon.3 devname.3 directory.3 dirname.3 \
cpuset.3 ctermid.3 ctype.3 daemon.3 devname.3 directory.3 dirname.3 \
endutxent.3 err.3 exec.3 extattr.3 \
fmtcheck.3 fmtmsg.3 fnmatch.3 fpclassify.3 fpgetmask.3 \
ftok.3 fts.3 ftw.3 \
@ -76,6 +76,8 @@ MAN+= alarm.3 arc4random.3 basename.3 bswap.3 clock.3 closefrom.3 confstr.3 \
wordexp.3
MLINKS+=bswap.3 bswap16.3 bswap.3 bswap32.3 bswap.3 bswap64.3
MLINKS+=cpuset.3 cpuset_create.3 cpuset_destroy.3 cpuset_zero.3 \
cpuset_set.3 cpuset_clr.3 cpuset_isset.3 cpuset_size.3
MLINKS+=directory.3 closedir.3 directory.3 dirfd.3 directory.3 opendir.3 \
directory.3 readdir.3 directory.3 readdir_r.3 directory.3 \
rewinddir.3 directory.3 seekdir.3 directory.3 telldir.3

118
lib/libc/gen/cpuset.3 Normal file
View File

@ -0,0 +1,118 @@
.\" $NetBSD: cpuset.3,v 1.1 2008/06/16 14:25:49 rmind Exp $
.\"
.\" Copyright (c) 2008 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Mindaugas Rasiukevicius <rmind at NetBSD org>.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd June 16, 2008
.Dt CPUSET 3
.Os
.Sh NAME
.Nm cpuset_create ,
.Nm cpuset_destroy ,
.Nm cpuset_zero ,
.Nm cpuset_set ,
.Nm cpuset_clr ,
.Nm cpuset_isset ,
.Nm cpuset_size
.Nd dynamic CPU sets
.Sh SYNOPSIS
.In sched.h
.Ft cpuset_t
.Fn cpuset_create "void"
.Ft void
.Fn cpuset_destroy "cpuset_t *set"
.Ft void
.Fn cpuset_zero "cpuset_t *set"
.Ft int
.Fn cpuset_set "cpuset_t *set" "cpuid_t cpu"
.Ft int
.Fn cpuset_clr "cpuset_t *set" "cpuid_t cpu"
.Ft int
.Fn cpuset_isset "const cpuset_t *set" "cpuid_t cpu"
.Ft size_t
.Fn cpuset_size "const cpuset_t *set"
.Sh DESCRIPTION
This section describes the functions used to create, set, use and destroy
the dynamic CPU sets.
.Pp
This API can be used with the POSIX threads, see
.Xr pthread 3
and
.Xr affinity 3 .
.Pp
The ID of primary CPU in the system is counted from 0.
.Sh FUNCTIONS
.Bl -tag -width compact
.It Fn cpuset_create
Allocates and initializes a clean CPU-set.
Returns the pointer to the CPU-set, or
.Dv NULL
on failure.
.It Fn cpuset_destroy set
Destroy the CPU-set specified by
.Fa set .
.It Fn cpuset_zero set
Makes the CPU-set specified by
.Fa set
clean, that is, memory is initialized to zero bytes, and none of
the CPUs set.
.It Fn cpuset_set set cpu
Sets the CPU specified by
.Fa cpu
in
.Fa set .
Returns zero on success, and \-1 if
.Fa cpu
is invalid.
.It Fn cpuset_clr set cpu
Clears the CPU specified by
.Fa cpu
in the CPU-set
.Fa set .
Returns zero on success, and \-1 if
.Fa cpu
is invalid.
.It Fn cpuset_isset set cpu
Checks if CPU specified by
.Fa cpu
is set in the CPU-set
.Fa set .
Returns the positive number if set, zero if not set, and \-1 if
.Fa cpu
is invalid.
.It Fn cpuset_size set
Returns the size in bytes of CPU-set specified by
.Fa set .
.El
.Sh SEE ALSO
.Xr affinity 3 ,
.Xr pset 3 ,
.Xr sched 3 ,
.Xr schedctl 8
.Sh HISTORY
The dynamic CPU sets appeared in
.Nx 5.0 .

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.51 2008/03/10 14:47:06 rmind Exp $
# $NetBSD: Makefile,v 1.52 2008/06/16 14:25:49 rmind Exp $
#
WARNS= 4
@ -91,7 +91,7 @@ _context_u.po: _context_u.o
${_MKTARGET_CREATE}
cp _context_u.o _context_u.po
MAN+= pthread.3 \
MAN+= affinity.3 pthread.3 \
pthread_attr.3 \
pthread_attr_getname_np.3 \
pthread_attr_setcreatesuspend_np.3 \
@ -117,6 +117,8 @@ MAN+= pthread.3 \
pthread_spin_init.3 pthread_spin_lock.3 pthread_spin_unlock.3 \
pthread_suspend_np.3 pthread_testcancel.3
MLINKS+= affinity.3 pthread_setaffinity_np.3
MLINKS+= affinity.3 pthread_getaffinity_np.3
MLINKS+= pthread_attr.3 pthread_attr_init.3
MLINKS+= pthread_attr.3 pthread_attr_destroy.3
MLINKS+= pthread_attr.3 pthread_attr_setdetachstate.3

125
lib/libpthread/affinity.3 Normal file
View File

@ -0,0 +1,125 @@
.\" $NetBSD: affinity.3,v 1.1 2008/06/16 14:25:49 rmind Exp $
.\"
.\" Copyright (c) 2008 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Mindaugas Rasiukevicius <rmind at NetBSD org>.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd June 15, 2008
.Dt AFFINITY 3
.Os
.Sh NAME
.Nm pthread_setaffinity_np ,
.Nm pthread_getaffinity_np
.Nd affinity of threads
.Sh LIBRARY
.Lb libpthread
.Sh SYNOPSIS
.In pthread.h
.In sched.h
.Ft int
.Fn pthread_setaffinity_np "pthread_t thread" "size_t size" "cpuset_t *set"
.Ft int
.Fn pthread_getaffinity_np "pthread_t thread" "size_t size" "cpuset_t *set"
.Sh DESCRIPTION
Thread affinity allows to run the thread on specified CPU or CPUs only.
.Pp
.Fn pthread_setaffinity_np
function sets the affinity mask
.Fa set
for
.Fa thread .
At least one valid CPU must be set in the mask.
.Pp
The
.Fn pthread_getaffinity_np
function gets the affinity mask of
.Fa thread
into
.Fa set .
.Pp
.Fa set
must be created and initialized using the
.Xr cpuset 3
functions.
.Sh RETURN VALUES
The
.Fn pthread_setaffinity_np
and
.Fn pthread_getaffinity_np
functions return 0 on success.
Otherwise, an error number is returned to indicate the error.
.Sh EXAMPLES
An example of code fragment, which sets the affinity for the current
thread to the CPU whose ID is 0:
.Bd -literal
cpuset_t *cset;
pthread_t pth;
cpuid_t ci;
cset = cpuset_create();
if (cset == NULL) {
err(EXIT_FAILURE, "cpuset_create");
}
ci = 0;
cpuset_set(ci);
pth = pthread_self();
error = pthread_setaffinity_np(pth, cpuset_size(cset), cset);
if (error) {
...
}
cpuset_destroy(cset);
.Ed
.Sh ERRORS
The
.Fn pthread_setaffinity_np
and
.Fn pthread_getaffinity_np
functions fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
The specified
.Fa set
was invalid.
.It Bq Er EPERM
The calling process lacks the appropriate privileges to perform
the operation.
.It Bq Er ESRCH
No thread could be found corresponding to the one specified by
.Fa thread .
.El
.Sh NOTES
Thread affinity might be used together with the processor sets, see
.Xr pset 3 .
In such case, the affinity mask takes precedence over the assignment
to processor sets.
.Sh SEE ALSO
.Xr cpuset 3 ,
.Xr pset 3 ,
.Xr pthread_getschedparam 3 ,
.Xr pthread_setschedparam 3 ,
.Xr sched 3 ,
.Xr schedctl 8