blob: b383895113aa982fc76ed34c3093f5718b4be47b [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{resource} ---
Fred Drakef6863c11999-03-02 16:37:17 +00002 Resource usage information}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{builtin}{resource}
Fred Drakea54a8871999-03-02 17:03:42 +00005 \platform{Unix}
Fred Drakef6863c11999-03-02 16:37:17 +00006\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}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000010
Fred Drakeb91e9341998-07-23 17:59:49 +000011
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000012This module provides basic mechanisms for measuring and controlling
13system resources utilized by a program.
14
15Symbolic constants are used to specify particular system resources and
16to request usage information about either the current process or its
Fred Drakee9072081997-12-06 07:25:41 +000017children.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000018
Fred Drakee9072081997-12-06 07:25:41 +000019A single exception is defined for errors:
20
Fred Drakee9072081997-12-06 07:25:41 +000021
22\begin{excdesc}{error}
23 The functions described below may raise this error if the underlying
24 system call failures unexpectedly.
25\end{excdesc}
26
27\subsection{Resource Limits}
28
Fred Drake60ba4471998-03-11 06:18:15 +000029Resources usage can be limited using the \function{setrlimit()} function
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000030described below. Each resource is controlled by a pair of limits: a
31soft limit and a hard limit. The soft limit is the current limit, and
32may be lowered or raised by a process over time. The soft limit can
33never exceed the hard limit. The hard limit can be lowered to any
Fred Drakee9072081997-12-06 07:25:41 +000034value greater than the soft limit, but not raised. (Only processes with
35the effective UID of the super-user can raise a hard limit.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000036
37The specific resources that can be limited are system dependent. They
Fred Drake60ba4471998-03-11 06:18:15 +000038are described in the \manpage{getrlimit}{2} man page. The resources
Fred Drakee9072081997-12-06 07:25:41 +000039listed below are supported when the underlying operating system
40supports them; resources which cannot be checked or controlled by the
41operating system are not defined in this module for those platforms.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000042
Fred Drakee9072081997-12-06 07:25:41 +000043\begin{funcdesc}{getrlimit}{resource}
44 Returns a tuple \code{(\var{soft}, \var{hard})} with the current
Fred Drake60ba4471998-03-11 06:18:15 +000045 soft and hard limits of \var{resource}. Raises \exception{ValueError} if
46 an invalid resource is specified, or \exception{error} if the
Fred Drakee9072081997-12-06 07:25:41 +000047 underyling system call fails unexpectedly.
48\end{funcdesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000049
Fred Drakee9072081997-12-06 07:25:41 +000050\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.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000055
Fred Drake60ba4471998-03-11 06:18:15 +000056 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.
Fred Drakee9072081997-12-06 07:25:41 +000061\end{funcdesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000062
Fred Drakee9072081997-12-06 07:25:41 +000063These symbols define resources whose consumption can be controlled
Fred Drake60ba4471998-03-11 06:18:15 +000064using the \function{setrlimit()} and \function{getrlimit()} functions
65described below. The values of these symbols are exactly the constants
66used by \C{} programs.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000067
Fred Drake60ba4471998-03-11 06:18:15 +000068The \UNIX{} man page for \manpage{getrlimit}{2} lists the available
Fred Drakee9072081997-12-06 07:25:41 +000069resources. Note that not all systems use the same symbol or same
70value to denote the same resource.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000071
Fred Drakee9072081997-12-06 07:25:41 +000072\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.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000077\end{datadesc}
78
Fred Drakee9072081997-12-06 07:25:41 +000079\begin{datadesc}{RLIMIT_CPU}
Fred Drake8ee679f2001-07-14 02:50:55 +000080 The maximum amount of processor time (in seconds) that a process can
Fred Drake60ba4471998-03-11 06:18:15 +000081 use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to
Fred Drakef6863c11999-03-02 16:37:17 +000082 the process. (See the \refmodule{signal} module documentation for
Fred Drakee9072081997-12-06 07:25:41 +000083 information about how to catch this signal and do something useful,
84 e.g. flush open files to disk.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000085\end{datadesc}
86
Fred Drakee9072081997-12-06 07:25:41 +000087\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.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000090\end{datadesc}
91
Fred Drakee9072081997-12-06 07:25:41 +000092\begin{datadesc}{RLIMIT_DATA}
93 The maximum size (in bytes) of the process's heap.
94\end{datadesc}
95
96\begin{datadesc}{RLIMIT_STACK}
97 The maximum size (in bytes) of the call stack for the current
98 process.
99\end{datadesc}
100
101\begin{datadesc}{RLIMIT_RSS}
102 The maximum resident set size that should be made available to the
103 process.
104\end{datadesc}
105
106\begin{datadesc}{RLIMIT_NPROC}
107 The maximum number of processes the current process may create.
108\end{datadesc}
109
110\begin{datadesc}{RLIMIT_NOFILE}
111 The maximum number of open file descriptors for the current
112 process.
113\end{datadesc}
114
115\begin{datadesc}{RLIMIT_OFILE}
Fred Drake60ba4471998-03-11 06:18:15 +0000116 The BSD name for \constant{RLIMIT_NOFILE}.
Fred Drakee9072081997-12-06 07:25:41 +0000117\end{datadesc}
118
119\begin{datadesc}{RLIMIT_MEMLOC}
120 The maximm address space which may be locked in memory.
121\end{datadesc}
122
123\begin{datadesc}{RLIMIT_VMEM}
124 The largest area of mapped memory which the process may occupy.
125\end{datadesc}
126
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}
131
132\subsection{Resource Usage}
133
134These functiona are used to retrieve resource usage information:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000135
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
Fred Drakee9072081997-12-06 07:25:41 +0000139 by the \var{who} parameter. The \var{who} parameter should be
Fred Drakef6863c11999-03-02 16:37:17 +0000140 specified using one of the \constant{RUSAGE_*} constants described
Fred Drakee9072081997-12-06 07:25:41 +0000141 below.
142
143 The elements of the return value each
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000144 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.
148
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
Fred Drake60ba4471998-03-11 06:18:15 +0000152 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:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000155
Fred Drakeee601911998-04-11 20:53:03 +0000156\begin{tableii}{r|l}{code}{Offset}{Resource}
Fred Drakec4164451997-12-29 19:02:01 +0000157 \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}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000174
Fred Drake60ba4471998-03-11 06:18:15 +0000175 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.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000178\end{funcdesc}
179
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
Fred Drake60ba4471998-03-11 06:18:15 +0000184 third element of the tuple returned by \function{getrusage()} describes
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000185 memory usage in pages; multiplying by page size produces number of
186 bytes.
187\end{funcdesc}
188
Fred Drakef6863c11999-03-02 16:37:17 +0000189The following \constant{RUSAGE_*} symbols are passed to the
Fred Drake60ba4471998-03-11 06:18:15 +0000190\function{getrusage()} function to specify which processes information
Fred Drakee9072081997-12-06 07:25:41 +0000191should be provided for.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000192
Fred Drakee9072081997-12-06 07:25:41 +0000193\begin{datadesc}{RUSAGE_SELF}
Fred Drake60ba4471998-03-11 06:18:15 +0000194 \constant{RUSAGE_SELF} should be used to
Fred Drakee9072081997-12-06 07:25:41 +0000195 request information pertaining only to the process itself.
196\end{datadesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000197
Fred Drakee9072081997-12-06 07:25:41 +0000198\begin{datadesc}{RUSAGE_CHILDREN}
Fred Drake60ba4471998-03-11 06:18:15 +0000199 Pass to \function{getrusage()} to request resource information for
200 child processes of the calling process.
Fred Drakee9072081997-12-06 07:25:41 +0000201\end{datadesc}
202
203\begin{datadesc}{RUSAGE_BOTH}
Fred Drake60ba4471998-03-11 06:18:15 +0000204 Pass to \function{getrusage()} to request resources consumed by both
205 the current process and child processes. May not be available on all
Fred Drakee9072081997-12-06 07:25:41 +0000206 systems.
207\end{datadesc}