/Doc/library/resource.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 238 lines · 154 code · 84 blank · 0 comment · 0 complexity · dcee890db433ccfd92a66c4dd9c7cd76 MD5 · raw file

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