2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
# Module which supports allocation of memory from an mmap
|
|
|
|
#
|
|
|
|
# multiprocessing/heap.py
|
|
|
|
#
|
2010-12-13 21:41:07 -04:00
|
|
|
# Copyright (c) 2006-2008, R Oudkerk
|
2012-04-30 08:13:55 -03:00
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
|
|
|
|
import bisect
|
2013-08-14 11:35:41 -03:00
|
|
|
import itertools
|
2008-06-11 13:44:04 -03:00
|
|
|
import mmap
|
|
|
|
import os
|
|
|
|
import sys
|
2013-08-14 11:35:41 -03:00
|
|
|
import tempfile
|
2008-06-11 13:44:04 -03:00
|
|
|
import threading
|
|
|
|
import _multiprocessing
|
2013-08-14 11:35:41 -03:00
|
|
|
|
|
|
|
from . import popen
|
|
|
|
from . import reduction
|
|
|
|
from . import util
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
__all__ = ['BufferWrapper']
|
|
|
|
|
|
|
|
#
|
2013-07-30 10:12:49 -03:00
|
|
|
# Inheritable class which wraps an mmap, and from which blocks can be allocated
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
import _winapi
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
class Arena(object):
|
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
_rand = tempfile._RandomNameSequence()
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def __init__(self, size):
|
|
|
|
self.size = size
|
2013-08-14 11:35:41 -03:00
|
|
|
for i in range(100):
|
|
|
|
name = 'pym-%d-%s' % (os.getpid(), next(self._rand))
|
|
|
|
buf = mmap.mmap(-1, size, tagname=name)
|
|
|
|
if _winapi.GetLastError() == 0:
|
|
|
|
break
|
|
|
|
# We have reopened a preexisting mmap.
|
|
|
|
buf.close()
|
|
|
|
else:
|
|
|
|
raise FileExistsError('Cannot find name for new mmap')
|
|
|
|
self.name = name
|
|
|
|
self.buffer = buf
|
2008-06-11 13:44:04 -03:00
|
|
|
self._state = (self.size, self.name)
|
|
|
|
|
|
|
|
def __getstate__(self):
|
2013-08-14 11:35:41 -03:00
|
|
|
popen.assert_spawning(self)
|
2008-06-11 13:44:04 -03:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def __setstate__(self, state):
|
|
|
|
self.size, self.name = self._state = state
|
|
|
|
self.buffer = mmap.mmap(-1, self.size, tagname=self.name)
|
2012-04-18 15:51:15 -03:00
|
|
|
assert _winapi.GetLastError() == _winapi.ERROR_ALREADY_EXISTS
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
class Arena(object):
|
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
def __init__(self, size, fd=-1):
|
2008-06-11 13:44:04 -03:00
|
|
|
self.size = size
|
2013-08-14 11:35:41 -03:00
|
|
|
self.fd = fd
|
|
|
|
if fd == -1:
|
|
|
|
self.fd, name = tempfile.mkstemp(
|
|
|
|
prefix='pym-%d-'%os.getpid(), dir=util.get_temp_dir())
|
|
|
|
os.unlink(name)
|
|
|
|
util.Finalize(self, os.close, (self.fd,))
|
|
|
|
with open(self.fd, 'wb', closefd=False) as f:
|
|
|
|
f.write(b'\0'*size)
|
|
|
|
self.buffer = mmap.mmap(self.fd, self.size)
|
|
|
|
|
|
|
|
def reduce_arena(a):
|
|
|
|
if a.fd == -1:
|
|
|
|
raise ValueError('Arena is unpicklable because '
|
|
|
|
'forking was enabled when it was created')
|
|
|
|
return rebuild_arena, (a.size, reduction.DupFd(a.fd))
|
|
|
|
|
|
|
|
def rebuild_arena(size, dupfd):
|
|
|
|
return Arena(size, dupfd.detach())
|
|
|
|
|
|
|
|
reduction.register(Arena, reduce_arena)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Class allowing allocation of chunks of memory from arenas
|
|
|
|
#
|
|
|
|
|
|
|
|
class Heap(object):
|
|
|
|
|
|
|
|
_alignment = 8
|
|
|
|
|
|
|
|
def __init__(self, size=mmap.PAGESIZE):
|
|
|
|
self._lastpid = os.getpid()
|
|
|
|
self._lock = threading.Lock()
|
|
|
|
self._size = size
|
|
|
|
self._lengths = []
|
|
|
|
self._len_to_seq = {}
|
|
|
|
self._start_to_block = {}
|
|
|
|
self._stop_to_block = {}
|
|
|
|
self._allocated_blocks = set()
|
|
|
|
self._arenas = []
|
2011-07-02 09:35:49 -03:00
|
|
|
# list of pending blocks to free - see free() comment below
|
|
|
|
self._pending_free_blocks = []
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _roundup(n, alignment):
|
|
|
|
# alignment must be a power of 2
|
|
|
|
mask = alignment - 1
|
|
|
|
return (n + mask) & ~mask
|
|
|
|
|
|
|
|
def _malloc(self, size):
|
|
|
|
# returns a large enough block -- it might be much larger
|
|
|
|
i = bisect.bisect_left(self._lengths, size)
|
|
|
|
if i == len(self._lengths):
|
|
|
|
length = self._roundup(max(self._size, size), mmap.PAGESIZE)
|
|
|
|
self._size *= 2
|
2013-08-14 11:35:41 -03:00
|
|
|
util.info('allocating a new mmap of length %d', length)
|
2008-06-11 13:44:04 -03:00
|
|
|
arena = Arena(length)
|
|
|
|
self._arenas.append(arena)
|
|
|
|
return (arena, 0, length)
|
|
|
|
else:
|
|
|
|
length = self._lengths[i]
|
|
|
|
seq = self._len_to_seq[length]
|
|
|
|
block = seq.pop()
|
|
|
|
if not seq:
|
|
|
|
del self._len_to_seq[length], self._lengths[i]
|
|
|
|
|
|
|
|
(arena, start, stop) = block
|
|
|
|
del self._start_to_block[(arena, start)]
|
|
|
|
del self._stop_to_block[(arena, stop)]
|
|
|
|
return block
|
|
|
|
|
|
|
|
def _free(self, block):
|
|
|
|
# free location and try to merge with neighbours
|
|
|
|
(arena, start, stop) = block
|
|
|
|
|
|
|
|
try:
|
|
|
|
prev_block = self._stop_to_block[(arena, start)]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
start, _ = self._absorb(prev_block)
|
|
|
|
|
|
|
|
try:
|
|
|
|
next_block = self._start_to_block[(arena, stop)]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
_, stop = self._absorb(next_block)
|
|
|
|
|
|
|
|
block = (arena, start, stop)
|
|
|
|
length = stop - start
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._len_to_seq[length].append(block)
|
|
|
|
except KeyError:
|
|
|
|
self._len_to_seq[length] = [block]
|
|
|
|
bisect.insort(self._lengths, length)
|
|
|
|
|
|
|
|
self._start_to_block[(arena, start)] = block
|
|
|
|
self._stop_to_block[(arena, stop)] = block
|
|
|
|
|
|
|
|
def _absorb(self, block):
|
|
|
|
# deregister this block so it can be merged with a neighbour
|
|
|
|
(arena, start, stop) = block
|
|
|
|
del self._start_to_block[(arena, start)]
|
|
|
|
del self._stop_to_block[(arena, stop)]
|
|
|
|
|
|
|
|
length = stop - start
|
|
|
|
seq = self._len_to_seq[length]
|
|
|
|
seq.remove(block)
|
|
|
|
if not seq:
|
|
|
|
del self._len_to_seq[length]
|
|
|
|
self._lengths.remove(length)
|
|
|
|
|
|
|
|
return start, stop
|
|
|
|
|
2011-07-02 09:35:49 -03:00
|
|
|
def _free_pending_blocks(self):
|
|
|
|
# Free all the blocks in the pending list - called with the lock held.
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
block = self._pending_free_blocks.pop()
|
|
|
|
except IndexError:
|
|
|
|
break
|
|
|
|
self._allocated_blocks.remove(block)
|
|
|
|
self._free(block)
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
def free(self, block):
|
|
|
|
# free a block returned by malloc()
|
2011-07-02 09:35:49 -03:00
|
|
|
# Since free() can be called asynchronously by the GC, it could happen
|
|
|
|
# that it's called while self._lock is held: in that case,
|
|
|
|
# self._lock.acquire() would deadlock (issue #12352). To avoid that, a
|
|
|
|
# trylock is used instead, and if the lock can't be acquired
|
|
|
|
# immediately, the block is added to a list of blocks to be freed
|
|
|
|
# synchronously sometimes later from malloc() or free(), by calling
|
|
|
|
# _free_pending_blocks() (appending and retrieving from a list is not
|
|
|
|
# strictly thread-safe but under cPython it's atomic thanks to the GIL).
|
2008-06-11 13:44:04 -03:00
|
|
|
assert os.getpid() == self._lastpid
|
2011-07-02 09:35:49 -03:00
|
|
|
if not self._lock.acquire(False):
|
|
|
|
# can't acquire the lock right now, add the block to the list of
|
|
|
|
# pending blocks to free
|
|
|
|
self._pending_free_blocks.append(block)
|
|
|
|
else:
|
|
|
|
# we hold the lock
|
|
|
|
try:
|
|
|
|
self._free_pending_blocks()
|
|
|
|
self._allocated_blocks.remove(block)
|
|
|
|
self._free(block)
|
|
|
|
finally:
|
|
|
|
self._lock.release()
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def malloc(self, size):
|
|
|
|
# return a block of right size (possibly rounded up)
|
|
|
|
assert 0 <= size < sys.maxsize
|
|
|
|
if os.getpid() != self._lastpid:
|
|
|
|
self.__init__() # reinitialize after fork
|
|
|
|
self._lock.acquire()
|
2011-07-02 09:35:49 -03:00
|
|
|
self._free_pending_blocks()
|
2008-06-11 13:44:04 -03:00
|
|
|
try:
|
|
|
|
size = self._roundup(max(size,1), self._alignment)
|
|
|
|
(arena, start, stop) = self._malloc(size)
|
|
|
|
new_stop = start + size
|
|
|
|
if new_stop < stop:
|
|
|
|
self._free((arena, new_stop, stop))
|
|
|
|
block = (arena, start, new_stop)
|
|
|
|
self._allocated_blocks.add(block)
|
|
|
|
return block
|
|
|
|
finally:
|
|
|
|
self._lock.release()
|
|
|
|
|
|
|
|
#
|
2012-05-26 18:09:59 -03:00
|
|
|
# Class representing a chunk of an mmap -- can be inherited by child process
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
|
|
|
|
class BufferWrapper(object):
|
|
|
|
|
|
|
|
_heap = Heap()
|
|
|
|
|
|
|
|
def __init__(self, size):
|
|
|
|
assert 0 <= size < sys.maxsize
|
|
|
|
block = BufferWrapper._heap.malloc(size)
|
|
|
|
self._state = (block, size)
|
2013-08-14 11:35:41 -03:00
|
|
|
util.Finalize(self, BufferWrapper._heap.free, args=(block,))
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2012-05-26 18:09:59 -03:00
|
|
|
def create_memoryview(self):
|
2008-06-11 13:44:04 -03:00
|
|
|
(arena, start, stop), size = self._state
|
2012-05-26 18:09:59 -03:00
|
|
|
return memoryview(arena.buffer)[start:start+size]
|