-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU-Affinity.html
More file actions
225 lines (183 loc) · 9.92 KB
/
CPU-Affinity.html
File metadata and controls
225 lines (183 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<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.9">
<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.11, last updated 2007-09-09,
of `The GNU C Library Reference Manual', for version 2.7.
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007 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.2 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 are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF 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>
</head>
<body>
<div class="node">
<p>
<a name="CPU-Affinity"></a>
Previous: <a rel="previous" accesskey="p" href="Traditional-Scheduling.html#Traditional-Scheduling">Traditional Scheduling</a>,
Up: <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 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">
— Data Type: <b>cpu_set_t</b><var><a name="index-cpu_005fset_005ft-2763"></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">
— Macro: int <b>CPU_SETSIZE</b><var><a name="index-CPU_005fSETSIZE-2764"></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">
— Macro: void <b>CPU_ZERO</b> (<var>cpu_set_t *set</var>)<var><a name="index-CPU_005fZERO-2765"></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">
— Macro: void <b>CPU_SET</b> (<var>int cpu, cpu_set_t *set</var>)<var><a name="index-CPU_005fSET-2766"></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">
— Macro: void <b>CPU_CLR</b> (<var>int cpu, cpu_set_t *set</var>)<var><a name="index-CPU_005fCLR-2767"></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">
— Macro: int <b>CPU_ISSET</b> (<var>int cpu, const cpu_set_t *set</var>)<var><a name="index-CPU_005fISSET-2768"></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">
— Function: int <b>sched_getaffinity</b> (<var>pid_t pid, size_t cpusetsize, cpu_set_t *cpuset</var>)<var><a name="index-sched_005fgetaffinity-2769"></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">
— 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-2770"></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>