|
1 | 1 | """Module containnig a memory memory manager which provides a sliding window on a number of memory mapped files""" |
2 | 2 |
|
3 | | -__all__ = [] |
| 3 | +__all__ = ["MappedMemoryManager"] |
| 4 | + |
| 5 | +import os |
| 6 | +import mmap |
| 7 | + |
| 8 | +from mmap import PAGESIZE |
| 9 | + |
| 10 | +#{ Utilities |
| 11 | + |
| 12 | +def align_to_page(num, round_up): |
| 13 | + """Align the given integer number to the closest page offset, which usually is 4096 bytes. |
| 14 | + :param round_up: if True, the next higher multiple of page size is used, otherwise |
| 15 | + the lower page_size will be used (i.e. if True, 1 becomes 4096, otherwise it becomes 0) |
| 16 | + :return: num rounded to closest page""" |
| 17 | + res = (num / PAGESIZE) * PAGESIZE; |
| 18 | + if round_up and (res != num): |
| 19 | + res += PAGESIZE; |
| 20 | + #END handle size |
| 21 | + return res; |
| 22 | + |
| 23 | +#}END utilities |
| 24 | + |
| 25 | +class Window(object): |
| 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 | + |
| 32 | + def __init__(self, offset, size): |
| 33 | + self.ofs = offset |
| 34 | + self.size = size |
| 35 | + |
| 36 | + def __repr__(self): |
| 37 | + return "Window(%i, %i)" % (self.ofs, self.size) |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def from_region(cls, region): |
| 41 | + """:return: new window from a region""" |
| 42 | + return cls(region.ofs_begin(), region.size()) |
| 43 | + |
| 44 | + def ofs_end(self): |
| 45 | + return self.ofs + self.size |
| 46 | + |
| 47 | + def align(self): |
| 48 | + self.ofs = align_to_page(self.ofs, 0) |
| 49 | + self.size = align_to_page(self.size, 1) |
| 50 | + |
| 51 | + def extend_left_to(self, window, max_size): |
| 52 | + """Adjust the offset to start where the given window on our left ends if possible, |
| 53 | + but don't make yourself larger than max_size. |
| 54 | + The resize will assure that the new window still contains the old window area""" |
| 55 | + rofs = self.ofs - window.ofs_end() |
| 56 | + nsize = rofs + self.size |
| 57 | + rofs -= nsize - min(nsize, max_size) |
| 58 | + self.ofs = self.ofs - rofs |
| 59 | + self.size += rofs |
| 60 | + |
| 61 | + def extend_right_to(self, window, max_size): |
| 62 | + """Adjust the size to make our window end where the right window begins, but don't |
| 63 | + get larger than max_size""" |
| 64 | + self.size = min(self.size + (window.ofs - self.ofs_end()), max_size) |
| 65 | + |
| 66 | + |
| 67 | +class Region(object): |
| 68 | + """Defines a mapped region of memory, aligned to pagesizes |
| 69 | + :note: deallocates used region automatically on destruction""" |
| 70 | + __slots__ = ( |
| 71 | + '_b' , # beginning of mapping |
| 72 | + '_mf', # mapped memory chunk (as returned by mmap) |
| 73 | + '_nc', # number of clients using this region |
| 74 | + '_uc' # total amount of usages |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | + def __init__(self, path, ofs, size): |
| 79 | + """Initialize a region, allocate the memory map |
| 80 | + :param path: path to the file to map |
| 81 | + :param ofs: **aligned** offset into the file to be mapped |
| 82 | + :param size: if size is larger then the file on disk, the whole file will be |
| 83 | + allocated the the size automatically adjusted |
| 84 | + :raise Exception: if no memory can be allocated""" |
| 85 | + self._b = ofs |
| 86 | + self._nc = 0 |
| 87 | + self._uc = 0 |
| 88 | + |
| 89 | + fd = os.open(path, os.O_RDONLY|getattr(os, 'O_BINARY', 0)) |
| 90 | + try: |
| 91 | + self._mf = mmap.mmap(fd, size, access=mmap.ACCESS_READ, offset=ofs) |
| 92 | + finally: |
| 93 | + os.close(fd) |
| 94 | + #END close file handle |
| 95 | + |
| 96 | + |
| 97 | +class MappedMemoryManager(object): |
| 98 | + """Maintains a list of ranges of mapped memory regions in one or more files and allows to easily |
| 99 | + obtain additional regions assuring there is no overlap. |
| 100 | + Once a certain memory limit is reached globally, or if there cannot be more open file handles |
| 101 | + which result from each mmap call, the least recently used, and currently unused mapped regions |
| 102 | + are unloaded automatically. |
| 103 | + |
| 104 | + :note: currently not thread-safe ! |
| 105 | + :note: in the current implementation, we will automatically unload windows if we either cannot |
| 106 | + create more memory maps (as the open file handles limit is hit) or if we have allocated more than |
| 107 | + a safe amount of memory already, which would possibly cause memory allocations to fail as our address |
| 108 | + space is full.""" |
| 109 | + |
| 110 | + __slots__ = tuple() |
| 111 | + |
0 commit comments