blob: 887268eed557569353fe71768c5cf54f9e8b91ba [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{resource}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{resource}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +00003
Fred Drakeb91e9341998-07-23 17:59:49 +00004
5\modulesynopsis{An interface to provide resource usage information on the current
6process.}
7
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +00008This module provides basic mechanisms for measuring and controlling
9system resources utilized by a program.
10
11Symbolic constants are used to specify particular system resources and
12to request usage information about either the current process or its
Fred Drakee9072081997-12-06 07:25:41 +000013children.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000014
Fred Drakee9072081997-12-06 07:25:41 +000015A single exception is defined for errors:
16
Fred Drakee9072081997-12-06 07:25:41 +000017
18\begin{excdesc}{error}
19 The functions described below may raise this error if the underlying
20 system call failures unexpectedly.
21\end{excdesc}
22
23\subsection{Resource Limits}
24
Fred Drake60ba4471998-03-11 06:18:15 +000025Resources usage can be limited using the \function{setrlimit()} function
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000026described below. Each resource is controlled by a pair of limits: a
27soft limit and a hard limit. The soft limit is the current limit, and
28may be lowered or raised by a process over time. The soft limit can
29never exceed the hard limit. The hard limit can be lowered to any
Fred Drakee9072081997-12-06 07:25:41 +000030value greater than the soft limit, but not raised. (Only processes with
31the effective UID of the super-user can raise a hard limit.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000032
33The specific resources that can be limited are system dependent. They
Fred Drake60ba4471998-03-11 06:18:15 +000034are described in the \manpage{getrlimit}{2} man page. The resources
Fred Drakee9072081997-12-06 07:25:41 +000035listed below are supported when the underlying operating system
36supports them; resources which cannot be checked or controlled by the
37operating system are not defined in this module for those platforms.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000038
Fred Drakee9072081997-12-06 07:25:41 +000039\begin{funcdesc}{getrlimit}{resource}
40 Returns a tuple \code{(\var{soft}, \var{hard})} with the current
Fred Drake60ba4471998-03-11 06:18:15 +000041 soft and hard limits of \var{resource}. Raises \exception{ValueError} if
42 an invalid resource is specified, or \exception{error} if the
Fred Drakee9072081997-12-06 07:25:41 +000043 underyling system call fails unexpectedly.
44\end{funcdesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000045
Fred Drakee9072081997-12-06 07:25:41 +000046\begin{funcdesc}{setrlimit}{resource, limits}
47 Sets new limits of consumption of \var{resource}. The \var{limits}
48 argument must be a tuple \code{(\var{soft}, \var{hard})} of two
49 integers describing the new limits. A value of \code{-1} can be used to
50 specify the maximum possible upper limit.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000051
Fred Drake60ba4471998-03-11 06:18:15 +000052 Raises \exception{ValueError} if an invalid resource is specified,
53 if the new soft limit exceeds the hard limit, or if a process tries
54 to raise its hard limit (unless the process has an effective UID of
55 super-user). Can also raise \exception{error} if the underyling
56 system call fails.
Fred Drakee9072081997-12-06 07:25:41 +000057\end{funcdesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000058
Fred Drakee9072081997-12-06 07:25:41 +000059These symbols define resources whose consumption can be controlled
Fred Drake60ba4471998-03-11 06:18:15 +000060using the \function{setrlimit()} and \function{getrlimit()} functions
61described below. The values of these symbols are exactly the constants
62used by \C{} programs.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000063
Fred Drake60ba4471998-03-11 06:18:15 +000064The \UNIX{} man page for \manpage{getrlimit}{2} lists the available
Fred Drakee9072081997-12-06 07:25:41 +000065resources. Note that not all systems use the same symbol or same
66value to denote the same resource.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000067
Fred Drakee9072081997-12-06 07:25:41 +000068\begin{datadesc}{RLIMIT_CORE}
69 The maximum size (in bytes) of a core file that the current process
70 can create. This may result in the creation of a partial core file
71 if a larger core would be required to contain the entire process
72 image.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000073\end{datadesc}
74
Fred Drakee9072081997-12-06 07:25:41 +000075\begin{datadesc}{RLIMIT_CPU}
76 The maximum amount of CPU time (in seconds) that a process can
Fred Drake60ba4471998-03-11 06:18:15 +000077 use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to
78 the process. (See the \module{signal} module documentation for
Fred Drakee9072081997-12-06 07:25:41 +000079 information about how to catch this signal and do something useful,
80 e.g. flush open files to disk.)
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000081\end{datadesc}
82
Fred Drakee9072081997-12-06 07:25:41 +000083\begin{datadesc}{RLIMIT_FSIZE}
84 The maximum size of a file which the process may create. This only
85 affects the stack of the main thread in a multi-threaded process.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +000086\end{datadesc}
87
Fred Drakee9072081997-12-06 07:25:41 +000088\begin{datadesc}{RLIMIT_DATA}
89 The maximum size (in bytes) of the process's heap.
90\end{datadesc}
91
92\begin{datadesc}{RLIMIT_STACK}
93 The maximum size (in bytes) of the call stack for the current
94 process.
95\end{datadesc}
96
97\begin{datadesc}{RLIMIT_RSS}
98 The maximum resident set size that should be made available to the
99 process.
100\end{datadesc}
101
102\begin{datadesc}{RLIMIT_NPROC}
103 The maximum number of processes the current process may create.
104\end{datadesc}
105
106\begin{datadesc}{RLIMIT_NOFILE}
107 The maximum number of open file descriptors for the current
108 process.
109\end{datadesc}
110
111\begin{datadesc}{RLIMIT_OFILE}
Fred Drake60ba4471998-03-11 06:18:15 +0000112 The BSD name for \constant{RLIMIT_NOFILE}.
Fred Drakee9072081997-12-06 07:25:41 +0000113\end{datadesc}
114
115\begin{datadesc}{RLIMIT_MEMLOC}
116 The maximm address space which may be locked in memory.
117\end{datadesc}
118
119\begin{datadesc}{RLIMIT_VMEM}
120 The largest area of mapped memory which the process may occupy.
121\end{datadesc}
122
123\begin{datadesc}{RLIMIT_AS}
124 The maximum area (in bytes) of address space which may be taken by
125 the process.
126\end{datadesc}
127
128\subsection{Resource Usage}
129
130These functiona are used to retrieve resource usage information:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000131
132\begin{funcdesc}{getrusage}{who}
133 This function returns a large tuple that describes the resources
134 consumed by either the current process or its children, as specified
Fred Drakee9072081997-12-06 07:25:41 +0000135 by the \var{who} parameter. The \var{who} parameter should be
Fred Drake60ba4471998-03-11 06:18:15 +0000136 specified using one of the \code{RUSAGE_*} constants described
Fred Drakee9072081997-12-06 07:25:41 +0000137 below.
138
139 The elements of the return value each
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000140 describe how a particular system resource has been used, e.g. amount
141 of time spent running is user mode or number of times the process was
142 swapped out of main memory. Some values are dependent on the clock
143 tick internal, e.g. the amount of memory the process is using.
144
145 The first two elements of the return value are floating point values
146 representing the amount of time spent executing in user mode and the
147 amount of time spent executing in system mode, respectively. The
Fred Drake60ba4471998-03-11 06:18:15 +0000148 remaining values are integers. Consult the \manpage{getrusage}{2}
149 man page for detailed information about these values. A brief
150 summary is presented here:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000151
Fred Drakeee601911998-04-11 20:53:03 +0000152\begin{tableii}{r|l}{code}{Offset}{Resource}
Fred Drakec4164451997-12-29 19:02:01 +0000153 \lineii{0}{time in user mode (float)}
154 \lineii{1}{time in system mode (float)}
155 \lineii{2}{maximum resident set size}
156 \lineii{3}{shared memory size}
157 \lineii{4}{unshared memory size}
158 \lineii{5}{unshared stack size}
159 \lineii{6}{page faults not requiring I/O}
160 \lineii{7}{page faults requiring I/O}
161 \lineii{8}{number of swap outs}
162 \lineii{9}{block input operations}
163 \lineii{10}{block output operations}
164 \lineii{11}{messages sent}
165 \lineii{12}{messages received}
166 \lineii{13}{signals received}
167 \lineii{14}{voluntary context switches}
168 \lineii{15}{involuntary context switches}
169\end{tableii}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000170
Fred Drake60ba4471998-03-11 06:18:15 +0000171 This function will raise a \exception{ValueError} if an invalid
172 \var{who} parameter is specified. It may also raise
173 \exception{error} exception in unusual circumstances.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000174\end{funcdesc}
175
176\begin{funcdesc}{getpagesize}{}
177 Returns the number of bytes in a system page. (This need not be the
178 same as the hardware page size.) This function is useful for
179 determining the number of bytes of memory a process is using. The
Fred Drake60ba4471998-03-11 06:18:15 +0000180 third element of the tuple returned by \function{getrusage()} describes
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000181 memory usage in pages; multiplying by page size produces number of
182 bytes.
183\end{funcdesc}
184
Fred Drake60ba4471998-03-11 06:18:15 +0000185The following \code{RUSAGE_*} symbols are passed to the
186\function{getrusage()} function to specify which processes information
Fred Drakee9072081997-12-06 07:25:41 +0000187should be provided for.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000188
Fred Drakee9072081997-12-06 07:25:41 +0000189\begin{datadesc}{RUSAGE_SELF}
Fred Drake60ba4471998-03-11 06:18:15 +0000190 \constant{RUSAGE_SELF} should be used to
Fred Drakee9072081997-12-06 07:25:41 +0000191 request information pertaining only to the process itself.
192\end{datadesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000193
Fred Drakee9072081997-12-06 07:25:41 +0000194\begin{datadesc}{RUSAGE_CHILDREN}
Fred Drake60ba4471998-03-11 06:18:15 +0000195 Pass to \function{getrusage()} to request resource information for
196 child processes of the calling process.
Fred Drakee9072081997-12-06 07:25:41 +0000197\end{datadesc}
198
199\begin{datadesc}{RUSAGE_BOTH}
Fred Drake60ba4471998-03-11 06:18:15 +0000200 Pass to \function{getrusage()} to request resources consumed by both
201 the current process and child processes. May not be available on all
Fred Drakee9072081997-12-06 07:25:41 +0000202 systems.
203\end{datadesc}