util.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. """Module containing a memory memory manager which provides a sliding window on a number of memory mapped files"""
  2. import os
  3. import sys
  4. from mmap import mmap, ACCESS_READ
  5. from mmap import ALLOCATIONGRANULARITY
  6. __all__ = ["align_to_mmap", "is_64_bit",
  7. "MapWindow", "MapRegion", "MapRegionList", "ALLOCATIONGRANULARITY"]
  8. #{ Utilities
  9. def align_to_mmap(num, round_up):
  10. """
  11. Align the given integer number to the closest page offset, which usually is 4096 bytes.
  12. :param round_up: if True, the next higher multiple of page size is used, otherwise
  13. the lower page_size will be used (i.e. if True, 1 becomes 4096, otherwise it becomes 0)
  14. :return: num rounded to closest page"""
  15. res = (num // ALLOCATIONGRANULARITY) * ALLOCATIONGRANULARITY
  16. if round_up and (res != num):
  17. res += ALLOCATIONGRANULARITY
  18. # END handle size
  19. return res
  20. def is_64_bit():
  21. """:return: True if the system is 64 bit. Otherwise it can be assumed to be 32 bit"""
  22. return sys.maxsize > (1 << 32) - 1
  23. #}END utilities
  24. #{ Utility Classes
  25. class MapWindow:
  26. """Utility type which is used to snap windows towards each other, and to adjust their size"""
  27. __slots__ = (
  28. 'ofs', # offset into the file in bytes
  29. 'size' # size of the window in bytes
  30. )
  31. def __init__(self, offset, size):
  32. self.ofs = offset
  33. self.size = size
  34. def __repr__(self):
  35. return "MapWindow(%i, %i)" % (self.ofs, self.size)
  36. @classmethod
  37. def from_region(cls, region):
  38. """:return: new window from a region"""
  39. return cls(region._b, region.size())
  40. def ofs_end(self):
  41. return self.ofs + self.size
  42. def align(self):
  43. """Assures the previous window area is contained in the new one"""
  44. nofs = align_to_mmap(self.ofs, 0)
  45. self.size += self.ofs - nofs # keep size constant
  46. self.ofs = nofs
  47. self.size = align_to_mmap(self.size, 1)
  48. def extend_left_to(self, window, max_size):
  49. """Adjust the offset to start where the given window on our left ends if possible,
  50. but don't make yourself larger than max_size.
  51. The resize will assure that the new window still contains the old window area"""
  52. rofs = self.ofs - window.ofs_end()
  53. nsize = rofs + self.size
  54. rofs -= nsize - min(nsize, max_size)
  55. self.ofs = self.ofs - rofs
  56. self.size += rofs
  57. def extend_right_to(self, window, max_size):
  58. """Adjust the size to make our window end where the right window begins, but don't
  59. get larger than max_size"""
  60. self.size = min(self.size + (window.ofs - self.ofs_end()), max_size)
  61. class MapRegion:
  62. """Defines a mapped region of memory, aligned to pagesizes
  63. **Note:** deallocates used region automatically on destruction"""
  64. __slots__ = [
  65. '_b', # beginning of mapping
  66. '_mf', # mapped memory chunk (as returned by mmap)
  67. '_uc', # total amount of usages
  68. '_size', # cached size of our memory map
  69. '__weakref__'
  70. ]
  71. #{ Configuration
  72. #} END configuration
  73. def __init__(self, path_or_fd, ofs, size, flags=0):
  74. """Initialize a region, allocate the memory map
  75. :param path_or_fd: path to the file to map, or the opened file descriptor
  76. :param ofs: **aligned** offset into the file to be mapped
  77. :param size: if size is larger then the file on disk, the whole file will be
  78. allocated the the size automatically adjusted
  79. :param flags: additional flags to be given when opening the file.
  80. :raise Exception: if no memory can be allocated"""
  81. self._b = ofs
  82. self._size = 0
  83. self._uc = 0
  84. if isinstance(path_or_fd, int):
  85. fd = path_or_fd
  86. else:
  87. fd = os.open(path_or_fd, os.O_RDONLY | getattr(os, 'O_BINARY', 0) | flags)
  88. # END handle fd
  89. try:
  90. kwargs = dict(access=ACCESS_READ, offset=ofs)
  91. corrected_size = size
  92. sizeofs = ofs
  93. # have to correct size, otherwise (instead of the c version) it will
  94. # bark that the size is too large ... many extra file accesses because
  95. # if this ... argh !
  96. actual_size = min(os.fstat(fd).st_size - sizeofs, corrected_size)
  97. self._mf = mmap(fd, actual_size, **kwargs)
  98. # END handle memory mode
  99. self._size = len(self._mf)
  100. finally:
  101. if isinstance(path_or_fd, str):
  102. os.close(fd)
  103. # END only close it if we opened it
  104. # END close file handle
  105. # We assume the first one to use us keeps us around
  106. self.increment_client_count()
  107. def __repr__(self):
  108. return "MapRegion<%i, %i>" % (self._b, self.size())
  109. #{ Interface
  110. def buffer(self):
  111. """:return: a buffer containing the memory"""
  112. return self._mf
  113. def map(self):
  114. """:return: a memory map containing the memory"""
  115. return self._mf
  116. def ofs_begin(self):
  117. """:return: absolute byte offset to the first byte of the mapping"""
  118. return self._b
  119. def size(self):
  120. """:return: total size of the mapped region in bytes"""
  121. return self._size
  122. def ofs_end(self):
  123. """:return: Absolute offset to one byte beyond the mapping into the file"""
  124. return self._b + self._size
  125. def includes_ofs(self, ofs):
  126. """:return: True if the given offset can be read in our mapped region"""
  127. return self._b <= ofs < self._b + self._size
  128. def client_count(self):
  129. """:return: number of clients currently using this region"""
  130. return self._uc
  131. def increment_client_count(self, ofs = 1):
  132. """Adjust the usage count by the given positive or negative offset.
  133. If usage count equals 0, we will auto-release our resources
  134. :return: True if we released resources, False otherwise. In the latter case, we can still be used"""
  135. self._uc += ofs
  136. assert self._uc > -1, "Increments must match decrements, usage counter negative: %i" % self._uc
  137. if self.client_count() == 0:
  138. self.release()
  139. return True
  140. else:
  141. return False
  142. # end handle release
  143. def release(self):
  144. """Release all resources this instance might hold. Must only be called if there usage_count() is zero"""
  145. self._mf.close()
  146. #} END interface
  147. class MapRegionList(list):
  148. """List of MapRegion instances associating a path with a list of regions."""
  149. __slots__ = (
  150. '_path_or_fd', # path or file descriptor which is mapped by all our regions
  151. '_file_size' # total size of the file we map
  152. )
  153. def __new__(cls, path):
  154. return super().__new__(cls)
  155. def __init__(self, path_or_fd):
  156. self._path_or_fd = path_or_fd
  157. self._file_size = None
  158. def path_or_fd(self):
  159. """:return: path or file descriptor we are attached to"""
  160. return self._path_or_fd
  161. def file_size(self):
  162. """:return: size of file we manager"""
  163. if self._file_size is None:
  164. if isinstance(self._path_or_fd, str):
  165. self._file_size = os.stat(self._path_or_fd).st_size
  166. else:
  167. self._file_size = os.fstat(self._path_or_fd).st_size
  168. # END handle path type
  169. # END update file size
  170. return self._file_size
  171. #} END utility classes