Saved and restored logging._handlerList at the same time as saving/restoring logging...
[python.git] / Doc / lib / libresource.tex
blob8e102b8eec5f63457d7777b0adeb87a2709e53e3
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}{jeremy@alum.mit.edu}
9 \sectionauthor{Jeremy Hylton}{jeremy@alum.mit.edu}
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 underlying 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 underlying
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. This module does not attempt to
71 mask platform differences --- symbols not defined for a platform will
72 not be available from this module on that platform.
74 \begin{datadesc}{RLIMIT_CORE}
75 The maximum size (in bytes) of a core file that the current process
76 can create. This may result in the creation of a partial core file
77 if a larger core would be required to contain the entire process
78 image.
79 \end{datadesc}
81 \begin{datadesc}{RLIMIT_CPU}
82 The maximum amount of processor time (in seconds) that a process can
83 use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to
84 the process. (See the \refmodule{signal} module documentation for
85 information about how to catch this signal and do something useful,
86 e.g. flush open files to disk.)
87 \end{datadesc}
89 \begin{datadesc}{RLIMIT_FSIZE}
90 The maximum size of a file which the process may create. This only
91 affects the stack of the main thread in a multi-threaded process.
92 \end{datadesc}
94 \begin{datadesc}{RLIMIT_DATA}
95 The maximum size (in bytes) of the process's heap.
96 \end{datadesc}
98 \begin{datadesc}{RLIMIT_STACK}
99 The maximum size (in bytes) of the call stack for the current
100 process.
101 \end{datadesc}
103 \begin{datadesc}{RLIMIT_RSS}
104 The maximum resident set size that should be made available to the
105 process.
106 \end{datadesc}
108 \begin{datadesc}{RLIMIT_NPROC}
109 The maximum number of processes the current process may create.
110 \end{datadesc}
112 \begin{datadesc}{RLIMIT_NOFILE}
113 The maximum number of open file descriptors for the current
114 process.
115 \end{datadesc}
117 \begin{datadesc}{RLIMIT_OFILE}
118 The BSD name for \constant{RLIMIT_NOFILE}.
119 \end{datadesc}
121 \begin{datadesc}{RLIMIT_MEMLOCK}
122 The maximum address space which may be locked in memory.
123 \end{datadesc}
125 \begin{datadesc}{RLIMIT_VMEM}
126 The largest area of mapped memory which the process may occupy.
127 \end{datadesc}
129 \begin{datadesc}{RLIMIT_AS}
130 The maximum area (in bytes) of address space which may be taken by
131 the process.
132 \end{datadesc}
134 \subsection{Resource Usage}
136 These functions are used to retrieve resource usage information:
138 \begin{funcdesc}{getrusage}{who}
139 This function returns an object that describes the resources
140 consumed by either the current process or its children, as specified
141 by the \var{who} parameter. The \var{who} parameter should be
142 specified using one of the \constant{RUSAGE_*} constants described
143 below.
145 The fields of the return value each describe how a particular system
146 resource has been used, e.g. amount of time spent running is user mode
147 or number of times the process was swapped out of main memory. Some
148 values are dependent on the clock tick internal, e.g. the amount of
149 memory the process is using.
151 For backward compatibility, the return value is also accessible as
152 a tuple of 16 elements.
154 The fields \member{ru_utime} and \member{ru_stime} of the return value
155 are floating point values representing the amount of time spent
156 executing in user mode and the amount of time spent executing in system
157 mode, respectively. The remaining values are integers. Consult the
158 \manpage{getrusage}{2} man page for detailed information about these
159 values. A brief summary is presented here:
161 \begin{tableiii}{r|l|l}{code}{Index}{Field}{Resource}
162 \lineiii{0}{\member{ru_utime}}{time in user mode (float)}
163 \lineiii{1}{\member{ru_stime}}{time in system mode (float)}
164 \lineiii{2}{\member{ru_maxrss}}{maximum resident set size}
165 \lineiii{3}{\member{ru_ixrss}}{shared memory size}
166 \lineiii{4}{\member{ru_idrss}}{unshared memory size}
167 \lineiii{5}{\member{ru_isrss}}{unshared stack size}
168 \lineiii{6}{\member{ru_minflt}}{page faults not requiring I/O}
169 \lineiii{7}{\member{ru_majflt}}{page faults requiring I/O}
170 \lineiii{8}{\member{ru_nswap}}{number of swap outs}
171 \lineiii{9}{\member{ru_inblock}}{block input operations}
172 \lineiii{10}{\member{ru_oublock}}{block output operations}
173 \lineiii{11}{\member{ru_msgsnd}}{messages sent}
174 \lineiii{12}{\member{ru_msgrcv}}{messages received}
175 \lineiii{13}{\member{ru_nsignals}}{signals received}
176 \lineiii{14}{\member{ru_nvcsw}}{voluntary context switches}
177 \lineiii{15}{\member{ru_nivcsw}}{involuntary context switches}
178 \end{tableiii}
180 This function will raise a \exception{ValueError} if an invalid
181 \var{who} parameter is specified. It may also raise
182 \exception{error} exception in unusual circumstances.
184 \versionchanged[Added access to values as attributes of the
185 returned object]{2.3}
186 \end{funcdesc}
188 \begin{funcdesc}{getpagesize}{}
189 Returns the number of bytes in a system page. (This need not be the
190 same as the hardware page size.) This function is useful for
191 determining the number of bytes of memory a process is using. The
192 third element of the tuple returned by \function{getrusage()} describes
193 memory usage in pages; multiplying by page size produces number of
194 bytes.
195 \end{funcdesc}
197 The following \constant{RUSAGE_*} symbols are passed to the
198 \function{getrusage()} function to specify which processes information
199 should be provided for.
201 \begin{datadesc}{RUSAGE_SELF}
202 \constant{RUSAGE_SELF} should be used to
203 request information pertaining only to the process itself.
204 \end{datadesc}
206 \begin{datadesc}{RUSAGE_CHILDREN}
207 Pass to \function{getrusage()} to request resource information for
208 child processes of the calling process.
209 \end{datadesc}
211 \begin{datadesc}{RUSAGE_BOTH}
212 Pass to \function{getrusage()} to request resources consumed by both
213 the current process and child processes. May not be available on all
214 systems.
215 \end{datadesc}