blob: 8e102b8eec5f63457d7777b0adeb87a2709e53e3 [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
Raymond Hettinger68804312005-01-01 00:28:46 +000047 underlying system call fails unexpectedly.
Fred Drakee9072081997-12-06 07:25:41 +000048\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
Raymond Hettinger68804312005-01-01 00:28:46 +000059 super-user). Can also raise \exception{error} if the underlying
Fred Drake60ba4471998-03-11 06:18:15 +000060 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
Raymond Hettinger62727372003-07-13 00:46:40 +0000121\begin{datadesc}{RLIMIT_MEMLOCK}
122 The maximum address space which may be locked in memory.
Fred Drakee9072081997-12-06 07:25:41 +0000123\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
Martin v. Löwis688357e2002-04-08 21:28:20 +0000136These functions are used to retrieve resource usage information:
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000137
138\begin{funcdesc}{getrusage}{who}
Martin v. Löwis688357e2002-04-08 21:28:20 +0000139 This function returns an object that describes the resources
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000140 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
Martin v. Löwis688357e2002-04-08 21:28:20 +0000145 The fields of the return value each describe how a particular system
146 resource has been used, e.g. amount of time spent running is user mode
147 or number of times the process was swapped out of main memory. Some
148 values are dependent on the clock tick internal, e.g. the amount of
149 memory the process is using.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000150
Martin v. Löwis688357e2002-04-08 21:28:20 +0000151 For backward compatibility, the return value is also accessible as
152 a tuple of 16 elements.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000153
Martin v. Löwis688357e2002-04-08 21:28:20 +0000154 The fields \member{ru_utime} and \member{ru_stime} of the return value
155 are floating point values representing the amount of time spent
156 executing in user mode and the amount of time spent executing in system
157 mode, respectively. The remaining values are integers. Consult the
158 \manpage{getrusage}{2} man page for detailed information about these
159 values. A brief summary is presented here:
160
161\begin{tableiii}{r|l|l}{code}{Index}{Field}{Resource}
162 \lineiii{0}{\member{ru_utime}}{time in user mode (float)}
163 \lineiii{1}{\member{ru_stime}}{time in system mode (float)}
164 \lineiii{2}{\member{ru_maxrss}}{maximum resident set size}
165 \lineiii{3}{\member{ru_ixrss}}{shared memory size}
166 \lineiii{4}{\member{ru_idrss}}{unshared memory size}
167 \lineiii{5}{\member{ru_isrss}}{unshared stack size}
168 \lineiii{6}{\member{ru_minflt}}{page faults not requiring I/O}
169 \lineiii{7}{\member{ru_majflt}}{page faults requiring I/O}
170 \lineiii{8}{\member{ru_nswap}}{number of swap outs}
171 \lineiii{9}{\member{ru_inblock}}{block input operations}
172 \lineiii{10}{\member{ru_oublock}}{block output operations}
173 \lineiii{11}{\member{ru_msgsnd}}{messages sent}
174 \lineiii{12}{\member{ru_msgrcv}}{messages received}
175 \lineiii{13}{\member{ru_nsignals}}{signals received}
176 \lineiii{14}{\member{ru_nvcsw}}{voluntary context switches}
177 \lineiii{15}{\member{ru_nivcsw}}{involuntary context switches}
178\end{tableiii}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000179
Fred Drake60ba4471998-03-11 06:18:15 +0000180 This function will raise a \exception{ValueError} if an invalid
181 \var{who} parameter is specified. It may also raise
182 \exception{error} exception in unusual circumstances.
Martin v. Löwis688357e2002-04-08 21:28:20 +0000183
184 \versionchanged[Added access to values as attributes of the
185 returned object]{2.3}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000186\end{funcdesc}
187
188\begin{funcdesc}{getpagesize}{}
189 Returns the number of bytes in a system page. (This need not be the
190 same as the hardware page size.) This function is useful for
191 determining the number of bytes of memory a process is using. The
Fred Drake60ba4471998-03-11 06:18:15 +0000192 third element of the tuple returned by \function{getrusage()} describes
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000193 memory usage in pages; multiplying by page size produces number of
194 bytes.
195\end{funcdesc}
196
Fred Drakef6863c11999-03-02 16:37:17 +0000197The following \constant{RUSAGE_*} symbols are passed to the
Fred Drake60ba4471998-03-11 06:18:15 +0000198\function{getrusage()} function to specify which processes information
Fred Drakee9072081997-12-06 07:25:41 +0000199should be provided for.
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000200
Fred Drakee9072081997-12-06 07:25:41 +0000201\begin{datadesc}{RUSAGE_SELF}
Fred Drake60ba4471998-03-11 06:18:15 +0000202 \constant{RUSAGE_SELF} should be used to
Fred Drakee9072081997-12-06 07:25:41 +0000203 request information pertaining only to the process itself.
204\end{datadesc}
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +0000205
Fred Drakee9072081997-12-06 07:25:41 +0000206\begin{datadesc}{RUSAGE_CHILDREN}
Fred Drake60ba4471998-03-11 06:18:15 +0000207 Pass to \function{getrusage()} to request resource information for
208 child processes of the calling process.
Fred Drakee9072081997-12-06 07:25:41 +0000209\end{datadesc}
210
211\begin{datadesc}{RUSAGE_BOTH}
Fred Drake60ba4471998-03-11 06:18:15 +0000212 Pass to \function{getrusage()} to request resources consumed by both
213 the current process and child processes. May not be available on all
Fred Drakee9072081997-12-06 07:25:41 +0000214 systems.
215\end{datadesc}