jpayne@69: # jpayne@69: # Module which supports allocation of memory from an mmap jpayne@69: # jpayne@69: # multiprocessing/heap.py jpayne@69: # jpayne@69: # Copyright (c) 2006-2008, R Oudkerk jpayne@69: # Licensed to PSF under a Contributor Agreement. jpayne@69: # jpayne@69: jpayne@69: import bisect jpayne@69: from collections import defaultdict jpayne@69: import mmap jpayne@69: import os jpayne@69: import sys jpayne@69: import tempfile jpayne@69: import threading jpayne@69: jpayne@69: from .context import reduction, assert_spawning jpayne@69: from . import util jpayne@69: jpayne@69: __all__ = ['BufferWrapper'] jpayne@69: jpayne@69: # jpayne@69: # Inheritable class which wraps an mmap, and from which blocks can be allocated jpayne@69: # jpayne@69: jpayne@69: if sys.platform == 'win32': jpayne@69: jpayne@69: import _winapi jpayne@69: jpayne@69: class Arena(object): jpayne@69: """ jpayne@69: A shared memory area backed by anonymous memory (Windows). jpayne@69: """ jpayne@69: jpayne@69: _rand = tempfile._RandomNameSequence() jpayne@69: jpayne@69: def __init__(self, size): jpayne@69: self.size = size jpayne@69: for i in range(100): jpayne@69: name = 'pym-%d-%s' % (os.getpid(), next(self._rand)) jpayne@69: buf = mmap.mmap(-1, size, tagname=name) jpayne@69: if _winapi.GetLastError() == 0: jpayne@69: break jpayne@69: # We have reopened a preexisting mmap. jpayne@69: buf.close() jpayne@69: else: jpayne@69: raise FileExistsError('Cannot find name for new mmap') jpayne@69: self.name = name jpayne@69: self.buffer = buf jpayne@69: self._state = (self.size, self.name) jpayne@69: jpayne@69: def __getstate__(self): jpayne@69: assert_spawning(self) jpayne@69: return self._state jpayne@69: jpayne@69: def __setstate__(self, state): jpayne@69: self.size, self.name = self._state = state jpayne@69: # Reopen existing mmap jpayne@69: self.buffer = mmap.mmap(-1, self.size, tagname=self.name) jpayne@69: # XXX Temporarily preventing buildbot failures while determining jpayne@69: # XXX the correct long-term fix. See issue 23060 jpayne@69: #assert _winapi.GetLastError() == _winapi.ERROR_ALREADY_EXISTS jpayne@69: jpayne@69: else: jpayne@69: jpayne@69: class Arena(object): jpayne@69: """ jpayne@69: A shared memory area backed by a temporary file (POSIX). jpayne@69: """ jpayne@69: jpayne@69: if sys.platform == 'linux': jpayne@69: _dir_candidates = ['/dev/shm'] jpayne@69: else: jpayne@69: _dir_candidates = [] jpayne@69: jpayne@69: def __init__(self, size, fd=-1): jpayne@69: self.size = size jpayne@69: self.fd = fd jpayne@69: if fd == -1: jpayne@69: # Arena is created anew (if fd != -1, it means we're coming jpayne@69: # from rebuild_arena() below) jpayne@69: self.fd, name = tempfile.mkstemp( jpayne@69: prefix='pym-%d-'%os.getpid(), jpayne@69: dir=self._choose_dir(size)) jpayne@69: os.unlink(name) jpayne@69: util.Finalize(self, os.close, (self.fd,)) jpayne@69: os.ftruncate(self.fd, size) jpayne@69: self.buffer = mmap.mmap(self.fd, self.size) jpayne@69: jpayne@69: def _choose_dir(self, size): jpayne@69: # Choose a non-storage backed directory if possible, jpayne@69: # to improve performance jpayne@69: for d in self._dir_candidates: jpayne@69: st = os.statvfs(d) jpayne@69: if st.f_bavail * st.f_frsize >= size: # enough free space? jpayne@69: return d jpayne@69: return util.get_temp_dir() jpayne@69: jpayne@69: def reduce_arena(a): jpayne@69: if a.fd == -1: jpayne@69: raise ValueError('Arena is unpicklable because ' jpayne@69: 'forking was enabled when it was created') jpayne@69: return rebuild_arena, (a.size, reduction.DupFd(a.fd)) jpayne@69: jpayne@69: def rebuild_arena(size, dupfd): jpayne@69: return Arena(size, dupfd.detach()) jpayne@69: jpayne@69: reduction.register(Arena, reduce_arena) jpayne@69: jpayne@69: # jpayne@69: # Class allowing allocation of chunks of memory from arenas jpayne@69: # jpayne@69: jpayne@69: class Heap(object): jpayne@69: jpayne@69: # Minimum malloc() alignment jpayne@69: _alignment = 8 jpayne@69: jpayne@69: _DISCARD_FREE_SPACE_LARGER_THAN = 4 * 1024 ** 2 # 4 MB jpayne@69: _DOUBLE_ARENA_SIZE_UNTIL = 4 * 1024 ** 2 jpayne@69: jpayne@69: def __init__(self, size=mmap.PAGESIZE): jpayne@69: self._lastpid = os.getpid() jpayne@69: self._lock = threading.Lock() jpayne@69: # Current arena allocation size jpayne@69: self._size = size jpayne@69: # A sorted list of available block sizes in arenas jpayne@69: self._lengths = [] jpayne@69: jpayne@69: # Free block management: jpayne@69: # - map each block size to a list of `(Arena, start, stop)` blocks jpayne@69: self._len_to_seq = {} jpayne@69: # - map `(Arena, start)` tuple to the `(Arena, start, stop)` block jpayne@69: # starting at that offset jpayne@69: self._start_to_block = {} jpayne@69: # - map `(Arena, stop)` tuple to the `(Arena, start, stop)` block jpayne@69: # ending at that offset jpayne@69: self._stop_to_block = {} jpayne@69: jpayne@69: # Map arenas to their `(Arena, start, stop)` blocks in use jpayne@69: self._allocated_blocks = defaultdict(set) jpayne@69: self._arenas = [] jpayne@69: jpayne@69: # List of pending blocks to free - see comment in free() below jpayne@69: self._pending_free_blocks = [] jpayne@69: jpayne@69: # Statistics jpayne@69: self._n_mallocs = 0 jpayne@69: self._n_frees = 0 jpayne@69: jpayne@69: @staticmethod jpayne@69: def _roundup(n, alignment): jpayne@69: # alignment must be a power of 2 jpayne@69: mask = alignment - 1 jpayne@69: return (n + mask) & ~mask jpayne@69: jpayne@69: def _new_arena(self, size): jpayne@69: # Create a new arena with at least the given *size* jpayne@69: length = self._roundup(max(self._size, size), mmap.PAGESIZE) jpayne@69: # We carve larger and larger arenas, for efficiency, until we jpayne@69: # reach a large-ish size (roughly L3 cache-sized) jpayne@69: if self._size < self._DOUBLE_ARENA_SIZE_UNTIL: jpayne@69: self._size *= 2 jpayne@69: util.info('allocating a new mmap of length %d', length) jpayne@69: arena = Arena(length) jpayne@69: self._arenas.append(arena) jpayne@69: return (arena, 0, length) jpayne@69: jpayne@69: def _discard_arena(self, arena): jpayne@69: # Possibly delete the given (unused) arena jpayne@69: length = arena.size jpayne@69: # Reusing an existing arena is faster than creating a new one, so jpayne@69: # we only reclaim space if it's large enough. jpayne@69: if length < self._DISCARD_FREE_SPACE_LARGER_THAN: jpayne@69: return jpayne@69: blocks = self._allocated_blocks.pop(arena) jpayne@69: assert not blocks jpayne@69: del self._start_to_block[(arena, 0)] jpayne@69: del self._stop_to_block[(arena, length)] jpayne@69: self._arenas.remove(arena) jpayne@69: seq = self._len_to_seq[length] jpayne@69: seq.remove((arena, 0, length)) jpayne@69: if not seq: jpayne@69: del self._len_to_seq[length] jpayne@69: self._lengths.remove(length) jpayne@69: jpayne@69: def _malloc(self, size): jpayne@69: # returns a large enough block -- it might be much larger jpayne@69: i = bisect.bisect_left(self._lengths, size) jpayne@69: if i == len(self._lengths): jpayne@69: return self._new_arena(size) jpayne@69: else: jpayne@69: length = self._lengths[i] jpayne@69: seq = self._len_to_seq[length] jpayne@69: block = seq.pop() jpayne@69: if not seq: jpayne@69: del self._len_to_seq[length], self._lengths[i] jpayne@69: jpayne@69: (arena, start, stop) = block jpayne@69: del self._start_to_block[(arena, start)] jpayne@69: del self._stop_to_block[(arena, stop)] jpayne@69: return block jpayne@69: jpayne@69: def _add_free_block(self, block): jpayne@69: # make block available and try to merge with its neighbours in the arena jpayne@69: (arena, start, stop) = block jpayne@69: jpayne@69: try: jpayne@69: prev_block = self._stop_to_block[(arena, start)] jpayne@69: except KeyError: jpayne@69: pass jpayne@69: else: jpayne@69: start, _ = self._absorb(prev_block) jpayne@69: jpayne@69: try: jpayne@69: next_block = self._start_to_block[(arena, stop)] jpayne@69: except KeyError: jpayne@69: pass jpayne@69: else: jpayne@69: _, stop = self._absorb(next_block) jpayne@69: jpayne@69: block = (arena, start, stop) jpayne@69: length = stop - start jpayne@69: jpayne@69: try: jpayne@69: self._len_to_seq[length].append(block) jpayne@69: except KeyError: jpayne@69: self._len_to_seq[length] = [block] jpayne@69: bisect.insort(self._lengths, length) jpayne@69: jpayne@69: self._start_to_block[(arena, start)] = block jpayne@69: self._stop_to_block[(arena, stop)] = block jpayne@69: jpayne@69: def _absorb(self, block): jpayne@69: # deregister this block so it can be merged with a neighbour jpayne@69: (arena, start, stop) = block jpayne@69: del self._start_to_block[(arena, start)] jpayne@69: del self._stop_to_block[(arena, stop)] jpayne@69: jpayne@69: length = stop - start jpayne@69: seq = self._len_to_seq[length] jpayne@69: seq.remove(block) jpayne@69: if not seq: jpayne@69: del self._len_to_seq[length] jpayne@69: self._lengths.remove(length) jpayne@69: jpayne@69: return start, stop jpayne@69: jpayne@69: def _remove_allocated_block(self, block): jpayne@69: arena, start, stop = block jpayne@69: blocks = self._allocated_blocks[arena] jpayne@69: blocks.remove((start, stop)) jpayne@69: if not blocks: jpayne@69: # Arena is entirely free, discard it from this process jpayne@69: self._discard_arena(arena) jpayne@69: jpayne@69: def _free_pending_blocks(self): jpayne@69: # Free all the blocks in the pending list - called with the lock held. jpayne@69: while True: jpayne@69: try: jpayne@69: block = self._pending_free_blocks.pop() jpayne@69: except IndexError: jpayne@69: break jpayne@69: self._add_free_block(block) jpayne@69: self._remove_allocated_block(block) jpayne@69: jpayne@69: def free(self, block): jpayne@69: # free a block returned by malloc() jpayne@69: # Since free() can be called asynchronously by the GC, it could happen jpayne@69: # that it's called while self._lock is held: in that case, jpayne@69: # self._lock.acquire() would deadlock (issue #12352). To avoid that, a jpayne@69: # trylock is used instead, and if the lock can't be acquired jpayne@69: # immediately, the block is added to a list of blocks to be freed jpayne@69: # synchronously sometimes later from malloc() or free(), by calling jpayne@69: # _free_pending_blocks() (appending and retrieving from a list is not jpayne@69: # strictly thread-safe but under CPython it's atomic thanks to the GIL). jpayne@69: if os.getpid() != self._lastpid: jpayne@69: raise ValueError( jpayne@69: "My pid ({0:n}) is not last pid {1:n}".format( jpayne@69: os.getpid(),self._lastpid)) jpayne@69: if not self._lock.acquire(False): jpayne@69: # can't acquire the lock right now, add the block to the list of jpayne@69: # pending blocks to free jpayne@69: self._pending_free_blocks.append(block) jpayne@69: else: jpayne@69: # we hold the lock jpayne@69: try: jpayne@69: self._n_frees += 1 jpayne@69: self._free_pending_blocks() jpayne@69: self._add_free_block(block) jpayne@69: self._remove_allocated_block(block) jpayne@69: finally: jpayne@69: self._lock.release() jpayne@69: jpayne@69: def malloc(self, size): jpayne@69: # return a block of right size (possibly rounded up) jpayne@69: if size < 0: jpayne@69: raise ValueError("Size {0:n} out of range".format(size)) jpayne@69: if sys.maxsize <= size: jpayne@69: raise OverflowError("Size {0:n} too large".format(size)) jpayne@69: if os.getpid() != self._lastpid: jpayne@69: self.__init__() # reinitialize after fork jpayne@69: with self._lock: jpayne@69: self._n_mallocs += 1 jpayne@69: # allow pending blocks to be marked available jpayne@69: self._free_pending_blocks() jpayne@69: size = self._roundup(max(size, 1), self._alignment) jpayne@69: (arena, start, stop) = self._malloc(size) jpayne@69: real_stop = start + size jpayne@69: if real_stop < stop: jpayne@69: # if the returned block is larger than necessary, mark jpayne@69: # the remainder available jpayne@69: self._add_free_block((arena, real_stop, stop)) jpayne@69: self._allocated_blocks[arena].add((start, real_stop)) jpayne@69: return (arena, start, real_stop) jpayne@69: jpayne@69: # jpayne@69: # Class wrapping a block allocated out of a Heap -- can be inherited by child process jpayne@69: # jpayne@69: jpayne@69: class BufferWrapper(object): jpayne@69: jpayne@69: _heap = Heap() jpayne@69: jpayne@69: def __init__(self, size): jpayne@69: if size < 0: jpayne@69: raise ValueError("Size {0:n} out of range".format(size)) jpayne@69: if sys.maxsize <= size: jpayne@69: raise OverflowError("Size {0:n} too large".format(size)) jpayne@69: block = BufferWrapper._heap.malloc(size) jpayne@69: self._state = (block, size) jpayne@69: util.Finalize(self, BufferWrapper._heap.free, args=(block,)) jpayne@69: jpayne@69: def create_memoryview(self): jpayne@69: (arena, start, stop), size = self._state jpayne@69: return memoryview(arena.buffer)[start:start+size]