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 | |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 136 | These functions 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} |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 139 | This function returns an object that describes the resources |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 145 | 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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 150 | |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 151 | For backward compatibility, the return value is also accessible as |
| 152 | a tuple of 16 elements. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 153 | |
Martin v. Löwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 154 | 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 Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 179 | |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 180 | 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öwis | 688357e | 2002-04-08 21:28:20 +0000 | [diff] [blame] | 183 | |
| 184 | \versionchanged[Added access to values as attributes of the |
| 185 | returned object]{2.3} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 186 | \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 Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 192 | third element of the tuple returned by \function{getrusage()} describes |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 193 | memory usage in pages; multiplying by page size produces number of |
| 194 | bytes. |
| 195 | \end{funcdesc} |
| 196 | |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 197 | The following \constant{RUSAGE_*} symbols are passed to the |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 198 | \function{getrusage()} function to specify which processes information |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 199 | should be provided for. |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 200 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 201 | \begin{datadesc}{RUSAGE_SELF} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 202 | \constant{RUSAGE_SELF} should be used to |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 203 | request information pertaining only to the process itself. |
| 204 | \end{datadesc} |
Guido van Rossum | 3c7b2dc | 1996-12-18 18:37:05 +0000 | [diff] [blame] | 205 | |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 206 | \begin{datadesc}{RUSAGE_CHILDREN} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 207 | Pass to \function{getrusage()} to request resource information for |
| 208 | child processes of the calling process. |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 209 | \end{datadesc} |
| 210 | |
| 211 | \begin{datadesc}{RUSAGE_BOTH} |
Fred Drake | 60ba447 | 1998-03-11 06:18:15 +0000 | [diff] [blame] | 212 | Pass to \function{getrusage()} to request resources consumed by both |
| 213 | the current process and child processes. May not be available on all |
Fred Drake | e907208 | 1997-12-06 07:25:41 +0000 | [diff] [blame] | 214 | systems. |
| 215 | \end{datadesc} |