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