blob: 5c93fa609738c92343500a30ff475e70a7c591d1 [file] [log] [blame]
Guido van Rossum3c7b2dc1996-12-18 18:37:05 +00001\section{Built-in Module \sectcode{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
10children.
11
12Resources usage can be limited using the \code{setrlimit} function
13described below. Each resource is controlled by a pair of limits: a
14soft limit and a hard limit. The soft limit is the current limit, and
15may be lowered or raised by a process over time. The soft limit can
16never exceed the hard limit. The hard limit can be lowered to any
17value greater than the soft limit, but not raised. (Only process with
18the effective UID of the super-user can raise a hard limit).
19
20The specific resources that can be limited are system dependent. They
21are described in the \code{getrlimit} man page. Typical resources
22include:
23
24\begin{description}
25
26\item[RLIMIT_CORE]
27The maximum size (in bytes) of a core file that the current process
28can create.
29
30\item[RLIMIT_CPU]
31The maximum amount of CPU time (in seconds) that a process can use. If
32this limit is exceeded, a \code{SIGXCPU} signal is sent to the
33process. (See the \code{signal} module documentation for information
34about how to catch this signal and do something useful, e.g. flush
35open files to disk.)
36
37\end{description}
38
39\begin{datadesc}{RLIMIT_*}
40 These symbols define resources whose consumption can be controlled
41 using the \code{setrlimit} and \code{getrlimit} functions defined
42 below. The values of these symbols are exactly the constants used
43 by C programs.
44
45 The \UNIX{} man page for \file{getrlimit} lists the available
46 resources. Note that not all systems use the same symbol or same
47 value to denote the same resource.
48\end{datadesc}
49
50\begin{datadesc}{RUSAGE_*}
51 These symbols are passed to the \code{getrusage} function to specify
52 whether usage information is being request for the current process,
53 \code{RUSAGE_SELF} or its child processes \code{RUSAGE_CHILDREN}. On
54 some system, \code{RUSAGE_BOTH} requests information for both.
55\end{datadesc}
56
57\begin{datadesc}{error}
58 The functions described below may raise this error if the underlying
59 system call failures unexpectedly.
60\end{datadesc}
61
62The resource module defines the following functions:
63
64\begin{funcdesc}{getrusage}{who}
65 This function returns a large tuple that describes the resources
66 consumed by either the current process or its children, as specified
67 by the \var{who} parameter. The elements of the return value each
68 describe how a particular system resource has been used, e.g. amount
69 of time spent running is user mode or number of times the process was
70 swapped out of main memory. Some values are dependent on the clock
71 tick internal, e.g. the amount of memory the process is using.
72
73 The first two elements of the return value are floating point values
74 representing the amount of time spent executing in user mode and the
75 amount of time spent executing in system mode, respectively. The
76 remaining values are integers. Consult the \code{getrusage} man page
77 for detailed information about these values. A brief summary is
78 presented here:
79
80\begin{tabular}{rl}
81 \emph{offset} & \emph{resource} \\
82 0 & time in user mode (float) \\
83 1 & time in system mode (float) \\
84 2 & maximum resident set size \\
85 3 & shared memory size \\
86 4 & unshared memory size \\
87 5 & unshared stack size \\
88 6 & page faults not requiring I/O \\
89 7 & page faults requiring I/O \\
90 8 & number of swap outs \\
91 9 & block input operations \\
92 10 & block output operations \\
93 11 & messages sent \\
94 12 & messages received \\
95 13 & signals received \\
96 14 & voluntary context switches \\
97 15 & involuntary context switches \\
98\end{tabular}
99
100 This function will raise a ValueError if an invalid \var{who}
101 parameter is specified. It may also raise a \code{resource.error}
102 exception in unusual circumstances.
103\end{funcdesc}
104
105\begin{funcdesc}{getpagesize}{}
106 Returns the number of bytes in a system page. (This need not be the
107 same as the hardware page size.) This function is useful for
108 determining the number of bytes of memory a process is using. The
109 third element of the tuple returned by \code{getrusage} describes
110 memory usage in pages; multiplying by page size produces number of
111 bytes.
112\end{funcdesc}
113
114\begin{funcdesc}{getrlimit}{resource}
115 Returns a tuple \code{(\var{soft}, \var{hard})} with the current
116 soft and hard limits of \var{resource}. Raises ValueError if
117 an invalid resource is specified, or \code{resource.error} if the
118 underyling system call fails unexpectedly.
119\end{funcdesc}
120
121\begin{funcdesc}{setrlimit}{resource\, limits}
122 Sets new limits of consumption of \var{resource}. The \var{limits}
123 argument must be a tuple \code{(\var{soft}, \var{hard})} of two
124 integers describing the new limits. A value of -1 can be used to
125 specify the maximum possible upper limit.
126
127 Raises ValueError if an invalid resource is specified, if the new
128 soft limit exceeds the hard limit, or if a process tries to raise its
129 hard limit (unless the process has an effective UID of
130 super-user). Can also raise a \code{resource.error} if the
131 underyling system call fails.
132\end{funcdesc}