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