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