PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib_pypy/resource.py

https://bitbucket.org/dac_io/pypy
Python | 182 lines | 152 code | 27 blank | 3 comment | 20 complexity | 3cf0c6e27bb1fe76191b8b45ebbe9bbe MD5 | raw file
  1. import sys
  2. if sys.platform == 'win32':
  3. raise ImportError('resource module not available for win32')
  4. # load the platform-specific cache made by running resource.ctc.py
  5. from ctypes_config_cache._resource_cache import *
  6. from ctypes_support import standard_c_lib as libc
  7. from ctypes_support import get_errno
  8. from ctypes import Structure, c_int, c_long, byref, POINTER
  9. from errno import EINVAL, EPERM
  10. import _structseq
  11. try: from __pypy__ import builtinify
  12. except ImportError: builtinify = lambda f: f
  13. class error(Exception):
  14. pass
  15. # Read required libc functions
  16. _getrusage = libc.getrusage
  17. _getrlimit = libc.getrlimit
  18. _setrlimit = libc.setrlimit
  19. try:
  20. _getpagesize = libc.getpagesize
  21. _getpagesize.argtypes = ()
  22. _getpagesize.restype = c_int
  23. except AttributeError:
  24. from os import sysconf
  25. _getpagesize = None
  26. class timeval(Structure):
  27. _fields_ = (
  28. ("tv_sec", c_long),
  29. ("tv_usec", c_long),
  30. )
  31. def __str__(self):
  32. return "(%s, %s)" % (self.tv_sec, self.tv_usec)
  33. def __float__(self):
  34. return self.tv_sec + self.tv_usec/1000000.0
  35. class _struct_rusage(Structure):
  36. _fields_ = (
  37. ("ru_utime", timeval),
  38. ("ru_stime", timeval),
  39. ("ru_maxrss", c_long),
  40. ("ru_ixrss", c_long),
  41. ("ru_idrss", c_long),
  42. ("ru_isrss", c_long),
  43. ("ru_minflt", c_long),
  44. ("ru_majflt", c_long),
  45. ("ru_nswap", c_long),
  46. ("ru_inblock", c_long),
  47. ("ru_oublock", c_long),
  48. ("ru_msgsnd", c_long),
  49. ("ru_msgrcv", c_long),
  50. ("ru_nsignals", c_long),
  51. ("ru_nvcsw", c_long),
  52. ("ru_nivcsw", c_long),
  53. )
  54. _getrusage.argtypes = (c_int, POINTER(_struct_rusage))
  55. _getrusage.restype = c_int
  56. class struct_rusage:
  57. __metaclass__ = _structseq.structseqtype
  58. ru_utime = _structseq.structseqfield(0)
  59. ru_stime = _structseq.structseqfield(1)
  60. ru_maxrss = _structseq.structseqfield(2)
  61. ru_ixrss = _structseq.structseqfield(3)
  62. ru_idrss = _structseq.structseqfield(4)
  63. ru_isrss = _structseq.structseqfield(5)
  64. ru_minflt = _structseq.structseqfield(6)
  65. ru_majflt = _structseq.structseqfield(7)
  66. ru_nswap = _structseq.structseqfield(8)
  67. ru_inblock = _structseq.structseqfield(9)
  68. ru_oublock = _structseq.structseqfield(10)
  69. ru_msgsnd = _structseq.structseqfield(11)
  70. ru_msgrcv = _structseq.structseqfield(12)
  71. ru_nsignals = _structseq.structseqfield(13)
  72. ru_nvcsw = _structseq.structseqfield(14)
  73. ru_nivcsw = _structseq.structseqfield(15)
  74. @builtinify
  75. def rlimit_check_bounds(rlim_cur, rlim_max):
  76. if rlim_cur > rlim_t_max:
  77. raise ValueError("%d does not fit into rlim_t" % rlim_cur)
  78. if rlim_max > rlim_t_max:
  79. raise ValueError("%d does not fit into rlim_t" % rlim_max)
  80. class rlimit(Structure):
  81. _fields_ = (
  82. ("rlim_cur", rlim_t),
  83. ("rlim_max", rlim_t),
  84. )
  85. _getrlimit.argtypes = (c_int, POINTER(rlimit))
  86. _getrlimit.restype = c_int
  87. _setrlimit.argtypes = (c_int, POINTER(rlimit))
  88. _setrlimit.restype = c_int
  89. @builtinify
  90. def getrusage(who):
  91. ru = _struct_rusage()
  92. ret = _getrusage(who, byref(ru))
  93. if ret == -1:
  94. errno = get_errno()
  95. if errno == EINVAL:
  96. raise ValueError("invalid who parameter")
  97. raise error(errno)
  98. return struct_rusage((
  99. float(ru.ru_utime),
  100. float(ru.ru_stime),
  101. ru.ru_maxrss,
  102. ru.ru_ixrss,
  103. ru.ru_idrss,
  104. ru.ru_isrss,
  105. ru.ru_minflt,
  106. ru.ru_majflt,
  107. ru.ru_nswap,
  108. ru.ru_inblock,
  109. ru.ru_oublock,
  110. ru.ru_msgsnd,
  111. ru.ru_msgrcv,
  112. ru.ru_nsignals,
  113. ru.ru_nvcsw,
  114. ru.ru_nivcsw,
  115. ))
  116. @builtinify
  117. def getrlimit(resource):
  118. if not(0 <= resource < RLIM_NLIMITS):
  119. return ValueError("invalid resource specified")
  120. rlim = rlimit()
  121. ret = _getrlimit(resource, byref(rlim))
  122. if ret == -1:
  123. errno = get_errno()
  124. raise error(errno)
  125. return (rlim.rlim_cur, rlim.rlim_max)
  126. @builtinify
  127. def setrlimit(resource, rlim):
  128. if not(0 <= resource < RLIM_NLIMITS):
  129. return ValueError("invalid resource specified")
  130. rlimit_check_bounds(*rlim)
  131. rlim = rlimit(rlim[0], rlim[1])
  132. ret = _setrlimit(resource, byref(rlim))
  133. if ret == -1:
  134. errno = get_errno()
  135. if errno == EINVAL:
  136. return ValueError("current limit exceeds maximum limit")
  137. elif errno == EPERM:
  138. return ValueError("not allowed to raise maximum limit")
  139. else:
  140. raise error(errno)
  141. @builtinify
  142. def getpagesize():
  143. if _getpagesize:
  144. return _getpagesize()
  145. else:
  146. try:
  147. return sysconf("SC_PAGE_SIZE")
  148. except ValueError:
  149. # Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE
  150. return sysconf("SC_PAGESIZE")
  151. __all__ = ALL_CONSTANTS + (
  152. 'error', 'timeval', 'struct_rusage', 'rlimit',
  153. 'getrusage', 'getrlimit', 'setrlimit', 'getpagesize',
  154. )
  155. del ALL_CONSTANTS