Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Built-in Module \module{resource}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-resource} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 3 | |
| 4 | \bimodindex{resource} |
| 5 | This module provides basic mechanisms for measuring and controlling |
| 6 | system resources utilized by a program. |
| 7 | |
| 8 | Symbolic constants are used to specify particular system resources and |
| 9 | to request usage information about either the current process or its |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 10 | children. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 11 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 12 | A single exception is defined for errors: |
| 13 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 14 | |
| 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 Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 22 | Resources usage can be limited using the \function{setrlimit()} function |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 23 | described below. Each resource is controlled by a pair of limits: a |
| 24 | soft limit and a hard limit. The soft limit is the current limit, and |
| 25 | may be lowered or raised by a process over time. The soft limit can |
| 26 | never exceed the hard limit. The hard limit can be lowered to any |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 27 | value greater than the soft limit, but not raised. (Only processes with |
| 28 | the effective UID of the super-user can raise a hard limit.) |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 29 | |
| 30 | The specific resources that can be limited are system dependent. They |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 31 | are described in the \manpage{getrlimit}{2} man page. The resources |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 32 | listed below are supported when the underlying operating system |
| 33 | supports them; resources which cannot be checked or controlled by the |
| 34 | operating system are not defined in this module for those platforms. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 35 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 36 | \begin{funcdesc}{getrlimit}{resource} |
| 37 | Returns a tuple \code{(\var{soft}, \var{hard})} with the current |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 38 | soft and hard limits of \var{resource}. Raises \exception{ValueError} if |
| 39 | an invalid resource is specified, or \exception{error} if the |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 40 | underyling system call fails unexpectedly. |
| 41 | \end{funcdesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 42 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 43 | \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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 48 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 49 | 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 Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 54 | \end{funcdesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 55 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 56 | These symbols define resources whose consumption can be controlled |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 57 | using the \function{setrlimit()} and \function{getrlimit()} functions |
| 58 | described below. The values of these symbols are exactly the constants |
| 59 | used by \C{} programs. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 60 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 61 | The \UNIX{} man page for \manpage{getrlimit}{2} lists the available |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 62 | resources. Note that not all systems use the same symbol or same |
| 63 | value to denote the same resource. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 64 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 65 | \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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 70 | \end{datadesc} |
| 71 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 72 | \begin{datadesc}{RLIMIT_CPU} |
| 73 | The maximum amount of CPU time (in seconds) that a process can |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 74 | use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to |
| 75 | the process. (See the \module{signal} module documentation for |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 76 | information about how to catch this signal and do something useful, |
| 77 | e.g. flush open files to disk.) |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 78 | \end{datadesc} |
| 79 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 80 | \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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 83 | \end{datadesc} |
| 84 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 85 | \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 Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 109 | The BSD name for \constant{RLIMIT_NOFILE}. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 110 | \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 | |
| 127 | These functiona are used to retrieve resource usage information: |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 128 | |
| 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 Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 132 | by the \var{who} parameter. The \var{who} parameter should be |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 133 | specified using one of the \code{RUSAGE_*} constants described |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 134 | below. |
| 135 | |
| 136 | The elements of the return value each |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 137 | 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 Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 145 | 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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 148 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 149 | \begin{tableii}{r|l}{code}{Offset}{Resource} |
Fred Drake | c416445 | 1997-12-29 19:02:01 +0000 | [diff] [blame] | 150 | \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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 167 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 168 | 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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 171 | \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 Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 177 | third element of the tuple returned by \function{getrusage()} describes |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 178 | memory usage in pages; multiplying by page size produces number of |
| 179 | bytes. |
| 180 | \end{funcdesc} |
| 181 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 182 | The following \code{RUSAGE_*} symbols are passed to the |
| 183 | \function{getrusage()} function to specify which processes information |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 184 | should be provided for. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 185 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 186 | \begin{datadesc}{RUSAGE_SELF} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 187 | \constant{RUSAGE_SELF} should be used to |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 188 | request information pertaining only to the process itself. |
| 189 | \end{datadesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 190 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 191 | \begin{datadesc}{RUSAGE_CHILDREN} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 192 | Pass to \function{getrusage()} to request resource information for |
| 193 | child processes of the calling process. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 194 | \end{datadesc} |
| 195 | |
| 196 | \begin{datadesc}{RUSAGE_BOTH} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 197 | Pass to \function{getrusage()} to request resources consumed by both |
| 198 | the current process and child processes. May not be available on all |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 199 | systems. |
| 200 | \end{datadesc} |