blob: 4217329641ebfcb9c72bb8806480d6ef858c6c2d [file] [log] [blame]
<html lang="en">
<head>
<title>CPU Affinity - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Priority.html#Priority" title="Priority">
<link rel="prev" href="Traditional-Scheduling.html#Traditional-Scheduling" title="Traditional Scheduling">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="CPU-Affinity"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Traditional-Scheduling.html#Traditional-Scheduling">Traditional Scheduling</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Priority.html#Priority">Priority</a>
<hr>
</div>
<h4 class="subsection">22.3.5 Limiting execution to certain CPUs</h4>
<p>On a multi-processor system the operating system usually distributes
the different processes which are runnable on all available CPUs in a
way which allows the system to work most efficiently. Which processes
and threads run can be to some extend be control with the scheduling
functionality described in the last sections. But which CPU finally
executes which process or thread is not covered.
<p>There are a number of reasons why a program might want to have control
over this aspect of the system as well:
<ul>
<li>One thread or process is responsible for absolutely critical work
which under no circumstances must be interrupted or hindered from
making process by other process or threads using CPU resources. In
this case the special process would be confined to a CPU which no
other process or thread is allowed to use.
<li>The access to certain resources (RAM, I/O ports) has different costs
from different CPUs. This is the case in NUMA (Non-Uniform Memory
Architecture) machines. Preferably memory should be accessed locally
but this requirement is usually not visible to the scheduler.
Therefore forcing a process or thread to the CPUs which have local
access to the mostly used memory helps to significantly boost the
performance.
<li>In controlled runtimes resource allocation and book-keeping work (for
instance garbage collection) is performance local to processors. This
can help to reduce locking costs if the resources do not have to be
protected from concurrent accesses from different processors.
</ul>
<p>The POSIX standard up to this date is of not much help to solve this
problem. The Linux kernel provides a set of interfaces to allow
specifying <em>affinity sets</em> for a process. The scheduler will
schedule the thread or process on CPUs specified by the affinity
masks. The interfaces which the GNU C library define follow to some
extend the Linux kernel interface.
<!-- sched.h -->
<!-- GNU -->
<div class="defun">
&mdash; Data Type: <b>cpu_set_t</b><var><a name="index-cpu_005fset_005ft-2764"></a></var><br>
<blockquote><p>This data set is a bitset where each bit represents a CPU. How the
system's CPUs are mapped to bits in the bitset is system dependent.
The data type has a fixed size; in the unlikely case that the number
of bits are not sufficient to describe the CPUs of the system a
different interface has to be used.
<p>This type is a GNU extension and is defined in <samp><span class="file">sched.h</span></samp>.
</p></blockquote></div>
<p>To manipulate the bitset, to set and reset bits, a number of macros is
defined. Some of the macros take a CPU number as a parameter. Here
it is important to never exceed the size of the bitset. The following
macro specifies the number of bits in the <code>cpu_set_t</code> bitset.
<!-- sched.h -->
<!-- GNU -->
<div class="defun">
&mdash; Macro: int <b>CPU_SETSIZE</b><var><a name="index-CPU_005fSETSIZE-2765"></a></var><br>
<blockquote><p>The value of this macro is the maximum number of CPUs which can be
handled with a <code>cpu_set_t</code> object.
</p></blockquote></div>
<p>The type <code>cpu_set_t</code> should be considered opaque; all
manipulation should happen via the next four macros.
<!-- sched.h -->
<!-- GNU -->
<div class="defun">
&mdash; Macro: void <b>CPU_ZERO</b> (<var>cpu_set_t *set</var>)<var><a name="index-CPU_005fZERO-2766"></a></var><br>
<blockquote><p>This macro initializes the CPU set <var>set</var> to be the empty set.
<p>This macro is a GNU extension and is defined in <samp><span class="file">sched.h</span></samp>.
</p></blockquote></div>
<!-- sched.h -->
<!-- GNU -->
<div class="defun">
&mdash; Macro: void <b>CPU_SET</b> (<var>int cpu, cpu_set_t *set</var>)<var><a name="index-CPU_005fSET-2767"></a></var><br>
<blockquote><p>This macro adds <var>cpu</var> to the CPU set <var>set</var>.
<p>The <var>cpu</var> parameter must not have side effects since it is
evaluated more than once.
<p>This macro is a GNU extension and is defined in <samp><span class="file">sched.h</span></samp>.
</p></blockquote></div>
<!-- sched.h -->
<!-- GNU -->
<div class="defun">
&mdash; Macro: void <b>CPU_CLR</b> (<var>int cpu, cpu_set_t *set</var>)<var><a name="index-CPU_005fCLR-2768"></a></var><br>
<blockquote><p>This macro removes <var>cpu</var> from the CPU set <var>set</var>.
<p>The <var>cpu</var> parameter must not have side effects since it is
evaluated more than once.
<p>This macro is a GNU extension and is defined in <samp><span class="file">sched.h</span></samp>.
</p></blockquote></div>
<!-- sched.h -->
<!-- GNU -->
<div class="defun">
&mdash; Macro: int <b>CPU_ISSET</b> (<var>int cpu, const cpu_set_t *set</var>)<var><a name="index-CPU_005fISSET-2769"></a></var><br>
<blockquote><p>This macro returns a nonzero value (true) if <var>cpu</var> is a member
of the CPU set <var>set</var>, and zero (false) otherwise.
<p>The <var>cpu</var> parameter must not have side effects since it is
evaluated more than once.
<p>This macro is a GNU extension and is defined in <samp><span class="file">sched.h</span></samp>.
</p></blockquote></div>
<p>CPU bitsets can be constructed from scratch or the currently installed
affinity mask can be retrieved from the system.
<!-- sched.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>sched_getaffinity</b> (<var>pid_t pid, size_t cpusetsize, cpu_set_t *cpuset</var>)<var><a name="index-sched_005fgetaffinity-2770"></a></var><br>
<blockquote>
<p>This functions stores the CPU affinity mask for the process or thread
with the ID <var>pid</var> in the <var>cpusetsize</var> bytes long bitmap
pointed to by <var>cpuset</var>. If successful, the function always
initializes all bits in the <code>cpu_set_t</code> object and returns zero.
<p>If <var>pid</var> does not correspond to a process or thread on the system
the or the function fails for some other reason, it returns <code>-1</code>
and <code>errno</code> is set to represent the error condition.
<dl>
<dt><code>ESRCH</code><dd>No process or thread with the given ID found.
<br><dt><code>EFAULT</code><dd>The pointer <var>cpuset</var> is does not point to a valid object.
</dl>
<p>This function is a GNU extension and is declared in <samp><span class="file">sched.h</span></samp>.
</p></blockquote></div>
<p>Note that it is not portably possible to use this information to
retrieve the information for different POSIX threads. A separate
interface must be provided for that.
<!-- sched.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>sched_setaffinity</b> (<var>pid_t pid, size_t cpusetsize, const cpu_set_t *cpuset</var>)<var><a name="index-sched_005fsetaffinity-2771"></a></var><br>
<blockquote>
<p>This function installs the <var>cpusetsize</var> bytes long affinity mask
pointed to by <var>cpuset</var> for the process or thread with the ID <var>pid</var>.
If successful the function returns zero and the scheduler will in future
take the affinity information into account.
<p>If the function fails it will return <code>-1</code> and <code>errno</code> is set
to the error code:
<dl>
<dt><code>ESRCH</code><dd>No process or thread with the given ID found.
<br><dt><code>EFAULT</code><dd>The pointer <var>cpuset</var> is does not point to a valid object.
<br><dt><code>EINVAL</code><dd>The bitset is not valid. This might mean that the affinity set might
not leave a processor for the process or thread to run on.
</dl>
<p>This function is a GNU extension and is declared in <samp><span class="file">sched.h</span></samp>.
</p></blockquote></div>
</body></html>