NEWS: Add advisories.
[glibc.git] / manual / resource.texi
blob37462abc9e467690d34d2c010966084fd07409e7
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.
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.
12 * Memory Resources::            Querying memory available resources.
13 * Processor Resources::         Learn about the processors available.
14 @end menu
17 @node Resource Usage
18 @section Resource Usage
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}.
25 @deftypefun int getrusage (int @var{processes}, struct rusage *@var{rusage})
26 @standards{BSD, sys/resource.h}
27 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28 @c On HURD, this calls task_info 3 times.  On UNIX, it's a syscall.
29 This function reports resource usage totals for processes specified by
30 @var{processes}, storing the information in @code{*@var{rusage}}.
32 In most systems, @var{processes} has only two valid values:
34 @vtable @code
35 @item RUSAGE_SELF
36 @standards{BSD, sys/resource.h}
37 Just the current process.
39 @item RUSAGE_CHILDREN
40 @standards{BSD, sys/resource.h}
41 All child processes (direct and indirect) that have already terminated.
42 @end vtable
44 The return value of @code{getrusage} is zero for success, and @code{-1}
45 for failure.
47 @table @code
48 @item EINVAL
49 The argument @var{processes} is not valid.
50 @end table
51 @end deftypefun
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}.
57 @deftp {Data Type} {struct rusage}
58 @standards{BSD, sys/resource.h}
59 This data type stores various resource usage statistics.  It has the
60 following members, and possibly others:
62 @table @code
63 @item struct timeval ru_utime
64 Time spent executing user instructions.
66 @item struct timeval ru_stime
67 Time spent in operating system code on behalf of @var{processes}.
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.
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.
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.
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.
87 @item long int ru_minflt
88 The number of page faults which were serviced without requiring any I/O.
90 @item long int ru_majflt
91 The number of page faults which were serviced by doing I/O.
93 @item long int ru_nswap
94 The number of times @var{processes} was swapped entirely out of main memory.
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}.
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}.
104 @item long int ru_msgsnd
105 Number of IPC messages sent.
107 @item long int ru_msgrcv
108 Number of IPC messages received.
110 @item long int ru_nsignals
111 Number of signals received.
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).
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
124 @node Limits on Resources
125 @section Limiting Resource Usage
126 @cindex resource limits
127 @cindex limits on resource usage
128 @cindex usage limits
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.
136 There are two per-process limits associated with a resource:
137 @cindex limit
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
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
157 @pindex sys/resource.h
158 The symbols for use with @code{getrlimit}, @code{setrlimit},
159 @code{getrlimit64}, and @code{setrlimit64} are defined in
160 @file{sys/resource.h}.
162 @deftypefun int getrlimit (int @var{resource}, struct rlimit *@var{rlp})
163 @standards{BSD, sys/resource.h}
164 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
165 @c Direct syscall on most systems.
166 Read the current and maximum limits for the resource @var{resource}
167 and store them in @code{*@var{rlp}}.
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}.
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
177 @deftypefun int getrlimit64 (int @var{resource}, struct rlimit64 *@var{rlp})
178 @standards{Unix98, sys/resource.h}
179 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
180 @c Direct syscall on most systems, wrapper to getrlimit otherwise.
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}.
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
191 @deftypefun int setrlimit (int @var{resource}, const struct rlimit *@var{rlp})
192 @standards{BSD, sys/resource.h}
193 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
194 @c Direct syscall on most systems; lock-taking critical section on HURD.
195 Store the current and maximum limits for the resource @var{resource}
196 in @code{*@var{rlp}}.
198 The return value is @code{0} on success and @code{-1} on failure.  The
199 following @code{errno} error condition is possible:
201 @table @code
202 @item EPERM
203 @itemize @bullet
204 @item
205 The process tried to raise a current limit beyond the maximum limit.
207 @item
208 The process tried to raise a maximum limit, but is not superuser.
209 @end itemize
210 @end table
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
217 @deftypefun int setrlimit64 (int @var{resource}, const struct rlimit64 *@var{rlp})
218 @standards{Unix98, sys/resource.h}
219 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
220 @c Wrapper for setrlimit or direct syscall.
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}.
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
231 @deftp {Data Type} {struct rlimit}
232 @standards{BSD, sys/resource.h}
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:
237 @table @code
238 @item rlim_t rlim_cur
239 The current limit
241 @item rlim_t rlim_max
242 The maximum limit.
243 @end table
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
249 For the LFS functions a similar type is defined in @file{sys/resource.h}.
251 @deftp {Data Type} {struct rlimit64}
252 @standards{Unix98, sys/resource.h}
253 This structure is analogous to the @code{rlimit} structure above, but
254 its components have wider ranges.  It has two fields:
256 @table @code
257 @item rlim64_t rlim_cur
258 This is analogous to @code{rlimit.rlim_cur}, but with a different type.
260 @item rlim64_t rlim_max
261 This is analogous to @code{rlimit.rlim_max}, but with a different type.
262 @end table
264 @end deftp
266 Here is a list of resources for which you can specify a limit.  Memory
267 and file sizes are measured in bytes.
269 @vtable @code
270 @item RLIMIT_CPU
271 @standards{BSD, sys/resource.h}
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}.
276 @item RLIMIT_FSIZE
277 @standards{BSD, sys/resource.h}
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}.
282 @item RLIMIT_DATA
283 @standards{BSD, sys/resource.h}
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.
288 @item RLIMIT_STACK
289 @standards{BSD, sys/resource.h}
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}.
294 @item RLIMIT_CORE
295 @standards{BSD, sys/resource.h}
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.
301 @item RLIMIT_RSS
302 @standards{BSD, sys/resource.h}
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.
308 @item RLIMIT_MEMLOCK
309 @standards{BSD, sys/resource.h}
310 The maximum amount of memory that can be locked into physical memory (so
311 it will never be paged out).
313 @item RLIMIT_NPROC
314 @standards{BSD, sys/resource.h}
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}.
319 @item RLIMIT_NOFILE
320 @itemx RLIMIT_OFILE
321 @standardsx{RLIMIT_NOFILE, BSD, sys/resource.h}
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.
327 @item RLIMIT_AS
328 @standards{Unix98, sys/resource.h}
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.
334 @item RLIM_NLIMITS
335 @standards{BSD, sys/resource.h}
336 The number of different resource limits.  Any valid @var{resource}
337 operand must be less than @code{RLIM_NLIMITS}.
338 @end vtable
340 @deftypevr Constant rlim_t RLIM_INFINITY
341 @standards{BSD, sys/resource.h}
342 This constant stands for a value of ``infinity'' when supplied as
343 the limit value in @code{setrlimit}.
344 @end deftypevr
347 The following are historical functions to do some of what the functions
348 above do.  The functions above are better choices.
350 @code{ulimit} and the command symbols are declared in @file{ulimit.h}.
351 @pindex ulimit.h
353 @deftypefun {long int} ulimit (int @var{cmd}, @dots{})
354 @standards{BSD, ulimit.h}
355 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
356 @c Wrapper for getrlimit, setrlimit or
357 @c sysconf(_SC_OPEN_MAX)->getdtablesize->getrlimit.
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
361 command @var{cmd}.
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.
368 The @var{cmd} values and the operations they specify are:
369 @vtable @code
371 @item GETFSIZE
372 Get the current limit on the size of a file, in units of 512 bytes.
374 @item SETFSIZE
375 Set the current and maximum limit on the size of a file to @var{limit} *
376 512 bytes.
378 @end vtable
380 There are also some other @var{cmd} values that may do things on some
381 systems, but they are not supported.
383 Only the superuser may increase a maximum limit.
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:
390 @table @code
391 @item EPERM
392 A process tried to increase a maximum limit, but is not superuser.
393 @end table
396 @end deftypefun
398 @code{vlimit} and its resource symbols are declared in @file{sys/vlimit.h}.
399 @pindex sys/vlimit.h
401 @deftypefun int vlimit (int @var{resource}, int @var{limit})
402 @standards{BSD, sys/vlimit.h}
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.
408 @code{vlimit} sets the current limit for a resource for a process.
410 @var{resource} identifies the resource:
412 @vtable @code
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}.
425 @end vtable
427 The return value is zero for success, and @code{-1} with @code{errno} set
428 accordingly for failure:
430 @table @code
431 @item EPERM
432 The process tried to set its current limit beyond its maximum limit.
433 @end table
435 @end deftypefun
437 @node Priority
438 @section Process CPU Priority And Scheduling
439 @cindex process priority
440 @cindex cpu priority
441 @cindex priority of a process
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
446 @glibcadj{} functions to control it.
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.
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
460 section discusses the general concepts, some specifics of systems
461 that commonly use @theglibc{}, and some standards.
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.
469 The functions described in this section are all defined by the POSIX.1
470 and POSIX.1b standards (the @code{sched@dots{}} functions are POSIX.1b).
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.
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
482 * CPU Affinity::                    Limiting execution to certain CPUs
483 @end menu
487 @node Absolute Priority
488 @subsection Absolute Priority
489 @cindex absolute priority
490 @cindex priority, absolute
492 Every process has an absolute priority, and it is represented by a number.
493 The higher the number, the higher the absolute priority.
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
499 accommodate realtime systems, in which it is vital that certain processes
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.
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
508 process with the lower priority is already using the CPU (i.e., the
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.
514 @cindex runnable process
515 @strong{NB:}  The term ``runnable'' is a synonym for ``ready to run.''
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
519 CPU is determined by the scheduling policy.  If the processes have
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.
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.
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.
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.
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.
543 @subsubsection Using Absolute Priority
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
548 good CPU run are interrupts and page faults.
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.
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,
560 because as soon as the I/O is complete, the higher priority process will
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}.
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.
575 There are two ways to avoid this: 1) keep a shell running somewhere with
576 a higher absolute priority or 2) keep a controlling terminal attached to
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.
581 Some systems use absolute priority as a means of allocating a fixed
582 percentage of CPU time to a process.  To do this, a super high priority
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.
587 @strong{NB:}  The absolute priority is sometimes called the ``static
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.
592 @node Realtime Scheduling
593 @subsection Realtime Scheduling
594 @cindex realtime scheduling
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.
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}.
606 Each process has a scheduling policy.  For processes with absolute
607 priority other than zero, there are two available:
609 @enumerate
610 @item
611 First Come First Served
612 @item
613 Round Robin
614 @end enumerate
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.
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.
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.
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.
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.
639 To understand how scheduling works when processes of different scheduling
640 policies occupy the same absolute priority, you have to know the nitty
641 gritty details of how processes enter and exit the ready to run list.
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.
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.
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.
664 Only a privileged process can set the scheduling policy of a process
665 that has absolute priority higher than 0.
667 @node Basic Scheduling Functions
668 @subsection Basic Scheduling Functions
670 This section describes functions in @theglibc{} for setting the
671 absolute priority and scheduling policy of a process.
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>}.
677 For the case that the scheduling policy is traditional scheduling, more
678 functions to fine tune the scheduling are in @ref{Traditional Scheduling}.
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
683 on systems that use @theglibc{} is the inverse of what the POSIX
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.
690 The symbols in this section are declared by including file @file{sched.h}.
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
700 @deftp {Data Type} {struct sched_param}
701 @standards{POSIX, sched.h}
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
709 @deftypefun int sched_setscheduler (pid_t @var{pid}, int @var{policy}, const struct sched_param *@var{param})
710 @standards{POSIX, sched.h}
711 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
712 @c Direct syscall, Linux only.
714 This function sets both the absolute priority and the scheduling policy
715 for a task.
717 It assigns the absolute priority value given by @var{param} and the
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
720 negative, @code{sched_setscheduler} keeps the existing scheduling policy.
722 The following macros represent the valid values for @var{policy}:
724 @vtable @code
725 @item SCHED_OTHER
726 Traditional Scheduling
727 @item SCHED_FIFO
728 First In First Out
729 @item SCHED_RR
730 Round Robin
731 @end vtable
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.
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:
741 @table @code
742 @item EPERM
743 @itemize @bullet
744 @item
745 The calling task does not have @code{CAP_SYS_NICE} permission and
746 @var{policy} is not @code{SCHED_OTHER} (or it's negative and the
747 existing policy is not @code{SCHED_OTHER}.
749 @item
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
753 @var{pid}.
754 @c We need a cross reference to the capabilities section, when written.
755 @end itemize
757 @item ESRCH
758 There is no task with pid @var{pid} and @var{pid} is not zero.
760 @item EINVAL
761 @itemize @bullet
762 @item
763 @var{policy} does not identify an existing scheduling policy.
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.
772 @item
773 @var{pid} is negative.
774 @end itemize
775 @end table
777 @end deftypefun
780 @deftypefun int sched_getscheduler (pid_t @var{pid})
781 @standards{POSIX, sched.h}
782 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
783 @c Direct syscall, Linux only.
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.
788 The return value is the scheduling policy.  See
789 @code{sched_setscheduler} for the possible values.
791 If the function fails, the return value is instead @code{-1} and
792 @code{errno} is set accordingly.
794 The @code{errno} values specific to this function are:
796 @table @code
798 @item ESRCH
799 There is no task with pid @var{pid} and it is not zero.
801 @item EINVAL
802 @var{pid} is negative.
804 @end table
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}.
811 @end deftypefun
814 @deftypefun int sched_setparam (pid_t @var{pid}, const struct sched_param *@var{param})
815 @standards{POSIX, sched.h}
816 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
817 @c Direct syscall, Linux only.
819 This function sets a task's absolute priority.
821 It is functionally identical to @code{sched_setscheduler} with
822 @var{policy} = @code{-1}.
824 @c in fact, that's how it's implemented in Linux.
826 @end deftypefun
828 @deftypefun int sched_getparam (pid_t @var{pid}, struct sched_param *@var{param})
829 @standards{POSIX, sched.h}
830 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
831 @c Direct syscall, Linux only.
833 This function returns a task's absolute priority.
835 @var{pid} is the task ID of the task whose absolute priority you want
836 to know.
838 @var{param} is a pointer to a structure in which the function stores the
839 absolute priority of the task.
841 On success, the return value is @code{0}.  Otherwise, it is @code{-1}
842 and @code{errno} is set accordingly.  The @code{errno} values specific
843 to this function are:
845 @table @code
847 @item ESRCH
848 There is no task with ID @var{pid} and it is not zero.
850 @item EINVAL
851 @var{pid} is negative.
853 @end table
855 @end deftypefun
858 @deftypefun int sched_get_priority_min (int @var{policy})
859 @standards{POSIX, sched.h}
860 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
861 @c Direct syscall, Linux only.
863 This function returns the lowest absolute priority value that is
864 allowable for a task with scheduling policy @var{policy}.
866 On Linux, it is 0 for SCHED_OTHER and 1 for everything else.
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:
872 @table @code
873 @item EINVAL
874 @var{policy} does not identify an existing scheduling policy.
875 @end table
877 @end deftypefun
879 @deftypefun int sched_get_priority_max (int @var{policy})
880 @standards{POSIX, sched.h}
881 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
882 @c Direct syscall, Linux only.
884 This function returns the highest absolute priority value that is
885 allowable for a task that with scheduling policy @var{policy}.
887 On Linux, it is 0 for SCHED_OTHER and 99 for everything else.
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:
893 @table @code
894 @item EINVAL
895 @var{policy} does not identify an existing scheduling policy.
896 @end table
898 @end deftypefun
900 @deftypefun int sched_rr_get_interval (pid_t @var{pid}, struct timespec *@var{interval})
901 @standards{POSIX, sched.h}
902 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
903 @c Direct syscall, Linux only.
905 This function returns the length of the quantum (time slice) used with
906 the Round Robin scheduling policy, if it is used, for the task with
907 task ID @var{pid}.
909 It returns the length of time as @var{interval}.
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.
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.
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.
923 @end deftypefun
925 @deftypefun int sched_yield (void)
926 @standards{POSIX, sched.h}
927 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
928 @c Direct syscall on Linux; alias to swtch on HURD.
930 This function voluntarily gives up the task's claim on the CPU.
932 Technically, @code{sched_yield} causes the calling task to be made
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
935 gets pushed onto the tail of the queue of tasks that share its
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
939 tasks.
941 If there are no other tasks that share the calling task's absolute
942 priority, this function doesn't have any effect.
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.
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.
953 @end deftypefun
955 @node Traditional Scheduling
956 @subsection Traditional Scheduling
957 @cindex scheduling, traditional
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
961 are left over after the processes with higher absolute priority have
962 taken all they want, the scheduling described herein determines who
963 among the great unwashed processes gets them.
965 @menu
966 * Traditional Scheduling Intro::
967 * Traditional Scheduling Functions::
968 @end menu
970 @node Traditional Scheduling Intro
971 @subsubsection Introduction To Traditional Scheduling
973 Long before there was absolute priority (See @ref{Absolute Priority}),
974 Unix systems were scheduling the CPU using this system.  When POSIX came
975 in like the Romans and imposed absolute priorities to accommodate the
976 needs of realtime processing, it left the indigenous Absolute Priority
977 Zero processes to govern themselves by their own familiar scheduling
978 policy.
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.
986 But just to be clear about the scope of this scheduling: Any time a
987 process with an absolute priority of 0 and a process with an absolute
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.
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
995 over time.  The dynamic priority is meaningless for processes with
996 an absolute priority higher than zero.
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.
1002 In Linux, the value is a combination of these things, but mostly it
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.
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.
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.
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
1031 today.)
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.
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.
1046 @glibcadj{} functions for getting and setting nice values are described in
1047 @xref{Traditional Scheduling Functions}.
1049 @node Traditional Scheduling Functions
1050 @subsubsection Functions For Traditional Scheduling
1052 @pindex sys/resource.h
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}.
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.
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
1063 priority values:
1065 @vtable @code
1066 @item PRIO_MIN
1067 @standards{BSD, sys/resource.h}
1068 The lowest valid nice value.
1070 @item PRIO_MAX
1071 @standards{BSD, sys/resource.h}
1072 The highest valid nice value.
1073 @end vtable
1075 @deftypefun int getpriority (int @var{class}, int @var{id})
1076 @standards{BSD, sys/resource.h}
1077 @standards{POSIX, sys/resource.h}
1078 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1079 @c Direct syscall on UNIX.  On HURD, calls _hurd_priority_which_map.
1080 Return the nice value of a set of processes; @var{class} and @var{id}
1081 specify which ones (see below).  If the processes specified do not all
1082 have the same nice value, this returns the lowest value that any of them
1083 has.
1085 On success, the return value is @code{0}.  Otherwise, it is @code{-1}
1086 and @code{errno} is set accordingly.  The @code{errno} values specific
1087 to this function are:
1089 @table @code
1090 @item ESRCH
1091 The combination of @var{class} and @var{id} does not match any existing
1092 process.
1094 @item EINVAL
1095 The value of @var{class} is not valid.
1096 @end table
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.
1102 @end deftypefun
1104 @deftypefun int setpriority (int @var{class}, int @var{id}, int @var{niceval})
1105 @standards{BSD, sys/resource.h}
1106 @standards{POSIX, sys/resource.h}
1107 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1108 @c Direct syscall on UNIX.  On HURD, calls _hurd_priority_which_map.
1109 Set the nice value of a set of processes to @var{niceval}; @var{class}
1110 and @var{id} specify which ones (see below).
1112 The return value is @code{0} on success, and @code{-1} on
1113 failure.  The following @code{errno} error condition are possible for
1114 this function:
1116 @table @code
1117 @item ESRCH
1118 The combination of @var{class} and @var{id} does not match any existing
1119 process.
1121 @item EINVAL
1122 The value of @var{class} is not valid.
1124 @item EPERM
1125 The call would set the nice value of a process which is owned by a different
1126 user than the calling process (i.e., the target process' real or effective
1127 uid does not match the calling process' effective uid) and the calling
1128 process does not have @code{CAP_SYS_NICE} permission.
1130 @item EACCES
1131 The call would lower the process' nice value and the process does not have
1132 @code{CAP_SYS_NICE} permission.
1133 @end table
1135 @end deftypefun
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}:
1141 @vtable @code
1142 @item PRIO_PROCESS
1143 @standards{BSD, sys/resource.h}
1144 One particular process.  The argument @var{id} is a process ID (pid).
1146 @item PRIO_PGRP
1147 @standards{BSD, sys/resource.h}
1148 All the processes in a particular process group.  The argument @var{id} is
1149 a process group ID (pgid).
1151 @item PRIO_USER
1152 @standards{BSD, sys/resource.h}
1153 All the processes owned by a particular user (i.e., whose real uid
1154 indicates the user).  The argument @var{id} is a user ID (uid).
1155 @end vtable
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}.
1160 @deftypefun int nice (int @var{increment})
1161 @standards{BSD, unistd.h}
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.
1167 Increment the nice value of the calling process by @var{increment}.
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}.
1173 Here is an equivalent definition of @code{nice}:
1175 @smallexample
1177 nice (int increment)
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;
1186 @end smallexample
1187 @end deftypefun
1190 @node CPU Affinity
1191 @subsection Limiting execution to certain CPUs
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.
1200 There are a number of reasons why a program might want to have control
1201 over this aspect of the system as well:
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
1207 making progress by other processes or threads using CPU resources.  In
1208 this case the special process would be confined to a CPU which no
1209 other process or thread is allowed to use.
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
1214 Architecture) machines.  Preferably memory should be accessed locally
1215 but this requirement is usually not visible to the scheduler.
1216 Therefore forcing a process or thread to the CPUs which have local
1217 access to the most-used memory helps to significantly boost the
1218 performance.
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
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
1230 schedule the thread or process on CPUs specified by the affinity
1231 masks.  The interfaces which @theglibc{} define follow to some
1232 extent the Linux kernel interface.
1234 @deftp {Data Type} cpu_set_t
1235 @standards{GNU, sched.h}
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.
1242 This type is a GNU extension and is defined in @file{sched.h}.
1243 @end deftp
1245 To manipulate the bitset, to set and reset bits, a number of macros are
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.
1250 @deftypevr Macro int CPU_SETSIZE
1251 @standards{GNU, sched.h}
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
1256 The type @code{cpu_set_t} should be considered opaque; all
1257 manipulation should happen via the next four macros.
1259 @deftypefn Macro void CPU_ZERO (cpu_set_t *@var{set})
1260 @standards{GNU, sched.h}
1261 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1262 @c CPU_ZERO ok
1263 @c  __CPU_ZERO_S ok
1264 @c   memset dup ok
1265 This macro initializes the CPU set @var{set} to be the empty set.
1267 This macro is a GNU extension and is defined in @file{sched.h}.
1268 @end deftypefn
1270 @deftypefn Macro void CPU_SET (int @var{cpu}, cpu_set_t *@var{set})
1271 @standards{GNU, sched.h}
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
1277 This macro adds @var{cpu} to the CPU set @var{set}.
1279 The @var{cpu} parameter must not have side effects since it is
1280 evaluated more than once.
1282 This macro is a GNU extension and is defined in @file{sched.h}.
1283 @end deftypefn
1285 @deftypefn Macro void CPU_CLR (int @var{cpu}, cpu_set_t *@var{set})
1286 @standards{GNU, sched.h}
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
1292 This macro removes @var{cpu} from the CPU set @var{set}.
1294 The @var{cpu} parameter must not have side effects since it is
1295 evaluated more than once.
1297 This macro is a GNU extension and is defined in @file{sched.h}.
1298 @end deftypefn
1300 @deftypefn Macro int CPU_ISSET (int @var{cpu}, const cpu_set_t *@var{set})
1301 @standards{GNU, sched.h}
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
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.
1310 The @var{cpu} parameter must not have side effects since it is
1311 evaluated more than once.
1313 This macro is a GNU extension and is defined in @file{sched.h}.
1314 @end deftypefn
1317 CPU bitsets can be constructed from scratch or the currently installed
1318 affinity mask can be retrieved from the system.
1320 @deftypefun int sched_getaffinity (pid_t @var{pid}, size_t @var{cpusetsize}, cpu_set_t *@var{cpuset})
1321 @standards{GNU, sched.h}
1322 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1323 @c Wrapped syscall to zero out past the kernel cpu set size; Linux
1324 @c only.
1326 This function stores the CPU affinity mask for the process or thread
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.
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.
1335 @table @code
1336 @item ESRCH
1337 No process or thread with the given ID found.
1339 @item EFAULT
1340 The pointer @var{cpuset} does not point to a valid object.
1341 @end table
1343 This function is a GNU extension and is declared in @file{sched.h}.
1344 @end deftypefun
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.
1350 @deftypefun int sched_setaffinity (pid_t @var{pid}, size_t @var{cpusetsize}, const cpu_set_t *@var{cpuset})
1351 @standards{GNU, sched.h}
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.
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}.
1358 If successful the function returns zero and the scheduler will in the future
1359 take the affinity information into account.
1361 If the function fails it will return @code{-1} and @code{errno} is set
1362 to the error code:
1364 @table @code
1365 @item ESRCH
1366 No process or thread with the given ID found.
1368 @item EFAULT
1369 The pointer @var{cpuset} does not point to a valid object.
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
1376 This function is a GNU extension and is declared in @file{sched.h}.
1377 @end deftypefun
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.
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:
1393 @table @code
1394 @item ENOSYS
1395 The operating system does not support this function.
1396 @end table
1398 This function is Linux-specific and is declared in @file{sched.h}.
1399 @end deftypefun
1401 @node Memory Resources
1402 @section Querying memory available resources
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
1406 functions like @code{mmap} it is necessary to know about the size of
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
1410 Unix systems will be given.
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
1418 @node Memory Subsystem
1419 @subsection Overview about traditional Unix memory handling
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.
1431 @cindex shared memory
1432 Using a virtual address space has several advantages.  The most important
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).
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
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
1462 of two bytes.  The smallest page size in use today is 4096, with 8192,
1463 16384, and 65536 being other popular sizes.
1465 @node Query Memory Parameters
1466 @subsection How to get information about the memory subsystem?
1468 The page size of the virtual memory the process sees is essential to
1469 know in several situations.  Some programming interfaces (e.g.,
1470 @code{mmap}, @pxref{Memory-mapped I/O}) require the user to provide
1471 information adjusted to the page size.  In the case of @code{mmap} it is
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
1482 this overhead must not push the total size over the page size multiple.)
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.
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.
1496 @deftypefun int getpagesize (void)
1497 @standards{BSD, unistd.h}
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.
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.
1505 The function is declared in @file{unistd.h}.
1506 @end deftypefun
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
1511 @vindex _SC_PHYS_PAGES
1512 @cindex sysconf
1513 @smallexample
1514   sysconf (_SC_PHYS_PAGES)
1515 @end smallexample
1517 @noindent
1518 returns the total number of pages of physical memory the system has.
1519 This does not mean all this memory is available.  This information can
1520 be found using
1522 @vindex _SC_AVPHYS_PAGES
1523 @cindex sysconf
1524 @smallexample
1525   sysconf (_SC_AVPHYS_PAGES)
1526 @end smallexample
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.
1536 @Theglibc{} provides in addition to these already described way to
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.
1541 @deftypefun {long int} get_phys_pages (void)
1542 @standards{GNU, sys/sysinfo.h}
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.
1545 The @code{get_phys_pages} function returns the total number of pages of
1546 physical memory the system has.  To get the amount of memory this number has to
1547 be multiplied by the page size.
1549 This function is a GNU extension.
1550 @end deftypefun
1552 @deftypefun {long int} get_avphys_pages (void)
1553 @standards{GNU, sys/sysinfo.h}
1554 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
1555 The @code{get_avphys_pages} function returns the number of available pages of
1556 physical memory the system has.  To get the amount of memory this number has to
1557 be multiplied by the page size.
1559 This function is a GNU extension.
1560 @end deftypefun
1562 @node Processor Resources
1563 @section Learn about the processors available
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
1572 @vindex _SC_NPROCESSORS_CONF
1573 @cindex sysconf
1574 @smallexample
1575   sysconf (_SC_NPROCESSORS_CONF)
1576 @end smallexample
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
1583 @vindex _SC_NPROCESSORS_ONLN
1584 @cindex sysconf
1585 @smallexample
1586   sysconf (_SC_NPROCESSORS_ONLN)
1587 @end smallexample
1589 @noindent
1590 returns the number of processors which are currently online (i.e.,
1591 available).
1593 For these two pieces of information @theglibc{} also provides
1594 functions to get the information directly.  The functions are declared
1595 in @file{sys/sysinfo.h}.
1597 @deftypefun int get_nprocs_conf (void)
1598 @standards{GNU, sys/sysinfo.h}
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.
1602 The @code{get_nprocs_conf} function returns the number of processors the
1603 operating system configured.
1605 This function is a GNU extension.
1606 @end deftypefun
1608 @deftypefun int get_nprocs (void)
1609 @standards{GNU, sys/sysinfo.h}
1610 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
1611 @c This function reads from /proc using file descriptor I/O.
1612 The @code{get_nprocs} function returns the number of available processors.
1614 This function is a GNU extension.
1615 @end deftypefun
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
1621 running.  This number is an average over different periods of time
1622 (normally 1, 5, and 15 minutes).
1624 @deftypefun int getloadavg (double @var{loadavg}[], int @var{nelem})
1625 @standards{BSD, stdlib.h}
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.
1630 This function gets the 1, 5 and 15 minute load averages of the
1631 system.  The values are placed in @var{loadavg}.  @code{getloadavg} will
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.
1636 This function is declared in @file{stdlib.h}.
1637 @end deftypefun