Blame manual/resource.texi

Packit 6c4009
@node Resource Usage And Limitation, Non-Local Exits, Date and Time, Top
Packit 6c4009
@c %MENU% Functions for examining resource usage and getting and setting limits
Packit 6c4009
@chapter Resource Usage And Limitation
Packit 6c4009
This chapter describes functions for examining how much of various kinds of
Packit 6c4009
resources (CPU time, memory, etc.) a process has used and getting and setting
Packit 6c4009
limits on future usage.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Resource Usage::		Measuring various resources used.
Packit 6c4009
* Limits on Resources::		Specifying limits on resource usage.
Packit 6c4009
* Priority::			Reading or setting process run priority.
Packit 6c4009
* Memory Resources::            Querying memory available resources.
Packit 6c4009
* Processor Resources::         Learn about the processors available.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Resource Usage
Packit 6c4009
@section Resource Usage
Packit 6c4009
Packit 6c4009
@pindex sys/resource.h
Packit 6c4009
The function @code{getrusage} and the data type @code{struct rusage}
Packit 6c4009
are used to examine the resource usage of a process.  They are declared
Packit 6c4009
in @file{sys/resource.h}.
Packit 6c4009
Packit 6c4009
@deftypefun int getrusage (int @var{processes}, struct rusage *@var{rusage})
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c On HURD, this calls task_info 3 times.  On UNIX, it's a syscall.
Packit 6c4009
This function reports resource usage totals for processes specified by
Packit 6c4009
@var{processes}, storing the information in @code{*@var{rusage}}.
Packit 6c4009
Packit 6c4009
In most systems, @var{processes} has only two valid values:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item RUSAGE_SELF
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
Just the current process.
Packit 6c4009
Packit 6c4009
@item RUSAGE_CHILDREN
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
All child processes (direct and indirect) that have already terminated.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
The return value of @code{getrusage} is zero for success, and @code{-1}
Packit 6c4009
for failure.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EINVAL
Packit 6c4009
The argument @var{processes} is not valid.
Packit 6c4009
@end table
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
One way of getting resource usage for a particular child process is with
Packit 6c4009
the function @code{wait4}, which returns totals for a child when it
Packit 6c4009
terminates.  @xref{BSD Wait Functions}.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct rusage}
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
This data type stores various resource usage statistics.  It has the
Packit 6c4009
following members, and possibly others:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item struct timeval ru_utime
Packit 6c4009
Time spent executing user instructions.
Packit 6c4009
Packit 6c4009
@item struct timeval ru_stime
Packit 6c4009
Time spent in operating system code on behalf of @var{processes}.
Packit 6c4009
Packit 6c4009
@item long int ru_maxrss
Packit 6c4009
The maximum resident set size used, in kilobytes.  That is, the maximum
Packit 6c4009
number of kilobytes of physical memory that @var{processes} used
Packit 6c4009
simultaneously.
Packit 6c4009
Packit 6c4009
@item long int ru_ixrss
Packit 6c4009
An integral value expressed in kilobytes times ticks of execution, which
Packit 6c4009
indicates the amount of memory used by text that was shared with other
Packit 6c4009
processes.
Packit 6c4009
Packit 6c4009
@item long int ru_idrss
Packit 6c4009
An integral value expressed the same way, which is the amount of
Packit 6c4009
unshared memory used for data.
Packit 6c4009
Packit 6c4009
@item long int ru_isrss
Packit 6c4009
An integral value expressed the same way, which is the amount of
Packit 6c4009
unshared memory used for stack space.
Packit 6c4009
Packit 6c4009
@item long int ru_minflt
Packit 6c4009
The number of page faults which were serviced without requiring any I/O.
Packit 6c4009
Packit 6c4009
@item long int ru_majflt
Packit 6c4009
The number of page faults which were serviced by doing I/O.
Packit 6c4009
Packit 6c4009
@item long int ru_nswap
Packit 6c4009
The number of times @var{processes} was swapped entirely out of main memory.
Packit 6c4009
Packit 6c4009
@item long int ru_inblock
Packit 6c4009
The number of times the file system had to read from the disk on behalf
Packit 6c4009
of @var{processes}.
Packit 6c4009
Packit 6c4009
@item long int ru_oublock
Packit 6c4009
The number of times the file system had to write to the disk on behalf
Packit 6c4009
of @var{processes}.
Packit 6c4009
Packit 6c4009
@item long int ru_msgsnd
Packit 6c4009
Number of IPC messages sent.
Packit 6c4009
Packit 6c4009
@item long int ru_msgrcv
Packit 6c4009
Number of IPC messages received.
Packit 6c4009
Packit 6c4009
@item long int ru_nsignals
Packit 6c4009
Number of signals received.
Packit 6c4009
Packit 6c4009
@item long int ru_nvcsw
Packit 6c4009
The number of times @var{processes} voluntarily invoked a context switch
Packit 6c4009
(usually to wait for some service).
Packit 6c4009
Packit 6c4009
@item long int ru_nivcsw
Packit 6c4009
The number of times an involuntary context switch took place (because
Packit 6c4009
a time slice expired, or another process of higher priority was
Packit 6c4009
scheduled).
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@code{vtimes} is a historical function that does some of what
Packit 6c4009
@code{getrusage} does.  @code{getrusage} is a better choice.
Packit 6c4009
Packit 6c4009
@code{vtimes} and its @code{vtimes} data structure are declared in
Packit 6c4009
@file{sys/vtimes.h}.
Packit 6c4009
@pindex sys/vtimes.h
Packit 6c4009
Packit 6c4009
@deftypefun int vtimes (struct vtimes *@var{current}, struct vtimes *@var{child})
Packit 6c4009
@standards{???, sys/vtimes.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Calls getrusage twice.
Packit 6c4009
Packit 6c4009
@code{vtimes} reports resource usage totals for a process.
Packit 6c4009
Packit 6c4009
If @var{current} is non-null, @code{vtimes} stores resource usage totals for
Packit 6c4009
the invoking process alone in the structure to which it points.  If
Packit 6c4009
@var{child} is non-null, @code{vtimes} stores resource usage totals for all
Packit 6c4009
past children (which have terminated) of the invoking process in the structure
Packit 6c4009
to which it points.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct vtimes}
Packit 6c4009
This data type contains information about the resource usage of a process.
Packit 6c4009
Each member corresponds to a member of the @code{struct rusage} data type
Packit 6c4009
described above.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item vm_utime
Packit 6c4009
User CPU time.  Analogous to @code{ru_utime} in @code{struct rusage}
Packit 6c4009
@item vm_stime
Packit 6c4009
System CPU time.  Analogous to @code{ru_stime} in @code{struct rusage}
Packit 6c4009
@item vm_idsrss
Packit 6c4009
Data and stack memory.  The sum of the values that would be reported as
Packit 6c4009
@code{ru_idrss} and @code{ru_isrss} in @code{struct rusage}
Packit 6c4009
@item vm_ixrss
Packit 6c4009
Shared memory.  Analogous to @code{ru_ixrss} in @code{struct rusage}
Packit 6c4009
@item vm_maxrss
Packit 6c4009
Maximent resident set size.  Analogous to @code{ru_maxrss} in
Packit 6c4009
@code{struct rusage}
Packit 6c4009
@item vm_majflt
Packit 6c4009
Major page faults.  Analogous to @code{ru_majflt} in @code{struct rusage}
Packit 6c4009
@item vm_minflt
Packit 6c4009
Minor page faults.  Analogous to @code{ru_minflt} in @code{struct rusage}
Packit 6c4009
@item vm_nswap
Packit 6c4009
Swap count.  Analogous to @code{ru_nswap} in @code{struct rusage}
Packit 6c4009
@item vm_inblk
Packit 6c4009
Disk reads.  Analogous to @code{ru_inblk} in @code{struct rusage}
Packit 6c4009
@item vm_oublk
Packit 6c4009
Disk writes.  Analogous to @code{ru_oublk} in @code{struct rusage}
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
Packit 6c4009
The return value is zero if the function succeeds; @code{-1} otherwise.
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
An additional historical function for examining resource usage,
Packit 6c4009
@code{vtimes}, is supported but not documented here.  It is declared in
Packit 6c4009
@file{sys/vtimes.h}.
Packit 6c4009
Packit 6c4009
@node Limits on Resources
Packit 6c4009
@section Limiting Resource Usage
Packit 6c4009
@cindex resource limits
Packit 6c4009
@cindex limits on resource usage
Packit 6c4009
@cindex usage limits
Packit 6c4009
Packit 6c4009
You can specify limits for the resource usage of a process.  When the
Packit 6c4009
process tries to exceed a limit, it may get a signal, or the system call
Packit 6c4009
by which it tried to do so may fail, depending on the resource.  Each
Packit 6c4009
process initially inherits its limit values from its parent, but it can
Packit 6c4009
subsequently change them.
Packit 6c4009
Packit 6c4009
There are two per-process limits associated with a resource:
Packit 6c4009
@cindex limit
Packit 6c4009
Packit 6c4009
@table @dfn
Packit 6c4009
@item current limit
Packit 6c4009
The current limit is the value the system will not allow usage to
Packit 6c4009
exceed.  It is also called the ``soft limit'' because the process being
Packit 6c4009
limited can generally raise the current limit at will.
Packit 6c4009
@cindex current limit
Packit 6c4009
@cindex soft limit
Packit 6c4009
Packit 6c4009
@item maximum limit
Packit 6c4009
The maximum limit is the maximum value to which a process is allowed to
Packit 6c4009
set its current limit.  It is also called the ``hard limit'' because
Packit 6c4009
there is no way for a process to get around it.  A process may lower
Packit 6c4009
its own maximum limit, but only the superuser may increase a maximum
Packit 6c4009
limit.
Packit 6c4009
@cindex maximum limit
Packit 6c4009
@cindex hard limit
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@pindex sys/resource.h
Packit 6c4009
The symbols for use with @code{getrlimit}, @code{setrlimit},
Packit 6c4009
@code{getrlimit64}, and @code{setrlimit64} are defined in
Packit 6c4009
@file{sys/resource.h}.
Packit 6c4009
Packit 6c4009
@deftypefun int getrlimit (int @var{resource}, struct rlimit *@var{rlp})
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall on most systems.
Packit 6c4009
Read the current and maximum limits for the resource @var{resource}
Packit 6c4009
and store them in @code{*@var{rlp}}.
Packit 6c4009
Packit 6c4009
The return value is @code{0} on success and @code{-1} on failure.  The
Packit 6c4009
only possible @code{errno} error condition is @code{EFAULT}.
Packit 6c4009
Packit 6c4009
When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
Packit 6c4009
32-bit system this function is in fact @code{getrlimit64}.  Thus, the
Packit 6c4009
LFS interface transparently replaces the old interface.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int getrlimit64 (int @var{resource}, struct rlimit64 *@var{rlp})
Packit 6c4009
@standards{Unix98, sys/resource.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall on most systems, wrapper to getrlimit otherwise.
Packit 6c4009
This function is similar to @code{getrlimit} but its second parameter is
Packit 6c4009
a pointer to a variable of type @code{struct rlimit64}, which allows it
Packit 6c4009
to read values which wouldn't fit in the member of a @code{struct
Packit 6c4009
rlimit}.
Packit 6c4009
Packit 6c4009
If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
Packit 6c4009
32-bit machine, this function is available under the name
Packit 6c4009
@code{getrlimit} and so transparently replaces the old interface.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int setrlimit (int @var{resource}, const struct rlimit *@var{rlp})
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall on most systems; lock-taking critical section on HURD.
Packit 6c4009
Store the current and maximum limits for the resource @var{resource}
Packit 6c4009
in @code{*@var{rlp}}.
Packit 6c4009
Packit 6c4009
The return value is @code{0} on success and @code{-1} on failure.  The
Packit 6c4009
following @code{errno} error condition is possible:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EPERM
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
The process tried to raise a current limit beyond the maximum limit.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The process tried to raise a maximum limit, but is not superuser.
Packit 6c4009
@end itemize
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
Packit 6c4009
32-bit system this function is in fact @code{setrlimit64}.  Thus, the
Packit 6c4009
LFS interface transparently replaces the old interface.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int setrlimit64 (int @var{resource}, const struct rlimit64 *@var{rlp})
Packit 6c4009
@standards{Unix98, sys/resource.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Wrapper for setrlimit or direct syscall.
Packit 6c4009
This function is similar to @code{setrlimit} but its second parameter is
Packit 6c4009
a pointer to a variable of type @code{struct rlimit64} which allows it
Packit 6c4009
to set values which wouldn't fit in the member of a @code{struct
Packit 6c4009
rlimit}.
Packit 6c4009
Packit 6c4009
If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
Packit 6c4009
32-bit machine this function is available under the name
Packit 6c4009
@code{setrlimit} and so transparently replaces the old interface.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct rlimit}
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
This structure is used with @code{getrlimit} to receive limit values,
Packit 6c4009
and with @code{setrlimit} to specify limit values for a particular process
Packit 6c4009
and resource.  It has two fields:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item rlim_t rlim_cur
Packit 6c4009
The current limit
Packit 6c4009
Packit 6c4009
@item rlim_t rlim_max
Packit 6c4009
The maximum limit.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
For @code{getrlimit}, the structure is an output; it receives the current
Packit 6c4009
values.  For @code{setrlimit}, it specifies the new values.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
For the LFS functions a similar type is defined in @file{sys/resource.h}.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct rlimit64}
Packit 6c4009
@standards{Unix98, sys/resource.h}
Packit 6c4009
This structure is analogous to the @code{rlimit} structure above, but
Packit 6c4009
its components have wider ranges.  It has two fields:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item rlim64_t rlim_cur
Packit 6c4009
This is analogous to @code{rlimit.rlim_cur}, but with a different type.
Packit 6c4009
Packit 6c4009
@item rlim64_t rlim_max
Packit 6c4009
This is analogous to @code{rlimit.rlim_max}, but with a different type.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
Here is a list of resources for which you can specify a limit.  Memory
Packit 6c4009
and file sizes are measured in bytes.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item RLIMIT_CPU
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The maximum amount of CPU time the process can use.  If it runs for
Packit 6c4009
longer than this, it gets a signal: @code{SIGXCPU}.  The value is
Packit 6c4009
measured in seconds.  @xref{Operation Error Signals}.
Packit 6c4009
Packit 6c4009
@item RLIMIT_FSIZE
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The maximum size of file the process can create.  Trying to write a
Packit 6c4009
larger file causes a signal: @code{SIGXFSZ}.  @xref{Operation Error
Packit 6c4009
Signals}.
Packit 6c4009
Packit 6c4009
@item RLIMIT_DATA
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The maximum size of data memory for the process.  If the process tries
Packit 6c4009
to allocate data memory beyond this amount, the allocation function
Packit 6c4009
fails.
Packit 6c4009
Packit 6c4009
@item RLIMIT_STACK
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The maximum stack size for the process.  If the process tries to extend
Packit 6c4009
its stack past this size, it gets a @code{SIGSEGV} signal.
Packit 6c4009
@xref{Program Error Signals}.
Packit 6c4009
Packit 6c4009
@item RLIMIT_CORE
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The maximum size core file that this process can create.  If the process
Packit 6c4009
terminates and would dump a core file larger than this, then no core
Packit 6c4009
file is created.  So setting this limit to zero prevents core files from
Packit 6c4009
ever being created.
Packit 6c4009
Packit 6c4009
@item RLIMIT_RSS
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The maximum amount of physical memory that this process should get.
Packit 6c4009
This parameter is a guide for the system's scheduler and memory
Packit 6c4009
allocator; the system may give the process more memory when there is a
Packit 6c4009
surplus.
Packit 6c4009
Packit 6c4009
@item RLIMIT_MEMLOCK
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The maximum amount of memory that can be locked into physical memory (so
Packit 6c4009
it will never be paged out).
Packit 6c4009
Packit 6c4009
@item RLIMIT_NPROC
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The maximum number of processes that can be created with the same user ID.
Packit 6c4009
If you have reached the limit for your user ID, @code{fork} will fail
Packit 6c4009
with @code{EAGAIN}.  @xref{Creating a Process}.
Packit 6c4009
Packit 6c4009
@item RLIMIT_NOFILE
Packit 6c4009
@itemx RLIMIT_OFILE
Packit 6c4009
@standardsx{RLIMIT_NOFILE, BSD, sys/resource.h}
Packit 6c4009
The maximum number of files that the process can open.  If it tries to
Packit 6c4009
open more files than this, its open attempt fails with @code{errno}
Packit 6c4009
@code{EMFILE}.  @xref{Error Codes}.  Not all systems support this limit;
Packit 6c4009
GNU does, and 4.4 BSD does.
Packit 6c4009
Packit 6c4009
@item RLIMIT_AS
Packit 6c4009
@standards{Unix98, sys/resource.h}
Packit 6c4009
The maximum size of total memory that this process should get.  If the
Packit 6c4009
process tries to allocate more memory beyond this amount with, for
Packit 6c4009
example, @code{brk}, @code{malloc}, @code{mmap} or @code{sbrk}, the
Packit 6c4009
allocation function fails.
Packit 6c4009
Packit 6c4009
@item RLIM_NLIMITS
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The number of different resource limits.  Any valid @var{resource}
Packit 6c4009
operand must be less than @code{RLIM_NLIMITS}.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@deftypevr Constant rlim_t RLIM_INFINITY
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
This constant stands for a value of ``infinity'' when supplied as
Packit 6c4009
the limit value in @code{setrlimit}.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
Packit 6c4009
The following are historical functions to do some of what the functions
Packit 6c4009
above do.  The functions above are better choices.
Packit 6c4009
Packit 6c4009
@code{ulimit} and the command symbols are declared in @file{ulimit.h}.
Packit 6c4009
@pindex ulimit.h
Packit 6c4009
Packit 6c4009
@deftypefun {long int} ulimit (int @var{cmd}, @dots{})
Packit 6c4009
@standards{BSD, ulimit.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Wrapper for getrlimit, setrlimit or
Packit 6c4009
@c sysconf(_SC_OPEN_MAX)->getdtablesize->getrlimit.
Packit 6c4009
Packit 6c4009
@code{ulimit} gets the current limit or sets the current and maximum
Packit 6c4009
limit for a particular resource for the calling process according to the
Packit 6c4009
command @var{cmd}.
Packit 6c4009
Packit 6c4009
If you are getting a limit, the command argument is the only argument.
Packit 6c4009
If you are setting a limit, there is a second argument:
Packit 6c4009
@code{long int} @var{limit} which is the value to which you are setting
Packit 6c4009
the limit.
Packit 6c4009
Packit 6c4009
The @var{cmd} values and the operations they specify are:
Packit 6c4009
@vtable @code
Packit 6c4009
Packit 6c4009
@item GETFSIZE
Packit 6c4009
Get the current limit on the size of a file, in units of 512 bytes.
Packit 6c4009
Packit 6c4009
@item SETFSIZE
Packit 6c4009
Set the current and maximum limit on the size of a file to @var{limit} *
Packit 6c4009
512 bytes.
Packit 6c4009
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
There are also some other @var{cmd} values that may do things on some
Packit 6c4009
systems, but they are not supported.
Packit 6c4009
Packit 6c4009
Only the superuser may increase a maximum limit.
Packit 6c4009
Packit 6c4009
When you successfully get a limit, the return value of @code{ulimit} is
Packit 6c4009
that limit, which is never negative.  When you successfully set a limit,
Packit 6c4009
the return value is zero.  When the function fails, the return value is
Packit 6c4009
@code{-1} and @code{errno} is set according to the reason:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EPERM
Packit 6c4009
A process tried to increase a maximum limit, but is not superuser.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@code{vlimit} and its resource symbols are declared in @file{sys/vlimit.h}.
Packit 6c4009
@pindex sys/vlimit.h
Packit 6c4009
Packit 6c4009
@deftypefun int vlimit (int @var{resource}, int @var{limit})
Packit 6c4009
@standards{BSD, sys/vlimit.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:setrlimit}}@asunsafe{}@acsafe{}}
Packit 6c4009
@c It calls getrlimit and modifies the rlim_cur field before calling
Packit 6c4009
@c setrlimit.  There's a window for a concurrent call to setrlimit that
Packit 6c4009
@c modifies e.g. rlim_max, which will be lost if running as super-user.
Packit 6c4009
Packit 6c4009
@code{vlimit} sets the current limit for a resource for a process.
Packit 6c4009
Packit 6c4009
@var{resource} identifies the resource:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item LIM_CPU
Packit 6c4009
Maximum CPU time.  Same as @code{RLIMIT_CPU} for @code{setrlimit}.
Packit 6c4009
@item LIM_FSIZE
Packit 6c4009
Maximum file size.  Same as @code{RLIMIT_FSIZE} for @code{setrlimit}.
Packit 6c4009
@item LIM_DATA
Packit 6c4009
Maximum data memory.  Same as @code{RLIMIT_DATA} for @code{setrlimit}.
Packit 6c4009
@item LIM_STACK
Packit 6c4009
Maximum stack size.  Same as @code{RLIMIT_STACK} for @code{setrlimit}.
Packit 6c4009
@item LIM_CORE
Packit 6c4009
Maximum core file size.  Same as @code{RLIMIT_COR} for @code{setrlimit}.
Packit 6c4009
@item LIM_MAXRSS
Packit 6c4009
Maximum physical memory.  Same as @code{RLIMIT_RSS} for @code{setrlimit}.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
The return value is zero for success, and @code{-1} with @code{errno} set
Packit 6c4009
accordingly for failure:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EPERM
Packit 6c4009
The process tried to set its current limit beyond its maximum limit.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Priority
Packit 6c4009
@section Process CPU Priority And Scheduling
Packit 6c4009
@cindex process priority
Packit 6c4009
@cindex cpu priority
Packit 6c4009
@cindex priority of a process
Packit 6c4009
Packit 6c4009
When multiple processes simultaneously require CPU time, the system's
Packit 6c4009
scheduling policy and process CPU priorities determine which processes
Packit 6c4009
get it.  This section describes how that determination is made and
Packit 6c4009
@glibcadj{} functions to control it.
Packit 6c4009
Packit 6c4009
It is common to refer to CPU scheduling simply as scheduling and a
Packit 6c4009
process' CPU priority simply as the process' priority, with the CPU
Packit 6c4009
resource being implied.  Bear in mind, though, that CPU time is not the
Packit 6c4009
only resource a process uses or that processes contend for.  In some
Packit 6c4009
cases, it is not even particularly important.  Giving a process a high
Packit 6c4009
``priority'' may have very little effect on how fast a process runs with
Packit 6c4009
respect to other processes.  The priorities discussed in this section
Packit 6c4009
apply only to CPU time.
Packit 6c4009
Packit 6c4009
CPU scheduling is a complex issue and different systems do it in wildly
Packit 6c4009
different ways.  New ideas continually develop and find their way into
Packit 6c4009
the intricacies of the various systems' scheduling algorithms.  This
Packit 6c4009
section discusses the general concepts, some specifics of systems
Packit 6c4009
that commonly use @theglibc{}, and some standards.
Packit 6c4009
Packit 6c4009
For simplicity, we talk about CPU contention as if there is only one CPU
Packit 6c4009
in the system.  But all the same principles apply when a processor has
Packit 6c4009
multiple CPUs, and knowing that the number of processes that can run at
Packit 6c4009
any one time is equal to the number of CPUs, you can easily extrapolate
Packit 6c4009
the information.
Packit 6c4009
Packit 6c4009
The functions described in this section are all defined by the POSIX.1
Packit 6c4009
and POSIX.1b standards (the @code{sched@dots{}} functions are POSIX.1b).
Packit 6c4009
However, POSIX does not define any semantics for the values that these
Packit 6c4009
functions get and set.  In this chapter, the semantics are based on the
Packit 6c4009
Linux kernel's implementation of the POSIX standard.  As you will see,
Packit 6c4009
the Linux implementation is quite the inverse of what the authors of the
Packit 6c4009
POSIX syntax had in mind.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Absolute Priority::               The first tier of priority.  Posix
Packit 6c4009
* Realtime Scheduling::             Scheduling among the process nobility
Packit 6c4009
* Basic Scheduling Functions::      Get/set scheduling policy, priority
Packit 6c4009
* Traditional Scheduling::          Scheduling among the vulgar masses
Packit 6c4009
* CPU Affinity::                    Limiting execution to certain CPUs
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Absolute Priority
Packit 6c4009
@subsection Absolute Priority
Packit 6c4009
@cindex absolute priority
Packit 6c4009
@cindex priority, absolute
Packit 6c4009
Packit 6c4009
Every process has an absolute priority, and it is represented by a number.
Packit 6c4009
The higher the number, the higher the absolute priority.
Packit 6c4009
Packit 6c4009
@cindex realtime CPU scheduling
Packit 6c4009
On systems of the past, and most systems today, all processes have
Packit 6c4009
absolute priority 0 and this section is irrelevant.  In that case,
Packit 6c4009
@xref{Traditional Scheduling}.  Absolute priorities were invented to
Packit 6c4009
accommodate realtime systems, in which it is vital that certain processes
Packit 6c4009
be able to respond to external events happening in real time, which
Packit 6c4009
means they cannot wait around while some other process that @emph{wants
Packit 6c4009
to}, but doesn't @emph{need to} run occupies the CPU.
Packit 6c4009
Packit 6c4009
@cindex ready to run
Packit 6c4009
@cindex preemptive scheduling
Packit 6c4009
When two processes are in contention to use the CPU at any instant, the
Packit 6c4009
one with the higher absolute priority always gets it.  This is true even if the
Packit 6c4009
process with the lower priority is already using the CPU (i.e., the
Packit 6c4009
scheduling is preemptive).  Of course, we're only talking about
Packit 6c4009
processes that are running or ``ready to run,'' which means they are
Packit 6c4009
ready to execute instructions right now.  When a process blocks to wait
Packit 6c4009
for something like I/O, its absolute priority is irrelevant.
Packit 6c4009
Packit 6c4009
@cindex runnable process
Packit 6c4009
@strong{NB:}  The term ``runnable'' is a synonym for ``ready to run.''
Packit 6c4009
Packit 6c4009
When two processes are running or ready to run and both have the same
Packit 6c4009
absolute priority, it's more interesting.  In that case, who gets the
Packit 6c4009
CPU is determined by the scheduling policy.  If the processes have
Packit 6c4009
absolute priority 0, the traditional scheduling policy described in
Packit 6c4009
@ref{Traditional Scheduling} applies.  Otherwise, the policies described
Packit 6c4009
in @ref{Realtime Scheduling} apply.
Packit 6c4009
Packit 6c4009
You normally give an absolute priority above 0 only to a process that
Packit 6c4009
can be trusted not to hog the CPU.  Such processes are designed to block
Packit 6c4009
(or terminate) after relatively short CPU runs.
Packit 6c4009
Packit 6c4009
A process begins life with the same absolute priority as its parent
Packit 6c4009
process.  Functions described in @ref{Basic Scheduling Functions} can
Packit 6c4009
change it.
Packit 6c4009
Packit 6c4009
Only a privileged process can change a process' absolute priority to
Packit 6c4009
something other than @code{0}.  Only a privileged process or the
Packit 6c4009
target process' owner can change its absolute priority at all.
Packit 6c4009
Packit 6c4009
POSIX requires absolute priority values used with the realtime
Packit 6c4009
scheduling policies to be consecutive with a range of at least 32.  On
Packit 6c4009
Linux, they are 1 through 99.  The functions
Packit 6c4009
@code{sched_get_priority_max} and @code{sched_set_priority_min} portably
Packit 6c4009
tell you what the range is on a particular system.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@subsubsection Using Absolute Priority
Packit 6c4009
Packit 6c4009
One thing you must keep in mind when designing real time applications is
Packit 6c4009
that having higher absolute priority than any other process doesn't
Packit 6c4009
guarantee the process can run continuously.  Two things that can wreck a
Packit 6c4009
good CPU run are interrupts and page faults.
Packit 6c4009
Packit 6c4009
Interrupt handlers live in that limbo between processes.  The CPU is
Packit 6c4009
executing instructions, but they aren't part of any process.  An
Packit 6c4009
interrupt will stop even the highest priority process.  So you must
Packit 6c4009
allow for slight delays and make sure that no device in the system has
Packit 6c4009
an interrupt handler that could cause too long a delay between
Packit 6c4009
instructions for your process.
Packit 6c4009
Packit 6c4009
Similarly, a page fault causes what looks like a straightforward
Packit 6c4009
sequence of instructions to take a long time.  The fact that other
Packit 6c4009
processes get to run while the page faults in is of no consequence,
Packit 6c4009
because as soon as the I/O is complete, the higher priority process will
Packit 6c4009
kick them out and run again, but the wait for the I/O itself could be a
Packit 6c4009
problem.  To neutralize this threat, use @code{mlock} or
Packit 6c4009
@code{mlockall}.
Packit 6c4009
Packit 6c4009
There are a few ramifications of the absoluteness of this priority on a
Packit 6c4009
single-CPU system that you need to keep in mind when you choose to set a
Packit 6c4009
priority and also when you're working on a program that runs with high
Packit 6c4009
absolute priority.  Consider a process that has higher absolute priority
Packit 6c4009
than any other process in the system and due to a bug in its program, it
Packit 6c4009
gets into an infinite loop.  It will never cede the CPU.  You can't run
Packit 6c4009
a command to kill it because your command would need to get the CPU in
Packit 6c4009
order to run.  The errant program is in complete control.  It controls
Packit 6c4009
the vertical, it controls the horizontal.
Packit 6c4009
Packit 6c4009
There are two ways to avoid this: 1) keep a shell running somewhere with
Packit 6c4009
a higher absolute priority or 2) keep a controlling terminal attached to
Packit 6c4009
the high priority process group.  All the priority in the world won't
Packit 6c4009
stop an interrupt handler from running and delivering a signal to the
Packit 6c4009
process if you hit Control-C.
Packit 6c4009
Packit 6c4009
Some systems use absolute priority as a means of allocating a fixed
Packit 6c4009
percentage of CPU time to a process.  To do this, a super high priority
Packit 6c4009
privileged process constantly monitors the process' CPU usage and raises
Packit 6c4009
its absolute priority when the process isn't getting its entitled share
Packit 6c4009
and lowers it when the process is exceeding it.
Packit 6c4009
Packit 6c4009
@strong{NB:}  The absolute priority is sometimes called the ``static
Packit 6c4009
priority.''  We don't use that term in this manual because it misses the
Packit 6c4009
most important feature of the absolute priority:  its absoluteness.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Realtime Scheduling
Packit 6c4009
@subsection Realtime Scheduling
Packit 6c4009
@cindex realtime scheduling
Packit 6c4009
Packit 6c4009
Whenever two processes with the same absolute priority are ready to run,
Packit 6c4009
the kernel has a decision to make, because only one can run at a time.
Packit 6c4009
If the processes have absolute priority 0, the kernel makes this decision
Packit 6c4009
as described in @ref{Traditional Scheduling}.  Otherwise, the decision
Packit 6c4009
is as described in this section.
Packit 6c4009
Packit 6c4009
If two processes are ready to run but have different absolute priorities,
Packit 6c4009
the decision is much simpler, and is described in @ref{Absolute
Packit 6c4009
Priority}.
Packit 6c4009
Packit 6c4009
Each process has a scheduling policy.  For processes with absolute
Packit 6c4009
priority other than zero, there are two available:
Packit 6c4009
Packit 6c4009
@enumerate
Packit 6c4009
@item
Packit 6c4009
First Come First Served
Packit 6c4009
@item
Packit 6c4009
Round Robin
Packit 6c4009
@end enumerate
Packit 6c4009
Packit 6c4009
The most sensible case is where all the processes with a certain
Packit 6c4009
absolute priority have the same scheduling policy.  We'll discuss that
Packit 6c4009
first.
Packit 6c4009
Packit 6c4009
In Round Robin, processes share the CPU, each one running for a small
Packit 6c4009
quantum of time (``time slice'') and then yielding to another in a
Packit 6c4009
circular fashion.  Of course, only processes that are ready to run and
Packit 6c4009
have the same absolute priority are in this circle.
Packit 6c4009
Packit 6c4009
In First Come First Served, the process that has been waiting the
Packit 6c4009
longest to run gets the CPU, and it keeps it until it voluntarily
Packit 6c4009
relinquishes the CPU, runs out of things to do (blocks), or gets
Packit 6c4009
preempted by a higher priority process.
Packit 6c4009
Packit 6c4009
First Come First Served, along with maximal absolute priority and
Packit 6c4009
careful control of interrupts and page faults, is the one to use when a
Packit 6c4009
process absolutely, positively has to run at full CPU speed or not at
Packit 6c4009
all.
Packit 6c4009
Packit 6c4009
Judicious use of @code{sched_yield} function invocations by processes
Packit 6c4009
with First Come First Served scheduling policy forms a good compromise
Packit 6c4009
between Round Robin and First Come First Served.
Packit 6c4009
Packit 6c4009
To understand how scheduling works when processes of different scheduling
Packit 6c4009
policies occupy the same absolute priority, you have to know the nitty
Packit 6c4009
gritty details of how processes enter and exit the ready to run list.
Packit 6c4009
Packit 6c4009
In both cases, the ready to run list is organized as a true queue, where
Packit 6c4009
a process gets pushed onto the tail when it becomes ready to run and is
Packit 6c4009
popped off the head when the scheduler decides to run it.  Note that
Packit 6c4009
ready to run and running are two mutually exclusive states.  When the
Packit 6c4009
scheduler runs a process, that process is no longer ready to run and no
Packit 6c4009
longer in the ready to run list.  When the process stops running, it
Packit 6c4009
may go back to being ready to run again.
Packit 6c4009
Packit 6c4009
The only difference between a process that is assigned the Round Robin
Packit 6c4009
scheduling policy and a process that is assigned First Come First Serve
Packit 6c4009
is that in the former case, the process is automatically booted off the
Packit 6c4009
CPU after a certain amount of time.  When that happens, the process goes
Packit 6c4009
back to being ready to run, which means it enters the queue at the tail.
Packit 6c4009
The time quantum we're talking about is small.  Really small.  This is
Packit 6c4009
not your father's timesharing.  For example, with the Linux kernel, the
Packit 6c4009
round robin time slice is a thousand times shorter than its typical
Packit 6c4009
time slice for traditional scheduling.
Packit 6c4009
Packit 6c4009
A process begins life with the same scheduling policy as its parent process.
Packit 6c4009
Functions described in @ref{Basic Scheduling Functions} can change it.
Packit 6c4009
Packit 6c4009
Only a privileged process can set the scheduling policy of a process
Packit 6c4009
that has absolute priority higher than 0.
Packit 6c4009
Packit 6c4009
@node Basic Scheduling Functions
Packit 6c4009
@subsection Basic Scheduling Functions
Packit 6c4009
Packit 6c4009
This section describes functions in @theglibc{} for setting the
Packit 6c4009
absolute priority and scheduling policy of a process.
Packit 6c4009
Packit 6c4009
@strong{Portability Note:}  On systems that have the functions in this
Packit 6c4009
section, the macro _POSIX_PRIORITY_SCHEDULING is defined in
Packit 6c4009
@file{<unistd.h>}.
Packit 6c4009
Packit 6c4009
For the case that the scheduling policy is traditional scheduling, more
Packit 6c4009
functions to fine tune the scheduling are in @ref{Traditional Scheduling}.
Packit 6c4009
Packit 6c4009
Don't try to make too much out of the naming and structure of these
Packit 6c4009
functions.  They don't match the concepts described in this manual
Packit 6c4009
because the functions are as defined by POSIX.1b, but the implementation
Packit 6c4009
on systems that use @theglibc{} is the inverse of what the POSIX
Packit 6c4009
structure contemplates.  The POSIX scheme assumes that the primary
Packit 6c4009
scheduling parameter is the scheduling policy and that the priority
Packit 6c4009
value, if any, is a parameter of the scheduling policy.  In the
Packit 6c4009
implementation, though, the priority value is king and the scheduling
Packit 6c4009
policy, if anything, only fine tunes the effect of that priority.
Packit 6c4009
Packit 6c4009
The symbols in this section are declared by including file @file{sched.h}.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct sched_param}
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
This structure describes an absolute priority.
Packit 6c4009
@table @code
Packit 6c4009
@item int sched_priority
Packit 6c4009
absolute priority value
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftypefun int sched_setscheduler (pid_t @var{pid}, int @var{policy}, const struct sched_param *@var{param})
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall, Linux only.
Packit 6c4009
Packit 6c4009
This function sets both the absolute priority and the scheduling policy
Packit 6c4009
for a process.
Packit 6c4009
Packit 6c4009
It assigns the absolute priority value given by @var{param} and the
Packit 6c4009
scheduling policy @var{policy} to the process with Process ID @var{pid},
Packit 6c4009
or the calling process if @var{pid} is zero.  If @var{policy} is
Packit 6c4009
negative, @code{sched_setscheduler} keeps the existing scheduling policy.
Packit 6c4009
Packit 6c4009
The following macros represent the valid values for @var{policy}:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item SCHED_OTHER
Packit 6c4009
Traditional Scheduling
Packit 6c4009
@item SCHED_FIFO
Packit 6c4009
First In First Out
Packit 6c4009
@item SCHED_RR
Packit 6c4009
Round Robin
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@c The Linux kernel code (in sched.c) actually reschedules the process,
Packit 6c4009
@c but it puts it at the head of the run queue, so I'm not sure just what
Packit 6c4009
@c the effect is, but it must be subtle.
Packit 6c4009
Packit 6c4009
On success, the return value is @code{0}.  Otherwise, it is @code{-1}
Packit 6c4009
and @code{ERRNO} is set accordingly.  The @code{errno} values specific
Packit 6c4009
to this function are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EPERM
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
The calling process does not have @code{CAP_SYS_NICE} permission and
Packit 6c4009
@var{policy} is not @code{SCHED_OTHER} (or it's negative and the
Packit 6c4009
existing policy is not @code{SCHED_OTHER}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The calling process does not have @code{CAP_SYS_NICE} permission and its
Packit 6c4009
owner is not the target process' owner.  I.e., the effective uid of the
Packit 6c4009
calling process is neither the effective nor the real uid of process
Packit 6c4009
@var{pid}.
Packit 6c4009
@c We need a cross reference to the capabilities section, when written.
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
@item ESRCH
Packit 6c4009
There is no process with pid @var{pid} and @var{pid} is not zero.
Packit 6c4009
Packit 6c4009
@item EINVAL
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
@var{policy} does not identify an existing scheduling policy.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The absolute priority value identified by *@var{param} is outside the
Packit 6c4009
valid range for the scheduling policy @var{policy} (or the existing
Packit 6c4009
scheduling policy if @var{policy} is negative) or @var{param} is
Packit 6c4009
null.  @code{sched_get_priority_max} and @code{sched_get_priority_min}
Packit 6c4009
tell you what the valid range is.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@var{pid} is negative.
Packit 6c4009
@end itemize
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun int sched_getscheduler (pid_t @var{pid})
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall, Linux only.
Packit 6c4009
Packit 6c4009
This function returns the scheduling policy assigned to the process with
Packit 6c4009
Process ID (pid) @var{pid}, or the calling process if @var{pid} is zero.
Packit 6c4009
Packit 6c4009
The return value is the scheduling policy.  See
Packit 6c4009
@code{sched_setscheduler} for the possible values.
Packit 6c4009
Packit 6c4009
If the function fails, the return value is instead @code{-1} and
Packit 6c4009
@code{errno} is set accordingly.
Packit 6c4009
Packit 6c4009
The @code{errno} values specific to this function are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
Packit 6c4009
@item ESRCH
Packit 6c4009
There is no process with pid @var{pid} and it is not zero.
Packit 6c4009
Packit 6c4009
@item EINVAL
Packit 6c4009
@var{pid} is negative.
Packit 6c4009
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
Note that this function is not an exact mate to @code{sched_setscheduler}
Packit 6c4009
because while that function sets the scheduling policy and the absolute
Packit 6c4009
priority, this function gets only the scheduling policy.  To get the
Packit 6c4009
absolute priority, use @code{sched_getparam}.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun int sched_setparam (pid_t @var{pid}, const struct sched_param *@var{param})
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall, Linux only.
Packit 6c4009
Packit 6c4009
This function sets a process' absolute priority.
Packit 6c4009
Packit 6c4009
It is functionally identical to @code{sched_setscheduler} with
Packit 6c4009
@var{policy} = @code{-1}.
Packit 6c4009
Packit 6c4009
@c in fact, that's how it's implemented in Linux.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int sched_getparam (pid_t @var{pid}, struct sched_param *@var{param})
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall, Linux only.
Packit 6c4009
Packit 6c4009
This function returns a process' absolute priority.
Packit 6c4009
Packit 6c4009
@var{pid} is the Process ID (pid) of the process whose absolute priority
Packit 6c4009
you want to know.
Packit 6c4009
Packit 6c4009
@var{param} is a pointer to a structure in which the function stores the
Packit 6c4009
absolute priority of the process.
Packit 6c4009
Packit 6c4009
On success, the return value is @code{0}.  Otherwise, it is @code{-1}
Packit 6c4009
and @code{errno} is set accordingly.  The @code{errno} values specific
Packit 6c4009
to this function are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
Packit 6c4009
@item ESRCH
Packit 6c4009
There is no process with pid @var{pid} and it is not zero.
Packit 6c4009
Packit 6c4009
@item EINVAL
Packit 6c4009
@var{pid} is negative.
Packit 6c4009
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun int sched_get_priority_min (int @var{policy})
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall, Linux only.
Packit 6c4009
Packit 6c4009
This function returns the lowest absolute priority value that is
Packit 6c4009
allowable for a process with scheduling policy @var{policy}.
Packit 6c4009
Packit 6c4009
On Linux, it is 0 for SCHED_OTHER and 1 for everything else.
Packit 6c4009
Packit 6c4009
On success, the return value is @code{0}.  Otherwise, it is @code{-1}
Packit 6c4009
and @code{ERRNO} is set accordingly.  The @code{errno} values specific
Packit 6c4009
to this function are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EINVAL
Packit 6c4009
@var{policy} does not identify an existing scheduling policy.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int sched_get_priority_max (int @var{policy})
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall, Linux only.
Packit 6c4009
Packit 6c4009
This function returns the highest absolute priority value that is
Packit 6c4009
allowable for a process that with scheduling policy @var{policy}.
Packit 6c4009
Packit 6c4009
On Linux, it is 0 for SCHED_OTHER and 99 for everything else.
Packit 6c4009
Packit 6c4009
On success, the return value is @code{0}.  Otherwise, it is @code{-1}
Packit 6c4009
and @code{ERRNO} is set accordingly.  The @code{errno} values specific
Packit 6c4009
to this function are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EINVAL
Packit 6c4009
@var{policy} does not identify an existing scheduling policy.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int sched_rr_get_interval (pid_t @var{pid}, struct timespec *@var{interval})
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall, Linux only.
Packit 6c4009
Packit 6c4009
This function returns the length of the quantum (time slice) used with
Packit 6c4009
the Round Robin scheduling policy, if it is used, for the process with
Packit 6c4009
Process ID @var{pid}.
Packit 6c4009
Packit 6c4009
It returns the length of time as @var{interval}.
Packit 6c4009
@c We need a cross-reference to where timespec is explained.  But that
Packit 6c4009
@c section doesn't exist yet, and the time chapter needs to be slightly
Packit 6c4009
@c reorganized so there is a place to put it (which will be right next
Packit 6c4009
@c to timeval, which is presently misplaced).  2000.05.07.
Packit 6c4009
Packit 6c4009
With a Linux kernel, the round robin time slice is always 150
Packit 6c4009
microseconds, and @var{pid} need not even be a real pid.
Packit 6c4009
Packit 6c4009
The return value is @code{0} on success and in the pathological case
Packit 6c4009
that it fails, the return value is @code{-1} and @code{errno} is set
Packit 6c4009
accordingly.  There is nothing specific that can go wrong with this
Packit 6c4009
function, so there are no specific @code{errno} values.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int sched_yield (void)
Packit 6c4009
@standards{POSIX, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall on Linux; alias to swtch on HURD.
Packit 6c4009
Packit 6c4009
This function voluntarily gives up the process' claim on the CPU.
Packit 6c4009
Packit 6c4009
Technically, @code{sched_yield} causes the calling process to be made
Packit 6c4009
immediately ready to run (as opposed to running, which is what it was
Packit 6c4009
before).  This means that if it has absolute priority higher than 0, it
Packit 6c4009
gets pushed onto the tail of the queue of processes that share its
Packit 6c4009
absolute priority and are ready to run, and it will run again when its
Packit 6c4009
turn next arrives.  If its absolute priority is 0, it is more
Packit 6c4009
complicated, but still has the effect of yielding the CPU to other
Packit 6c4009
processes.
Packit 6c4009
Packit 6c4009
If there are no other processes that share the calling process' absolute
Packit 6c4009
priority, this function doesn't have any effect.
Packit 6c4009
Packit 6c4009
To the extent that the containing program is oblivious to what other
Packit 6c4009
processes in the system are doing and how fast it executes, this
Packit 6c4009
function appears as a no-op.
Packit 6c4009
Packit 6c4009
The return value is @code{0} on success and in the pathological case
Packit 6c4009
that it fails, the return value is @code{-1} and @code{errno} is set
Packit 6c4009
accordingly.  There is nothing specific that can go wrong with this
Packit 6c4009
function, so there are no specific @code{errno} values.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Traditional Scheduling
Packit 6c4009
@subsection Traditional Scheduling
Packit 6c4009
@cindex scheduling, traditional
Packit 6c4009
Packit 6c4009
This section is about the scheduling among processes whose absolute
Packit 6c4009
priority is 0.  When the system hands out the scraps of CPU time that
Packit 6c4009
are left over after the processes with higher absolute priority have
Packit 6c4009
taken all they want, the scheduling described herein determines who
Packit 6c4009
among the great unwashed processes gets them.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Traditional Scheduling Intro::
Packit 6c4009
* Traditional Scheduling Functions::
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Traditional Scheduling Intro
Packit 6c4009
@subsubsection Introduction To Traditional Scheduling
Packit 6c4009
Packit 6c4009
Long before there was absolute priority (See @ref{Absolute Priority}),
Packit 6c4009
Unix systems were scheduling the CPU using this system.  When POSIX came
Packit 6c4009
in like the Romans and imposed absolute priorities to accommodate the
Packit 6c4009
needs of realtime processing, it left the indigenous Absolute Priority
Packit 6c4009
Zero processes to govern themselves by their own familiar scheduling
Packit 6c4009
policy.
Packit 6c4009
Packit 6c4009
Indeed, absolute priorities higher than zero are not available on many
Packit 6c4009
systems today and are not typically used when they are, being intended
Packit 6c4009
mainly for computers that do realtime processing.  So this section
Packit 6c4009
describes the only scheduling many programmers need to be concerned
Packit 6c4009
about.
Packit 6c4009
Packit 6c4009
But just to be clear about the scope of this scheduling: Any time a
Packit 6c4009
process with an absolute priority of 0 and a process with an absolute
Packit 6c4009
priority higher than 0 are ready to run at the same time, the one with
Packit 6c4009
absolute priority 0 does not run.  If it's already running when the
Packit 6c4009
higher priority ready-to-run process comes into existence, it stops
Packit 6c4009
immediately.
Packit 6c4009
Packit 6c4009
In addition to its absolute priority of zero, every process has another
Packit 6c4009
priority, which we will refer to as "dynamic priority" because it changes
Packit 6c4009
over time.  The dynamic priority is meaningless for processes with
Packit 6c4009
an absolute priority higher than zero.
Packit 6c4009
Packit 6c4009
The dynamic priority sometimes determines who gets the next turn on the
Packit 6c4009
CPU.  Sometimes it determines how long turns last.  Sometimes it
Packit 6c4009
determines whether a process can kick another off the CPU.
Packit 6c4009
Packit 6c4009
In Linux, the value is a combination of these things, but mostly it
Packit 6c4009
just determines the length of the time slice.  The higher a process'
Packit 6c4009
dynamic priority, the longer a shot it gets on the CPU when it gets one.
Packit 6c4009
If it doesn't use up its time slice before giving up the CPU to do
Packit 6c4009
something like wait for I/O, it is favored for getting the CPU back when
Packit 6c4009
it's ready for it, to finish out its time slice.  Other than that,
Packit 6c4009
selection of processes for new time slices is basically round robin.
Packit 6c4009
But the scheduler does throw a bone to the low priority processes: A
Packit 6c4009
process' dynamic priority rises every time it is snubbed in the
Packit 6c4009
scheduling process.  In Linux, even the fat kid gets to play.
Packit 6c4009
Packit 6c4009
The fluctuation of a process' dynamic priority is regulated by another
Packit 6c4009
value: The ``nice'' value.  The nice value is an integer, usually in the
Packit 6c4009
range -20 to 20, and represents an upper limit on a process' dynamic
Packit 6c4009
priority.  The higher the nice number, the lower that limit.
Packit 6c4009
Packit 6c4009
On a typical Linux system, for example, a process with a nice value of
Packit 6c4009
20 can get only 10 milliseconds on the CPU at a time, whereas a process
Packit 6c4009
with a nice value of -20 can achieve a high enough priority to get 400
Packit 6c4009
milliseconds.
Packit 6c4009
Packit 6c4009
The idea of the nice value is deferential courtesy.  In the beginning,
Packit 6c4009
in the Unix garden of Eden, all processes shared equally in the bounty
Packit 6c4009
of the computer system.  But not all processes really need the same
Packit 6c4009
share of CPU time, so the nice value gave a courteous process the
Packit 6c4009
ability to refuse its equal share of CPU time that others might prosper.
Packit 6c4009
Hence, the higher a process' nice value, the nicer the process is.
Packit 6c4009
(Then a snake came along and offered some process a negative nice value
Packit 6c4009
and the system became the crass resource allocation system we know
Packit 6c4009
today.)
Packit 6c4009
Packit 6c4009
Dynamic priorities tend upward and downward with an objective of
Packit 6c4009
smoothing out allocation of CPU time and giving quick response time to
Packit 6c4009
infrequent requests.  But they never exceed their nice limits, so on a
Packit 6c4009
heavily loaded CPU, the nice value effectively determines how fast a
Packit 6c4009
process runs.
Packit 6c4009
Packit 6c4009
In keeping with the socialistic heritage of Unix process priority, a
Packit 6c4009
process begins life with the same nice value as its parent process and
Packit 6c4009
can raise it at will.  A process can also raise the nice value of any
Packit 6c4009
other process owned by the same user (or effective user).  But only a
Packit 6c4009
privileged process can lower its nice value.  A privileged process can
Packit 6c4009
also raise or lower another process' nice value.
Packit 6c4009
Packit 6c4009
@glibcadj{} functions for getting and setting nice values are described in
Packit 6c4009
@xref{Traditional Scheduling Functions}.
Packit 6c4009
Packit 6c4009
@node Traditional Scheduling Functions
Packit 6c4009
@subsubsection Functions For Traditional Scheduling
Packit 6c4009
Packit 6c4009
@pindex sys/resource.h
Packit 6c4009
This section describes how you can read and set the nice value of a
Packit 6c4009
process.  All these symbols are declared in @file{sys/resource.h}.
Packit 6c4009
Packit 6c4009
The function and macro names are defined by POSIX, and refer to
Packit 6c4009
"priority," but the functions actually have to do with nice values, as
Packit 6c4009
the terms are used both in the manual and POSIX.
Packit 6c4009
Packit 6c4009
The range of valid nice values depends on the kernel, but typically it
Packit 6c4009
runs from @code{-20} to @code{20}.  A lower nice value corresponds to
Packit 6c4009
higher priority for the process.  These constants describe the range of
Packit 6c4009
priority values:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item PRIO_MIN
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The lowest valid nice value.
Packit 6c4009
Packit 6c4009
@item PRIO_MAX
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
The highest valid nice value.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@deftypefun int getpriority (int @var{class}, int @var{id})
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
@standards{POSIX, sys/resource.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall on UNIX.  On HURD, calls _hurd_priority_which_map.
Packit 6c4009
Return the nice value of a set of processes; @var{class} and @var{id}
Packit 6c4009
specify which ones (see below).  If the processes specified do not all
Packit 6c4009
have the same nice value, this returns the lowest value that any of them
Packit 6c4009
has.
Packit 6c4009
Packit 6c4009
On success, the return value is @code{0}.  Otherwise, it is @code{-1}
Packit 6c4009
and @code{errno} is set accordingly.  The @code{errno} values specific
Packit 6c4009
to this function are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item ESRCH
Packit 6c4009
The combination of @var{class} and @var{id} does not match any existing
Packit 6c4009
process.
Packit 6c4009
Packit 6c4009
@item EINVAL
Packit 6c4009
The value of @var{class} is not valid.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
If the return value is @code{-1}, it could indicate failure, or it could
Packit 6c4009
be the nice value.  The only way to make certain is to set @code{errno =
Packit 6c4009
0} before calling @code{getpriority}, then use @code{errno != 0}
Packit 6c4009
afterward as the criterion for failure.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int setpriority (int @var{class}, int @var{id}, int @var{niceval})
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
@standards{POSIX, sys/resource.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Direct syscall on UNIX.  On HURD, calls _hurd_priority_which_map.
Packit 6c4009
Set the nice value of a set of processes to @var{niceval}; @var{class}
Packit 6c4009
and @var{id} specify which ones (see below).
Packit 6c4009
Packit 6c4009
The return value is @code{0} on success, and @code{-1} on
Packit 6c4009
failure.  The following @code{errno} error condition are possible for
Packit 6c4009
this function:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item ESRCH
Packit 6c4009
The combination of @var{class} and @var{id} does not match any existing
Packit 6c4009
process.
Packit 6c4009
Packit 6c4009
@item EINVAL
Packit 6c4009
The value of @var{class} is not valid.
Packit 6c4009
Packit 6c4009
@item EPERM
Packit 6c4009
The call would set the nice value of a process which is owned by a different
Packit 6c4009
user than the calling process (i.e., the target process' real or effective
Packit 6c4009
uid does not match the calling process' effective uid) and the calling
Packit 6c4009
process does not have @code{CAP_SYS_NICE} permission.
Packit 6c4009
Packit 6c4009
@item EACCES
Packit 6c4009
The call would lower the process' nice value and the process does not have
Packit 6c4009
@code{CAP_SYS_NICE} permission.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The arguments @var{class} and @var{id} together specify a set of
Packit 6c4009
processes in which you are interested.  These are the possible values of
Packit 6c4009
@var{class}:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item PRIO_PROCESS
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
One particular process.  The argument @var{id} is a process ID (pid).
Packit 6c4009
Packit 6c4009
@item PRIO_PGRP
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
All the processes in a particular process group.  The argument @var{id} is
Packit 6c4009
a process group ID (pgid).
Packit 6c4009
Packit 6c4009
@item PRIO_USER
Packit 6c4009
@standards{BSD, sys/resource.h}
Packit 6c4009
All the processes owned by a particular user (i.e., whose real uid
Packit 6c4009
indicates the user).  The argument @var{id} is a user ID (uid).
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
If the argument @var{id} is 0, it stands for the calling process, its
Packit 6c4009
process group, or its owner (real uid), according to @var{class}.
Packit 6c4009
Packit 6c4009
@deftypefun int nice (int @var{increment})
Packit 6c4009
@standards{BSD, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:setpriority}}@asunsafe{}@acsafe{}}
Packit 6c4009
@c Calls getpriority before and after setpriority, using the result of
Packit 6c4009
@c the first call to compute the argument for setpriority.  This creates
Packit 6c4009
@c a window for a concurrent setpriority (or nice) call to be lost or
Packit 6c4009
@c exhibit surprising behavior.
Packit 6c4009
Increment the nice value of the calling process by @var{increment}.
Packit 6c4009
The return value is the new nice value on success, and @code{-1} on
Packit 6c4009
failure.  In the case of failure, @code{errno} will be set to the
Packit 6c4009
same values as for @code{setpriority}.
Packit 6c4009
Packit 6c4009
Packit 6c4009
Here is an equivalent definition of @code{nice}:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
int
Packit 6c4009
nice (int increment)
Packit 6c4009
@{
Packit 6c4009
  int result, old = getpriority (PRIO_PROCESS, 0);
Packit 6c4009
  result = setpriority (PRIO_PROCESS, 0, old + increment);
Packit 6c4009
  if (result != -1)
Packit 6c4009
      return old + increment;
Packit 6c4009
  else
Packit 6c4009
      return -1;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node CPU Affinity
Packit 6c4009
@subsection Limiting execution to certain CPUs
Packit 6c4009
Packit 6c4009
On a multi-processor system the operating system usually distributes
Packit 6c4009
the different processes which are runnable on all available CPUs in a
Packit 6c4009
way which allows the system to work most efficiently.  Which processes
Packit 6c4009
and threads run can be to some extend be control with the scheduling
Packit 6c4009
functionality described in the last sections.  But which CPU finally
Packit 6c4009
executes which process or thread is not covered.
Packit 6c4009
Packit 6c4009
There are a number of reasons why a program might want to have control
Packit 6c4009
over this aspect of the system as well:
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
One thread or process is responsible for absolutely critical work
Packit 6c4009
which under no circumstances must be interrupted or hindered from
Packit 6c4009
making progress by other processes or threads using CPU resources.  In
Packit 6c4009
this case the special process would be confined to a CPU which no
Packit 6c4009
other process or thread is allowed to use.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The access to certain resources (RAM, I/O ports) has different costs
Packit 6c4009
from different CPUs.  This is the case in NUMA (Non-Uniform Memory
Packit 6c4009
Architecture) machines.  Preferably memory should be accessed locally
Packit 6c4009
but this requirement is usually not visible to the scheduler.
Packit 6c4009
Therefore forcing a process or thread to the CPUs which have local
Packit 6c4009
access to the most-used memory helps to significantly boost the
Packit 6c4009
performance.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
In controlled runtimes resource allocation and book-keeping work (for
Packit 6c4009
instance garbage collection) is performance local to processors.  This
Packit 6c4009
can help to reduce locking costs if the resources do not have to be
Packit 6c4009
protected from concurrent accesses from different processors.
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
The POSIX standard up to this date is of not much help to solve this
Packit 6c4009
problem.  The Linux kernel provides a set of interfaces to allow
Packit 6c4009
specifying @emph{affinity sets} for a process.  The scheduler will
Packit 6c4009
schedule the thread or process on CPUs specified by the affinity
Packit 6c4009
masks.  The interfaces which @theglibc{} define follow to some
Packit 6c4009
extent the Linux kernel interface.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} cpu_set_t
Packit 6c4009
@standards{GNU, sched.h}
Packit 6c4009
This data set is a bitset where each bit represents a CPU.  How the
Packit 6c4009
system's CPUs are mapped to bits in the bitset is system dependent.
Packit 6c4009
The data type has a fixed size; in the unlikely case that the number
Packit 6c4009
of bits are not sufficient to describe the CPUs of the system a
Packit 6c4009
different interface has to be used.
Packit 6c4009
Packit 6c4009
This type is a GNU extension and is defined in @file{sched.h}.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
To manipulate the bitset, to set and reset bits, a number of macros are
Packit 6c4009
defined.  Some of the macros take a CPU number as a parameter.  Here
Packit 6c4009
it is important to never exceed the size of the bitset.  The following
Packit 6c4009
macro specifies the number of bits in the @code{cpu_set_t} bitset.
Packit 6c4009
Packit 6c4009
@deftypevr Macro int CPU_SETSIZE
Packit 6c4009
@standards{GNU, sched.h}
Packit 6c4009
The value of this macro is the maximum number of CPUs which can be
Packit 6c4009
handled with a @code{cpu_set_t} object.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
The type @code{cpu_set_t} should be considered opaque; all
Packit 6c4009
manipulation should happen via the next four macros.
Packit 6c4009
Packit 6c4009
@deftypefn Macro void CPU_ZERO (cpu_set_t *@var{set})
Packit 6c4009
@standards{GNU, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c CPU_ZERO ok
Packit 6c4009
@c  __CPU_ZERO_S ok
Packit 6c4009
@c   memset dup ok
Packit 6c4009
This macro initializes the CPU set @var{set} to be the empty set.
Packit 6c4009
Packit 6c4009
This macro is a GNU extension and is defined in @file{sched.h}.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro void CPU_SET (int @var{cpu}, cpu_set_t *@var{set})
Packit 6c4009
@standards{GNU, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c CPU_SET ok
Packit 6c4009
@c  __CPU_SET_S ok
Packit 6c4009
@c   __CPUELT ok
Packit 6c4009
@c   __CPUMASK ok
Packit 6c4009
This macro adds @var{cpu} to the CPU set @var{set}.
Packit 6c4009
Packit 6c4009
The @var{cpu} parameter must not have side effects since it is
Packit 6c4009
evaluated more than once.
Packit 6c4009
Packit 6c4009
This macro is a GNU extension and is defined in @file{sched.h}.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro void CPU_CLR (int @var{cpu}, cpu_set_t *@var{set})
Packit 6c4009
@standards{GNU, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c CPU_CLR ok
Packit 6c4009
@c  __CPU_CLR_S ok
Packit 6c4009
@c   __CPUELT dup ok
Packit 6c4009
@c   __CPUMASK dup ok
Packit 6c4009
This macro removes @var{cpu} from the CPU set @var{set}.
Packit 6c4009
Packit 6c4009
The @var{cpu} parameter must not have side effects since it is
Packit 6c4009
evaluated more than once.
Packit 6c4009
Packit 6c4009
This macro is a GNU extension and is defined in @file{sched.h}.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro int CPU_ISSET (int @var{cpu}, const cpu_set_t *@var{set})
Packit 6c4009
@standards{GNU, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c CPU_ISSET ok
Packit 6c4009
@c  __CPU_ISSET_S ok
Packit 6c4009
@c   __CPUELT dup ok
Packit 6c4009
@c   __CPUMASK dup ok
Packit 6c4009
This macro returns a nonzero value (true) if @var{cpu} is a member
Packit 6c4009
of the CPU set @var{set}, and zero (false) otherwise.
Packit 6c4009
Packit 6c4009
The @var{cpu} parameter must not have side effects since it is
Packit 6c4009
evaluated more than once.
Packit 6c4009
Packit 6c4009
This macro is a GNU extension and is defined in @file{sched.h}.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
Packit 6c4009
CPU bitsets can be constructed from scratch or the currently installed
Packit 6c4009
affinity mask can be retrieved from the system.
Packit 6c4009
Packit 6c4009
@deftypefun int sched_getaffinity (pid_t @var{pid}, size_t @var{cpusetsize}, cpu_set_t *@var{cpuset})
Packit 6c4009
@standards{GNU, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Wrapped syscall to zero out past the kernel cpu set size; Linux
Packit 6c4009
@c only.
Packit 6c4009
Packit 6c4009
This function stores the CPU affinity mask for the process or thread
Packit 6c4009
with the ID @var{pid} in the @var{cpusetsize} bytes long bitmap
Packit 6c4009
pointed to by @var{cpuset}.  If successful, the function always
Packit 6c4009
initializes all bits in the @code{cpu_set_t} object and returns zero.
Packit 6c4009
Packit 6c4009
If @var{pid} does not correspond to a process or thread on the system
Packit 6c4009
the or the function fails for some other reason, it returns @code{-1}
Packit 6c4009
and @code{errno} is set to represent the error condition.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item ESRCH
Packit 6c4009
No process or thread with the given ID found.
Packit 6c4009
Packit 6c4009
@item EFAULT
Packit 6c4009
The pointer @var{cpuset} does not point to a valid object.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
This function is a GNU extension and is declared in @file{sched.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Note that it is not portably possible to use this information to
Packit 6c4009
retrieve the information for different POSIX threads.  A separate
Packit 6c4009
interface must be provided for that.
Packit 6c4009
Packit 6c4009
@deftypefun int sched_setaffinity (pid_t @var{pid}, size_t @var{cpusetsize}, const cpu_set_t *@var{cpuset})
Packit 6c4009
@standards{GNU, sched.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Wrapped syscall to detect attempts to set bits past the kernel cpu
Packit 6c4009
@c set size; Linux only.
Packit 6c4009
Packit 6c4009
This function installs the @var{cpusetsize} bytes long affinity mask
Packit 6c4009
pointed to by @var{cpuset} for the process or thread with the ID @var{pid}.
Packit 6c4009
If successful the function returns zero and the scheduler will in the future
Packit 6c4009
take the affinity information into account.
Packit 6c4009
Packit 6c4009
If the function fails it will return @code{-1} and @code{errno} is set
Packit 6c4009
to the error code:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item ESRCH
Packit 6c4009
No process or thread with the given ID found.
Packit 6c4009
Packit 6c4009
@item EFAULT
Packit 6c4009
The pointer @var{cpuset} does not point to a valid object.
Packit 6c4009
Packit 6c4009
@item EINVAL
Packit 6c4009
The bitset is not valid.  This might mean that the affinity set might
Packit 6c4009
not leave a processor for the process or thread to run on.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
This function is a GNU extension and is declared in @file{sched.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Memory Resources
Packit 6c4009
@section Querying memory available resources
Packit 6c4009
Packit 6c4009
The amount of memory available in the system and the way it is organized
Packit 6c4009
determines oftentimes the way programs can and have to work.  For
Packit 6c4009
functions like @code{mmap} it is necessary to know about the size of
Packit 6c4009
individual memory pages and knowing how much memory is available enables
Packit 6c4009
a program to select appropriate sizes for, say, caches.  Before we get
Packit 6c4009
into these details a few words about memory subsystems in traditional
Packit 6c4009
Unix systems will be given.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Memory Subsystem::           Overview about traditional Unix memory handling.
Packit 6c4009
* Query Memory Parameters::    How to get information about the memory
Packit 6c4009
                                subsystem?
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Memory Subsystem
Packit 6c4009
@subsection Overview about traditional Unix memory handling
Packit 6c4009
Packit 6c4009
@cindex address space
Packit 6c4009
@cindex physical memory
Packit 6c4009
@cindex physical address
Packit 6c4009
Unix systems normally provide processes virtual address spaces.  This
Packit 6c4009
means that the addresses of the memory regions do not have to correspond
Packit 6c4009
directly to the addresses of the actual physical memory which stores the
Packit 6c4009
data.  An extra level of indirection is introduced which translates
Packit 6c4009
virtual addresses into physical addresses.  This is normally done by the
Packit 6c4009
hardware of the processor.
Packit 6c4009
Packit 6c4009
@cindex shared memory
Packit 6c4009
Using a virtual address space has several advantages.  The most important
Packit 6c4009
is process isolation.  The different processes running on the system
Packit 6c4009
cannot interfere directly with each other.  No process can write into
Packit 6c4009
the address space of another process (except when shared memory is used
Packit 6c4009
but then it is wanted and controlled).
Packit 6c4009
Packit 6c4009
Another advantage of virtual memory is that the address space the
Packit 6c4009
processes see can actually be larger than the physical memory available.
Packit 6c4009
The physical memory can be extended by storage on an external media
Packit 6c4009
where the content of currently unused memory regions is stored.  The
Packit 6c4009
address translation can then intercept accesses to these memory regions
Packit 6c4009
and make memory content available again by loading the data back into
Packit 6c4009
memory.  This concept makes it necessary that programs which have to use
Packit 6c4009
lots of memory know the difference between available virtual address
Packit 6c4009
space and available physical memory.  If the working set of virtual
Packit 6c4009
memory of all the processes is larger than the available physical memory
Packit 6c4009
the system will slow down dramatically due to constant swapping of
Packit 6c4009
memory content from the memory to the storage media and back.  This is
Packit 6c4009
called ``thrashing''.
Packit 6c4009
@cindex thrashing
Packit 6c4009
Packit 6c4009
@cindex memory page
Packit 6c4009
@cindex page, memory
Packit 6c4009
A final aspect of virtual memory which is important and follows from
Packit 6c4009
what is said in the last paragraph is the granularity of the virtual
Packit 6c4009
address space handling.  When we said that the virtual address handling
Packit 6c4009
stores memory content externally it cannot do this on a byte-by-byte
Packit 6c4009
basis.  The administrative overhead does not allow this (leaving alone
Packit 6c4009
the processor hardware).  Instead several thousand bytes are handled
Packit 6c4009
together and form a @dfn{page}.  The size of each page is always a power
Packit 6c4009
of two bytes.  The smallest page size in use today is 4096, with 8192,
Packit 6c4009
16384, and 65536 being other popular sizes.
Packit 6c4009
Packit 6c4009
@node Query Memory Parameters
Packit 6c4009
@subsection How to get information about the memory subsystem?
Packit 6c4009
Packit 6c4009
The page size of the virtual memory the process sees is essential to
Packit 6c4009
know in several situations.  Some programming interfaces (e.g.,
Packit 6c4009
@code{mmap}, @pxref{Memory-mapped I/O}) require the user to provide
Packit 6c4009
information adjusted to the page size.  In the case of @code{mmap} it is
Packit 6c4009
necessary to provide a length argument which is a multiple of the page
Packit 6c4009
size.  Another place where the knowledge about the page size is useful
Packit 6c4009
is in memory allocation.  If one allocates pieces of memory in larger
Packit 6c4009
chunks which are then subdivided by the application code it is useful to
Packit 6c4009
adjust the size of the larger blocks to the page size.  If the total
Packit 6c4009
memory requirement for the block is close (but not larger) to a multiple
Packit 6c4009
of the page size the kernel's memory handling can work more effectively
Packit 6c4009
since it only has to allocate memory pages which are fully used.  (To do
Packit 6c4009
this optimization it is necessary to know a bit about the memory
Packit 6c4009
allocator which will require a bit of memory itself for each block and
Packit 6c4009
this overhead must not push the total size over the page size multiple.)
Packit 6c4009
Packit 6c4009
The page size traditionally was a compile time constant.  But recent
Packit 6c4009
development of processors changed this.  Processors now support
Packit 6c4009
different page sizes and they can possibly even vary among different
Packit 6c4009
processes on the same system.  Therefore the system should be queried at
Packit 6c4009
runtime about the current page size and no assumptions (except about it
Packit 6c4009
being a power of two) should be made.
Packit 6c4009
Packit 6c4009
@vindex _SC_PAGESIZE
Packit 6c4009
The correct interface to query about the page size is @code{sysconf}
Packit 6c4009
(@pxref{Sysconf Definition}) with the parameter @code{_SC_PAGESIZE}.
Packit 6c4009
There is a much older interface available, too.
Packit 6c4009
Packit 6c4009
@deftypefun int getpagesize (void)
Packit 6c4009
@standards{BSD, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Obtained from the aux vec at program startup time.  GNU/Linux/m68k is
Packit 6c4009
@c the exception, with the possibility of a syscall.
Packit 6c4009
The @code{getpagesize} function returns the page size of the process.
Packit 6c4009
This value is fixed for the runtime of the process but can vary in
Packit 6c4009
different runs of the application.
Packit 6c4009
Packit 6c4009
The function is declared in @file{unistd.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Widely available on @w{System V} derived systems is a method to get
Packit 6c4009
information about the physical memory the system has.  The call
Packit 6c4009
Packit 6c4009
@vindex _SC_PHYS_PAGES
Packit 6c4009
@cindex sysconf
Packit 6c4009
@smallexample
Packit 6c4009
  sysconf (_SC_PHYS_PAGES)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
returns the total number of pages of physical memory the system has.
Packit 6c4009
This does not mean all this memory is available.  This information can
Packit 6c4009
be found using
Packit 6c4009
Packit 6c4009
@vindex _SC_AVPHYS_PAGES
Packit 6c4009
@cindex sysconf
Packit 6c4009
@smallexample
Packit 6c4009
  sysconf (_SC_AVPHYS_PAGES)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
These two values help to optimize applications.  The value returned for
Packit 6c4009
@code{_SC_AVPHYS_PAGES} is the amount of memory the application can use
Packit 6c4009
without hindering any other process (given that no other process
Packit 6c4009
increases its memory usage).  The value returned for
Packit 6c4009
@code{_SC_PHYS_PAGES} is more or less a hard limit for the working set.
Packit 6c4009
If all applications together constantly use more than that amount of
Packit 6c4009
memory the system is in trouble.
Packit 6c4009
Packit 6c4009
@Theglibc{} provides in addition to these already described way to
Packit 6c4009
get this information two functions.  They are declared in the file
Packit 6c4009
@file{sys/sysinfo.h}.  Programmers should prefer to use the
Packit 6c4009
@code{sysconf} method described above.
Packit 6c4009
Packit 6c4009
@deftypefun {long int} get_phys_pages (void)
Packit 6c4009
@standards{GNU, sys/sysinfo.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
@c This fopens a /proc file and scans it for the requested information.
Packit 6c4009
The @code{get_phys_pages} function returns the total number of pages of
Packit 6c4009
physical memory the system has.  To get the amount of memory this number has to
Packit 6c4009
be multiplied by the page size.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {long int} get_avphys_pages (void)
Packit 6c4009
@standards{GNU, sys/sysinfo.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
The @code{get_avphys_pages} function returns the number of available pages of
Packit 6c4009
physical memory the system has.  To get the amount of memory this number has to
Packit 6c4009
be multiplied by the page size.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Processor Resources
Packit 6c4009
@section Learn about the processors available
Packit 6c4009
Packit 6c4009
The use of threads or processes with shared memory allows an application
Packit 6c4009
to take advantage of all the processing power a system can provide.  If
Packit 6c4009
the task can be parallelized the optimal way to write an application is
Packit 6c4009
to have at any time as many processes running as there are processors.
Packit 6c4009
To determine the number of processors available to the system one can
Packit 6c4009
run
Packit 6c4009
Packit 6c4009
@vindex _SC_NPROCESSORS_CONF
Packit 6c4009
@cindex sysconf
Packit 6c4009
@smallexample
Packit 6c4009
  sysconf (_SC_NPROCESSORS_CONF)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
which returns the number of processors the operating system configured.
Packit 6c4009
But it might be possible for the operating system to disable individual
Packit 6c4009
processors and so the call
Packit 6c4009
Packit 6c4009
@vindex _SC_NPROCESSORS_ONLN
Packit 6c4009
@cindex sysconf
Packit 6c4009
@smallexample
Packit 6c4009
  sysconf (_SC_NPROCESSORS_ONLN)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
returns the number of processors which are currently online (i.e.,
Packit 6c4009
available).
Packit 6c4009
Packit 6c4009
For these two pieces of information @theglibc{} also provides
Packit 6c4009
functions to get the information directly.  The functions are declared
Packit 6c4009
in @file{sys/sysinfo.h}.
Packit 6c4009
Packit 6c4009
@deftypefun int get_nprocs_conf (void)
Packit 6c4009
@standards{GNU, sys/sysinfo.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
@c This function reads from from /sys using dir streams (single user, so
Packit 6c4009
@c no @mtasurace issue), and on some arches, from /proc using streams.
Packit 6c4009
The @code{get_nprocs_conf} function returns the number of processors the
Packit 6c4009
operating system configured.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int get_nprocs (void)
Packit 6c4009
@standards{GNU, sys/sysinfo.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
Packit 6c4009
@c This function reads from /proc using file descriptor I/O.
Packit 6c4009
The @code{get_nprocs} function returns the number of available processors.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex load average
Packit 6c4009
Before starting more threads it should be checked whether the processors
Packit 6c4009
are not already overused.  Unix systems calculate something called the
Packit 6c4009
@dfn{load average}.  This is a number indicating how many processes were
Packit 6c4009
running.  This number is an average over different periods of time
Packit 6c4009
(normally 1, 5, and 15 minutes).
Packit 6c4009
Packit 6c4009
@deftypefun int getloadavg (double @var{loadavg}[], int @var{nelem})
Packit 6c4009
@standards{BSD, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
Packit 6c4009
@c Calls host_info on HURD; on Linux, opens /proc/loadavg, reads from
Packit 6c4009
@c it, closes it, without cancellation point, and calls strtod_l with
Packit 6c4009
@c the C locale to convert the strings to doubles.
Packit 6c4009
This function gets the 1, 5 and 15 minute load averages of the
Packit 6c4009
system.  The values are placed in @var{loadavg}.  @code{getloadavg} will
Packit 6c4009
place at most @var{nelem} elements into the array but never more than
Packit 6c4009
three elements.  The return value is the number of elements written to
Packit 6c4009
@var{loadavg}, or -1 on error.
Packit 6c4009
Packit 6c4009
This function is declared in @file{stdlib.h}.
Packit 6c4009
@end deftypefun