]>
Commit | Line | Data |
---|---|---|
5ce8f203 UD |
1 | @node Resource Usage And Limitation, Non-Local Exits, Date and Time, Top |
2 | @c %MENU% Functions for examining resource usage and getting and setting limits | |
3 | @chapter Resource Usage And Limitation | |
4 | This chapter describes functions for examining how much of various kinds of | |
5 | resources (CPU time, memory, etc.) a process has used and getting and setting | |
6 | limits on future usage. | |
7 | ||
8 | @menu | |
9 | * Resource Usage:: Measuring various resources used. | |
10 | * Limits on Resources:: Specifying limits on resource usage. | |
11 | * Priority:: Reading or setting process run priority. | |
b642f101 UD |
12 | * Memory Resources:: Querying memory available resources. |
13 | * Processor Resources:: Learn about the processors available. | |
5ce8f203 UD |
14 | @end menu |
15 | ||
16 | ||
17 | @node Resource Usage | |
18 | @section Resource Usage | |
19 | ||
20 | @pindex sys/resource.h | |
21 | The function @code{getrusage} and the data type @code{struct rusage} | |
22 | are used to examine the resource usage of a process. They are declared | |
23 | in @file{sys/resource.h}. | |
24 | ||
5ce8f203 | 25 | @deftypefun int getrusage (int @var{processes}, struct rusage *@var{rusage}) |
d08a7e4c | 26 | @standards{BSD, sys/resource.h} |
c8ce789c AO |
27 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
28 | @c On HURD, this calls task_info 3 times. On UNIX, it's a syscall. | |
5ce8f203 UD |
29 | This function reports resource usage totals for processes specified by |
30 | @var{processes}, storing the information in @code{*@var{rusage}}. | |
31 | ||
32 | In most systems, @var{processes} has only two valid values: | |
33 | ||
a449fc68 | 34 | @vtable @code |
5ce8f203 | 35 | @item RUSAGE_SELF |
d08a7e4c | 36 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
37 | Just the current process. |
38 | ||
5ce8f203 | 39 | @item RUSAGE_CHILDREN |
d08a7e4c | 40 | @standards{BSD, sys/resource.h} |
5ce8f203 | 41 | All child processes (direct and indirect) that have already terminated. |
a449fc68 | 42 | @end vtable |
5ce8f203 | 43 | |
5ce8f203 UD |
44 | The return value of @code{getrusage} is zero for success, and @code{-1} |
45 | for failure. | |
46 | ||
47 | @table @code | |
48 | @item EINVAL | |
49 | The argument @var{processes} is not valid. | |
50 | @end table | |
51 | @end deftypefun | |
52 | ||
53 | One way of getting resource usage for a particular child process is with | |
54 | the function @code{wait4}, which returns totals for a child when it | |
55 | terminates. @xref{BSD Wait Functions}. | |
56 | ||
5ce8f203 | 57 | @deftp {Data Type} {struct rusage} |
d08a7e4c | 58 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
59 | This data type stores various resource usage statistics. It has the |
60 | following members, and possibly others: | |
61 | ||
62 | @table @code | |
63 | @item struct timeval ru_utime | |
64 | Time spent executing user instructions. | |
65 | ||
66 | @item struct timeval ru_stime | |
67 | Time spent in operating system code on behalf of @var{processes}. | |
68 | ||
69 | @item long int ru_maxrss | |
70 | The maximum resident set size used, in kilobytes. That is, the maximum | |
71 | number of kilobytes of physical memory that @var{processes} used | |
72 | simultaneously. | |
73 | ||
74 | @item long int ru_ixrss | |
75 | An integral value expressed in kilobytes times ticks of execution, which | |
76 | indicates the amount of memory used by text that was shared with other | |
77 | processes. | |
78 | ||
79 | @item long int ru_idrss | |
80 | An integral value expressed the same way, which is the amount of | |
81 | unshared memory used for data. | |
82 | ||
83 | @item long int ru_isrss | |
84 | An integral value expressed the same way, which is the amount of | |
85 | unshared memory used for stack space. | |
86 | ||
87 | @item long int ru_minflt | |
88 | The number of page faults which were serviced without requiring any I/O. | |
89 | ||
90 | @item long int ru_majflt | |
91 | The number of page faults which were serviced by doing I/O. | |
92 | ||
93 | @item long int ru_nswap | |
94 | The number of times @var{processes} was swapped entirely out of main memory. | |
95 | ||
96 | @item long int ru_inblock | |
97 | The number of times the file system had to read from the disk on behalf | |
98 | of @var{processes}. | |
99 | ||
100 | @item long int ru_oublock | |
101 | The number of times the file system had to write to the disk on behalf | |
102 | of @var{processes}. | |
103 | ||
104 | @item long int ru_msgsnd | |
105 | Number of IPC messages sent. | |
106 | ||
107 | @item long int ru_msgrcv | |
108 | Number of IPC messages received. | |
109 | ||
110 | @item long int ru_nsignals | |
111 | Number of signals received. | |
112 | ||
113 | @item long int ru_nvcsw | |
114 | The number of times @var{processes} voluntarily invoked a context switch | |
115 | (usually to wait for some service). | |
116 | ||
117 | @item long int ru_nivcsw | |
118 | The number of times an involuntary context switch took place (because | |
119 | a time slice expired, or another process of higher priority was | |
120 | scheduled). | |
121 | @end table | |
122 | @end deftp | |
123 | ||
5ce8f203 UD |
124 | @node Limits on Resources |
125 | @section Limiting Resource Usage | |
126 | @cindex resource limits | |
127 | @cindex limits on resource usage | |
128 | @cindex usage limits | |
129 | ||
130 | You can specify limits for the resource usage of a process. When the | |
131 | process tries to exceed a limit, it may get a signal, or the system call | |
132 | by which it tried to do so may fail, depending on the resource. Each | |
133 | process initially inherits its limit values from its parent, but it can | |
134 | subsequently change them. | |
135 | ||
136 | There are two per-process limits associated with a resource: | |
137 | @cindex limit | |
138 | ||
139 | @table @dfn | |
140 | @item current limit | |
141 | The current limit is the value the system will not allow usage to | |
142 | exceed. It is also called the ``soft limit'' because the process being | |
143 | limited can generally raise the current limit at will. | |
144 | @cindex current limit | |
145 | @cindex soft limit | |
146 | ||
147 | @item maximum limit | |
148 | The maximum limit is the maximum value to which a process is allowed to | |
149 | set its current limit. It is also called the ``hard limit'' because | |
150 | there is no way for a process to get around it. A process may lower | |
151 | its own maximum limit, but only the superuser may increase a maximum | |
152 | limit. | |
153 | @cindex maximum limit | |
154 | @cindex hard limit | |
155 | @end table | |
156 | ||
157 | @pindex sys/resource.h | |
158 | The symbols for use with @code{getrlimit}, @code{setrlimit}, | |
0bc93a2f | 159 | @code{getrlimit64}, and @code{setrlimit64} are defined in |
5ce8f203 UD |
160 | @file{sys/resource.h}. |
161 | ||
5ce8f203 | 162 | @deftypefun int getrlimit (int @var{resource}, struct rlimit *@var{rlp}) |
d08a7e4c | 163 | @standards{BSD, sys/resource.h} |
c8ce789c AO |
164 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
165 | @c Direct syscall on most systems. | |
5ce8f203 UD |
166 | Read the current and maximum limits for the resource @var{resource} |
167 | and store them in @code{*@var{rlp}}. | |
168 | ||
169 | The return value is @code{0} on success and @code{-1} on failure. The | |
170 | only possible @code{errno} error condition is @code{EFAULT}. | |
171 | ||
172 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
173 | 32-bit system this function is in fact @code{getrlimit64}. Thus, the | |
174 | LFS interface transparently replaces the old interface. | |
175 | @end deftypefun | |
176 | ||
5ce8f203 | 177 | @deftypefun int getrlimit64 (int @var{resource}, struct rlimit64 *@var{rlp}) |
d08a7e4c | 178 | @standards{Unix98, sys/resource.h} |
c8ce789c AO |
179 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
180 | @c Direct syscall on most systems, wrapper to getrlimit otherwise. | |
5ce8f203 UD |
181 | This function is similar to @code{getrlimit} but its second parameter is |
182 | a pointer to a variable of type @code{struct rlimit64}, which allows it | |
183 | to read values which wouldn't fit in the member of a @code{struct | |
184 | rlimit}. | |
185 | ||
186 | If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
187 | 32-bit machine, this function is available under the name | |
188 | @code{getrlimit} and so transparently replaces the old interface. | |
189 | @end deftypefun | |
190 | ||
5ce8f203 | 191 | @deftypefun int setrlimit (int @var{resource}, const struct rlimit *@var{rlp}) |
d08a7e4c | 192 | @standards{BSD, sys/resource.h} |
c8ce789c AO |
193 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
194 | @c Direct syscall on most systems; lock-taking critical section on HURD. | |
5ce8f203 UD |
195 | Store the current and maximum limits for the resource @var{resource} |
196 | in @code{*@var{rlp}}. | |
197 | ||
198 | The return value is @code{0} on success and @code{-1} on failure. The | |
199 | following @code{errno} error condition is possible: | |
200 | ||
201 | @table @code | |
202 | @item EPERM | |
203 | @itemize @bullet | |
204 | @item | |
205 | The process tried to raise a current limit beyond the maximum limit. | |
206 | ||
207 | @item | |
208 | The process tried to raise a maximum limit, but is not superuser. | |
209 | @end itemize | |
210 | @end table | |
211 | ||
212 | When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
213 | 32-bit system this function is in fact @code{setrlimit64}. Thus, the | |
214 | LFS interface transparently replaces the old interface. | |
215 | @end deftypefun | |
216 | ||
5ce8f203 | 217 | @deftypefun int setrlimit64 (int @var{resource}, const struct rlimit64 *@var{rlp}) |
d08a7e4c | 218 | @standards{Unix98, sys/resource.h} |
c8ce789c AO |
219 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
220 | @c Wrapper for setrlimit or direct syscall. | |
5ce8f203 UD |
221 | This function is similar to @code{setrlimit} but its second parameter is |
222 | a pointer to a variable of type @code{struct rlimit64} which allows it | |
223 | to set values which wouldn't fit in the member of a @code{struct | |
224 | rlimit}. | |
225 | ||
226 | If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a | |
227 | 32-bit machine this function is available under the name | |
228 | @code{setrlimit} and so transparently replaces the old interface. | |
229 | @end deftypefun | |
230 | ||
5ce8f203 | 231 | @deftp {Data Type} {struct rlimit} |
d08a7e4c | 232 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
233 | This structure is used with @code{getrlimit} to receive limit values, |
234 | and with @code{setrlimit} to specify limit values for a particular process | |
235 | and resource. It has two fields: | |
236 | ||
237 | @table @code | |
238 | @item rlim_t rlim_cur | |
239 | The current limit | |
240 | ||
241 | @item rlim_t rlim_max | |
242 | The maximum limit. | |
243 | @end table | |
244 | ||
245 | For @code{getrlimit}, the structure is an output; it receives the current | |
246 | values. For @code{setrlimit}, it specifies the new values. | |
247 | @end deftp | |
248 | ||
249 | For the LFS functions a similar type is defined in @file{sys/resource.h}. | |
250 | ||
5ce8f203 | 251 | @deftp {Data Type} {struct rlimit64} |
d08a7e4c | 252 | @standards{Unix98, sys/resource.h} |
5ce8f203 UD |
253 | This structure is analogous to the @code{rlimit} structure above, but |
254 | its components have wider ranges. It has two fields: | |
255 | ||
256 | @table @code | |
257 | @item rlim64_t rlim_cur | |
258 | This is analogous to @code{rlimit.rlim_cur}, but with a different type. | |
259 | ||
260 | @item rlim64_t rlim_max | |
261 | This is analogous to @code{rlimit.rlim_max}, but with a different type. | |
262 | @end table | |
263 | ||
264 | @end deftp | |
265 | ||
266 | Here is a list of resources for which you can specify a limit. Memory | |
267 | and file sizes are measured in bytes. | |
268 | ||
2fe82ca6 | 269 | @vtable @code |
5ce8f203 | 270 | @item RLIMIT_CPU |
d08a7e4c | 271 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
272 | The maximum amount of CPU time the process can use. If it runs for |
273 | longer than this, it gets a signal: @code{SIGXCPU}. The value is | |
274 | measured in seconds. @xref{Operation Error Signals}. | |
275 | ||
5ce8f203 | 276 | @item RLIMIT_FSIZE |
d08a7e4c | 277 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
278 | The maximum size of file the process can create. Trying to write a |
279 | larger file causes a signal: @code{SIGXFSZ}. @xref{Operation Error | |
280 | Signals}. | |
281 | ||
5ce8f203 | 282 | @item RLIMIT_DATA |
d08a7e4c | 283 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
284 | The maximum size of data memory for the process. If the process tries |
285 | to allocate data memory beyond this amount, the allocation function | |
286 | fails. | |
287 | ||
5ce8f203 | 288 | @item RLIMIT_STACK |
d08a7e4c | 289 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
290 | The maximum stack size for the process. If the process tries to extend |
291 | its stack past this size, it gets a @code{SIGSEGV} signal. | |
292 | @xref{Program Error Signals}. | |
293 | ||
5ce8f203 | 294 | @item RLIMIT_CORE |
d08a7e4c | 295 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
296 | The maximum size core file that this process can create. If the process |
297 | terminates and would dump a core file larger than this, then no core | |
298 | file is created. So setting this limit to zero prevents core files from | |
299 | ever being created. | |
300 | ||
5ce8f203 | 301 | @item RLIMIT_RSS |
d08a7e4c | 302 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
303 | The maximum amount of physical memory that this process should get. |
304 | This parameter is a guide for the system's scheduler and memory | |
305 | allocator; the system may give the process more memory when there is a | |
306 | surplus. | |
307 | ||
5ce8f203 | 308 | @item RLIMIT_MEMLOCK |
d08a7e4c | 309 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
310 | The maximum amount of memory that can be locked into physical memory (so |
311 | it will never be paged out). | |
312 | ||
5ce8f203 | 313 | @item RLIMIT_NPROC |
d08a7e4c | 314 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
315 | The maximum number of processes that can be created with the same user ID. |
316 | If you have reached the limit for your user ID, @code{fork} will fail | |
317 | with @code{EAGAIN}. @xref{Creating a Process}. | |
318 | ||
5ce8f203 | 319 | @item RLIMIT_NOFILE |
5ce8f203 | 320 | @itemx RLIMIT_OFILE |
d08a7e4c | 321 | @standardsx{RLIMIT_NOFILE, BSD, sys/resource.h} |
5ce8f203 UD |
322 | The maximum number of files that the process can open. If it tries to |
323 | open more files than this, its open attempt fails with @code{errno} | |
324 | @code{EMFILE}. @xref{Error Codes}. Not all systems support this limit; | |
325 | GNU does, and 4.4 BSD does. | |
326 | ||
5ce8f203 | 327 | @item RLIMIT_AS |
d08a7e4c | 328 | @standards{Unix98, sys/resource.h} |
5ce8f203 UD |
329 | The maximum size of total memory that this process should get. If the |
330 | process tries to allocate more memory beyond this amount with, for | |
331 | example, @code{brk}, @code{malloc}, @code{mmap} or @code{sbrk}, the | |
332 | allocation function fails. | |
333 | ||
5ce8f203 | 334 | @item RLIM_NLIMITS |
d08a7e4c | 335 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
336 | The number of different resource limits. Any valid @var{resource} |
337 | operand must be less than @code{RLIM_NLIMITS}. | |
2fe82ca6 | 338 | @end vtable |
5ce8f203 | 339 | |
8ded91fb | 340 | @deftypevr Constant rlim_t RLIM_INFINITY |
d08a7e4c | 341 | @standards{BSD, sys/resource.h} |
5ce8f203 UD |
342 | This constant stands for a value of ``infinity'' when supplied as |
343 | the limit value in @code{setrlimit}. | |
344 | @end deftypevr | |
345 | ||
346 | ||
347 | The following are historical functions to do some of what the functions | |
348 | above do. The functions above are better choices. | |
349 | ||
350 | @code{ulimit} and the command symbols are declared in @file{ulimit.h}. | |
351 | @pindex ulimit.h | |
5ce8f203 | 352 | |
8ded91fb | 353 | @deftypefun {long int} ulimit (int @var{cmd}, @dots{}) |
d08a7e4c | 354 | @standards{BSD, ulimit.h} |
c8ce789c AO |
355 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
356 | @c Wrapper for getrlimit, setrlimit or | |
357 | @c sysconf(_SC_OPEN_MAX)->getdtablesize->getrlimit. | |
5ce8f203 UD |
358 | |
359 | @code{ulimit} gets the current limit or sets the current and maximum | |
360 | limit for a particular resource for the calling process according to the | |
d3e22d59 | 361 | command @var{cmd}. |
5ce8f203 UD |
362 | |
363 | If you are getting a limit, the command argument is the only argument. | |
364 | If you are setting a limit, there is a second argument: | |
365 | @code{long int} @var{limit} which is the value to which you are setting | |
366 | the limit. | |
367 | ||
368 | The @var{cmd} values and the operations they specify are: | |
2fe82ca6 | 369 | @vtable @code |
5ce8f203 UD |
370 | |
371 | @item GETFSIZE | |
372 | Get the current limit on the size of a file, in units of 512 bytes. | |
373 | ||
374 | @item SETFSIZE | |
375 | Set the current and maximum limit on the size of a file to @var{limit} * | |
376 | 512 bytes. | |
377 | ||
2fe82ca6 | 378 | @end vtable |
5ce8f203 UD |
379 | |
380 | There are also some other @var{cmd} values that may do things on some | |
381 | systems, but they are not supported. | |
382 | ||
383 | Only the superuser may increase a maximum limit. | |
384 | ||
385 | When you successfully get a limit, the return value of @code{ulimit} is | |
386 | that limit, which is never negative. When you successfully set a limit, | |
387 | the return value is zero. When the function fails, the return value is | |
388 | @code{-1} and @code{errno} is set according to the reason: | |
389 | ||
390 | @table @code | |
391 | @item EPERM | |
392 | A process tried to increase a maximum limit, but is not superuser. | |
393 | @end table | |
394 | ||
395 | ||
396 | @end deftypefun | |
397 | ||
398 | @code{vlimit} and its resource symbols are declared in @file{sys/vlimit.h}. | |
5ce8f203 | 399 | @pindex sys/vlimit.h |
5ce8f203 UD |
400 | |
401 | @deftypefun int vlimit (int @var{resource}, int @var{limit}) | |
d08a7e4c | 402 | @standards{BSD, sys/vlimit.h} |
c8ce789c AO |
403 | @safety{@prelim{}@mtunsafe{@mtasurace{:setrlimit}}@asunsafe{}@acsafe{}} |
404 | @c It calls getrlimit and modifies the rlim_cur field before calling | |
405 | @c setrlimit. There's a window for a concurrent call to setrlimit that | |
406 | @c modifies e.g. rlim_max, which will be lost if running as super-user. | |
5ce8f203 UD |
407 | |
408 | @code{vlimit} sets the current limit for a resource for a process. | |
409 | ||
410 | @var{resource} identifies the resource: | |
411 | ||
2fe82ca6 | 412 | @vtable @code |
5ce8f203 UD |
413 | @item LIM_CPU |
414 | Maximum CPU time. Same as @code{RLIMIT_CPU} for @code{setrlimit}. | |
415 | @item LIM_FSIZE | |
416 | Maximum file size. Same as @code{RLIMIT_FSIZE} for @code{setrlimit}. | |
417 | @item LIM_DATA | |
418 | Maximum data memory. Same as @code{RLIMIT_DATA} for @code{setrlimit}. | |
419 | @item LIM_STACK | |
420 | Maximum stack size. Same as @code{RLIMIT_STACK} for @code{setrlimit}. | |
421 | @item LIM_CORE | |
422 | Maximum core file size. Same as @code{RLIMIT_COR} for @code{setrlimit}. | |
423 | @item LIM_MAXRSS | |
424 | Maximum physical memory. Same as @code{RLIMIT_RSS} for @code{setrlimit}. | |
2fe82ca6 | 425 | @end vtable |
5ce8f203 UD |
426 | |
427 | The return value is zero for success, and @code{-1} with @code{errno} set | |
428 | accordingly for failure: | |
429 | ||
430 | @table @code | |
431 | @item EPERM | |
432 | The process tried to set its current limit beyond its maximum limit. | |
433 | @end table | |
434 | ||
435 | @end deftypefun | |
436 | ||
437 | @node Priority | |
639c6286 | 438 | @section Process CPU Priority And Scheduling |
5ce8f203 | 439 | @cindex process priority |
639c6286 | 440 | @cindex cpu priority |
5ce8f203 UD |
441 | @cindex priority of a process |
442 | ||
639c6286 UD |
443 | When multiple processes simultaneously require CPU time, the system's |
444 | scheduling policy and process CPU priorities determine which processes | |
445 | get it. This section describes how that determination is made and | |
1f77f049 | 446 | @glibcadj{} functions to control it. |
639c6286 UD |
447 | |
448 | It is common to refer to CPU scheduling simply as scheduling and a | |
449 | process' CPU priority simply as the process' priority, with the CPU | |
450 | resource being implied. Bear in mind, though, that CPU time is not the | |
451 | only resource a process uses or that processes contend for. In some | |
452 | cases, it is not even particularly important. Giving a process a high | |
453 | ``priority'' may have very little effect on how fast a process runs with | |
454 | respect to other processes. The priorities discussed in this section | |
455 | apply only to CPU time. | |
456 | ||
457 | CPU scheduling is a complex issue and different systems do it in wildly | |
458 | different ways. New ideas continually develop and find their way into | |
459 | the intricacies of the various systems' scheduling algorithms. This | |
87b56f36 | 460 | section discusses the general concepts, some specifics of systems |
1f77f049 | 461 | that commonly use @theglibc{}, and some standards. |
639c6286 UD |
462 | |
463 | For simplicity, we talk about CPU contention as if there is only one CPU | |
464 | in the system. But all the same principles apply when a processor has | |
465 | multiple CPUs, and knowing that the number of processes that can run at | |
466 | any one time is equal to the number of CPUs, you can easily extrapolate | |
467 | the information. | |
468 | ||
469 | The functions described in this section are all defined by the POSIX.1 | |
95fdc6a0 | 470 | and POSIX.1b standards (the @code{sched@dots{}} functions are POSIX.1b). |
639c6286 UD |
471 | However, POSIX does not define any semantics for the values that these |
472 | functions get and set. In this chapter, the semantics are based on the | |
473 | Linux kernel's implementation of the POSIX standard. As you will see, | |
474 | the Linux implementation is quite the inverse of what the authors of the | |
475 | POSIX syntax had in mind. | |
476 | ||
477 | @menu | |
478 | * Absolute Priority:: The first tier of priority. Posix | |
479 | * Realtime Scheduling:: Scheduling among the process nobility | |
480 | * Basic Scheduling Functions:: Get/set scheduling policy, priority | |
481 | * Traditional Scheduling:: Scheduling among the vulgar masses | |
d9997a45 | 482 | * CPU Affinity:: Limiting execution to certain CPUs |
639c6286 UD |
483 | @end menu |
484 | ||
485 | ||
486 | ||
487 | @node Absolute Priority | |
488 | @subsection Absolute Priority | |
489 | @cindex absolute priority | |
490 | @cindex priority, absolute | |
491 | ||
492 | Every process has an absolute priority, and it is represented by a number. | |
493 | The higher the number, the higher the absolute priority. | |
494 | ||
495 | @cindex realtime CPU scheduling | |
496 | On systems of the past, and most systems today, all processes have | |
497 | absolute priority 0 and this section is irrelevant. In that case, | |
498 | @xref{Traditional Scheduling}. Absolute priorities were invented to | |
0bc93a2f | 499 | accommodate realtime systems, in which it is vital that certain processes |
639c6286 UD |
500 | be able to respond to external events happening in real time, which |
501 | means they cannot wait around while some other process that @emph{wants | |
502 | to}, but doesn't @emph{need to} run occupies the CPU. | |
503 | ||
504 | @cindex ready to run | |
505 | @cindex preemptive scheduling | |
506 | When two processes are in contention to use the CPU at any instant, the | |
507 | one with the higher absolute priority always gets it. This is true even if the | |
11bf311e | 508 | process with the lower priority is already using the CPU (i.e., the |
639c6286 UD |
509 | scheduling is preemptive). Of course, we're only talking about |
510 | processes that are running or ``ready to run,'' which means they are | |
511 | ready to execute instructions right now. When a process blocks to wait | |
512 | for something like I/O, its absolute priority is irrelevant. | |
513 | ||
514 | @cindex runnable process | |
48b22986 | 515 | @strong{NB:} The term ``runnable'' is a synonym for ``ready to run.'' |
639c6286 UD |
516 | |
517 | When two processes are running or ready to run and both have the same | |
518 | absolute priority, it's more interesting. In that case, who gets the | |
0bc93a2f | 519 | CPU is determined by the scheduling policy. If the processes have |
639c6286 UD |
520 | absolute priority 0, the traditional scheduling policy described in |
521 | @ref{Traditional Scheduling} applies. Otherwise, the policies described | |
522 | in @ref{Realtime Scheduling} apply. | |
523 | ||
524 | You normally give an absolute priority above 0 only to a process that | |
525 | can be trusted not to hog the CPU. Such processes are designed to block | |
526 | (or terminate) after relatively short CPU runs. | |
527 | ||
528 | A process begins life with the same absolute priority as its parent | |
529 | process. Functions described in @ref{Basic Scheduling Functions} can | |
530 | change it. | |
531 | ||
532 | Only a privileged process can change a process' absolute priority to | |
533 | something other than @code{0}. Only a privileged process or the | |
534 | target process' owner can change its absolute priority at all. | |
535 | ||
536 | POSIX requires absolute priority values used with the realtime | |
537 | scheduling policies to be consecutive with a range of at least 32. On | |
538 | Linux, they are 1 through 99. The functions | |
539 | @code{sched_get_priority_max} and @code{sched_set_priority_min} portably | |
540 | tell you what the range is on a particular system. | |
541 | ||
542 | ||
543 | @subsubsection Using Absolute Priority | |
544 | ||
545 | One thing you must keep in mind when designing real time applications is | |
546 | that having higher absolute priority than any other process doesn't | |
547 | guarantee the process can run continuously. Two things that can wreck a | |
87b56f36 | 548 | good CPU run are interrupts and page faults. |
639c6286 UD |
549 | |
550 | Interrupt handlers live in that limbo between processes. The CPU is | |
551 | executing instructions, but they aren't part of any process. An | |
552 | interrupt will stop even the highest priority process. So you must | |
553 | allow for slight delays and make sure that no device in the system has | |
554 | an interrupt handler that could cause too long a delay between | |
555 | instructions for your process. | |
556 | ||
557 | Similarly, a page fault causes what looks like a straightforward | |
558 | sequence of instructions to take a long time. The fact that other | |
559 | processes get to run while the page faults in is of no consequence, | |
d3e22d59 | 560 | because as soon as the I/O is complete, the higher priority process will |
639c6286 UD |
561 | kick them out and run again, but the wait for the I/O itself could be a |
562 | problem. To neutralize this threat, use @code{mlock} or | |
563 | @code{mlockall}. | |
564 | ||
565 | There are a few ramifications of the absoluteness of this priority on a | |
566 | single-CPU system that you need to keep in mind when you choose to set a | |
567 | priority and also when you're working on a program that runs with high | |
568 | absolute priority. Consider a process that has higher absolute priority | |
569 | than any other process in the system and due to a bug in its program, it | |
570 | gets into an infinite loop. It will never cede the CPU. You can't run | |
571 | a command to kill it because your command would need to get the CPU in | |
572 | order to run. The errant program is in complete control. It controls | |
573 | the vertical, it controls the horizontal. | |
574 | ||
575 | There are two ways to avoid this: 1) keep a shell running somewhere with | |
d3e22d59 | 576 | a higher absolute priority or 2) keep a controlling terminal attached to |
639c6286 UD |
577 | the high priority process group. All the priority in the world won't |
578 | stop an interrupt handler from running and delivering a signal to the | |
579 | process if you hit Control-C. | |
580 | ||
95fdc6a0 | 581 | Some systems use absolute priority as a means of allocating a fixed |
0bc93a2f | 582 | percentage of CPU time to a process. To do this, a super high priority |
639c6286 UD |
583 | privileged process constantly monitors the process' CPU usage and raises |
584 | its absolute priority when the process isn't getting its entitled share | |
585 | and lowers it when the process is exceeding it. | |
586 | ||
48b22986 | 587 | @strong{NB:} The absolute priority is sometimes called the ``static |
639c6286 UD |
588 | priority.'' We don't use that term in this manual because it misses the |
589 | most important feature of the absolute priority: its absoluteness. | |
590 | ||
591 | ||
592 | @node Realtime Scheduling | |
593 | @subsection Realtime Scheduling | |
b642f101 | 594 | @cindex realtime scheduling |
639c6286 UD |
595 | |
596 | Whenever two processes with the same absolute priority are ready to run, | |
597 | the kernel has a decision to make, because only one can run at a time. | |
598 | If the processes have absolute priority 0, the kernel makes this decision | |
599 | as described in @ref{Traditional Scheduling}. Otherwise, the decision | |
600 | is as described in this section. | |
601 | ||
602 | If two processes are ready to run but have different absolute priorities, | |
603 | the decision is much simpler, and is described in @ref{Absolute | |
604 | Priority}. | |
605 | ||
87b56f36 | 606 | Each process has a scheduling policy. For processes with absolute |
639c6286 UD |
607 | priority other than zero, there are two available: |
608 | ||
609 | @enumerate | |
610 | @item | |
611 | First Come First Served | |
612 | @item | |
613 | Round Robin | |
614 | @end enumerate | |
615 | ||
616 | The most sensible case is where all the processes with a certain | |
617 | absolute priority have the same scheduling policy. We'll discuss that | |
618 | first. | |
619 | ||
620 | In Round Robin, processes share the CPU, each one running for a small | |
621 | quantum of time (``time slice'') and then yielding to another in a | |
622 | circular fashion. Of course, only processes that are ready to run and | |
623 | have the same absolute priority are in this circle. | |
624 | ||
625 | In First Come First Served, the process that has been waiting the | |
626 | longest to run gets the CPU, and it keeps it until it voluntarily | |
627 | relinquishes the CPU, runs out of things to do (blocks), or gets | |
628 | preempted by a higher priority process. | |
629 | ||
630 | First Come First Served, along with maximal absolute priority and | |
631 | careful control of interrupts and page faults, is the one to use when a | |
632 | process absolutely, positively has to run at full CPU speed or not at | |
633 | all. | |
634 | ||
635 | Judicious use of @code{sched_yield} function invocations by processes | |
636 | with First Come First Served scheduling policy forms a good compromise | |
637 | between Round Robin and First Come First Served. | |
638 | ||
639 | To understand how scheduling works when processes of different scheduling | |
640 | policies occupy the same absolute priority, you have to know the nitty | |
d3e22d59 | 641 | gritty details of how processes enter and exit the ready to run list. |
639c6286 UD |
642 | |
643 | In both cases, the ready to run list is organized as a true queue, where | |
644 | a process gets pushed onto the tail when it becomes ready to run and is | |
645 | popped off the head when the scheduler decides to run it. Note that | |
646 | ready to run and running are two mutually exclusive states. When the | |
647 | scheduler runs a process, that process is no longer ready to run and no | |
648 | longer in the ready to run list. When the process stops running, it | |
649 | may go back to being ready to run again. | |
650 | ||
651 | The only difference between a process that is assigned the Round Robin | |
652 | scheduling policy and a process that is assigned First Come First Serve | |
653 | is that in the former case, the process is automatically booted off the | |
654 | CPU after a certain amount of time. When that happens, the process goes | |
655 | back to being ready to run, which means it enters the queue at the tail. | |
656 | The time quantum we're talking about is small. Really small. This is | |
657 | not your father's timesharing. For example, with the Linux kernel, the | |
658 | round robin time slice is a thousand times shorter than its typical | |
659 | time slice for traditional scheduling. | |
660 | ||
661 | A process begins life with the same scheduling policy as its parent process. | |
662 | Functions described in @ref{Basic Scheduling Functions} can change it. | |
663 | ||
664 | Only a privileged process can set the scheduling policy of a process | |
665 | that has absolute priority higher than 0. | |
666 | ||
667 | @node Basic Scheduling Functions | |
668 | @subsection Basic Scheduling Functions | |
669 | ||
1f77f049 | 670 | This section describes functions in @theglibc{} for setting the |
639c6286 UD |
671 | absolute priority and scheduling policy of a process. |
672 | ||
673 | @strong{Portability Note:} On systems that have the functions in this | |
674 | section, the macro _POSIX_PRIORITY_SCHEDULING is defined in | |
675 | @file{<unistd.h>}. | |
676 | ||
677 | For the case that the scheduling policy is traditional scheduling, more | |
678 | functions to fine tune the scheduling are in @ref{Traditional Scheduling}. | |
679 | ||
680 | Don't try to make too much out of the naming and structure of these | |
681 | functions. They don't match the concepts described in this manual | |
682 | because the functions are as defined by POSIX.1b, but the implementation | |
1f77f049 | 683 | on systems that use @theglibc{} is the inverse of what the POSIX |
639c6286 UD |
684 | structure contemplates. The POSIX scheme assumes that the primary |
685 | scheduling parameter is the scheduling policy and that the priority | |
686 | value, if any, is a parameter of the scheduling policy. In the | |
687 | implementation, though, the priority value is king and the scheduling | |
688 | policy, if anything, only fine tunes the effect of that priority. | |
689 | ||
690 | The symbols in this section are declared by including file @file{sched.h}. | |
691 | ||
c70824b9 FW |
692 | @strong{Portability Note:} In POSIX, the @code{pid_t} arguments of the |
693 | functions below refer to process IDs. On Linux, they are actually | |
694 | thread IDs, and control how specific threads are scheduled with | |
695 | regards to the entire system. The resulting behavior does not conform | |
696 | to POSIX. This is why the following description refers to tasks and | |
697 | tasks IDs, and not processes and process IDs. | |
698 | @c https://sourceware.org/bugzilla/show_bug.cgi?id=14829 | |
699 | ||
639c6286 | 700 | @deftp {Data Type} {struct sched_param} |
d08a7e4c | 701 | @standards{POSIX, sched.h} |
639c6286 UD |
702 | This structure describes an absolute priority. |
703 | @table @code | |
704 | @item int sched_priority | |
705 | absolute priority value | |
706 | @end table | |
707 | @end deftp | |
708 | ||
639c6286 | 709 | @deftypefun int sched_setscheduler (pid_t @var{pid}, int @var{policy}, const struct sched_param *@var{param}) |
d08a7e4c | 710 | @standards{POSIX, sched.h} |
c8ce789c AO |
711 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
712 | @c Direct syscall, Linux only. | |
639c6286 UD |
713 | |
714 | This function sets both the absolute priority and the scheduling policy | |
c70824b9 | 715 | for a task. |
639c6286 UD |
716 | |
717 | It assigns the absolute priority value given by @var{param} and the | |
c70824b9 FW |
718 | scheduling policy @var{policy} to the task with ID @var{pid}, |
719 | or the calling task if @var{pid} is zero. If @var{policy} is | |
0bc93a2f | 720 | negative, @code{sched_setscheduler} keeps the existing scheduling policy. |
639c6286 UD |
721 | |
722 | The following macros represent the valid values for @var{policy}: | |
723 | ||
2fe82ca6 | 724 | @vtable @code |
639c6286 UD |
725 | @item SCHED_OTHER |
726 | Traditional Scheduling | |
727 | @item SCHED_FIFO | |
87b56f36 | 728 | First In First Out |
639c6286 UD |
729 | @item SCHED_RR |
730 | Round Robin | |
2fe82ca6 | 731 | @end vtable |
639c6286 UD |
732 | |
733 | @c The Linux kernel code (in sched.c) actually reschedules the process, | |
734 | @c but it puts it at the head of the run queue, so I'm not sure just what | |
735 | @c the effect is, but it must be subtle. | |
736 | ||
737 | On success, the return value is @code{0}. Otherwise, it is @code{-1} | |
738 | and @code{ERRNO} is set accordingly. The @code{errno} values specific | |
739 | to this function are: | |
740 | ||
741 | @table @code | |
742 | @item EPERM | |
743 | @itemize @bullet | |
744 | @item | |
c70824b9 | 745 | The calling task does not have @code{CAP_SYS_NICE} permission and |
639c6286 UD |
746 | @var{policy} is not @code{SCHED_OTHER} (or it's negative and the |
747 | existing policy is not @code{SCHED_OTHER}. | |
748 | ||
749 | @item | |
c70824b9 FW |
750 | The calling task does not have @code{CAP_SYS_NICE} permission and its |
751 | owner is not the target task's owner. I.e., the effective uid of the | |
752 | calling task is neither the effective nor the real uid of task | |
639c6286 UD |
753 | @var{pid}. |
754 | @c We need a cross reference to the capabilities section, when written. | |
755 | @end itemize | |
756 | ||
757 | @item ESRCH | |
c70824b9 | 758 | There is no task with pid @var{pid} and @var{pid} is not zero. |
639c6286 UD |
759 | |
760 | @item EINVAL | |
761 | @itemize @bullet | |
762 | @item | |
763 | @var{policy} does not identify an existing scheduling policy. | |
764 | ||
765 | @item | |
766 | The absolute priority value identified by *@var{param} is outside the | |
767 | valid range for the scheduling policy @var{policy} (or the existing | |
768 | scheduling policy if @var{policy} is negative) or @var{param} is | |
769 | null. @code{sched_get_priority_max} and @code{sched_get_priority_min} | |
770 | tell you what the valid range is. | |
771 | ||
772 | @item | |
773 | @var{pid} is negative. | |
774 | @end itemize | |
775 | @end table | |
776 | ||
777 | @end deftypefun | |
778 | ||
779 | ||
639c6286 | 780 | @deftypefun int sched_getscheduler (pid_t @var{pid}) |
d08a7e4c | 781 | @standards{POSIX, sched.h} |
c8ce789c AO |
782 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
783 | @c Direct syscall, Linux only. | |
639c6286 | 784 | |
c70824b9 FW |
785 | This function returns the scheduling policy assigned to the task with |
786 | ID @var{pid}, or the calling task if @var{pid} is zero. | |
639c6286 UD |
787 | |
788 | The return value is the scheduling policy. See | |
789 | @code{sched_setscheduler} for the possible values. | |
790 | ||
791 | If the function fails, the return value is instead @code{-1} and | |
792 | @code{errno} is set accordingly. | |
793 | ||
794 | The @code{errno} values specific to this function are: | |
795 | ||
796 | @table @code | |
797 | ||
798 | @item ESRCH | |
c70824b9 | 799 | There is no task with pid @var{pid} and it is not zero. |
639c6286 UD |
800 | |
801 | @item EINVAL | |
802 | @var{pid} is negative. | |
803 | ||
804 | @end table | |
805 | ||
806 | Note that this function is not an exact mate to @code{sched_setscheduler} | |
807 | because while that function sets the scheduling policy and the absolute | |
808 | priority, this function gets only the scheduling policy. To get the | |
809 | absolute priority, use @code{sched_getparam}. | |
810 | ||
811 | @end deftypefun | |
812 | ||
813 | ||
639c6286 | 814 | @deftypefun int sched_setparam (pid_t @var{pid}, const struct sched_param *@var{param}) |
d08a7e4c | 815 | @standards{POSIX, sched.h} |
c8ce789c AO |
816 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
817 | @c Direct syscall, Linux only. | |
639c6286 | 818 | |
c70824b9 | 819 | This function sets a task's absolute priority. |
639c6286 UD |
820 | |
821 | It is functionally identical to @code{sched_setscheduler} with | |
822 | @var{policy} = @code{-1}. | |
823 | ||
824 | @c in fact, that's how it's implemented in Linux. | |
825 | ||
826 | @end deftypefun | |
827 | ||
8ded91fb | 828 | @deftypefun int sched_getparam (pid_t @var{pid}, struct sched_param *@var{param}) |
d08a7e4c | 829 | @standards{POSIX, sched.h} |
c8ce789c AO |
830 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
831 | @c Direct syscall, Linux only. | |
639c6286 | 832 | |
c70824b9 | 833 | This function returns a task's absolute priority. |
639c6286 | 834 | |
c70824b9 FW |
835 | @var{pid} is the task ID of the task whose absolute priority you want |
836 | to know. | |
639c6286 UD |
837 | |
838 | @var{param} is a pointer to a structure in which the function stores the | |
c70824b9 | 839 | absolute priority of the task. |
639c6286 UD |
840 | |
841 | On success, the return value is @code{0}. Otherwise, it is @code{-1} | |
d3e22d59 | 842 | and @code{errno} is set accordingly. The @code{errno} values specific |
639c6286 UD |
843 | to this function are: |
844 | ||
845 | @table @code | |
846 | ||
847 | @item ESRCH | |
c70824b9 | 848 | There is no task with ID @var{pid} and it is not zero. |
639c6286 UD |
849 | |
850 | @item EINVAL | |
851 | @var{pid} is negative. | |
852 | ||
853 | @end table | |
854 | ||
855 | @end deftypefun | |
856 | ||
857 | ||
8ded91fb | 858 | @deftypefun int sched_get_priority_min (int @var{policy}) |
d08a7e4c | 859 | @standards{POSIX, sched.h} |
c8ce789c AO |
860 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
861 | @c Direct syscall, Linux only. | |
639c6286 UD |
862 | |
863 | This function returns the lowest absolute priority value that is | |
c70824b9 | 864 | allowable for a task with scheduling policy @var{policy}. |
639c6286 UD |
865 | |
866 | On Linux, it is 0 for SCHED_OTHER and 1 for everything else. | |
867 | ||
868 | On success, the return value is @code{0}. Otherwise, it is @code{-1} | |
869 | and @code{ERRNO} is set accordingly. The @code{errno} values specific | |
870 | to this function are: | |
871 | ||
872 | @table @code | |
873 | @item EINVAL | |
874 | @var{policy} does not identify an existing scheduling policy. | |
875 | @end table | |
876 | ||
877 | @end deftypefun | |
878 | ||
8ded91fb | 879 | @deftypefun int sched_get_priority_max (int @var{policy}) |
d08a7e4c | 880 | @standards{POSIX, sched.h} |
c8ce789c AO |
881 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
882 | @c Direct syscall, Linux only. | |
639c6286 UD |
883 | |
884 | This function returns the highest absolute priority value that is | |
c70824b9 | 885 | allowable for a task that with scheduling policy @var{policy}. |
639c6286 UD |
886 | |
887 | On Linux, it is 0 for SCHED_OTHER and 99 for everything else. | |
888 | ||
889 | On success, the return value is @code{0}. Otherwise, it is @code{-1} | |
890 | and @code{ERRNO} is set accordingly. The @code{errno} values specific | |
891 | to this function are: | |
892 | ||
893 | @table @code | |
894 | @item EINVAL | |
895 | @var{policy} does not identify an existing scheduling policy. | |
896 | @end table | |
897 | ||
898 | @end deftypefun | |
899 | ||
639c6286 | 900 | @deftypefun int sched_rr_get_interval (pid_t @var{pid}, struct timespec *@var{interval}) |
d08a7e4c | 901 | @standards{POSIX, sched.h} |
c8ce789c AO |
902 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
903 | @c Direct syscall, Linux only. | |
639c6286 | 904 | |
87b56f36 | 905 | This function returns the length of the quantum (time slice) used with |
c70824b9 FW |
906 | the Round Robin scheduling policy, if it is used, for the task with |
907 | task ID @var{pid}. | |
639c6286 | 908 | |
87b56f36 | 909 | It returns the length of time as @var{interval}. |
639c6286 UD |
910 | @c We need a cross-reference to where timespec is explained. But that |
911 | @c section doesn't exist yet, and the time chapter needs to be slightly | |
912 | @c reorganized so there is a place to put it (which will be right next | |
913 | @c to timeval, which is presently misplaced). 2000.05.07. | |
914 | ||
915 | With a Linux kernel, the round robin time slice is always 150 | |
916 | microseconds, and @var{pid} need not even be a real pid. | |
917 | ||
918 | The return value is @code{0} on success and in the pathological case | |
919 | that it fails, the return value is @code{-1} and @code{errno} is set | |
920 | accordingly. There is nothing specific that can go wrong with this | |
921 | function, so there are no specific @code{errno} values. | |
922 | ||
923 | @end deftypefun | |
924 | ||
3c44837c | 925 | @deftypefun int sched_yield (void) |
d08a7e4c | 926 | @standards{POSIX, sched.h} |
c8ce789c AO |
927 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
928 | @c Direct syscall on Linux; alias to swtch on HURD. | |
639c6286 | 929 | |
c70824b9 | 930 | This function voluntarily gives up the task's claim on the CPU. |
639c6286 | 931 | |
c70824b9 | 932 | Technically, @code{sched_yield} causes the calling task to be made |
639c6286 UD |
933 | immediately ready to run (as opposed to running, which is what it was |
934 | before). This means that if it has absolute priority higher than 0, it | |
c70824b9 | 935 | gets pushed onto the tail of the queue of tasks that share its |
639c6286 UD |
936 | absolute priority and are ready to run, and it will run again when its |
937 | turn next arrives. If its absolute priority is 0, it is more | |
938 | complicated, but still has the effect of yielding the CPU to other | |
c70824b9 | 939 | tasks. |
639c6286 | 940 | |
c70824b9 | 941 | If there are no other tasks that share the calling task's absolute |
639c6286 UD |
942 | priority, this function doesn't have any effect. |
943 | ||
944 | To the extent that the containing program is oblivious to what other | |
945 | processes in the system are doing and how fast it executes, this | |
946 | function appears as a no-op. | |
947 | ||
948 | The return value is @code{0} on success and in the pathological case | |
949 | that it fails, the return value is @code{-1} and @code{errno} is set | |
950 | accordingly. There is nothing specific that can go wrong with this | |
951 | function, so there are no specific @code{errno} values. | |
952 | ||
953 | @end deftypefun | |
954 | ||
955 | @node Traditional Scheduling | |
956 | @subsection Traditional Scheduling | |
957 | @cindex scheduling, traditional | |
958 | ||
959 | This section is about the scheduling among processes whose absolute | |
960 | priority is 0. When the system hands out the scraps of CPU time that | |
0bc93a2f | 961 | are left over after the processes with higher absolute priority have |
639c6286 UD |
962 | taken all they want, the scheduling described herein determines who |
963 | among the great unwashed processes gets them. | |
964 | ||
965 | @menu | |
966 | * Traditional Scheduling Intro:: | |
967 | * Traditional Scheduling Functions:: | |
968 | @end menu | |
969 | ||
970 | @node Traditional Scheduling Intro | |
971 | @subsubsection Introduction To Traditional Scheduling | |
972 | ||
973 | Long before there was absolute priority (See @ref{Absolute Priority}), | |
d3e22d59 | 974 | Unix systems were scheduling the CPU using this system. When POSIX came |
0bc93a2f | 975 | in like the Romans and imposed absolute priorities to accommodate the |
639c6286 UD |
976 | needs of realtime processing, it left the indigenous Absolute Priority |
977 | Zero processes to govern themselves by their own familiar scheduling | |
978 | policy. | |
979 | ||
980 | Indeed, absolute priorities higher than zero are not available on many | |
981 | systems today and are not typically used when they are, being intended | |
982 | mainly for computers that do realtime processing. So this section | |
983 | describes the only scheduling many programmers need to be concerned | |
984 | about. | |
985 | ||
986 | But just to be clear about the scope of this scheduling: Any time a | |
9dcc8f11 | 987 | process with an absolute priority of 0 and a process with an absolute |
639c6286 UD |
988 | priority higher than 0 are ready to run at the same time, the one with |
989 | absolute priority 0 does not run. If it's already running when the | |
990 | higher priority ready-to-run process comes into existence, it stops | |
991 | immediately. | |
992 | ||
993 | In addition to its absolute priority of zero, every process has another | |
994 | priority, which we will refer to as "dynamic priority" because it changes | |
87b56f36 | 995 | over time. The dynamic priority is meaningless for processes with |
639c6286 UD |
996 | an absolute priority higher than zero. |
997 | ||
998 | The dynamic priority sometimes determines who gets the next turn on the | |
999 | CPU. Sometimes it determines how long turns last. Sometimes it | |
1000 | determines whether a process can kick another off the CPU. | |
1001 | ||
d3e22d59 | 1002 | In Linux, the value is a combination of these things, but mostly it |
639c6286 UD |
1003 | just determines the length of the time slice. The higher a process' |
1004 | dynamic priority, the longer a shot it gets on the CPU when it gets one. | |
1005 | If it doesn't use up its time slice before giving up the CPU to do | |
1006 | something like wait for I/O, it is favored for getting the CPU back when | |
1007 | it's ready for it, to finish out its time slice. Other than that, | |
1008 | selection of processes for new time slices is basically round robin. | |
1009 | But the scheduler does throw a bone to the low priority processes: A | |
1010 | process' dynamic priority rises every time it is snubbed in the | |
1011 | scheduling process. In Linux, even the fat kid gets to play. | |
1012 | ||
1013 | The fluctuation of a process' dynamic priority is regulated by another | |
1014 | value: The ``nice'' value. The nice value is an integer, usually in the | |
1015 | range -20 to 20, and represents an upper limit on a process' dynamic | |
1016 | priority. The higher the nice number, the lower that limit. | |
1017 | ||
1018 | On a typical Linux system, for example, a process with a nice value of | |
1019 | 20 can get only 10 milliseconds on the CPU at a time, whereas a process | |
1020 | with a nice value of -20 can achieve a high enough priority to get 400 | |
1021 | milliseconds. | |
1022 | ||
1023 | The idea of the nice value is deferential courtesy. In the beginning, | |
1024 | in the Unix garden of Eden, all processes shared equally in the bounty | |
1025 | of the computer system. But not all processes really need the same | |
1026 | share of CPU time, so the nice value gave a courteous process the | |
1027 | ability to refuse its equal share of CPU time that others might prosper. | |
1028 | Hence, the higher a process' nice value, the nicer the process is. | |
1029 | (Then a snake came along and offered some process a negative nice value | |
1030 | and the system became the crass resource allocation system we know | |
d3e22d59 | 1031 | today.) |
639c6286 UD |
1032 | |
1033 | Dynamic priorities tend upward and downward with an objective of | |
1034 | smoothing out allocation of CPU time and giving quick response time to | |
1035 | infrequent requests. But they never exceed their nice limits, so on a | |
1036 | heavily loaded CPU, the nice value effectively determines how fast a | |
1037 | process runs. | |
1038 | ||
1039 | In keeping with the socialistic heritage of Unix process priority, a | |
1040 | process begins life with the same nice value as its parent process and | |
1041 | can raise it at will. A process can also raise the nice value of any | |
1042 | other process owned by the same user (or effective user). But only a | |
1043 | privileged process can lower its nice value. A privileged process can | |
1044 | also raise or lower another process' nice value. | |
1045 | ||
1f77f049 | 1046 | @glibcadj{} functions for getting and setting nice values are described in |
639c6286 UD |
1047 | @xref{Traditional Scheduling Functions}. |
1048 | ||
1049 | @node Traditional Scheduling Functions | |
1050 | @subsubsection Functions For Traditional Scheduling | |
1051 | ||
5ce8f203 | 1052 | @pindex sys/resource.h |
639c6286 UD |
1053 | This section describes how you can read and set the nice value of a |
1054 | process. All these symbols are declared in @file{sys/resource.h}. | |
1055 | ||
1056 | The function and macro names are defined by POSIX, and refer to | |
1057 | "priority," but the functions actually have to do with nice values, as | |
1058 | the terms are used both in the manual and POSIX. | |
1059 | ||
1060 | The range of valid nice values depends on the kernel, but typically it | |
1061 | runs from @code{-20} to @code{20}. A lower nice value corresponds to | |
1062 | higher priority for the process. These constants describe the range of | |
5ce8f203 UD |
1063 | priority values: |
1064 | ||
b642f101 | 1065 | @vtable @code |
5ce8f203 | 1066 | @item PRIO_MIN |
d08a7e4c | 1067 | @standards{BSD, sys/resource.h} |
639c6286 | 1068 | The lowest valid nice value. |
5ce8f203 | 1069 | |
5ce8f203 | 1070 | @item PRIO_MAX |
d08a7e4c | 1071 | @standards{BSD, sys/resource.h} |
639c6286 | 1072 | The highest valid nice value. |
b642f101 | 1073 | @end vtable |
5ce8f203 | 1074 | |
5ce8f203 | 1075 | @deftypefun int getpriority (int @var{class}, int @var{id}) |
d08a7e4c RJ |
1076 | @standards{BSD, sys/resource.h} |
1077 | @standards{POSIX, sys/resource.h} | |
c8ce789c AO |
1078 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1079 | @c Direct syscall on UNIX. On HURD, calls _hurd_priority_which_map. | |
639c6286 | 1080 | Return the nice value of a set of processes; @var{class} and @var{id} |
5ce8f203 | 1081 | specify which ones (see below). If the processes specified do not all |
639c6286 | 1082 | have the same nice value, this returns the lowest value that any of them |
5ce8f203 UD |
1083 | has. |
1084 | ||
639c6286 | 1085 | On success, the return value is @code{0}. Otherwise, it is @code{-1} |
d3e22d59 | 1086 | and @code{errno} is set accordingly. The @code{errno} values specific |
639c6286 | 1087 | to this function are: |
5ce8f203 UD |
1088 | |
1089 | @table @code | |
1090 | @item ESRCH | |
1091 | The combination of @var{class} and @var{id} does not match any existing | |
1092 | process. | |
1093 | ||
1094 | @item EINVAL | |
1095 | The value of @var{class} is not valid. | |
1096 | @end table | |
1097 | ||
639c6286 UD |
1098 | If the return value is @code{-1}, it could indicate failure, or it could |
1099 | be the nice value. The only way to make certain is to set @code{errno = | |
1100 | 0} before calling @code{getpriority}, then use @code{errno != 0} | |
1101 | afterward as the criterion for failure. | |
5ce8f203 UD |
1102 | @end deftypefun |
1103 | ||
639c6286 | 1104 | @deftypefun int setpriority (int @var{class}, int @var{id}, int @var{niceval}) |
d08a7e4c RJ |
1105 | @standards{BSD, sys/resource.h} |
1106 | @standards{POSIX, sys/resource.h} | |
c8ce789c AO |
1107 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1108 | @c Direct syscall on UNIX. On HURD, calls _hurd_priority_which_map. | |
639c6286 | 1109 | Set the nice value of a set of processes to @var{niceval}; @var{class} |
5ce8f203 UD |
1110 | and @var{id} specify which ones (see below). |
1111 | ||
6a7a8b22 | 1112 | The return value is @code{0} on success, and @code{-1} on |
639c6286 UD |
1113 | failure. The following @code{errno} error condition are possible for |
1114 | this function: | |
5ce8f203 UD |
1115 | |
1116 | @table @code | |
1117 | @item ESRCH | |
1118 | The combination of @var{class} and @var{id} does not match any existing | |
1119 | process. | |
1120 | ||
1121 | @item EINVAL | |
1122 | The value of @var{class} is not valid. | |
1123 | ||
1124 | @item EPERM | |
639c6286 | 1125 | The call would set the nice value of a process which is owned by a different |
11bf311e | 1126 | user than the calling process (i.e., the target process' real or effective |
639c6286 UD |
1127 | uid does not match the calling process' effective uid) and the calling |
1128 | process does not have @code{CAP_SYS_NICE} permission. | |
5ce8f203 UD |
1129 | |
1130 | @item EACCES | |
639c6286 UD |
1131 | The call would lower the process' nice value and the process does not have |
1132 | @code{CAP_SYS_NICE} permission. | |
5ce8f203 | 1133 | @end table |
639c6286 | 1134 | |
5ce8f203 UD |
1135 | @end deftypefun |
1136 | ||
1137 | The arguments @var{class} and @var{id} together specify a set of | |
1138 | processes in which you are interested. These are the possible values of | |
1139 | @var{class}: | |
1140 | ||
b642f101 | 1141 | @vtable @code |
5ce8f203 | 1142 | @item PRIO_PROCESS |
d08a7e4c | 1143 | @standards{BSD, sys/resource.h} |
639c6286 | 1144 | One particular process. The argument @var{id} is a process ID (pid). |
5ce8f203 | 1145 | |
5ce8f203 | 1146 | @item PRIO_PGRP |
d08a7e4c | 1147 | @standards{BSD, sys/resource.h} |
639c6286 UD |
1148 | All the processes in a particular process group. The argument @var{id} is |
1149 | a process group ID (pgid). | |
5ce8f203 | 1150 | |
5ce8f203 | 1151 | @item PRIO_USER |
d08a7e4c | 1152 | @standards{BSD, sys/resource.h} |
11bf311e | 1153 | All the processes owned by a particular user (i.e., whose real uid |
639c6286 | 1154 | indicates the user). The argument @var{id} is a user ID (uid). |
b642f101 | 1155 | @end vtable |
5ce8f203 | 1156 | |
639c6286 UD |
1157 | If the argument @var{id} is 0, it stands for the calling process, its |
1158 | process group, or its owner (real uid), according to @var{class}. | |
5ce8f203 | 1159 | |
5ce8f203 | 1160 | @deftypefun int nice (int @var{increment}) |
d08a7e4c | 1161 | @standards{BSD, unistd.h} |
c8ce789c AO |
1162 | @safety{@prelim{}@mtunsafe{@mtasurace{:setpriority}}@asunsafe{}@acsafe{}} |
1163 | @c Calls getpriority before and after setpriority, using the result of | |
1164 | @c the first call to compute the argument for setpriority. This creates | |
1165 | @c a window for a concurrent setpriority (or nice) call to be lost or | |
1166 | @c exhibit surprising behavior. | |
639c6286 | 1167 | Increment the nice value of the calling process by @var{increment}. |
6a7a8b22 AJ |
1168 | The return value is the new nice value on success, and @code{-1} on |
1169 | failure. In the case of failure, @code{errno} will be set to the | |
1170 | same values as for @code{setpriority}. | |
1171 | ||
5ce8f203 UD |
1172 | |
1173 | Here is an equivalent definition of @code{nice}: | |
1174 | ||
1175 | @smallexample | |
1176 | int | |
1177 | nice (int increment) | |
1178 | @{ | |
6a7a8b22 AJ |
1179 | int result, old = getpriority (PRIO_PROCESS, 0); |
1180 | result = setpriority (PRIO_PROCESS, 0, old + increment); | |
1181 | if (result != -1) | |
1182 | return old + increment; | |
1183 | else | |
1184 | return -1; | |
5ce8f203 UD |
1185 | @} |
1186 | @end smallexample | |
1187 | @end deftypefun | |
b642f101 | 1188 | |
d9997a45 UD |
1189 | |
1190 | @node CPU Affinity | |
1191 | @subsection Limiting execution to certain CPUs | |
1192 | ||
1193 | On a multi-processor system the operating system usually distributes | |
1194 | the different processes which are runnable on all available CPUs in a | |
1195 | way which allows the system to work most efficiently. Which processes | |
1196 | and threads run can be to some extend be control with the scheduling | |
1197 | functionality described in the last sections. But which CPU finally | |
1198 | executes which process or thread is not covered. | |
1199 | ||
1200 | There are a number of reasons why a program might want to have control | |
1201 | over this aspect of the system as well: | |
1202 | ||
1203 | @itemize @bullet | |
1204 | @item | |
1205 | One thread or process is responsible for absolutely critical work | |
1206 | which under no circumstances must be interrupted or hindered from | |
d3e22d59 | 1207 | making progress by other processes or threads using CPU resources. In |
d9997a45 UD |
1208 | this case the special process would be confined to a CPU which no |
1209 | other process or thread is allowed to use. | |
1210 | ||
1211 | @item | |
1212 | The access to certain resources (RAM, I/O ports) has different costs | |
1213 | from different CPUs. This is the case in NUMA (Non-Uniform Memory | |
11bf311e | 1214 | Architecture) machines. Preferably memory should be accessed locally |
d9997a45 UD |
1215 | but this requirement is usually not visible to the scheduler. |
1216 | Therefore forcing a process or thread to the CPUs which have local | |
d3e22d59 | 1217 | access to the most-used memory helps to significantly boost the |
d9997a45 UD |
1218 | performance. |
1219 | ||
1220 | @item | |
1221 | In controlled runtimes resource allocation and book-keeping work (for | |
1222 | instance garbage collection) is performance local to processors. This | |
1223 | can help to reduce locking costs if the resources do not have to be | |
1224 | protected from concurrent accesses from different processors. | |
1225 | @end itemize | |
1226 | ||
1227 | The POSIX standard up to this date is of not much help to solve this | |
1228 | problem. The Linux kernel provides a set of interfaces to allow | |
1229 | specifying @emph{affinity sets} for a process. The scheduler will | |
bbf70ae9 | 1230 | schedule the thread or process on CPUs specified by the affinity |
1f77f049 | 1231 | masks. The interfaces which @theglibc{} define follow to some |
d3e22d59 | 1232 | extent the Linux kernel interface. |
d9997a45 | 1233 | |
d9997a45 | 1234 | @deftp {Data Type} cpu_set_t |
d08a7e4c | 1235 | @standards{GNU, sched.h} |
d9997a45 UD |
1236 | This data set is a bitset where each bit represents a CPU. How the |
1237 | system's CPUs are mapped to bits in the bitset is system dependent. | |
1238 | The data type has a fixed size; in the unlikely case that the number | |
1239 | of bits are not sufficient to describe the CPUs of the system a | |
1240 | different interface has to be used. | |
1241 | ||
1242 | This type is a GNU extension and is defined in @file{sched.h}. | |
1243 | @end deftp | |
1244 | ||
d3e22d59 | 1245 | To manipulate the bitset, to set and reset bits, a number of macros are |
d9997a45 UD |
1246 | defined. Some of the macros take a CPU number as a parameter. Here |
1247 | it is important to never exceed the size of the bitset. The following | |
1248 | macro specifies the number of bits in the @code{cpu_set_t} bitset. | |
1249 | ||
d9997a45 | 1250 | @deftypevr Macro int CPU_SETSIZE |
d08a7e4c | 1251 | @standards{GNU, sched.h} |
d9997a45 UD |
1252 | The value of this macro is the maximum number of CPUs which can be |
1253 | handled with a @code{cpu_set_t} object. | |
1254 | @end deftypevr | |
1255 | ||
1256 | The type @code{cpu_set_t} should be considered opaque; all | |
1257 | manipulation should happen via the next four macros. | |
1258 | ||
d9997a45 | 1259 | @deftypefn Macro void CPU_ZERO (cpu_set_t *@var{set}) |
d08a7e4c | 1260 | @standards{GNU, sched.h} |
c8ce789c AO |
1261 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1262 | @c CPU_ZERO ok | |
1263 | @c __CPU_ZERO_S ok | |
1264 | @c memset dup ok | |
d9997a45 UD |
1265 | This macro initializes the CPU set @var{set} to be the empty set. |
1266 | ||
1267 | This macro is a GNU extension and is defined in @file{sched.h}. | |
1268 | @end deftypefn | |
1269 | ||
d9997a45 | 1270 | @deftypefn Macro void CPU_SET (int @var{cpu}, cpu_set_t *@var{set}) |
d08a7e4c | 1271 | @standards{GNU, sched.h} |
c8ce789c AO |
1272 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1273 | @c CPU_SET ok | |
1274 | @c __CPU_SET_S ok | |
1275 | @c __CPUELT ok | |
1276 | @c __CPUMASK ok | |
d9997a45 UD |
1277 | This macro adds @var{cpu} to the CPU set @var{set}. |
1278 | ||
1279 | The @var{cpu} parameter must not have side effects since it is | |
1280 | evaluated more than once. | |
1281 | ||
1282 | This macro is a GNU extension and is defined in @file{sched.h}. | |
1283 | @end deftypefn | |
1284 | ||
d9997a45 | 1285 | @deftypefn Macro void CPU_CLR (int @var{cpu}, cpu_set_t *@var{set}) |
d08a7e4c | 1286 | @standards{GNU, sched.h} |
c8ce789c AO |
1287 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1288 | @c CPU_CLR ok | |
1289 | @c __CPU_CLR_S ok | |
1290 | @c __CPUELT dup ok | |
1291 | @c __CPUMASK dup ok | |
d9997a45 UD |
1292 | This macro removes @var{cpu} from the CPU set @var{set}. |
1293 | ||
1294 | The @var{cpu} parameter must not have side effects since it is | |
1295 | evaluated more than once. | |
1296 | ||
1297 | This macro is a GNU extension and is defined in @file{sched.h}. | |
1298 | @end deftypefn | |
1299 | ||
d9997a45 | 1300 | @deftypefn Macro int CPU_ISSET (int @var{cpu}, const cpu_set_t *@var{set}) |
d08a7e4c | 1301 | @standards{GNU, sched.h} |
c8ce789c AO |
1302 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1303 | @c CPU_ISSET ok | |
1304 | @c __CPU_ISSET_S ok | |
1305 | @c __CPUELT dup ok | |
1306 | @c __CPUMASK dup ok | |
d9997a45 UD |
1307 | This macro returns a nonzero value (true) if @var{cpu} is a member |
1308 | of the CPU set @var{set}, and zero (false) otherwise. | |
1309 | ||
1310 | The @var{cpu} parameter must not have side effects since it is | |
1311 | evaluated more than once. | |
1312 | ||
1313 | This macro is a GNU extension and is defined in @file{sched.h}. | |
1314 | @end deftypefn | |
1315 | ||
1316 | ||
1317 | CPU bitsets can be constructed from scratch or the currently installed | |
1318 | affinity mask can be retrieved from the system. | |
1319 | ||
6f0b2e1f | 1320 | @deftypefun int sched_getaffinity (pid_t @var{pid}, size_t @var{cpusetsize}, cpu_set_t *@var{cpuset}) |
d08a7e4c | 1321 | @standards{GNU, sched.h} |
c8ce789c AO |
1322 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1323 | @c Wrapped syscall to zero out past the kernel cpu set size; Linux | |
1324 | @c only. | |
d9997a45 | 1325 | |
d3e22d59 | 1326 | This function stores the CPU affinity mask for the process or thread |
6f0b2e1f RM |
1327 | with the ID @var{pid} in the @var{cpusetsize} bytes long bitmap |
1328 | pointed to by @var{cpuset}. If successful, the function always | |
1329 | initializes all bits in the @code{cpu_set_t} object and returns zero. | |
d9997a45 UD |
1330 | |
1331 | If @var{pid} does not correspond to a process or thread on the system | |
1332 | the or the function fails for some other reason, it returns @code{-1} | |
1333 | and @code{errno} is set to represent the error condition. | |
1334 | ||
1335 | @table @code | |
1336 | @item ESRCH | |
1337 | No process or thread with the given ID found. | |
1338 | ||
1339 | @item EFAULT | |
d3e22d59 | 1340 | The pointer @var{cpuset} does not point to a valid object. |
d9997a45 UD |
1341 | @end table |
1342 | ||
1343 | This function is a GNU extension and is declared in @file{sched.h}. | |
1344 | @end deftypefun | |
1345 | ||
1346 | Note that it is not portably possible to use this information to | |
1347 | retrieve the information for different POSIX threads. A separate | |
1348 | interface must be provided for that. | |
1349 | ||
6f0b2e1f | 1350 | @deftypefun int sched_setaffinity (pid_t @var{pid}, size_t @var{cpusetsize}, const cpu_set_t *@var{cpuset}) |
d08a7e4c | 1351 | @standards{GNU, sched.h} |
c8ce789c AO |
1352 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1353 | @c Wrapped syscall to detect attempts to set bits past the kernel cpu | |
1354 | @c set size; Linux only. | |
d9997a45 | 1355 | |
6f0b2e1f RM |
1356 | This function installs the @var{cpusetsize} bytes long affinity mask |
1357 | pointed to by @var{cpuset} for the process or thread with the ID @var{pid}. | |
d3e22d59 | 1358 | If successful the function returns zero and the scheduler will in the future |
6f0b2e1f | 1359 | take the affinity information into account. |
d9997a45 UD |
1360 | |
1361 | If the function fails it will return @code{-1} and @code{errno} is set | |
1362 | to the error code: | |
1363 | ||
1364 | @table @code | |
1365 | @item ESRCH | |
1366 | No process or thread with the given ID found. | |
1367 | ||
1368 | @item EFAULT | |
d3e22d59 | 1369 | The pointer @var{cpuset} does not point to a valid object. |
d9997a45 UD |
1370 | |
1371 | @item EINVAL | |
1372 | The bitset is not valid. This might mean that the affinity set might | |
1373 | not leave a processor for the process or thread to run on. | |
1374 | @end table | |
1375 | ||
1376 | This function is a GNU extension and is declared in @file{sched.h}. | |
1377 | @end deftypefun | |
1378 | ||
a092ca94 L |
1379 | @deftypefun int getcpu (unsigned int *cpu, unsigned int *node) |
1380 | @standards{Linux, <sched.h>} | |
1381 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
1382 | The @code{getcpu} function identifies the processor and node on which | |
1383 | the calling thread or process is currently running and writes them into | |
1384 | the integers pointed to by the @var{cpu} and @var{node} arguments. The | |
1385 | processor is a unique nonnegative integer identifying a CPU. The node | |
1386 | is a unique nonnegative integer identifying a NUMA node. When either | |
1387 | @var{cpu} or @var{node} is @code{NULL}, nothing is written to the | |
1388 | respective pointer. | |
1389 | ||
1390 | The return value is @code{0} on success and @code{-1} on failure. The | |
1391 | following @code{errno} error condition is defined for this function: | |
1392 | ||
1393 | @table @code | |
1394 | @item ENOSYS | |
1395 | The operating system does not support this function. | |
1396 | @end table | |
1397 | ||
1398 | This function is Linux-specific and is declared in @file{sched.h}. | |
1399 | @end deftypefun | |
d9997a45 | 1400 | |
b642f101 UD |
1401 | @node Memory Resources |
1402 | @section Querying memory available resources | |
1403 | ||
1404 | The amount of memory available in the system and the way it is organized | |
1405 | determines oftentimes the way programs can and have to work. For | |
5a7eedfb | 1406 | functions like @code{mmap} it is necessary to know about the size of |
b642f101 UD |
1407 | individual memory pages and knowing how much memory is available enables |
1408 | a program to select appropriate sizes for, say, caches. Before we get | |
1409 | into these details a few words about memory subsystems in traditional | |
5a7eedfb | 1410 | Unix systems will be given. |
b642f101 UD |
1411 | |
1412 | @menu | |
1413 | * Memory Subsystem:: Overview about traditional Unix memory handling. | |
1414 | * Query Memory Parameters:: How to get information about the memory | |
1415 | subsystem? | |
1416 | @end menu | |
1417 | ||
1418 | @node Memory Subsystem | |
1419 | @subsection Overview about traditional Unix memory handling | |
1420 | ||
1421 | @cindex address space | |
1422 | @cindex physical memory | |
1423 | @cindex physical address | |
1424 | Unix systems normally provide processes virtual address spaces. This | |
1425 | means that the addresses of the memory regions do not have to correspond | |
1426 | directly to the addresses of the actual physical memory which stores the | |
1427 | data. An extra level of indirection is introduced which translates | |
1428 | virtual addresses into physical addresses. This is normally done by the | |
1429 | hardware of the processor. | |
1430 | ||
1431 | @cindex shared memory | |
d3e22d59 | 1432 | Using a virtual address space has several advantages. The most important |
b642f101 UD |
1433 | is process isolation. The different processes running on the system |
1434 | cannot interfere directly with each other. No process can write into | |
1435 | the address space of another process (except when shared memory is used | |
1436 | but then it is wanted and controlled). | |
1437 | ||
1438 | Another advantage of virtual memory is that the address space the | |
1439 | processes see can actually be larger than the physical memory available. | |
1440 | The physical memory can be extended by storage on an external media | |
1441 | where the content of currently unused memory regions is stored. The | |
1442 | address translation can then intercept accesses to these memory regions | |
1443 | and make memory content available again by loading the data back into | |
1444 | memory. This concept makes it necessary that programs which have to use | |
1445 | lots of memory know the difference between available virtual address | |
1446 | space and available physical memory. If the working set of virtual | |
1447 | memory of all the processes is larger than the available physical memory | |
1448 | the system will slow down dramatically due to constant swapping of | |
1449 | memory content from the memory to the storage media and back. This is | |
1450 | called ``thrashing''. | |
1451 | @cindex thrashing | |
1452 | ||
1453 | @cindex memory page | |
1454 | @cindex page, memory | |
1455 | A final aspect of virtual memory which is important and follows from | |
1456 | what is said in the last paragraph is the granularity of the virtual | |
1457 | address space handling. When we said that the virtual address handling | |
1458 | stores memory content externally it cannot do this on a byte-by-byte | |
1459 | basis. The administrative overhead does not allow this (leaving alone | |
1460 | the processor hardware). Instead several thousand bytes are handled | |
1461 | together and form a @dfn{page}. The size of each page is always a power | |
d3e22d59 | 1462 | of two bytes. The smallest page size in use today is 4096, with 8192, |
b642f101 UD |
1463 | 16384, and 65536 being other popular sizes. |
1464 | ||
1465 | @node Query Memory Parameters | |
1466 | @subsection How to get information about the memory subsystem? | |
1467 | ||
1468 | The page size of the virtual memory the process sees is essential to | |
d3e22d59 | 1469 | know in several situations. Some programming interfaces (e.g., |
b642f101 | 1470 | @code{mmap}, @pxref{Memory-mapped I/O}) require the user to provide |
d3e22d59 | 1471 | information adjusted to the page size. In the case of @code{mmap} it is |
b642f101 UD |
1472 | necessary to provide a length argument which is a multiple of the page |
1473 | size. Another place where the knowledge about the page size is useful | |
1474 | is in memory allocation. If one allocates pieces of memory in larger | |
1475 | chunks which are then subdivided by the application code it is useful to | |
1476 | adjust the size of the larger blocks to the page size. If the total | |
1477 | memory requirement for the block is close (but not larger) to a multiple | |
1478 | of the page size the kernel's memory handling can work more effectively | |
1479 | since it only has to allocate memory pages which are fully used. (To do | |
1480 | this optimization it is necessary to know a bit about the memory | |
1481 | allocator which will require a bit of memory itself for each block and | |
d3e22d59 | 1482 | this overhead must not push the total size over the page size multiple.) |
b642f101 UD |
1483 | |
1484 | The page size traditionally was a compile time constant. But recent | |
1485 | development of processors changed this. Processors now support | |
1486 | different page sizes and they can possibly even vary among different | |
1487 | processes on the same system. Therefore the system should be queried at | |
1488 | runtime about the current page size and no assumptions (except about it | |
1489 | being a power of two) should be made. | |
1490 | ||
1491 | @vindex _SC_PAGESIZE | |
1492 | The correct interface to query about the page size is @code{sysconf} | |
1493 | (@pxref{Sysconf Definition}) with the parameter @code{_SC_PAGESIZE}. | |
1494 | There is a much older interface available, too. | |
1495 | ||
b642f101 | 1496 | @deftypefun int getpagesize (void) |
d08a7e4c | 1497 | @standards{BSD, unistd.h} |
c8ce789c AO |
1498 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
1499 | @c Obtained from the aux vec at program startup time. GNU/Linux/m68k is | |
1500 | @c the exception, with the possibility of a syscall. | |
b642f101 UD |
1501 | The @code{getpagesize} function returns the page size of the process. |
1502 | This value is fixed for the runtime of the process but can vary in | |
1503 | different runs of the application. | |
1504 | ||
1505 | The function is declared in @file{unistd.h}. | |
1506 | @end deftypefun | |
1507 | ||
1508 | Widely available on @w{System V} derived systems is a method to get | |
1509 | information about the physical memory the system has. The call | |
1510 | ||
1511 | @vindex _SC_PHYS_PAGES | |
1512 | @cindex sysconf | |
1513 | @smallexample | |
1514 | sysconf (_SC_PHYS_PAGES) | |
1515 | @end smallexample | |
1516 | ||
cb4fe8a2 | 1517 | @noindent |
d3e22d59 | 1518 | returns the total number of pages of physical memory the system has. |
b642f101 UD |
1519 | This does not mean all this memory is available. This information can |
1520 | be found using | |
1521 | ||
1522 | @vindex _SC_AVPHYS_PAGES | |
1523 | @cindex sysconf | |
1524 | @smallexample | |
1525 | sysconf (_SC_AVPHYS_PAGES) | |
1526 | @end smallexample | |
1527 | ||
1528 | These two values help to optimize applications. The value returned for | |
1529 | @code{_SC_AVPHYS_PAGES} is the amount of memory the application can use | |
1530 | without hindering any other process (given that no other process | |
1531 | increases its memory usage). The value returned for | |
1532 | @code{_SC_PHYS_PAGES} is more or less a hard limit for the working set. | |
1533 | If all applications together constantly use more than that amount of | |
1534 | memory the system is in trouble. | |
1535 | ||
1f77f049 | 1536 | @Theglibc{} provides in addition to these already described way to |
cb4fe8a2 UD |
1537 | get this information two functions. They are declared in the file |
1538 | @file{sys/sysinfo.h}. Programmers should prefer to use the | |
1539 | @code{sysconf} method described above. | |
1540 | ||
4c78249d | 1541 | @deftypefun {long int} get_phys_pages (void) |
d08a7e4c | 1542 | @standards{GNU, sys/sysinfo.h} |
c8ce789c AO |
1543 | @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}} |
1544 | @c This fopens a /proc file and scans it for the requested information. | |
cb4fe8a2 | 1545 | The @code{get_phys_pages} function returns the total number of pages of |
d3e22d59 | 1546 | physical memory the system has. To get the amount of memory this number has to |
cb4fe8a2 UD |
1547 | be multiplied by the page size. |
1548 | ||
1549 | This function is a GNU extension. | |
1550 | @end deftypefun | |
1551 | ||
4c78249d | 1552 | @deftypefun {long int} get_avphys_pages (void) |
d08a7e4c | 1553 | @standards{GNU, sys/sysinfo.h} |
c8ce789c | 1554 | @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}} |
cd1fb604 | 1555 | The @code{get_avphys_pages} function returns the number of available pages of |
d3e22d59 | 1556 | physical memory the system has. To get the amount of memory this number has to |
cb4fe8a2 UD |
1557 | be multiplied by the page size. |
1558 | ||
1559 | This function is a GNU extension. | |
1560 | @end deftypefun | |
1561 | ||
b642f101 UD |
1562 | @node Processor Resources |
1563 | @section Learn about the processors available | |
1564 | ||
1565 | The use of threads or processes with shared memory allows an application | |
1566 | to take advantage of all the processing power a system can provide. If | |
1567 | the task can be parallelized the optimal way to write an application is | |
1568 | to have at any time as many processes running as there are processors. | |
1569 | To determine the number of processors available to the system one can | |
1570 | run | |
1571 | ||
1572 | @vindex _SC_NPROCESSORS_CONF | |
1573 | @cindex sysconf | |
1574 | @smallexample | |
1575 | sysconf (_SC_NPROCESSORS_CONF) | |
1576 | @end smallexample | |
1577 | ||
1578 | @noindent | |
1579 | which returns the number of processors the operating system configured. | |
1580 | But it might be possible for the operating system to disable individual | |
1581 | processors and so the call | |
1582 | ||
1583 | @vindex _SC_NPROCESSORS_ONLN | |
1584 | @cindex sysconf | |
1585 | @smallexample | |
1586 | sysconf (_SC_NPROCESSORS_ONLN) | |
1587 | @end smallexample | |
1588 | ||
1589 | @noindent | |
26428b7c | 1590 | returns the number of processors which are currently online (i.e., |
b642f101 | 1591 | available). |
e4cf5229 | 1592 | |
1f77f049 | 1593 | For these two pieces of information @theglibc{} also provides |
cb4fe8a2 UD |
1594 | functions to get the information directly. The functions are declared |
1595 | in @file{sys/sysinfo.h}. | |
1596 | ||
cb4fe8a2 | 1597 | @deftypefun int get_nprocs_conf (void) |
d08a7e4c | 1598 | @standards{GNU, sys/sysinfo.h} |
c8ce789c AO |
1599 | @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}} |
1600 | @c This function reads from from /sys using dir streams (single user, so | |
1601 | @c no @mtasurace issue), and on some arches, from /proc using streams. | |
cb4fe8a2 UD |
1602 | The @code{get_nprocs_conf} function returns the number of processors the |
1603 | operating system configured. | |
1604 | ||
1605 | This function is a GNU extension. | |
1606 | @end deftypefun | |
1607 | ||
cb4fe8a2 | 1608 | @deftypefun int get_nprocs (void) |
d08a7e4c | 1609 | @standards{GNU, sys/sysinfo.h} |
c8ce789c AO |
1610 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}} |
1611 | @c This function reads from /proc using file descriptor I/O. | |
cb4fe8a2 UD |
1612 | The @code{get_nprocs} function returns the number of available processors. |
1613 | ||
1614 | This function is a GNU extension. | |
1615 | @end deftypefun | |
1616 | ||
e4cf5229 UD |
1617 | @cindex load average |
1618 | Before starting more threads it should be checked whether the processors | |
1619 | are not already overused. Unix systems calculate something called the | |
1620 | @dfn{load average}. This is a number indicating how many processes were | |
d3e22d59 | 1621 | running. This number is an average over different periods of time |
e4cf5229 UD |
1622 | (normally 1, 5, and 15 minutes). |
1623 | ||
e4cf5229 | 1624 | @deftypefun int getloadavg (double @var{loadavg}[], int @var{nelem}) |
d08a7e4c | 1625 | @standards{BSD, stdlib.h} |
c8ce789c AO |
1626 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}} |
1627 | @c Calls host_info on HURD; on Linux, opens /proc/loadavg, reads from | |
1628 | @c it, closes it, without cancellation point, and calls strtod_l with | |
1629 | @c the C locale to convert the strings to doubles. | |
e4cf5229 | 1630 | This function gets the 1, 5 and 15 minute load averages of the |
cf822e3c | 1631 | system. The values are placed in @var{loadavg}. @code{getloadavg} will |
e4cf5229 UD |
1632 | place at most @var{nelem} elements into the array but never more than |
1633 | three elements. The return value is the number of elements written to | |
1634 | @var{loadavg}, or -1 on error. | |
1635 | ||
1636 | This function is declared in @file{stdlib.h}. | |
1637 | @end deftypefun |