Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{resource} --- |
| 2 | Resource usage information.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{builtin}{resource} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 4 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | |
| 6 | \modulesynopsis{An interface to provide resource usage information on the current |
| 7 | process.} |
| 8 | |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 9 | This module provides basic mechanisms for measuring and controlling |
| 10 | system resources utilized by a program. |
| 11 | |
| 12 | Symbolic constants are used to specify particular system resources and |
| 13 | to request usage information about either the current process or its |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 14 | children. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 15 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 16 | A single exception is defined for errors: |
| 17 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 18 | |
| 19 | \begin{excdesc}{error} |
| 20 | The functions described below may raise this error if the underlying |
| 21 | system call failures unexpectedly. |
| 22 | \end{excdesc} |
| 23 | |
| 24 | \subsection{Resource Limits} |
| 25 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 26 | Resources usage can be limited using the \function{setrlimit()} function |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 27 | described below. Each resource is controlled by a pair of limits: a |
| 28 | soft limit and a hard limit. The soft limit is the current limit, and |
| 29 | may be lowered or raised by a process over time. The soft limit can |
| 30 | 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] | 31 | value greater than the soft limit, but not raised. (Only processes with |
| 32 | 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] | 33 | |
| 34 | The specific resources that can be limited are system dependent. They |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 35 | are described in the \manpage{getrlimit}{2} man page. The resources |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 36 | listed below are supported when the underlying operating system |
| 37 | supports them; resources which cannot be checked or controlled by the |
| 38 | operating system are not defined in this module for those platforms. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 39 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 40 | \begin{funcdesc}{getrlimit}{resource} |
| 41 | Returns a tuple \code{(\var{soft}, \var{hard})} with the current |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 42 | soft and hard limits of \var{resource}. Raises \exception{ValueError} if |
| 43 | an invalid resource is specified, or \exception{error} if the |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 44 | underyling system call fails unexpectedly. |
| 45 | \end{funcdesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 46 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 47 | \begin{funcdesc}{setrlimit}{resource, limits} |
| 48 | Sets new limits of consumption of \var{resource}. The \var{limits} |
| 49 | argument must be a tuple \code{(\var{soft}, \var{hard})} of two |
| 50 | integers describing the new limits. A value of \code{-1} can be used to |
| 51 | specify the maximum possible upper limit. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 52 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 53 | Raises \exception{ValueError} if an invalid resource is specified, |
| 54 | if the new soft limit exceeds the hard limit, or if a process tries |
| 55 | to raise its hard limit (unless the process has an effective UID of |
| 56 | super-user). Can also raise \exception{error} if the underyling |
| 57 | system call fails. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 58 | \end{funcdesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 59 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 60 | These symbols define resources whose consumption can be controlled |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 61 | using the \function{setrlimit()} and \function{getrlimit()} functions |
| 62 | described below. The values of these symbols are exactly the constants |
| 63 | used by \C{} programs. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 64 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 65 | The \UNIX{} man page for \manpage{getrlimit}{2} lists the available |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 66 | resources. Note that not all systems use the same symbol or same |
| 67 | value to denote the same resource. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 68 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 69 | \begin{datadesc}{RLIMIT_CORE} |
| 70 | The maximum size (in bytes) of a core file that the current process |
| 71 | can create. This may result in the creation of a partial core file |
| 72 | if a larger core would be required to contain the entire process |
| 73 | image. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 74 | \end{datadesc} |
| 75 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 76 | \begin{datadesc}{RLIMIT_CPU} |
| 77 | The maximum amount of CPU time (in seconds) that a process can |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 78 | use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to |
| 79 | the process. (See the \module{signal} module documentation for |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 80 | information about how to catch this signal and do something useful, |
| 81 | e.g. flush open files to disk.) |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 82 | \end{datadesc} |
| 83 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 84 | \begin{datadesc}{RLIMIT_FSIZE} |
| 85 | The maximum size of a file which the process may create. This only |
| 86 | 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] | 87 | \end{datadesc} |
| 88 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 89 | \begin{datadesc}{RLIMIT_DATA} |
| 90 | The maximum size (in bytes) of the process's heap. |
| 91 | \end{datadesc} |
| 92 | |
| 93 | \begin{datadesc}{RLIMIT_STACK} |
| 94 | The maximum size (in bytes) of the call stack for the current |
| 95 | process. |
| 96 | \end{datadesc} |
| 97 | |
| 98 | \begin{datadesc}{RLIMIT_RSS} |
| 99 | The maximum resident set size that should be made available to the |
| 100 | process. |
| 101 | \end{datadesc} |
| 102 | |
| 103 | \begin{datadesc}{RLIMIT_NPROC} |
| 104 | The maximum number of processes the current process may create. |
| 105 | \end{datadesc} |
| 106 | |
| 107 | \begin{datadesc}{RLIMIT_NOFILE} |
| 108 | The maximum number of open file descriptors for the current |
| 109 | process. |
| 110 | \end{datadesc} |
| 111 | |
| 112 | \begin{datadesc}{RLIMIT_OFILE} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 113 | The BSD name for \constant{RLIMIT_NOFILE}. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 114 | \end{datadesc} |
| 115 | |
| 116 | \begin{datadesc}{RLIMIT_MEMLOC} |
| 117 | The maximm address space which may be locked in memory. |
| 118 | \end{datadesc} |
| 119 | |
| 120 | \begin{datadesc}{RLIMIT_VMEM} |
| 121 | The largest area of mapped memory which the process may occupy. |
| 122 | \end{datadesc} |
| 123 | |
| 124 | \begin{datadesc}{RLIMIT_AS} |
| 125 | The maximum area (in bytes) of address space which may be taken by |
| 126 | the process. |
| 127 | \end{datadesc} |
| 128 | |
| 129 | \subsection{Resource Usage} |
| 130 | |
| 131 | These functiona are used to retrieve resource usage information: |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 132 | |
| 133 | \begin{funcdesc}{getrusage}{who} |
| 134 | This function returns a large tuple that describes the resources |
| 135 | consumed by either the current process or its children, as specified |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 136 | by the \var{who} parameter. The \var{who} parameter should be |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 137 | specified using one of the \code{RUSAGE_*} constants described |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 138 | below. |
| 139 | |
| 140 | The elements of the return value each |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 141 | describe how a particular system resource has been used, e.g. amount |
| 142 | of time spent running is user mode or number of times the process was |
| 143 | swapped out of main memory. Some values are dependent on the clock |
| 144 | tick internal, e.g. the amount of memory the process is using. |
| 145 | |
| 146 | The first two elements of the return value are floating point values |
| 147 | representing the amount of time spent executing in user mode and the |
| 148 | amount of time spent executing in system mode, respectively. The |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 149 | remaining values are integers. Consult the \manpage{getrusage}{2} |
| 150 | man page for detailed information about these values. A brief |
| 151 | summary is presented here: |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 152 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 153 | \begin{tableii}{r|l}{code}{Offset}{Resource} |
Fred Drake | c416445 | 1997-12-29 19:02:01 +0000 | [diff] [blame] | 154 | \lineii{0}{time in user mode (float)} |
| 155 | \lineii{1}{time in system mode (float)} |
| 156 | \lineii{2}{maximum resident set size} |
| 157 | \lineii{3}{shared memory size} |
| 158 | \lineii{4}{unshared memory size} |
| 159 | \lineii{5}{unshared stack size} |
| 160 | \lineii{6}{page faults not requiring I/O} |
| 161 | \lineii{7}{page faults requiring I/O} |
| 162 | \lineii{8}{number of swap outs} |
| 163 | \lineii{9}{block input operations} |
| 164 | \lineii{10}{block output operations} |
| 165 | \lineii{11}{messages sent} |
| 166 | \lineii{12}{messages received} |
| 167 | \lineii{13}{signals received} |
| 168 | \lineii{14}{voluntary context switches} |
| 169 | \lineii{15}{involuntary context switches} |
| 170 | \end{tableii} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 171 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 172 | This function will raise a \exception{ValueError} if an invalid |
| 173 | \var{who} parameter is specified. It may also raise |
| 174 | \exception{error} exception in unusual circumstances. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 175 | \end{funcdesc} |
| 176 | |
| 177 | \begin{funcdesc}{getpagesize}{} |
| 178 | Returns the number of bytes in a system page. (This need not be the |
| 179 | same as the hardware page size.) This function is useful for |
| 180 | determining the number of bytes of memory a process is using. The |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 181 | third element of the tuple returned by \function{getrusage()} describes |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 182 | memory usage in pages; multiplying by page size produces number of |
| 183 | bytes. |
| 184 | \end{funcdesc} |
| 185 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 186 | The following \code{RUSAGE_*} symbols are passed to the |
| 187 | \function{getrusage()} function to specify which processes information |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 188 | should be provided for. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 189 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 190 | \begin{datadesc}{RUSAGE_SELF} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 191 | \constant{RUSAGE_SELF} should be used to |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 192 | request information pertaining only to the process itself. |
| 193 | \end{datadesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 194 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 195 | \begin{datadesc}{RUSAGE_CHILDREN} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 196 | Pass to \function{getrusage()} to request resource information for |
| 197 | child processes of the calling process. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 198 | \end{datadesc} |
| 199 | |
| 200 | \begin{datadesc}{RUSAGE_BOTH} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 201 | Pass to \function{getrusage()} to request resources consumed by both |
| 202 | the current process and child processes. May not be available on all |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 203 | systems. |
| 204 | \end{datadesc} |