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