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