blob: 33de531947e86cef8ce517aa404ff5a6060a7d36 [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.}
Fred Drakefcc16332001-10-04 20:40:07 +00008\moduleauthor{Jeremy Hylton}{jeremy@alum.mit.edu}
9\sectionauthor{Jeremy Hylton}{jeremy@alum.mit.edu}
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
Fred Drake5d9a6b52001-10-22 14:18:23 +000070value to denote the same resource. This module does not attempt to
71mask platform differences --- symbols not defined for a platform will
72not be available from this module on that platform.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000073
Fred Drakee9072081997-12-06 07:25:41 +000074\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.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000079\end{datadesc}
80
Fred Drakee9072081997-12-06 07:25:41 +000081\begin{datadesc}{RLIMIT_CPU}
Fred Drake8ee679f2001-07-14 02:50:55 +000082 The maximum amount of processor time (in seconds) that a process can
Fred Drake60ba4471998-03-11 06:18:15 +000083 use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to
Fred Drakef6863c11999-03-02 16:37:17 +000084 the process. (See the \refmodule{signal} module documentation for
Fred Drakee9072081997-12-06 07:25:41 +000085 information about how to catch this signal and do something useful,
86 e.g. flush open files to disk.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000087\end{datadesc}
88
Fred Drakee9072081997-12-06 07:25:41 +000089\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.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000092\end{datadesc}
93
Fred Drakee9072081997-12-06 07:25:41 +000094\begin{datadesc}{RLIMIT_DATA}
95 The maximum size (in bytes) of the process's heap.
96\end{datadesc}
97
98\begin{datadesc}{RLIMIT_STACK}
99 The maximum size (in bytes) of the call stack for the current
100 process.
101\end{datadesc}
102
103\begin{datadesc}{RLIMIT_RSS}
104 The maximum resident set size that should be made available to the
105 process.
106\end{datadesc}
107
108\begin{datadesc}{RLIMIT_NPROC}
109 The maximum number of processes the current process may create.
110\end{datadesc}
111
112\begin{datadesc}{RLIMIT_NOFILE}
113 The maximum number of open file descriptors for the current
114 process.
115\end{datadesc}
116
117\begin{datadesc}{RLIMIT_OFILE}
Fred Drake60ba4471998-03-11 06:18:15 +0000118 The BSD name for \constant{RLIMIT_NOFILE}.
Fred Drakee9072081997-12-06 07:25:41 +0000119\end{datadesc}
120
121\begin{datadesc}{RLIMIT_MEMLOC}
122 The maximm address space which may be locked in memory.
123\end{datadesc}
124
125\begin{datadesc}{RLIMIT_VMEM}
126 The largest area of mapped memory which the process may occupy.
127\end{datadesc}
128
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}
133
134\subsection{Resource Usage}
135
136These functiona are used to retrieve resource usage information:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000137
138\begin{funcdesc}{getrusage}{who}
139 This function returns a large tuple that describes the resources
140 consumed by either the current process or its children, as specified
Fred Drakee9072081997-12-06 07:25:41 +0000141 by the \var{who} parameter. The \var{who} parameter should be
Fred Drakef6863c11999-03-02 16:37:17 +0000142 specified using one of the \constant{RUSAGE_*} constants described
Fred Drakee9072081997-12-06 07:25:41 +0000143 below.
144
145 The elements of the return value each
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000146 describe how a particular system resource has been used, e.g. amount
147 of time spent running is user mode or number of times the process was
148 swapped out of main memory. Some values are dependent on the clock
149 tick internal, e.g. the amount of memory the process is using.
150
151 The first two elements of the return value are floating point values
152 representing the amount of time spent executing in user mode and the
153 amount of time spent executing in system mode, respectively. The
Fred Drake60ba4471998-03-11 06:18:15 +0000154 remaining values are integers. Consult the \manpage{getrusage}{2}
155 man page for detailed information about these values. A brief
156 summary is presented here:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000157
Fred Drakeee601911998-04-11 20:53:03 +0000158\begin{tableii}{r|l}{code}{Offset}{Resource}
Fred Drakec4164451997-12-29 19:02:01 +0000159 \lineii{0}{time in user mode (float)}
160 \lineii{1}{time in system mode (float)}
161 \lineii{2}{maximum resident set size}
162 \lineii{3}{shared memory size}
163 \lineii{4}{unshared memory size}
164 \lineii{5}{unshared stack size}
165 \lineii{6}{page faults not requiring I/O}
166 \lineii{7}{page faults requiring I/O}
167 \lineii{8}{number of swap outs}
168 \lineii{9}{block input operations}
169 \lineii{10}{block output operations}
170 \lineii{11}{messages sent}
171 \lineii{12}{messages received}
172 \lineii{13}{signals received}
173 \lineii{14}{voluntary context switches}
174 \lineii{15}{involuntary context switches}
175\end{tableii}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000176
Fred Drake60ba4471998-03-11 06:18:15 +0000177 This function will raise a \exception{ValueError} if an invalid
178 \var{who} parameter is specified. It may also raise
179 \exception{error} exception in unusual circumstances.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000180\end{funcdesc}
181
182\begin{funcdesc}{getpagesize}{}
183 Returns the number of bytes in a system page. (This need not be the
184 same as the hardware page size.) This function is useful for
185 determining the number of bytes of memory a process is using. The
Fred Drake60ba4471998-03-11 06:18:15 +0000186 third element of the tuple returned by \function{getrusage()} describes
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000187 memory usage in pages; multiplying by page size produces number of
188 bytes.
189\end{funcdesc}
190
Fred Drakef6863c11999-03-02 16:37:17 +0000191The following \constant{RUSAGE_*} symbols are passed to the
Fred Drake60ba4471998-03-11 06:18:15 +0000192\function{getrusage()} function to specify which processes information
Fred Drakee9072081997-12-06 07:25:41 +0000193should be provided for.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000194
Fred Drakee9072081997-12-06 07:25:41 +0000195\begin{datadesc}{RUSAGE_SELF}
Fred Drake60ba4471998-03-11 06:18:15 +0000196 \constant{RUSAGE_SELF} should be used to
Fred Drakee9072081997-12-06 07:25:41 +0000197 request information pertaining only to the process itself.
198\end{datadesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000199
Fred Drakee9072081997-12-06 07:25:41 +0000200\begin{datadesc}{RUSAGE_CHILDREN}
Fred Drake60ba4471998-03-11 06:18:15 +0000201 Pass to \function{getrusage()} to request resource information for
202 child processes of the calling process.
Fred Drakee9072081997-12-06 07:25:41 +0000203\end{datadesc}
204
205\begin{datadesc}{RUSAGE_BOTH}
Fred Drake60ba4471998-03-11 06:18:15 +0000206 Pass to \function{getrusage()} to request resources consumed by both
207 the current process and child processes. May not be available on all
Fred Drakee9072081997-12-06 07:25:41 +0000208 systems.
209\end{datadesc}