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. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. moduleauthor:: Jeremy Hylton <jeremy@alum.mit.edu> |
| 9 | .. sectionauthor:: Jeremy Hylton <jeremy@alum.mit.edu> |
| 10 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
| 13 | This module provides basic mechanisms for measuring and controlling system |
| 14 | resources utilized by a program. |
| 15 | |
| 16 | Symbolic constants are used to specify particular system resources and to |
| 17 | request usage information about either the current process or its children. |
| 18 | |
Benjamin Peterson | 2122cf7 | 2011-12-10 17:50:22 -0500 | [diff] [blame] | 19 | An :exc:`OSError` is raised on syscall failure. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | .. exception:: error |
| 23 | |
Benjamin Peterson | 2122cf7 | 2011-12-10 17:50:22 -0500 | [diff] [blame] | 24 | A deprecated alias of :exc:`OSError`. |
| 25 | |
| 26 | .. versionchanged:: 3.3 |
| 27 | Following :pep:`3151`, this class was made an alias of :exc:`OSError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | Resource Limits |
| 31 | --------------- |
| 32 | |
| 33 | Resources usage can be limited using the :func:`setrlimit` function described |
| 34 | below. Each resource is controlled by a pair of limits: a soft limit and a hard |
| 35 | limit. The soft limit is the current limit, and may be lowered or raised by a |
| 36 | process over time. The soft limit can never exceed the hard limit. The hard |
| 37 | limit can be lowered to any value greater than the soft limit, but not raised. |
| 38 | (Only processes with the effective UID of the super-user can raise a hard |
| 39 | limit.) |
| 40 | |
| 41 | The specific resources that can be limited are system dependent. They are |
| 42 | described in the :manpage:`getrlimit(2)` man page. The resources listed below |
| 43 | are supported when the underlying operating system supports them; resources |
| 44 | which cannot be checked or controlled by the operating system are not defined in |
| 45 | this module for those platforms. |
| 46 | |
| 47 | |
R David Murray | bdf940d | 2013-04-20 13:37:34 -0400 | [diff] [blame] | 48 | .. data:: RLIM_INFINITY |
| 49 | |
Senthil Kumaran | b4760ef | 2015-06-14 17:35:37 -0700 | [diff] [blame] | 50 | Constant used to represent the limit for an unlimited resource. |
R David Murray | bdf940d | 2013-04-20 13:37:34 -0400 | [diff] [blame] | 51 | |
| 52 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | .. function:: getrlimit(resource) |
| 54 | |
| 55 | Returns a tuple ``(soft, hard)`` with the current soft and hard limits of |
| 56 | *resource*. Raises :exc:`ValueError` if an invalid resource is specified, or |
| 57 | :exc:`error` if the underlying system call fails unexpectedly. |
| 58 | |
| 59 | |
| 60 | .. function:: setrlimit(resource, limits) |
| 61 | |
| 62 | Sets new limits of consumption of *resource*. The *limits* argument must be a |
| 63 | 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] | 64 | :data:`~resource.RLIM_INFINITY` can be used to request a limit that is |
| 65 | unlimited. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
| 67 | 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] | 68 | limit exceeds the hard limit, or if a process tries to raise its hard limit. |
| 69 | Specifying a limit of :data:`~resource.RLIM_INFINITY` when the hard or |
| 70 | system limit for that resource is not unlimited will result in a |
| 71 | :exc:`ValueError`. A process with the effective UID of super-user can |
| 72 | request any valid limit value, including unlimited, but :exc:`ValueError` |
| 73 | will still be raised if the requested limit exceeds the system imposed |
| 74 | limit. |
| 75 | |
| 76 | ``setrlimit`` may also raise :exc:`error` if the underlying system call |
| 77 | fails. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
Lihua Zhao | 693c104 | 2019-04-17 23:41:33 +0800 | [diff] [blame] | 79 | VxWorks only supports setting :data:`RLIMIT_NOFILE`. |
| 80 | |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 81 | .. function:: prlimit(pid, resource[, limits]) |
| 82 | |
| 83 | Combines :func:`setrlimit` and :func:`getrlimit` in one function and |
| 84 | supports to get and set the resources limits of an arbitrary process. If |
| 85 | *pid* is 0, then the call applies to the current process. *resource* and |
| 86 | *limits* have the same meaning as in :func:`setrlimit`, except that |
| 87 | *limits* is optional. |
| 88 | |
| 89 | When *limits* is not given the function returns the *resource* limit of the |
| 90 | process *pid*. When *limits* is given the *resource* limit of the process is |
| 91 | set and the former resource limit is returned. |
| 92 | |
| 93 | Raises :exc:`ProcessLookupError` when *pid* can't be found and |
| 94 | :exc:`PermissionError` when the user doesn't have ``CAP_SYS_RESOURCE`` for |
| 95 | the process. |
| 96 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 97 | .. availability:: Linux 2.6.36 or later with glibc 2.13 or later. |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 98 | |
| 99 | .. versionadded:: 3.4 |
| 100 | |
| 101 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | These symbols define resources whose consumption can be controlled using the |
| 103 | :func:`setrlimit` and :func:`getrlimit` functions described below. The values of |
| 104 | these symbols are exactly the constants used by C programs. |
| 105 | |
| 106 | The Unix man page for :manpage:`getrlimit(2)` lists the available resources. |
| 107 | Note that not all systems use the same symbol or same value to denote the same |
| 108 | resource. This module does not attempt to mask platform differences --- symbols |
| 109 | not defined for a platform will not be available from this module on that |
| 110 | platform. |
| 111 | |
| 112 | |
| 113 | .. data:: RLIMIT_CORE |
| 114 | |
| 115 | The maximum size (in bytes) of a core file that the current process can create. |
| 116 | This may result in the creation of a partial core file if a larger core would be |
| 117 | required to contain the entire process image. |
| 118 | |
| 119 | |
| 120 | .. data:: RLIMIT_CPU |
| 121 | |
| 122 | The maximum amount of processor time (in seconds) that a process can use. If |
| 123 | this limit is exceeded, a :const:`SIGXCPU` signal is sent to the process. (See |
| 124 | the :mod:`signal` module documentation for information about how to catch this |
| 125 | signal and do something useful, e.g. flush open files to disk.) |
| 126 | |
| 127 | |
| 128 | .. data:: RLIMIT_FSIZE |
| 129 | |
Zachary Ware | 48e3f98 | 2016-07-19 16:41:20 -0500 | [diff] [blame] | 130 | The maximum size of a file which the process may create. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | .. data:: RLIMIT_DATA |
| 134 | |
| 135 | The maximum size (in bytes) of the process's heap. |
| 136 | |
| 137 | |
| 138 | .. data:: RLIMIT_STACK |
| 139 | |
Zachary Ware | 48e3f98 | 2016-07-19 16:41:20 -0500 | [diff] [blame] | 140 | The maximum size (in bytes) of the call stack for the current process. This only |
| 141 | affects the stack of the main thread in a multi-threaded process. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | .. data:: RLIMIT_RSS |
| 145 | |
| 146 | The maximum resident set size that should be made available to the process. |
| 147 | |
| 148 | |
| 149 | .. data:: RLIMIT_NPROC |
| 150 | |
| 151 | The maximum number of processes the current process may create. |
| 152 | |
| 153 | |
| 154 | .. data:: RLIMIT_NOFILE |
| 155 | |
| 156 | The maximum number of open file descriptors for the current process. |
| 157 | |
| 158 | |
| 159 | .. data:: RLIMIT_OFILE |
| 160 | |
| 161 | The BSD name for :const:`RLIMIT_NOFILE`. |
| 162 | |
| 163 | |
| 164 | .. data:: RLIMIT_MEMLOCK |
| 165 | |
| 166 | The maximum address space which may be locked in memory. |
| 167 | |
| 168 | |
| 169 | .. data:: RLIMIT_VMEM |
| 170 | |
| 171 | The largest area of mapped memory which the process may occupy. |
| 172 | |
| 173 | |
| 174 | .. data:: RLIMIT_AS |
| 175 | |
| 176 | The maximum area (in bytes) of address space which may be taken by the process. |
| 177 | |
| 178 | |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 179 | .. data:: RLIMIT_MSGQUEUE |
| 180 | |
| 181 | The number of bytes that can be allocated for POSIX message queues. |
| 182 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 183 | .. availability:: Linux 2.6.8 or later. |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 184 | |
| 185 | .. versionadded:: 3.4 |
| 186 | |
| 187 | |
| 188 | .. data:: RLIMIT_NICE |
| 189 | |
| 190 | The ceiling for the process's nice level (calculated as 20 - rlim_cur). |
| 191 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 192 | .. availability:: Linux 2.6.12 or later. |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 193 | |
| 194 | .. versionadded:: 3.4 |
| 195 | |
| 196 | |
| 197 | .. data:: RLIMIT_RTPRIO |
| 198 | |
| 199 | The ceiling of the real-time priority. |
| 200 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 201 | .. availability:: Linux 2.6.12 or later. |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 202 | |
| 203 | .. versionadded:: 3.4 |
| 204 | |
| 205 | |
| 206 | .. data:: RLIMIT_RTTIME |
| 207 | |
| 208 | The time limit (in microseconds) on CPU time that a process can spend |
| 209 | under real-time scheduling without making a blocking syscall. |
| 210 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 211 | .. availability:: Linux 2.6.25 or later. |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 212 | |
| 213 | .. versionadded:: 3.4 |
| 214 | |
| 215 | |
| 216 | .. data:: RLIMIT_SIGPENDING |
| 217 | |
| 218 | The number of signals which the process may queue. |
| 219 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 220 | .. availability:: Linux 2.6.8 or later. |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 221 | |
| 222 | .. versionadded:: 3.4 |
| 223 | |
Christian Heimes | 5bb414d | 2013-12-08 14:35:55 +0100 | [diff] [blame] | 224 | .. data:: RLIMIT_SBSIZE |
| 225 | |
| 226 | The maximum size (in bytes) of socket buffer usage for this user. |
| 227 | This limits the amount of network memory, and hence the amount of mbufs, |
| 228 | that this user may hold at any time. |
| 229 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 230 | .. availability:: FreeBSD 9 or later. |
Christian Heimes | 5bb414d | 2013-12-08 14:35:55 +0100 | [diff] [blame] | 231 | |
| 232 | .. versionadded:: 3.4 |
| 233 | |
| 234 | .. data:: RLIMIT_SWAP |
| 235 | |
| 236 | The maximum size (in bytes) of the swap space that may be reserved or |
| 237 | used by all of this user id's processes. |
| 238 | This limit is enforced only if bit 1 of the vm.overcommit sysctl is set. |
| 239 | Please see :manpage:`tuning(7)` for a complete description of this sysctl. |
| 240 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 241 | .. availability:: FreeBSD 9 or later. |
Christian Heimes | 5bb414d | 2013-12-08 14:35:55 +0100 | [diff] [blame] | 242 | |
| 243 | .. versionadded:: 3.4 |
| 244 | |
| 245 | .. data:: RLIMIT_NPTS |
| 246 | |
| 247 | The maximum number of pseudo-terminals created by this user id. |
| 248 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 249 | .. availability:: FreeBSD 9 or later. |
Christian Heimes | 5bb414d | 2013-12-08 14:35:55 +0100 | [diff] [blame] | 250 | |
| 251 | .. versionadded:: 3.4 |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 252 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 253 | Resource Usage |
| 254 | -------------- |
| 255 | |
| 256 | These functions are used to retrieve resource usage information: |
| 257 | |
| 258 | |
| 259 | .. function:: getrusage(who) |
| 260 | |
| 261 | This function returns an object that describes the resources consumed by either |
| 262 | the current process or its children, as specified by the *who* parameter. The |
| 263 | *who* parameter should be specified using one of the :const:`RUSAGE_\*` |
| 264 | constants described below. |
| 265 | |
Beomsoo Kim | b912f93 | 2018-12-17 04:34:08 +0900 | [diff] [blame] | 266 | A simple example:: |
| 267 | |
| 268 | from resource import * |
| 269 | import time |
| 270 | |
| 271 | # a non CPU-bound task |
| 272 | time.sleep(3) |
| 273 | print(getrusage(RUSAGE_SELF)) |
| 274 | |
| 275 | # a CPU-bound task |
| 276 | for i in range(10 ** 8): |
| 277 | _ = 1 + 1 |
| 278 | print(getrusage(RUSAGE_SELF)) |
| 279 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 | The fields of the return value each describe how a particular system resource |
| 281 | has been used, e.g. amount of time spent running is user mode or number of times |
| 282 | the process was swapped out of main memory. Some values are dependent on the |
| 283 | clock tick internal, e.g. the amount of memory the process is using. |
| 284 | |
| 285 | For backward compatibility, the return value is also accessible as a tuple of 16 |
| 286 | elements. |
| 287 | |
| 288 | The fields :attr:`ru_utime` and :attr:`ru_stime` of the return value are |
| 289 | floating point values representing the amount of time spent executing in user |
| 290 | mode and the amount of time spent executing in system mode, respectively. The |
| 291 | remaining values are integers. Consult the :manpage:`getrusage(2)` man page for |
| 292 | detailed information about these values. A brief summary is presented here: |
| 293 | |
Beomsoo Kim | b912f93 | 2018-12-17 04:34:08 +0900 | [diff] [blame] | 294 | +--------+---------------------+---------------------------------------+ |
| 295 | | Index | Field | Resource | |
| 296 | +========+=====================+=======================================+ |
| 297 | | ``0`` | :attr:`ru_utime` | time in user mode (float seconds) | |
| 298 | +--------+---------------------+---------------------------------------+ |
| 299 | | ``1`` | :attr:`ru_stime` | time in system mode (float seconds) | |
| 300 | +--------+---------------------+---------------------------------------+ |
| 301 | | ``2`` | :attr:`ru_maxrss` | maximum resident set size | |
| 302 | +--------+---------------------+---------------------------------------+ |
| 303 | | ``3`` | :attr:`ru_ixrss` | shared memory size | |
| 304 | +--------+---------------------+---------------------------------------+ |
| 305 | | ``4`` | :attr:`ru_idrss` | unshared memory size | |
| 306 | +--------+---------------------+---------------------------------------+ |
| 307 | | ``5`` | :attr:`ru_isrss` | unshared stack size | |
| 308 | +--------+---------------------+---------------------------------------+ |
| 309 | | ``6`` | :attr:`ru_minflt` | page faults not requiring I/O | |
| 310 | +--------+---------------------+---------------------------------------+ |
| 311 | | ``7`` | :attr:`ru_majflt` | page faults requiring I/O | |
| 312 | +--------+---------------------+---------------------------------------+ |
| 313 | | ``8`` | :attr:`ru_nswap` | number of swap outs | |
| 314 | +--------+---------------------+---------------------------------------+ |
| 315 | | ``9`` | :attr:`ru_inblock` | block input operations | |
| 316 | +--------+---------------------+---------------------------------------+ |
| 317 | | ``10`` | :attr:`ru_oublock` | block output operations | |
| 318 | +--------+---------------------+---------------------------------------+ |
| 319 | | ``11`` | :attr:`ru_msgsnd` | messages sent | |
| 320 | +--------+---------------------+---------------------------------------+ |
| 321 | | ``12`` | :attr:`ru_msgrcv` | messages received | |
| 322 | +--------+---------------------+---------------------------------------+ |
| 323 | | ``13`` | :attr:`ru_nsignals` | signals received | |
| 324 | +--------+---------------------+---------------------------------------+ |
| 325 | | ``14`` | :attr:`ru_nvcsw` | voluntary context switches | |
| 326 | +--------+---------------------+---------------------------------------+ |
| 327 | | ``15`` | :attr:`ru_nivcsw` | involuntary context switches | |
| 328 | +--------+---------------------+---------------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 329 | |
| 330 | This function will raise a :exc:`ValueError` if an invalid *who* parameter is |
| 331 | specified. It may also raise :exc:`error` exception in unusual circumstances. |
| 332 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 333 | |
| 334 | .. function:: getpagesize() |
| 335 | |
| 336 | Returns the number of bytes in a system page. (This need not be the same as the |
Martin Panter | f8f66eb | 2015-11-17 22:13:47 +0000 | [diff] [blame] | 337 | hardware page size.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | |
| 339 | The following :const:`RUSAGE_\*` symbols are passed to the :func:`getrusage` |
| 340 | function to specify which processes information should be provided for. |
| 341 | |
| 342 | |
| 343 | .. data:: RUSAGE_SELF |
| 344 | |
Antoine Pitrou | b6d4ee5 | 2010-11-17 16:19:35 +0000 | [diff] [blame] | 345 | Pass to :func:`getrusage` to request resources consumed by the calling |
| 346 | 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] | 347 | |
| 348 | |
| 349 | .. data:: RUSAGE_CHILDREN |
| 350 | |
Antoine Pitrou | b6d4ee5 | 2010-11-17 16:19:35 +0000 | [diff] [blame] | 351 | Pass to :func:`getrusage` to request resources consumed by child processes |
| 352 | of the calling process which have been terminated and waited for. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 353 | |
| 354 | |
| 355 | .. data:: RUSAGE_BOTH |
| 356 | |
| 357 | Pass to :func:`getrusage` to request resources consumed by both the current |
| 358 | process and child processes. May not be available on all systems. |
| 359 | |
Antoine Pitrou | b6d4ee5 | 2010-11-17 16:19:35 +0000 | [diff] [blame] | 360 | |
| 361 | .. data:: RUSAGE_THREAD |
| 362 | |
| 363 | Pass to :func:`getrusage` to request resources consumed by the current |
| 364 | thread. May not be available on all systems. |
| 365 | |
| 366 | .. versionadded:: 3.2 |