Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{resource} --- |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 2 | Resource usage information} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{resource} |
Fred Drake | a54a887 | 1999-03-02 17:03:42 +0000 | [diff] [blame] | 5 | \platform{Unix} |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 6 | \modulesynopsis{An interface to provide resource usage information on |
| 7 | the current process.} |
Fred Drake | fcc1633 | 2001-10-04 20:40:07 +0000 | [diff] [blame] | 8 | \moduleauthor{Jeremy Hylton}{jeremy@alum.mit.edu} |
| 9 | \sectionauthor{Jeremy Hylton}{jeremy@alum.mit.edu} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 10 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 12 | This module provides basic mechanisms for measuring and controlling |
| 13 | system resources utilized by a program. |
| 14 | |
| 15 | Symbolic constants are used to specify particular system resources and |
| 16 | to request usage information about either the current process or its |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 17 | children. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 18 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 19 | A single exception is defined for errors: |
| 20 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 21 | |
| 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 Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 29 | Resources usage can be limited using the \function{setrlimit()} function |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 30 | described below. Each resource is controlled by a pair of limits: a |
| 31 | soft limit and a hard limit. The soft limit is the current limit, and |
| 32 | may be lowered or raised by a process over time. The soft limit can |
| 33 | 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] | 34 | value greater than the soft limit, but not raised. (Only processes with |
| 35 | 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] | 36 | |
| 37 | The specific resources that can be limited are system dependent. They |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 38 | are described in the \manpage{getrlimit}{2} man page. The resources |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 39 | listed below are supported when the underlying operating system |
| 40 | supports them; resources which cannot be checked or controlled by the |
| 41 | operating system are not defined in this module for those platforms. |
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}{getrlimit}{resource} |
| 44 | Returns a tuple \code{(\var{soft}, \var{hard})} with the current |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 45 | soft and hard limits of \var{resource}. Raises \exception{ValueError} if |
| 46 | an invalid resource is specified, or \exception{error} if the |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 47 | underyling system call fails unexpectedly. |
| 48 | \end{funcdesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 49 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 50 | \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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 55 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 56 | 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 |
| 59 | super-user). Can also raise \exception{error} if the underyling |
| 60 | system call fails. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 61 | \end{funcdesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 62 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 63 | These symbols define resources whose consumption can be controlled |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 64 | using the \function{setrlimit()} and \function{getrlimit()} functions |
| 65 | described below. The values of these symbols are exactly the constants |
| 66 | used by \C{} programs. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 67 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 68 | The \UNIX{} man page for \manpage{getrlimit}{2} lists the available |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 69 | resources. Note that not all systems use the same symbol or same |
Fred Drake | 5d9a6b5 | 2001-10-22 14:18:23 +0000 | [diff] [blame] | 70 | value to denote the same resource. This module does not attempt to |
| 71 | mask platform differences --- symbols not defined for a platform will |
| 72 | not be available from this module on that platform. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 73 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 74 | \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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 79 | \end{datadesc} |
| 80 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 81 | \begin{datadesc}{RLIMIT_CPU} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 82 | The maximum amount of processor time (in seconds) that a process can |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 83 | use. If this limit is exceeded, a \constant{SIGXCPU} signal is sent to |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 84 | the process. (See the \refmodule{signal} module documentation for |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 85 | information about how to catch this signal and do something useful, |
| 86 | e.g. flush open files to disk.) |
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_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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 92 | \end{datadesc} |
| 93 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 94 | \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 Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 118 | The BSD name for \constant{RLIMIT_NOFILE}. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 119 | \end{datadesc} |
| 120 | |
| 121 | \begin{datadesc}{RLIMIT_MEMLOC} |
| 122 | The maximm address space which may be locked in memory. |
| 123 | \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 | |
| 136 | These functiona are used to retrieve resource usage information: |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 137 | |
| 138 | \begin{funcdesc}{getrusage}{who} |
| 139 | This function returns a large tuple that describes the resources |
| 140 | consumed by either the current process or its children, as specified |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 141 | by the \var{who} parameter. The \var{who} parameter should be |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 142 | specified using one of the \constant{RUSAGE_*} constants described |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 143 | below. |
| 144 | |
| 145 | The elements of the return value each |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 146 | describe how a particular system resource has been used, e.g. amount |
| 147 | of time spent running is user mode or number of times the process was |
| 148 | swapped out of main memory. Some values are dependent on the clock |
| 149 | tick internal, e.g. the amount of memory the process is using. |
| 150 | |
| 151 | The first two elements of the return value are floating point values |
| 152 | representing the amount of time spent executing in user mode and the |
| 153 | amount of time spent executing in system mode, respectively. The |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 154 | remaining values are integers. Consult the \manpage{getrusage}{2} |
| 155 | man page for detailed information about these values. A brief |
| 156 | summary is presented here: |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 157 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 158 | \begin{tableii}{r|l}{code}{Offset}{Resource} |
Fred Drake | c416445 | 1997-12-29 19:02:01 +0000 | [diff] [blame] | 159 | \lineii{0}{time in user mode (float)} |
| 160 | \lineii{1}{time in system mode (float)} |
| 161 | \lineii{2}{maximum resident set size} |
| 162 | \lineii{3}{shared memory size} |
| 163 | \lineii{4}{unshared memory size} |
| 164 | \lineii{5}{unshared stack size} |
| 165 | \lineii{6}{page faults not requiring I/O} |
| 166 | \lineii{7}{page faults requiring I/O} |
| 167 | \lineii{8}{number of swap outs} |
| 168 | \lineii{9}{block input operations} |
| 169 | \lineii{10}{block output operations} |
| 170 | \lineii{11}{messages sent} |
| 171 | \lineii{12}{messages received} |
| 172 | \lineii{13}{signals received} |
| 173 | \lineii{14}{voluntary context switches} |
| 174 | \lineii{15}{involuntary context switches} |
| 175 | \end{tableii} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 176 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 177 | This function will raise a \exception{ValueError} if an invalid |
| 178 | \var{who} parameter is specified. It may also raise |
| 179 | \exception{error} exception in unusual circumstances. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 180 | \end{funcdesc} |
| 181 | |
| 182 | \begin{funcdesc}{getpagesize}{} |
| 183 | Returns the number of bytes in a system page. (This need not be the |
| 184 | same as the hardware page size.) This function is useful for |
| 185 | determining the number of bytes of memory a process is using. The |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 186 | third element of the tuple returned by \function{getrusage()} describes |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 187 | memory usage in pages; multiplying by page size produces number of |
| 188 | bytes. |
| 189 | \end{funcdesc} |
| 190 | |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 191 | The following \constant{RUSAGE_*} symbols are passed to the |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 192 | \function{getrusage()} function to specify which processes information |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 193 | should be provided for. |
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_SELF} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 196 | \constant{RUSAGE_SELF} should be used to |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 197 | request information pertaining only to the process itself. |
| 198 | \end{datadesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 199 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 200 | \begin{datadesc}{RUSAGE_CHILDREN} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 201 | Pass to \function{getrusage()} to request resource information for |
| 202 | child processes of the calling process. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 203 | \end{datadesc} |
| 204 | |
| 205 | \begin{datadesc}{RUSAGE_BOTH} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 206 | Pass to \function{getrusage()} to request resources consumed by both |
| 207 | the current process and child processes. May not be available on all |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 208 | systems. |
| 209 | \end{datadesc} |