Blame man2/sched_setaffinity.2

Packit 7cfc04
.\" Copyright (C) 2002 Robert Love
Packit 7cfc04
.\" and Copyright (C) 2006, 2015 Michael Kerrisk
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
Packit 7cfc04
.\" This is free documentation; you can redistribute it and/or
Packit 7cfc04
.\" modify it under the terms of the GNU General Public License as
Packit 7cfc04
.\" published by the Free Software Foundation; either version 2 of
Packit 7cfc04
.\" the License, or (at your option) any later version.
Packit 7cfc04
.\"
Packit 7cfc04
.\" The GNU General Public License's references to "object code"
Packit 7cfc04
.\" and "executables" are to be interpreted as the output of any
Packit 7cfc04
.\" document formatting or typesetting system, including
Packit 7cfc04
.\" intermediate and printed output.
Packit 7cfc04
.\"
Packit 7cfc04
.\" This manual is distributed in the hope that it will be useful,
Packit 7cfc04
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 7cfc04
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 7cfc04
.\" GNU General Public License for more details.
Packit 7cfc04
.\"
Packit 7cfc04
.\" You should have received a copy of the GNU General Public
Packit 7cfc04
.\" License along with this manual; if not, see
Packit 7cfc04
.\" <http://www.gnu.org/licenses/>.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" 2002-11-19 Robert Love <rml@tech9.net> - initial version
Packit 7cfc04
.\" 2004-04-20 mtk - fixed description of return value
Packit 7cfc04
.\" 2004-04-22 aeb - added glibc prototype history
Packit 7cfc04
.\" 2005-05-03 mtk - noted that sched_setaffinity may cause thread
Packit 7cfc04
.\"	migration and that CPU affinity is a per-thread attribute.
Packit 7cfc04
.\" 2006-02-03 mtk -- Major rewrite
Packit 7cfc04
.\" 2008-11-12, mtk, removed CPU_*() macro descriptions to a
Packit 7cfc04
.\" separate CPU_SET(3) page.
Packit 7cfc04
.\"
Packit 7cfc04
.TH SCHED_SETAFFINITY 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
sched_setaffinity, sched_getaffinity \- \
Packit 7cfc04
set and get a thread's CPU affinity mask
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
Packit 7cfc04
.B #include <sched.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int sched_setaffinity(pid_t " pid ", size_t " cpusetsize ,
Packit 7cfc04
.BI "                      const cpu_set_t *" mask );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int sched_getaffinity(pid_t " pid ", size_t " cpusetsize ,
Packit 7cfc04
.BI "                      cpu_set_t *" mask );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
A thread's CPU affinity mask determines the set of CPUs on which
Packit 7cfc04
it is eligible to run.
Packit 7cfc04
On a multiprocessor system, setting the CPU affinity mask
Packit 7cfc04
can be used to obtain performance benefits.
Packit 7cfc04
For example,
Packit 7cfc04
by dedicating one CPU to a particular thread
Packit 7cfc04
(i.e., setting the affinity mask of that thread to specify a single CPU,
Packit 7cfc04
and setting the affinity mask of all other threads to exclude that CPU),
Packit 7cfc04
it is possible to ensure maximum execution speed for that thread.
Packit 7cfc04
Restricting a thread to run on a single CPU also avoids
Packit 7cfc04
the performance cost caused by the cache invalidation that occurs
Packit 7cfc04
when a thread ceases to execute on one CPU and then
Packit 7cfc04
recommences execution on a different CPU.
Packit 7cfc04
.PP
Packit 7cfc04
A CPU affinity mask is represented by the
Packit 7cfc04
.I cpu_set_t
Packit 7cfc04
structure, a "CPU set", pointed to by
Packit 7cfc04
.IR mask .
Packit 7cfc04
A set of macros for manipulating CPU sets is described in
Packit 7cfc04
.BR CPU_SET (3).
Packit 7cfc04
.PP
Packit 7cfc04
.BR sched_setaffinity ()
Packit 7cfc04
sets the CPU affinity mask of the thread whose ID is
Packit 7cfc04
.I pid
Packit 7cfc04
to the value specified by
Packit 7cfc04
.IR mask .
Packit 7cfc04
If
Packit 7cfc04
.I pid
Packit 7cfc04
is zero, then the calling thread is used.
Packit 7cfc04
The argument
Packit 7cfc04
.I cpusetsize
Packit 7cfc04
is the length (in bytes) of the data pointed to by
Packit 7cfc04
.IR mask .
Packit 7cfc04
Normally this argument would be specified as
Packit 7cfc04
.IR "sizeof(cpu_set_t)" .
Packit 7cfc04
.PP
Packit 7cfc04
If the thread specified by
Packit 7cfc04
.I pid
Packit 7cfc04
is not currently running on one of the CPUs specified in
Packit 7cfc04
.IR mask ,
Packit 7cfc04
then that thread is migrated to one of the CPUs specified in
Packit 7cfc04
.IR mask .
Packit 7cfc04
.PP
Packit 7cfc04
.BR sched_getaffinity ()
Packit 7cfc04
writes the affinity mask of the thread whose ID is
Packit 7cfc04
.I pid
Packit 7cfc04
into the
Packit 7cfc04
.I cpu_set_t
Packit 7cfc04
structure pointed to by
Packit 7cfc04
.IR mask .
Packit 7cfc04
The
Packit 7cfc04
.I cpusetsize
Packit 7cfc04
argument specifies the size (in bytes) of
Packit 7cfc04
.IR mask .
Packit 7cfc04
If
Packit 7cfc04
.I pid
Packit 7cfc04
is zero, then the mask of the calling thread is returned.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR sched_setaffinity ()
Packit 7cfc04
and
Packit 7cfc04
.BR sched_getaffinity ()
Packit 7cfc04
return 0.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
A supplied memory address was invalid.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The affinity bit mask
Packit 7cfc04
.I mask
Packit 7cfc04
contains no processors that are currently physically on the system
Packit 7cfc04
and permitted to the thread according to any restrictions that
Packit 7cfc04
may be imposed by
Packit 7cfc04
.I cpuset
Packit 7cfc04
cgroups or the "cpuset" mechanism described in
Packit 7cfc04
.BR cpuset (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.RB ( sched_getaffinity ()
Packit 7cfc04
and, in kernels before 2.6.9,
Packit 7cfc04
.BR sched_setaffinity ())
Packit 7cfc04
.I cpusetsize
Packit 7cfc04
is smaller than the size of the affinity mask used by the kernel.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
.RB ( sched_setaffinity ())
Packit 7cfc04
The calling thread does not have appropriate privileges.
Packit 7cfc04
The caller needs an effective user ID equal to the real user ID
Packit 7cfc04
or effective user ID of the thread identified by
Packit 7cfc04
.IR pid ,
Packit 7cfc04
or it must possess the
Packit 7cfc04
.B CAP_SYS_NICE
Packit 7cfc04
capability in the user namespace of the thread
Packit 7cfc04
.IR pid .
Packit 7cfc04
.TP
Packit 7cfc04
.B ESRCH
Packit 7cfc04
The thread whose ID is \fIpid\fP could not be found.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
The CPU affinity system calls were introduced in Linux kernel 2.5.8.
Packit 7cfc04
The system call wrappers were introduced in glibc 2.3.
Packit 7cfc04
Initially, the glibc interfaces included a
Packit 7cfc04
.I cpusetsize
Packit 7cfc04
argument, typed as
Packit 7cfc04
.IR "unsigned int" .
Packit 7cfc04
In glibc 2.3.3, the
Packit 7cfc04
.I cpusetsize
Packit 7cfc04
argument was removed, but was then restored in glibc 2.3.4, with type
Packit 7cfc04
.IR size_t .
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
These system calls are Linux-specific.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
After a call to
Packit 7cfc04
.BR sched_setaffinity (),
Packit 7cfc04
the set of CPUs on which the thread will actually run is
Packit 7cfc04
the intersection of the set specified in the
Packit 7cfc04
.I mask
Packit 7cfc04
argument and the set of CPUs actually present on the system.
Packit 7cfc04
The system may further restrict the set of CPUs on which the thread
Packit 7cfc04
runs if the "cpuset" mechanism described in
Packit 7cfc04
.BR cpuset (7)
Packit 7cfc04
is being used.
Packit 7cfc04
These restrictions on the actual set of CPUs on which the thread
Packit 7cfc04
will run are silently imposed by the kernel.
Packit 7cfc04
.PP
Packit 7cfc04
There are various ways of determining the number of CPUs
Packit 7cfc04
available on the system, including: inspecting the contents of
Packit 7cfc04
.IR /proc/cpuinfo ;
Packit 7cfc04
using
Packit 7cfc04
.BR sysconf (3)
Packit 7cfc04
to obtain the values of the
Packit 7cfc04
.BR _SC_NPROCESSORS_CONF
Packit 7cfc04
and
Packit 7cfc04
.BR _SC_NPROCESSORS_ONLN
Packit 7cfc04
parameters; and inspecting the list of CPU directories under
Packit 7cfc04
.IR /sys/devices/system/cpu/ .
Packit 7cfc04
.PP
Packit 7cfc04
.BR sched (7)
Packit 7cfc04
has a description of the Linux scheduling scheme.
Packit 7cfc04
.PP
Packit 7cfc04
The affinity mask is a per-thread attribute that can be
Packit 7cfc04
adjusted independently for each of the threads in a thread group.
Packit 7cfc04
The value returned from a call to
Packit 7cfc04
.BR gettid (2)
Packit 7cfc04
can be passed in the argument
Packit 7cfc04
.IR pid .
Packit 7cfc04
Specifying
Packit 7cfc04
.I pid
Packit 7cfc04
as 0 will set the attribute for the calling thread,
Packit 7cfc04
and passing the value returned from a call to
Packit 7cfc04
.BR getpid (2)
Packit 7cfc04
will set the attribute for the main thread of the thread group.
Packit 7cfc04
(If you are using the POSIX threads API, then use
Packit 7cfc04
.BR pthread_setaffinity_np (3)
Packit 7cfc04
instead of
Packit 7cfc04
.BR sched_setaffinity ().)
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I isolcpus
Packit 7cfc04
boot option can be used to isolate one or more CPUs at boot time,
Packit 7cfc04
so that no processes are scheduled onto those CPUs.
Packit 7cfc04
Following the use of this boot option,
Packit 7cfc04
the only way to schedule processes onto the isolated CPUs is via
Packit 7cfc04
.BR sched_setaffinity ()
Packit 7cfc04
or the
Packit 7cfc04
.BR cpuset (7)
Packit 7cfc04
mechanism.
Packit 7cfc04
For further information, see the kernel source file
Packit 7cfc04
.IR Documentation/admin-guide/kernel-parameters.txt .
Packit 7cfc04
As noted in that file,
Packit 7cfc04
.I isolcpus
Packit 7cfc04
is the preferred mechanism of isolating CPUs
Packit 7cfc04
(versus the alternative of manually setting the CPU affinity
Packit 7cfc04
of all processes on the system).
Packit 7cfc04
.PP
Packit 7cfc04
A child created via
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
inherits its parent's CPU affinity mask.
Packit 7cfc04
The affinity mask is preserved across an
Packit 7cfc04
.BR execve (2).
Packit 7cfc04
.SS C library/kernel differences
Packit 7cfc04
This manual page describes the glibc interface for the CPU affinity calls.
Packit 7cfc04
The actual system call interface is slightly different, with the
Packit 7cfc04
.I mask
Packit 7cfc04
being typed as
Packit 7cfc04
.IR "unsigned long\ *" ,
Packit 7cfc04
reflecting the fact that the underlying implementation of CPU
Packit 7cfc04
sets is a simple bit mask.
Packit 7cfc04
On success, the raw
Packit 7cfc04
.BR sched_getaffinity ()
Packit 7cfc04
system call returns the size (in bytes) of the
Packit 7cfc04
.I cpumask_t
Packit 7cfc04
data type that is used internally by the kernel to
Packit 7cfc04
represent the CPU set bit mask.
Packit 7cfc04
.SS Handling systems with large CPU affinity masks
Packit 7cfc04
The underlying system calls (which represent CPU masks as bit masks of type
Packit 7cfc04
.IR "unsigned long\ *" )
Packit 7cfc04
impose no restriction on the size of the CPU mask.
Packit 7cfc04
However, the
Packit 7cfc04
.I cpu_set_t
Packit 7cfc04
data type used by glibc has a fixed size of 128 bytes,
Packit 7cfc04
meaning that the maximum CPU number that can be represented is 1023.
Packit 7cfc04
.\" FIXME . See https://sourceware.org/bugzilla/show_bug.cgi?id=15630
Packit 7cfc04
.\" and https://sourceware.org/ml/libc-alpha/2013-07/msg00288.html
Packit 7cfc04
If the kernel CPU affinity mask is larger than 1024,
Packit 7cfc04
then calls of the form:
Packit 7cfc04
.PP
Packit 7cfc04
    sched_getaffinity(pid, sizeof(cpu_set_t), &mask);
Packit 7cfc04
.PP
Packit 7cfc04
fail with the error
Packit 7cfc04
.BR EINVAL ,
Packit 7cfc04
the error produced by the underlying system call for the case where the
Packit 7cfc04
.I mask
Packit 7cfc04
size specified in
Packit 7cfc04
.I cpusetsize
Packit 7cfc04
is smaller than the size of the affinity mask used by the kernel.
Packit 7cfc04
(Depending on the system CPU topology, the kernel affinity mask can
Packit 7cfc04
be substantially larger than the number of active CPUs in the system.)
Packit 7cfc04
.PP
Packit 7cfc04
When working on systems with large kernel CPU affinity masks,
Packit 7cfc04
one must dynamically allocate the
Packit 7cfc04
.I mask
Packit 7cfc04
argument (see
Packit 7cfc04
.BR CPU_ALLOC (3)).
Packit 7cfc04
Currently, the only way to do this is by probing for the size
Packit 7cfc04
of the required mask using
Packit 7cfc04
.BR sched_getaffinity ()
Packit 7cfc04
calls with increasing mask sizes (until the call does not fail with the error
Packit 7cfc04
.BR EINVAL ).
Packit 7cfc04
.PP
Packit 7cfc04
Be aware that
Packit 7cfc04
.BR CPU_ALLOC (3)
Packit 7cfc04
may allocate a slightly larger CPU set than requested
Packit 7cfc04
(because CPU sets are implemented as bit masks allocated in units of
Packit 7cfc04
.IR sizeof(long) ).
Packit 7cfc04
Consequently,
Packit 7cfc04
.BR sched_getaffinity ()
Packit 7cfc04
can set bits beyond the requested allocation size, because the kernel
Packit 7cfc04
sees a few additional bits.
Packit 7cfc04
Therefore, the caller should iterate over the bits in the returned set,
Packit 7cfc04
counting those which are set, and stop upon reaching the value returned by
Packit 7cfc04
.BR CPU_COUNT (3)
Packit 7cfc04
(rather than iterating over the number of bits
Packit 7cfc04
requested to be allocated).
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The program below creates a child process.
Packit 7cfc04
The parent and child then each assign themselves to a specified CPU
Packit 7cfc04
and execute identical loops that consume some CPU time.
Packit 7cfc04
Before terminating, the parent waits for the child to complete.
Packit 7cfc04
The program takes three command-line arguments:
Packit 7cfc04
the CPU number for the parent,
Packit 7cfc04
the CPU number for the child,
Packit 7cfc04
and the number of loop iterations that both processes should perform.
Packit 7cfc04
.PP
Packit 7cfc04
As the sample runs below demonstrate, the amount of real and CPU time
Packit 7cfc04
consumed when running the program will depend on intra-core caching effects
Packit 7cfc04
and whether the processes are using the same CPU.
Packit 7cfc04
.PP
Packit 7cfc04
We first employ
Packit 7cfc04
.BR lscpu (1)
Packit 7cfc04
to determine that this (x86)
Packit 7cfc04
system has two cores, each with two CPUs:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fBlscpu | grep -i 'core.*:|socket'\fP
Packit 7cfc04
Thread(s) per core:    2
Packit 7cfc04
Core(s) per socket:    2
Packit 7cfc04
Socket(s):             1
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
We then time the operation of the example program for three cases:
Packit 7cfc04
both processes running on the same CPU;
Packit 7cfc04
both processes running on different CPUs on the same core;
Packit 7cfc04
and both processes running on different CPUs on different cores.
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fBtime \-p ./a.out 0 0 100000000\fP
Packit 7cfc04
real 14.75
Packit 7cfc04
user 3.02
Packit 7cfc04
sys 11.73
Packit 7cfc04
$ \fBtime \-p ./a.out 0 1 100000000\fP
Packit 7cfc04
real 11.52
Packit 7cfc04
user 3.98
Packit 7cfc04
sys 19.06
Packit 7cfc04
$ \fBtime \-p ./a.out 0 3 100000000\fP
Packit 7cfc04
real 7.89
Packit 7cfc04
user 3.29
Packit 7cfc04
sys 12.07
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#define _GNU_SOURCE
Packit 7cfc04
#include <sched.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <sys/wait.h>
Packit 7cfc04
Packit 7cfc04
#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
Packit 7cfc04
                        } while (0)
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    cpu_set_t set;
Packit 7cfc04
    int parentCPU, childCPU;
Packit 7cfc04
    int nloops, j;
Packit 7cfc04
Packit 7cfc04
    if (argc != 4) {
Packit 7cfc04
        fprintf(stderr, "Usage: %s parent\-cpu child\-cpu num\-loops\\n",
Packit 7cfc04
                argv[0]);
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    parentCPU = atoi(argv[1]);
Packit 7cfc04
    childCPU = atoi(argv[2]);
Packit 7cfc04
    nloops = atoi(argv[3]);
Packit 7cfc04
Packit 7cfc04
    CPU_ZERO(&set);
Packit 7cfc04
Packit 7cfc04
    switch (fork()) {
Packit 7cfc04
    case \-1:            /* Error */
Packit 7cfc04
        errExit("fork");
Packit 7cfc04
Packit 7cfc04
    case 0:             /* Child */
Packit 7cfc04
        CPU_SET(childCPU, &set);
Packit 7cfc04
Packit 7cfc04
        if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1)
Packit 7cfc04
            errExit("sched_setaffinity");
Packit 7cfc04
Packit 7cfc04
        for (j = 0; j < nloops; j++)
Packit 7cfc04
            getppid();
Packit 7cfc04
Packit 7cfc04
        exit(EXIT_SUCCESS);
Packit 7cfc04
Packit 7cfc04
    default:            /* Parent */
Packit 7cfc04
        CPU_SET(parentCPU, &set);
Packit 7cfc04
Packit 7cfc04
        if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1)
Packit 7cfc04
            errExit("sched_setaffinity");
Packit 7cfc04
Packit 7cfc04
        for (j = 0; j < nloops; j++)
Packit 7cfc04
            getppid();
Packit 7cfc04
Packit 7cfc04
        wait(NULL);     /* Wait for child to terminate */
Packit 7cfc04
        exit(EXIT_SUCCESS);
Packit 7cfc04
    }
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.ad l
Packit 7cfc04
.nh
Packit 7cfc04
.BR lscpu (1),
Packit 7cfc04
.BR nproc (1),
Packit 7cfc04
.BR taskset (1),
Packit 7cfc04
.BR clone (2),
Packit 7cfc04
.BR getcpu (2),
Packit 7cfc04
.BR getpriority (2),
Packit 7cfc04
.BR gettid (2),
Packit 7cfc04
.BR nice (2),
Packit 7cfc04
.BR sched_get_priority_max (2),
Packit 7cfc04
.BR sched_get_priority_min (2),
Packit 7cfc04
.BR sched_getscheduler (2),
Packit 7cfc04
.BR sched_setscheduler (2),
Packit 7cfc04
.BR setpriority (2),
Packit 7cfc04
.BR CPU_SET (3),
Packit 7cfc04
.BR get_nprocs (3),
Packit 7cfc04
.BR pthread_setaffinity_np (3),
Packit 7cfc04
.BR sched_getcpu (3),
Packit 7cfc04
.BR capabilities (7),
Packit 7cfc04
.BR cpuset (7),
Packit 7cfc04
.BR sched (7),
Packit 7cfc04
.BR numactl (8)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.