The 0.5 release happened on 2/15, not on 2/14. :-)
[python/dscho.git] / Doc / lib / libresource.tex
blob869bb17125f0b5f7e367d48cc27eca2076425da0
1 \section{\module{resource} ---
2 Resource usage information}
4 \declaremodule{builtin}{resource}
5 \platform{Unix}
6 \modulesynopsis{An interface to provide resource usage information on
7 the current process.}
8 \moduleauthor{Jeremy Hylton}{jhylton@cnri.reston.va.us}
9 \sectionauthor{Jeremy Hylton}{jhylton@cnri.reston.va.us}
12 This module provides basic mechanisms for measuring and controlling
13 system resources utilized by a program.
15 Symbolic constants are used to specify particular system resources and
16 to request usage information about either the current process or its
17 children.
19 A single exception is defined for errors:
22 \begin{excdesc}{error}
23 The functions described below may raise this error if the underlying
24 system call failures unexpectedly.
25 \end{excdesc}
27 \subsection{Resource Limits}
29 Resources usage can be limited using the \function{setrlimit()} function
30 described below. Each resource is controlled by a pair of limits: a
31 soft limit and a hard limit. The soft limit is the current limit, and
32 may be lowered or raised by a process over time. The soft limit can
33 never exceed the hard limit. The hard limit can be lowered to any
34 value greater than the soft limit, but not raised. (Only processes with
35 the effective UID of the super-user can raise a hard limit.)
37 The specific resources that can be limited are system dependent. They
38 are described in the \manpage{getrlimit}{2} man page. The resources
39 listed below are supported when the underlying operating system
40 supports them; resources which cannot be checked or controlled by the
41 operating system are not defined in this module for those platforms.
43 \begin{funcdesc}{getrlimit}{resource}
44 Returns a tuple \code{(\var{soft}, \var{hard})} with the current
45 soft and hard limits of \var{resource}. Raises \exception{ValueError} if
46 an invalid resource is specified, or \exception{error} if the
47 underyling system call fails unexpectedly.
48 \end{funcdesc}
50 \begin{funcdesc}{setrlimit}{resource, limits}
51 Sets new limits of consumption of \var{resource}. The \var{limits}
52 argument must be a tuple \code{(\var{soft}, \var{hard})} of two
53 integers describing the new limits. A value of \code{-1} can be used to
54 specify the maximum possible upper limit.
56 Raises \exception{ValueError} if an invalid resource is specified,
57 if the new soft limit exceeds the hard limit, or if a process tries
58 to raise its hard limit (unless the process has an effective UID of
59 super-user). Can also raise \exception{error} if the underyling
60 system call fails.
61 \end{funcdesc}
63 These symbols define resources whose consumption can be controlled
64 using the \function{setrlimit()} and \function{getrlimit()} functions
65 described below. The values of these symbols are exactly the constants
66 used by \C{} programs.
68 The \UNIX{} man page for \manpage{getrlimit}{2} lists the available
69 resources. Note that not all systems use the same symbol or same
70 value to denote the same resource.
72 \begin{datadesc}{RLIMIT_CORE}
73 The maximum size (in bytes) of a core file that the current process
74 can create. This may result in the creation of a partial core file
75 if a larger core would be required to contain the entire process
76 image.
77 \end{datadesc}
79 \begin{datadesc}{RLIMIT_CPU}
80 The maximum amount of CPU time (in seconds) that a process can
81 use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to
82 the process. (See the \refmodule{signal} module documentation for
83 information about how to catch this signal and do something useful,
84 e.g. flush open files to disk.)
85 \end{datadesc}
87 \begin{datadesc}{RLIMIT_FSIZE}
88 The maximum size of a file which the process may create. This only
89 affects the stack of the main thread in a multi-threaded process.
90 \end{datadesc}
92 \begin{datadesc}{RLIMIT_DATA}
93 The maximum size (in bytes) of the process's heap.
94 \end{datadesc}
96 \begin{datadesc}{RLIMIT_STACK}
97 The maximum size (in bytes) of the call stack for the current
98 process.
99 \end{datadesc}
101 \begin{datadesc}{RLIMIT_RSS}
102 The maximum resident set size that should be made available to the
103 process.
104 \end{datadesc}
106 \begin{datadesc}{RLIMIT_NPROC}
107 The maximum number of processes the current process may create.
108 \end{datadesc}
110 \begin{datadesc}{RLIMIT_NOFILE}
111 The maximum number of open file descriptors for the current
112 process.
113 \end{datadesc}
115 \begin{datadesc}{RLIMIT_OFILE}
116 The BSD name for \constant{RLIMIT_NOFILE}.
117 \end{datadesc}
119 \begin{datadesc}{RLIMIT_MEMLOC}
120 The maximm address space which may be locked in memory.
121 \end{datadesc}
123 \begin{datadesc}{RLIMIT_VMEM}
124 The largest area of mapped memory which the process may occupy.
125 \end{datadesc}
127 \begin{datadesc}{RLIMIT_AS}
128 The maximum area (in bytes) of address space which may be taken by
129 the process.
130 \end{datadesc}
132 \subsection{Resource Usage}
134 These functiona are used to retrieve resource usage information:
136 \begin{funcdesc}{getrusage}{who}
137 This function returns a large tuple that describes the resources
138 consumed by either the current process or its children, as specified
139 by the \var{who} parameter. The \var{who} parameter should be
140 specified using one of the \constant{RUSAGE_*} constants described
141 below.
143 The elements of the return value each
144 describe how a particular system resource has been used, e.g. amount
145 of time spent running is user mode or number of times the process was
146 swapped out of main memory. Some values are dependent on the clock
147 tick internal, e.g. the amount of memory the process is using.
149 The first two elements of the return value are floating point values
150 representing the amount of time spent executing in user mode and the
151 amount of time spent executing in system mode, respectively. The
152 remaining values are integers. Consult the \manpage{getrusage}{2}
153 man page for detailed information about these values. A brief
154 summary is presented here:
156 \begin{tableii}{r|l}{code}{Offset}{Resource}
157 \lineii{0}{time in user mode (float)}
158 \lineii{1}{time in system mode (float)}
159 \lineii{2}{maximum resident set size}
160 \lineii{3}{shared memory size}
161 \lineii{4}{unshared memory size}
162 \lineii{5}{unshared stack size}
163 \lineii{6}{page faults not requiring I/O}
164 \lineii{7}{page faults requiring I/O}
165 \lineii{8}{number of swap outs}
166 \lineii{9}{block input operations}
167 \lineii{10}{block output operations}
168 \lineii{11}{messages sent}
169 \lineii{12}{messages received}
170 \lineii{13}{signals received}
171 \lineii{14}{voluntary context switches}
172 \lineii{15}{involuntary context switches}
173 \end{tableii}
175 This function will raise a \exception{ValueError} if an invalid
176 \var{who} parameter is specified. It may also raise
177 \exception{error} exception in unusual circumstances.
178 \end{funcdesc}
180 \begin{funcdesc}{getpagesize}{}
181 Returns the number of bytes in a system page. (This need not be the
182 same as the hardware page size.) This function is useful for
183 determining the number of bytes of memory a process is using. The
184 third element of the tuple returned by \function{getrusage()} describes
185 memory usage in pages; multiplying by page size produces number of
186 bytes.
187 \end{funcdesc}
189 The following \constant{RUSAGE_*} symbols are passed to the
190 \function{getrusage()} function to specify which processes information
191 should be provided for.
193 \begin{datadesc}{RUSAGE_SELF}
194 \constant{RUSAGE_SELF} should be used to
195 request information pertaining only to the process itself.
196 \end{datadesc}
198 \begin{datadesc}{RUSAGE_CHILDREN}
199 Pass to \function{getrusage()} to request resource information for
200 child processes of the calling process.
201 \end{datadesc}
203 \begin{datadesc}{RUSAGE_BOTH}
204 Pass to \function{getrusage()} to request resources consumed by both
205 the current process and child processes. May not be available on all
206 systems.
207 \end{datadesc}