blob: 1d70e9e8c3d917adb30ba3afddfa00e2f71c7ed5 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{resource} ---
2 Resource usage information.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{resource}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +00004
Fred Drakeb91e9341998-07-23 17:59:49 +00005
6\modulesynopsis{An interface to provide resource usage information on the current
7process.}
8
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +00009This module provides basic mechanisms for measuring and controlling
10system resources utilized by a program.
11
12Symbolic constants are used to specify particular system resources and
13to request usage information about either the current process or its
Fred Drakee9072081997-12-06 07:25:41 +000014children.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000015
Fred Drakee9072081997-12-06 07:25:41 +000016A single exception is defined for errors:
17
Fred Drakee9072081997-12-06 07:25:41 +000018
19\begin{excdesc}{error}
20 The functions described below may raise this error if the underlying
21 system call failures unexpectedly.
22\end{excdesc}
23
24\subsection{Resource Limits}
25
Fred Drake60ba4471998-03-11 06:18:15 +000026Resources usage can be limited using the \function{setrlimit()} function
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000027described below. Each resource is controlled by a pair of limits: a
28soft limit and a hard limit. The soft limit is the current limit, and
29may be lowered or raised by a process over time. The soft limit can
30never exceed the hard limit. The hard limit can be lowered to any
Fred Drakee9072081997-12-06 07:25:41 +000031value greater than the soft limit, but not raised. (Only processes with
32the effective UID of the super-user can raise a hard limit.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000033
34The specific resources that can be limited are system dependent. They
Fred Drake60ba4471998-03-11 06:18:15 +000035are described in the \manpage{getrlimit}{2} man page. The resources
Fred Drakee9072081997-12-06 07:25:41 +000036listed below are supported when the underlying operating system
37supports them; resources which cannot be checked or controlled by the
38operating system are not defined in this module for those platforms.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000039
Fred Drakee9072081997-12-06 07:25:41 +000040\begin{funcdesc}{getrlimit}{resource}
41 Returns a tuple \code{(\var{soft}, \var{hard})} with the current
Fred Drake60ba4471998-03-11 06:18:15 +000042 soft and hard limits of \var{resource}. Raises \exception{ValueError} if
43 an invalid resource is specified, or \exception{error} if the
Fred Drakee9072081997-12-06 07:25:41 +000044 underyling system call fails unexpectedly.
45\end{funcdesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000046
Fred Drakee9072081997-12-06 07:25:41 +000047\begin{funcdesc}{setrlimit}{resource, limits}
48 Sets new limits of consumption of \var{resource}. The \var{limits}
49 argument must be a tuple \code{(\var{soft}, \var{hard})} of two
50 integers describing the new limits. A value of \code{-1} can be used to
51 specify the maximum possible upper limit.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000052
Fred Drake60ba4471998-03-11 06:18:15 +000053 Raises \exception{ValueError} if an invalid resource is specified,
54 if the new soft limit exceeds the hard limit, or if a process tries
55 to raise its hard limit (unless the process has an effective UID of
56 super-user). Can also raise \exception{error} if the underyling
57 system call fails.
Fred Drakee9072081997-12-06 07:25:41 +000058\end{funcdesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000059
Fred Drakee9072081997-12-06 07:25:41 +000060These symbols define resources whose consumption can be controlled
Fred Drake60ba4471998-03-11 06:18:15 +000061using the \function{setrlimit()} and \function{getrlimit()} functions
62described below. The values of these symbols are exactly the constants
63used by \C{} programs.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000064
Fred Drake60ba4471998-03-11 06:18:15 +000065The \UNIX{} man page for \manpage{getrlimit}{2} lists the available
Fred Drakee9072081997-12-06 07:25:41 +000066resources. Note that not all systems use the same symbol or same
67value to denote the same resource.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000068
Fred Drakee9072081997-12-06 07:25:41 +000069\begin{datadesc}{RLIMIT_CORE}
70 The maximum size (in bytes) of a core file that the current process
71 can create. This may result in the creation of a partial core file
72 if a larger core would be required to contain the entire process
73 image.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000074\end{datadesc}
75
Fred Drakee9072081997-12-06 07:25:41 +000076\begin{datadesc}{RLIMIT_CPU}
77 The maximum amount of CPU time (in seconds) that a process can
Fred Drake60ba4471998-03-11 06:18:15 +000078 use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to
79 the process. (See the \module{signal} module documentation for
Fred Drakee9072081997-12-06 07:25:41 +000080 information about how to catch this signal and do something useful,
81 e.g. flush open files to disk.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000082\end{datadesc}
83
Fred Drakee9072081997-12-06 07:25:41 +000084\begin{datadesc}{RLIMIT_FSIZE}
85 The maximum size of a file which the process may create. This only
86 affects the stack of the main thread in a multi-threaded process.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000087\end{datadesc}
88
Fred Drakee9072081997-12-06 07:25:41 +000089\begin{datadesc}{RLIMIT_DATA}
90 The maximum size (in bytes) of the process's heap.
91\end{datadesc}
92
93\begin{datadesc}{RLIMIT_STACK}
94 The maximum size (in bytes) of the call stack for the current
95 process.
96\end{datadesc}
97
98\begin{datadesc}{RLIMIT_RSS}
99 The maximum resident set size that should be made available to the
100 process.
101\end{datadesc}
102
103\begin{datadesc}{RLIMIT_NPROC}
104 The maximum number of processes the current process may create.
105\end{datadesc}
106
107\begin{datadesc}{RLIMIT_NOFILE}
108 The maximum number of open file descriptors for the current
109 process.
110\end{datadesc}
111
112\begin{datadesc}{RLIMIT_OFILE}
Fred Drake60ba4471998-03-11 06:18:15 +0000113 The BSD name for \constant{RLIMIT_NOFILE}.
Fred Drakee9072081997-12-06 07:25:41 +0000114\end{datadesc}
115
116\begin{datadesc}{RLIMIT_MEMLOC}
117 The maximm address space which may be locked in memory.
118\end{datadesc}
119
120\begin{datadesc}{RLIMIT_VMEM}
121 The largest area of mapped memory which the process may occupy.
122\end{datadesc}
123
124\begin{datadesc}{RLIMIT_AS}
125 The maximum area (in bytes) of address space which may be taken by
126 the process.
127\end{datadesc}
128
129\subsection{Resource Usage}
130
131These functiona are used to retrieve resource usage information:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000132
133\begin{funcdesc}{getrusage}{who}
134 This function returns a large tuple that describes the resources
135 consumed by either the current process or its children, as specified
Fred Drakee9072081997-12-06 07:25:41 +0000136 by the \var{who} parameter. The \var{who} parameter should be
Fred Drake60ba4471998-03-11 06:18:15 +0000137 specified using one of the \code{RUSAGE_*} constants described
Fred Drakee9072081997-12-06 07:25:41 +0000138 below.
139
140 The elements of the return value each
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000141 describe how a particular system resource has been used, e.g. amount
142 of time spent running is user mode or number of times the process was
143 swapped out of main memory. Some values are dependent on the clock
144 tick internal, e.g. the amount of memory the process is using.
145
146 The first two elements of the return value are floating point values
147 representing the amount of time spent executing in user mode and the
148 amount of time spent executing in system mode, respectively. The
Fred Drake60ba4471998-03-11 06:18:15 +0000149 remaining values are integers. Consult the \manpage{getrusage}{2}
150 man page for detailed information about these values. A brief
151 summary is presented here:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000152
Fred Drakeee601911998-04-11 20:53:03 +0000153\begin{tableii}{r|l}{code}{Offset}{Resource}
Fred Drakec4164451997-12-29 19:02:01 +0000154 \lineii{0}{time in user mode (float)}
155 \lineii{1}{time in system mode (float)}
156 \lineii{2}{maximum resident set size}
157 \lineii{3}{shared memory size}
158 \lineii{4}{unshared memory size}
159 \lineii{5}{unshared stack size}
160 \lineii{6}{page faults not requiring I/O}
161 \lineii{7}{page faults requiring I/O}
162 \lineii{8}{number of swap outs}
163 \lineii{9}{block input operations}
164 \lineii{10}{block output operations}
165 \lineii{11}{messages sent}
166 \lineii{12}{messages received}
167 \lineii{13}{signals received}
168 \lineii{14}{voluntary context switches}
169 \lineii{15}{involuntary context switches}
170\end{tableii}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000171
Fred Drake60ba4471998-03-11 06:18:15 +0000172 This function will raise a \exception{ValueError} if an invalid
173 \var{who} parameter is specified. It may also raise
174 \exception{error} exception in unusual circumstances.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000175\end{funcdesc}
176
177\begin{funcdesc}{getpagesize}{}
178 Returns the number of bytes in a system page. (This need not be the
179 same as the hardware page size.) This function is useful for
180 determining the number of bytes of memory a process is using. The
Fred Drake60ba4471998-03-11 06:18:15 +0000181 third element of the tuple returned by \function{getrusage()} describes
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000182 memory usage in pages; multiplying by page size produces number of
183 bytes.
184\end{funcdesc}
185
Fred Drake60ba4471998-03-11 06:18:15 +0000186The following \code{RUSAGE_*} symbols are passed to the
187\function{getrusage()} function to specify which processes information
Fred Drakee9072081997-12-06 07:25:41 +0000188should be provided for.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000189
Fred Drakee9072081997-12-06 07:25:41 +0000190\begin{datadesc}{RUSAGE_SELF}
Fred Drake60ba4471998-03-11 06:18:15 +0000191 \constant{RUSAGE_SELF} should be used to
Fred Drakee9072081997-12-06 07:25:41 +0000192 request information pertaining only to the process itself.
193\end{datadesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000194
Fred Drakee9072081997-12-06 07:25:41 +0000195\begin{datadesc}{RUSAGE_CHILDREN}
Fred Drake60ba4471998-03-11 06:18:15 +0000196 Pass to \function{getrusage()} to request resource information for
197 child processes of the calling process.
Fred Drakee9072081997-12-06 07:25:41 +0000198\end{datadesc}
199
200\begin{datadesc}{RUSAGE_BOTH}
Fred Drake60ba4471998-03-11 06:18:15 +0000201 Pass to \function{getrusage()} to request resources consumed by both
202 the current process and child processes. May not be available on all
Fred Drakee9072081997-12-06 07:25:41 +0000203 systems.
204\end{datadesc}