Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`resource` --- Resource usage information |
| 3 | ============================================== |
| 4 | |
| 5 | .. module:: resource |
| 6 | :platform: Unix |
| 7 | :synopsis: An interface to provide resource usage information on the current process. |
| 8 | .. moduleauthor:: Jeremy Hylton <jeremy@alum.mit.edu> |
| 9 | .. sectionauthor:: Jeremy Hylton <jeremy@alum.mit.edu> |
| 10 | |
| 11 | |
| 12 | This module provides basic mechanisms for measuring and controlling system |
| 13 | resources utilized by a program. |
| 14 | |
| 15 | Symbolic constants are used to specify particular system resources and to |
| 16 | request usage information about either the current process or its children. |
| 17 | |
| 18 | A single exception is defined for errors: |
| 19 | |
| 20 | |
| 21 | .. exception:: error |
| 22 | |
| 23 | The functions described below may raise this error if the underlying system call |
| 24 | failures unexpectedly. |
| 25 | |
| 26 | |
| 27 | Resource Limits |
| 28 | --------------- |
| 29 | |
| 30 | Resources usage can be limited using the :func:`setrlimit` function described |
| 31 | below. Each resource is controlled by a pair of limits: a soft limit and a hard |
| 32 | limit. The soft limit is the current limit, and may be lowered or raised by a |
| 33 | process over time. The soft limit can never exceed the hard limit. The hard |
| 34 | limit can be lowered to any value greater than the soft limit, but not raised. |
| 35 | (Only processes with the effective UID of the super-user can raise a hard |
| 36 | limit.) |
| 37 | |
| 38 | The specific resources that can be limited are system dependent. They are |
| 39 | described in the :manpage:`getrlimit(2)` man page. The resources listed below |
| 40 | are supported when the underlying operating system supports them; resources |
| 41 | which cannot be checked or controlled by the operating system are not defined in |
| 42 | this module for those platforms. |
| 43 | |
| 44 | |
| 45 | .. function:: getrlimit(resource) |
| 46 | |
| 47 | Returns a tuple ``(soft, hard)`` with the current soft and hard limits of |
| 48 | *resource*. Raises :exc:`ValueError` if an invalid resource is specified, or |
| 49 | :exc:`error` if the underlying system call fails unexpectedly. |
| 50 | |
| 51 | |
| 52 | .. function:: setrlimit(resource, limits) |
| 53 | |
| 54 | Sets new limits of consumption of *resource*. The *limits* argument must be a |
| 55 | tuple ``(soft, hard)`` of two integers describing the new limits. A value of |
| 56 | ``-1`` can be used to specify the maximum possible upper limit. |
| 57 | |
| 58 | Raises :exc:`ValueError` if an invalid resource is specified, if the new soft |
| 59 | limit exceeds the hard limit, or if a process tries to raise its hard limit |
| 60 | (unless the process has an effective UID of super-user). Can also raise |
| 61 | :exc:`error` if the underlying system call fails. |
| 62 | |
| 63 | These symbols define resources whose consumption can be controlled using the |
| 64 | :func:`setrlimit` and :func:`getrlimit` functions described below. The values of |
| 65 | these symbols are exactly the constants used by C programs. |
| 66 | |
| 67 | The Unix man page for :manpage:`getrlimit(2)` lists the available resources. |
| 68 | Note that not all systems use the same symbol or same value to denote the same |
| 69 | resource. This module does not attempt to mask platform differences --- symbols |
| 70 | not defined for a platform will not be available from this module on that |
| 71 | platform. |
| 72 | |
| 73 | |
| 74 | .. data:: RLIMIT_CORE |
| 75 | |
| 76 | The maximum size (in bytes) of a core file that the current process can create. |
| 77 | This may result in the creation of a partial core file if a larger core would be |
| 78 | required to contain the entire process image. |
| 79 | |
| 80 | |
| 81 | .. data:: RLIMIT_CPU |
| 82 | |
| 83 | The maximum amount of processor time (in seconds) that a process can use. If |
| 84 | this limit is exceeded, a :const:`SIGXCPU` signal is sent to the process. (See |
| 85 | the :mod:`signal` module documentation for information about how to catch this |
| 86 | signal and do something useful, e.g. flush open files to disk.) |
| 87 | |
| 88 | |
| 89 | .. data:: RLIMIT_FSIZE |
| 90 | |
| 91 | The maximum size of a file which the process may create. This only affects the |
| 92 | stack of the main thread in a multi-threaded process. |
| 93 | |
| 94 | |
| 95 | .. data:: RLIMIT_DATA |
| 96 | |
| 97 | The maximum size (in bytes) of the process's heap. |
| 98 | |
| 99 | |
| 100 | .. data:: RLIMIT_STACK |
| 101 | |
| 102 | The maximum size (in bytes) of the call stack for the current process. |
| 103 | |
| 104 | |
| 105 | .. data:: RLIMIT_RSS |
| 106 | |
| 107 | The maximum resident set size that should be made available to the process. |
| 108 | |
| 109 | |
| 110 | .. data:: RLIMIT_NPROC |
| 111 | |
| 112 | The maximum number of processes the current process may create. |
| 113 | |
| 114 | |
| 115 | .. data:: RLIMIT_NOFILE |
| 116 | |
| 117 | The maximum number of open file descriptors for the current process. |
| 118 | |
| 119 | |
| 120 | .. data:: RLIMIT_OFILE |
| 121 | |
| 122 | The BSD name for :const:`RLIMIT_NOFILE`. |
| 123 | |
| 124 | |
| 125 | .. data:: RLIMIT_MEMLOCK |
| 126 | |
| 127 | The maximum address space which may be locked in memory. |
| 128 | |
| 129 | |
| 130 | .. data:: RLIMIT_VMEM |
| 131 | |
| 132 | The largest area of mapped memory which the process may occupy. |
| 133 | |
| 134 | |
| 135 | .. data:: RLIMIT_AS |
| 136 | |
| 137 | The maximum area (in bytes) of address space which may be taken by the process. |
| 138 | |
| 139 | |
| 140 | Resource Usage |
| 141 | -------------- |
| 142 | |
| 143 | These functions are used to retrieve resource usage information: |
| 144 | |
| 145 | |
| 146 | .. function:: getrusage(who) |
| 147 | |
| 148 | This function returns an object that describes the resources consumed by either |
| 149 | the current process or its children, as specified by the *who* parameter. The |
| 150 | *who* parameter should be specified using one of the :const:`RUSAGE_\*` |
| 151 | constants described below. |
| 152 | |
| 153 | The fields of the return value each describe how a particular system resource |
| 154 | has been used, e.g. amount of time spent running is user mode or number of times |
| 155 | the process was swapped out of main memory. Some values are dependent on the |
| 156 | clock tick internal, e.g. the amount of memory the process is using. |
| 157 | |
| 158 | For backward compatibility, the return value is also accessible as a tuple of 16 |
| 159 | elements. |
| 160 | |
| 161 | The fields :attr:`ru_utime` and :attr:`ru_stime` of the return value are |
| 162 | floating point values representing the amount of time spent executing in user |
| 163 | mode and the amount of time spent executing in system mode, respectively. The |
| 164 | remaining values are integers. Consult the :manpage:`getrusage(2)` man page for |
| 165 | detailed information about these values. A brief summary is presented here: |
| 166 | |
| 167 | +--------+---------------------+-------------------------------+ |
| 168 | | Index | Field | Resource | |
| 169 | +========+=====================+===============================+ |
| 170 | | ``0`` | :attr:`ru_utime` | time in user mode (float) | |
| 171 | +--------+---------------------+-------------------------------+ |
| 172 | | ``1`` | :attr:`ru_stime` | time in system mode (float) | |
| 173 | +--------+---------------------+-------------------------------+ |
| 174 | | ``2`` | :attr:`ru_maxrss` | maximum resident set size | |
| 175 | +--------+---------------------+-------------------------------+ |
| 176 | | ``3`` | :attr:`ru_ixrss` | shared memory size | |
| 177 | +--------+---------------------+-------------------------------+ |
| 178 | | ``4`` | :attr:`ru_idrss` | unshared memory size | |
| 179 | +--------+---------------------+-------------------------------+ |
| 180 | | ``5`` | :attr:`ru_isrss` | unshared stack size | |
| 181 | +--------+---------------------+-------------------------------+ |
| 182 | | ``6`` | :attr:`ru_minflt` | page faults not requiring I/O | |
| 183 | +--------+---------------------+-------------------------------+ |
| 184 | | ``7`` | :attr:`ru_majflt` | page faults requiring I/O | |
| 185 | +--------+---------------------+-------------------------------+ |
| 186 | | ``8`` | :attr:`ru_nswap` | number of swap outs | |
| 187 | +--------+---------------------+-------------------------------+ |
| 188 | | ``9`` | :attr:`ru_inblock` | block input operations | |
| 189 | +--------+---------------------+-------------------------------+ |
| 190 | | ``10`` | :attr:`ru_oublock` | block output operations | |
| 191 | +--------+---------------------+-------------------------------+ |
| 192 | | ``11`` | :attr:`ru_msgsnd` | messages sent | |
| 193 | +--------+---------------------+-------------------------------+ |
| 194 | | ``12`` | :attr:`ru_msgrcv` | messages received | |
| 195 | +--------+---------------------+-------------------------------+ |
| 196 | | ``13`` | :attr:`ru_nsignals` | signals received | |
| 197 | +--------+---------------------+-------------------------------+ |
| 198 | | ``14`` | :attr:`ru_nvcsw` | voluntary context switches | |
| 199 | +--------+---------------------+-------------------------------+ |
| 200 | | ``15`` | :attr:`ru_nivcsw` | involuntary context switches | |
| 201 | +--------+---------------------+-------------------------------+ |
| 202 | |
| 203 | This function will raise a :exc:`ValueError` if an invalid *who* parameter is |
| 204 | specified. It may also raise :exc:`error` exception in unusual circumstances. |
| 205 | |
| 206 | .. versionchanged:: 2.3 |
| 207 | Added access to values as attributes of the returned object. |
| 208 | |
| 209 | |
| 210 | .. function:: getpagesize() |
| 211 | |
| 212 | Returns the number of bytes in a system page. (This need not be the same as the |
| 213 | hardware page size.) This function is useful for determining the number of bytes |
| 214 | of memory a process is using. The third element of the tuple returned by |
| 215 | :func:`getrusage` describes memory usage in pages; multiplying by page size |
| 216 | produces number of bytes. |
| 217 | |
| 218 | The following :const:`RUSAGE_\*` symbols are passed to the :func:`getrusage` |
| 219 | function to specify which processes information should be provided for. |
| 220 | |
| 221 | |
| 222 | .. data:: RUSAGE_SELF |
| 223 | |
| 224 | :const:`RUSAGE_SELF` should be used to request information pertaining only to |
| 225 | the process itself. |
| 226 | |
| 227 | |
| 228 | .. data:: RUSAGE_CHILDREN |
| 229 | |
| 230 | Pass to :func:`getrusage` to request resource information for child processes of |
| 231 | the calling process. |
| 232 | |
| 233 | |
| 234 | .. data:: RUSAGE_BOTH |
| 235 | |
| 236 | Pass to :func:`getrusage` to request resources consumed by both the current |
| 237 | process and child processes. May not be available on all systems. |
| 238 | |