blob: bb0bff21831241aede067222fcea08bf54991fa0 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{resource}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-resource}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +00003
4\bimodindex{resource}
5This module provides basic mechanisms for measuring and controlling
6system resources utilized by a program.
7
8Symbolic constants are used to specify particular system resources and
9to request usage information about either the current process or its
Fred Drakee9072081997-12-06 07:25:41 +000010children.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000011
Fred Drakee9072081997-12-06 07:25:41 +000012A single exception is defined for errors:
13
Fred Drakee9072081997-12-06 07:25:41 +000014
15\begin{excdesc}{error}
16 The functions described below may raise this error if the underlying
17 system call failures unexpectedly.
18\end{excdesc}
19
20\subsection{Resource Limits}
21
Fred Drake60ba4471998-03-11 06:18:15 +000022Resources usage can be limited using the \function{setrlimit()} function
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000023described below. Each resource is controlled by a pair of limits: a
24soft limit and a hard limit. The soft limit is the current limit, and
25may be lowered or raised by a process over time. The soft limit can
26never exceed the hard limit. The hard limit can be lowered to any
Fred Drakee9072081997-12-06 07:25:41 +000027value greater than the soft limit, but not raised. (Only processes with
28the effective UID of the super-user can raise a hard limit.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000029
30The specific resources that can be limited are system dependent. They
Fred Drake60ba4471998-03-11 06:18:15 +000031are described in the \manpage{getrlimit}{2} man page. The resources
Fred Drakee9072081997-12-06 07:25:41 +000032listed below are supported when the underlying operating system
33supports them; resources which cannot be checked or controlled by the
34operating system are not defined in this module for those platforms.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000035
Fred Drakee9072081997-12-06 07:25:41 +000036\begin{funcdesc}{getrlimit}{resource}
37 Returns a tuple \code{(\var{soft}, \var{hard})} with the current
Fred Drake60ba4471998-03-11 06:18:15 +000038 soft and hard limits of \var{resource}. Raises \exception{ValueError} if
39 an invalid resource is specified, or \exception{error} if the
Fred Drakee9072081997-12-06 07:25:41 +000040 underyling system call fails unexpectedly.
41\end{funcdesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000042
Fred Drakee9072081997-12-06 07:25:41 +000043\begin{funcdesc}{setrlimit}{resource, limits}
44 Sets new limits of consumption of \var{resource}. The \var{limits}
45 argument must be a tuple \code{(\var{soft}, \var{hard})} of two
46 integers describing the new limits. A value of \code{-1} can be used to
47 specify the maximum possible upper limit.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000048
Fred Drake60ba4471998-03-11 06:18:15 +000049 Raises \exception{ValueError} if an invalid resource is specified,
50 if the new soft limit exceeds the hard limit, or if a process tries
51 to raise its hard limit (unless the process has an effective UID of
52 super-user). Can also raise \exception{error} if the underyling
53 system call fails.
Fred Drakee9072081997-12-06 07:25:41 +000054\end{funcdesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000055
Fred Drakee9072081997-12-06 07:25:41 +000056These symbols define resources whose consumption can be controlled
Fred Drake60ba4471998-03-11 06:18:15 +000057using the \function{setrlimit()} and \function{getrlimit()} functions
58described below. The values of these symbols are exactly the constants
59used by \C{} programs.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000060
Fred Drake60ba4471998-03-11 06:18:15 +000061The \UNIX{} man page for \manpage{getrlimit}{2} lists the available
Fred Drakee9072081997-12-06 07:25:41 +000062resources. Note that not all systems use the same symbol or same
63value to denote the same resource.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000064
Fred Drakee9072081997-12-06 07:25:41 +000065\begin{datadesc}{RLIMIT_CORE}
66 The maximum size (in bytes) of a core file that the current process
67 can create. This may result in the creation of a partial core file
68 if a larger core would be required to contain the entire process
69 image.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000070\end{datadesc}
71
Fred Drakee9072081997-12-06 07:25:41 +000072\begin{datadesc}{RLIMIT_CPU}
73 The maximum amount of CPU time (in seconds) that a process can
Fred Drake60ba4471998-03-11 06:18:15 +000074 use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to
75 the process. (See the \module{signal} module documentation for
Fred Drakee9072081997-12-06 07:25:41 +000076 information about how to catch this signal and do something useful,
77 e.g. flush open files to disk.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000078\end{datadesc}
79
Fred Drakee9072081997-12-06 07:25:41 +000080\begin{datadesc}{RLIMIT_FSIZE}
81 The maximum size of a file which the process may create. This only
82 affects the stack of the main thread in a multi-threaded process.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000083\end{datadesc}
84
Fred Drakee9072081997-12-06 07:25:41 +000085\begin{datadesc}{RLIMIT_DATA}
86 The maximum size (in bytes) of the process's heap.
87\end{datadesc}
88
89\begin{datadesc}{RLIMIT_STACK}
90 The maximum size (in bytes) of the call stack for the current
91 process.
92\end{datadesc}
93
94\begin{datadesc}{RLIMIT_RSS}
95 The maximum resident set size that should be made available to the
96 process.
97\end{datadesc}
98
99\begin{datadesc}{RLIMIT_NPROC}
100 The maximum number of processes the current process may create.
101\end{datadesc}
102
103\begin{datadesc}{RLIMIT_NOFILE}
104 The maximum number of open file descriptors for the current
105 process.
106\end{datadesc}
107
108\begin{datadesc}{RLIMIT_OFILE}
Fred Drake60ba4471998-03-11 06:18:15 +0000109 The BSD name for \constant{RLIMIT_NOFILE}.
Fred Drakee9072081997-12-06 07:25:41 +0000110\end{datadesc}
111
112\begin{datadesc}{RLIMIT_MEMLOC}
113 The maximm address space which may be locked in memory.
114\end{datadesc}
115
116\begin{datadesc}{RLIMIT_VMEM}
117 The largest area of mapped memory which the process may occupy.
118\end{datadesc}
119
120\begin{datadesc}{RLIMIT_AS}
121 The maximum area (in bytes) of address space which may be taken by
122 the process.
123\end{datadesc}
124
125\subsection{Resource Usage}
126
127These functiona are used to retrieve resource usage information:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000128
129\begin{funcdesc}{getrusage}{who}
130 This function returns a large tuple that describes the resources
131 consumed by either the current process or its children, as specified
Fred Drakee9072081997-12-06 07:25:41 +0000132 by the \var{who} parameter. The \var{who} parameter should be
Fred Drake60ba4471998-03-11 06:18:15 +0000133 specified using one of the \code{RUSAGE_*} constants described
Fred Drakee9072081997-12-06 07:25:41 +0000134 below.
135
136 The elements of the return value each
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000137 describe how a particular system resource has been used, e.g. amount
138 of time spent running is user mode or number of times the process was
139 swapped out of main memory. Some values are dependent on the clock
140 tick internal, e.g. the amount of memory the process is using.
141
142 The first two elements of the return value are floating point values
143 representing the amount of time spent executing in user mode and the
144 amount of time spent executing in system mode, respectively. The
Fred Drake60ba4471998-03-11 06:18:15 +0000145 remaining values are integers. Consult the \manpage{getrusage}{2}
146 man page for detailed information about these values. A brief
147 summary is presented here:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000148
Fred Drakeee601911998-04-11 20:53:03 +0000149\begin{tableii}{r|l}{code}{Offset}{Resource}
Fred Drakec4164451997-12-29 19:02:01 +0000150 \lineii{0}{time in user mode (float)}
151 \lineii{1}{time in system mode (float)}
152 \lineii{2}{maximum resident set size}
153 \lineii{3}{shared memory size}
154 \lineii{4}{unshared memory size}
155 \lineii{5}{unshared stack size}
156 \lineii{6}{page faults not requiring I/O}
157 \lineii{7}{page faults requiring I/O}
158 \lineii{8}{number of swap outs}
159 \lineii{9}{block input operations}
160 \lineii{10}{block output operations}
161 \lineii{11}{messages sent}
162 \lineii{12}{messages received}
163 \lineii{13}{signals received}
164 \lineii{14}{voluntary context switches}
165 \lineii{15}{involuntary context switches}
166\end{tableii}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000167
Fred Drake60ba4471998-03-11 06:18:15 +0000168 This function will raise a \exception{ValueError} if an invalid
169 \var{who} parameter is specified. It may also raise
170 \exception{error} exception in unusual circumstances.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000171\end{funcdesc}
172
173\begin{funcdesc}{getpagesize}{}
174 Returns the number of bytes in a system page. (This need not be the
175 same as the hardware page size.) This function is useful for
176 determining the number of bytes of memory a process is using. The
Fred Drake60ba4471998-03-11 06:18:15 +0000177 third element of the tuple returned by \function{getrusage()} describes
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000178 memory usage in pages; multiplying by page size produces number of
179 bytes.
180\end{funcdesc}
181
Fred Drake60ba4471998-03-11 06:18:15 +0000182The following \code{RUSAGE_*} symbols are passed to the
183\function{getrusage()} function to specify which processes information
Fred Drakee9072081997-12-06 07:25:41 +0000184should be provided for.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000185
Fred Drakee9072081997-12-06 07:25:41 +0000186\begin{datadesc}{RUSAGE_SELF}
Fred Drake60ba4471998-03-11 06:18:15 +0000187 \constant{RUSAGE_SELF} should be used to
Fred Drakee9072081997-12-06 07:25:41 +0000188 request information pertaining only to the process itself.
189\end{datadesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000190
Fred Drakee9072081997-12-06 07:25:41 +0000191\begin{datadesc}{RUSAGE_CHILDREN}
Fred Drake60ba4471998-03-11 06:18:15 +0000192 Pass to \function{getrusage()} to request resource information for
193 child processes of the calling process.
Fred Drakee9072081997-12-06 07:25:41 +0000194\end{datadesc}
195
196\begin{datadesc}{RUSAGE_BOTH}
Fred Drake60ba4471998-03-11 06:18:15 +0000197 Pass to \function{getrusage()} to request resources consumed by both
198 the current process and child processes. May not be available on all
Fred Drakee9072081997-12-06 07:25:41 +0000199 systems.
200\end{datadesc}