blob: 6217dfe12689b379f2dad6f1e4bc3bbf6af8f60a [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001#
2# Module which supports allocation of memory from an mmap
3#
4# multiprocessing/heap.py
5#
R. David Murrayd3820662010-12-14 01:41:07 +00006# Copyright (c) 2006-2008, R Oudkerk
Richard Oudkerk3e268aa2012-04-30 12:13:55 +01007# Licensed to PSF under a Contributor Agreement.
Benjamin Petersone711caf2008-06-11 16:44:04 +00008#
9
10import bisect
Antoine Pitroue4679cd2018-04-09 17:37:55 +020011from collections import defaultdict
Benjamin Petersone711caf2008-06-11 16:44:04 +000012import mmap
Benjamin Petersone711caf2008-06-11 16:44:04 +000013import os
14import sys
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010015import tempfile
Benjamin Petersone711caf2008-06-11 16:44:04 +000016import threading
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010017
Davin Potts54586472016-09-09 18:03:10 -050018from .context import reduction, assert_spawning
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010019from . import util
Benjamin Petersone711caf2008-06-11 16:44:04 +000020
21__all__ = ['BufferWrapper']
22
23#
Eli Bendersky25f043b2013-07-30 06:12:49 -070024# Inheritable class which wraps an mmap, and from which blocks can be allocated
Benjamin Petersone711caf2008-06-11 16:44:04 +000025#
26
27if sys.platform == 'win32':
28
Antoine Pitrou23bba4c2012-04-18 20:51:15 +020029 import _winapi
Benjamin Petersone711caf2008-06-11 16:44:04 +000030
31 class Arena(object):
Antoine Pitroue4679cd2018-04-09 17:37:55 +020032 """
33 A shared memory area backed by anonymous memory (Windows).
34 """
Benjamin Petersone711caf2008-06-11 16:44:04 +000035
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010036 _rand = tempfile._RandomNameSequence()
Benjamin Petersone711caf2008-06-11 16:44:04 +000037
38 def __init__(self, size):
39 self.size = size
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010040 for i in range(100):
41 name = 'pym-%d-%s' % (os.getpid(), next(self._rand))
42 buf = mmap.mmap(-1, size, tagname=name)
43 if _winapi.GetLastError() == 0:
44 break
45 # We have reopened a preexisting mmap.
46 buf.close()
47 else:
48 raise FileExistsError('Cannot find name for new mmap')
49 self.name = name
50 self.buffer = buf
Benjamin Petersone711caf2008-06-11 16:44:04 +000051 self._state = (self.size, self.name)
52
53 def __getstate__(self):
Davin Potts54586472016-09-09 18:03:10 -050054 assert_spawning(self)
Benjamin Petersone711caf2008-06-11 16:44:04 +000055 return self._state
56
57 def __setstate__(self, state):
58 self.size, self.name = self._state = state
Antoine Pitroue4679cd2018-04-09 17:37:55 +020059 # Reopen existing mmap
Benjamin Petersone711caf2008-06-11 16:44:04 +000060 self.buffer = mmap.mmap(-1, self.size, tagname=self.name)
Steve Dower438f4ab2014-12-17 06:35:49 -080061 # XXX Temporarily preventing buildbot failures while determining
62 # XXX the correct long-term fix. See issue 23060
63 #assert _winapi.GetLastError() == _winapi.ERROR_ALREADY_EXISTS
Benjamin Petersone711caf2008-06-11 16:44:04 +000064
65else:
66
67 class Arena(object):
Antoine Pitroue4679cd2018-04-09 17:37:55 +020068 """
69 A shared memory area backed by a temporary file (POSIX).
70 """
71
Antoine Pitrou3051f0b2017-07-23 13:05:26 +020072 if sys.platform == 'linux':
73 _dir_candidates = ['/dev/shm']
74 else:
75 _dir_candidates = []
Benjamin Petersone711caf2008-06-11 16:44:04 +000076
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010077 def __init__(self, size, fd=-1):
Benjamin Petersone711caf2008-06-11 16:44:04 +000078 self.size = size
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010079 self.fd = fd
80 if fd == -1:
Antoine Pitroue4679cd2018-04-09 17:37:55 +020081 # Arena is created anew (if fd != -1, it means we're coming
82 # from rebuild_arena() below)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010083 self.fd, name = tempfile.mkstemp(
Antoine Pitrou3051f0b2017-07-23 13:05:26 +020084 prefix='pym-%d-'%os.getpid(),
85 dir=self._choose_dir(size))
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010086 os.unlink(name)
87 util.Finalize(self, os.close, (self.fd,))
Antoine Pitrou3051f0b2017-07-23 13:05:26 +020088 os.ftruncate(self.fd, size)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010089 self.buffer = mmap.mmap(self.fd, self.size)
90
Antoine Pitrou3051f0b2017-07-23 13:05:26 +020091 def _choose_dir(self, size):
92 # Choose a non-storage backed directory if possible,
93 # to improve performance
94 for d in self._dir_candidates:
95 st = os.statvfs(d)
96 if st.f_bavail * st.f_frsize >= size: # enough free space?
97 return d
98 return util.get_temp_dir()
99
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100100 def reduce_arena(a):
101 if a.fd == -1:
102 raise ValueError('Arena is unpicklable because '
103 'forking was enabled when it was created')
104 return rebuild_arena, (a.size, reduction.DupFd(a.fd))
105
106 def rebuild_arena(size, dupfd):
107 return Arena(size, dupfd.detach())
108
109 reduction.register(Arena, reduce_arena)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000110
111#
112# Class allowing allocation of chunks of memory from arenas
113#
114
115class Heap(object):
116
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200117 # Minimum malloc() alignment
Benjamin Petersone711caf2008-06-11 16:44:04 +0000118 _alignment = 8
119
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200120 _DISCARD_FREE_SPACE_LARGER_THAN = 4 * 1024 ** 2 # 4 MB
121 _DOUBLE_ARENA_SIZE_UNTIL = 4 * 1024 ** 2
122
Benjamin Petersone711caf2008-06-11 16:44:04 +0000123 def __init__(self, size=mmap.PAGESIZE):
124 self._lastpid = os.getpid()
125 self._lock = threading.Lock()
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200126 # Current arena allocation size
Benjamin Petersone711caf2008-06-11 16:44:04 +0000127 self._size = size
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200128 # A sorted list of available block sizes in arenas
Benjamin Petersone711caf2008-06-11 16:44:04 +0000129 self._lengths = []
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200130
131 # Free block management:
132 # - map each block size to a list of `(Arena, start, stop)` blocks
Benjamin Petersone711caf2008-06-11 16:44:04 +0000133 self._len_to_seq = {}
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200134 # - map `(Arena, start)` tuple to the `(Arena, start, stop)` block
135 # starting at that offset
Benjamin Petersone711caf2008-06-11 16:44:04 +0000136 self._start_to_block = {}
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200137 # - map `(Arena, stop)` tuple to the `(Arena, start, stop)` block
138 # ending at that offset
Benjamin Petersone711caf2008-06-11 16:44:04 +0000139 self._stop_to_block = {}
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200140
141 # Map arenas to their `(Arena, start, stop)` blocks in use
142 self._allocated_blocks = defaultdict(set)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000143 self._arenas = []
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200144
145 # List of pending blocks to free - see comment in free() below
Charles-François Natali778db492011-07-02 14:35:49 +0200146 self._pending_free_blocks = []
Benjamin Petersone711caf2008-06-11 16:44:04 +0000147
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200148 # Statistics
149 self._n_mallocs = 0
150 self._n_frees = 0
151
Benjamin Petersone711caf2008-06-11 16:44:04 +0000152 @staticmethod
153 def _roundup(n, alignment):
154 # alignment must be a power of 2
155 mask = alignment - 1
156 return (n + mask) & ~mask
157
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200158 def _new_arena(self, size):
159 # Create a new arena with at least the given *size*
160 length = self._roundup(max(self._size, size), mmap.PAGESIZE)
161 # We carve larger and larger arenas, for efficiency, until we
162 # reach a large-ish size (roughly L3 cache-sized)
163 if self._size < self._DOUBLE_ARENA_SIZE_UNTIL:
164 self._size *= 2
165 util.info('allocating a new mmap of length %d', length)
166 arena = Arena(length)
167 self._arenas.append(arena)
168 return (arena, 0, length)
169
170 def _discard_arena(self, arena):
171 # Possibly delete the given (unused) arena
172 length = arena.size
173 # Reusing an existing arena is faster than creating a new one, so
174 # we only reclaim space if it's large enough.
175 if length < self._DISCARD_FREE_SPACE_LARGER_THAN:
176 return
177 blocks = self._allocated_blocks.pop(arena)
178 assert not blocks
179 del self._start_to_block[(arena, 0)]
180 del self._stop_to_block[(arena, length)]
181 self._arenas.remove(arena)
182 seq = self._len_to_seq[length]
183 seq.remove((arena, 0, length))
184 if not seq:
185 del self._len_to_seq[length]
186 self._lengths.remove(length)
187
Benjamin Petersone711caf2008-06-11 16:44:04 +0000188 def _malloc(self, size):
189 # returns a large enough block -- it might be much larger
190 i = bisect.bisect_left(self._lengths, size)
191 if i == len(self._lengths):
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200192 return self._new_arena(size)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000193 else:
194 length = self._lengths[i]
195 seq = self._len_to_seq[length]
196 block = seq.pop()
197 if not seq:
198 del self._len_to_seq[length], self._lengths[i]
199
200 (arena, start, stop) = block
201 del self._start_to_block[(arena, start)]
202 del self._stop_to_block[(arena, stop)]
203 return block
204
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200205 def _add_free_block(self, block):
206 # make block available and try to merge with its neighbours in the arena
Benjamin Petersone711caf2008-06-11 16:44:04 +0000207 (arena, start, stop) = block
208
209 try:
210 prev_block = self._stop_to_block[(arena, start)]
211 except KeyError:
212 pass
213 else:
214 start, _ = self._absorb(prev_block)
215
216 try:
217 next_block = self._start_to_block[(arena, stop)]
218 except KeyError:
219 pass
220 else:
221 _, stop = self._absorb(next_block)
222
223 block = (arena, start, stop)
224 length = stop - start
225
226 try:
227 self._len_to_seq[length].append(block)
228 except KeyError:
229 self._len_to_seq[length] = [block]
230 bisect.insort(self._lengths, length)
231
232 self._start_to_block[(arena, start)] = block
233 self._stop_to_block[(arena, stop)] = block
234
235 def _absorb(self, block):
236 # deregister this block so it can be merged with a neighbour
237 (arena, start, stop) = block
238 del self._start_to_block[(arena, start)]
239 del self._stop_to_block[(arena, stop)]
240
241 length = stop - start
242 seq = self._len_to_seq[length]
243 seq.remove(block)
244 if not seq:
245 del self._len_to_seq[length]
246 self._lengths.remove(length)
247
248 return start, stop
249
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200250 def _remove_allocated_block(self, block):
251 arena, start, stop = block
252 blocks = self._allocated_blocks[arena]
253 blocks.remove((start, stop))
254 if not blocks:
255 # Arena is entirely free, discard it from this process
256 self._discard_arena(arena)
257
Charles-François Natali778db492011-07-02 14:35:49 +0200258 def _free_pending_blocks(self):
259 # Free all the blocks in the pending list - called with the lock held.
260 while True:
261 try:
262 block = self._pending_free_blocks.pop()
263 except IndexError:
264 break
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200265 self._add_free_block(block)
266 self._remove_allocated_block(block)
Charles-François Natali778db492011-07-02 14:35:49 +0200267
268 def free(self, block):
269 # free a block returned by malloc()
270 # Since free() can be called asynchronously by the GC, it could happen
271 # that it's called while self._lock is held: in that case,
272 # self._lock.acquire() would deadlock (issue #12352). To avoid that, a
273 # trylock is used instead, and if the lock can't be acquired
274 # immediately, the block is added to a list of blocks to be freed
275 # synchronously sometimes later from malloc() or free(), by calling
276 # _free_pending_blocks() (appending and retrieving from a list is not
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200277 # strictly thread-safe but under CPython it's atomic thanks to the GIL).
Allen W. Smith, Ph.Dbd73e722017-08-29 17:52:18 -0500278 if os.getpid() != self._lastpid:
279 raise ValueError(
280 "My pid ({0:n}) is not last pid {1:n}".format(
281 os.getpid(),self._lastpid))
Charles-François Natali778db492011-07-02 14:35:49 +0200282 if not self._lock.acquire(False):
283 # can't acquire the lock right now, add the block to the list of
284 # pending blocks to free
285 self._pending_free_blocks.append(block)
286 else:
287 # we hold the lock
288 try:
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200289 self._n_frees += 1
Charles-François Natali778db492011-07-02 14:35:49 +0200290 self._free_pending_blocks()
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200291 self._add_free_block(block)
292 self._remove_allocated_block(block)
Charles-François Natali778db492011-07-02 14:35:49 +0200293 finally:
294 self._lock.release()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000295
296 def malloc(self, size):
297 # return a block of right size (possibly rounded up)
Allen W. Smith, Ph.Dbd73e722017-08-29 17:52:18 -0500298 if size < 0:
299 raise ValueError("Size {0:n} out of range".format(size))
300 if sys.maxsize <= size:
301 raise OverflowError("Size {0:n} too large".format(size))
Benjamin Petersone711caf2008-06-11 16:44:04 +0000302 if os.getpid() != self._lastpid:
303 self.__init__() # reinitialize after fork
Charles-François Natalia924fc72014-05-25 14:12:12 +0100304 with self._lock:
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200305 self._n_mallocs += 1
306 # allow pending blocks to be marked available
Charles-François Natalia924fc72014-05-25 14:12:12 +0100307 self._free_pending_blocks()
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200308 size = self._roundup(max(size, 1), self._alignment)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000309 (arena, start, stop) = self._malloc(size)
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200310 real_stop = start + size
311 if real_stop < stop:
312 # if the returned block is larger than necessary, mark
313 # the remainder available
314 self._add_free_block((arena, real_stop, stop))
315 self._allocated_blocks[arena].add((start, real_stop))
316 return (arena, start, real_stop)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000317
318#
Antoine Pitroue4679cd2018-04-09 17:37:55 +0200319# Class wrapping a block allocated out of a Heap -- can be inherited by child process
Benjamin Petersone711caf2008-06-11 16:44:04 +0000320#
321
322class BufferWrapper(object):
323
324 _heap = Heap()
325
326 def __init__(self, size):
Allen W. Smith, Ph.Dbd73e722017-08-29 17:52:18 -0500327 if size < 0:
328 raise ValueError("Size {0:n} out of range".format(size))
329 if sys.maxsize <= size:
330 raise OverflowError("Size {0:n} too large".format(size))
Benjamin Petersone711caf2008-06-11 16:44:04 +0000331 block = BufferWrapper._heap.malloc(size)
332 self._state = (block, size)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100333 util.Finalize(self, BufferWrapper._heap.free, args=(block,))
Benjamin Petersone711caf2008-06-11 16:44:04 +0000334
Richard Oudkerk26cdf1f2012-05-26 22:09:59 +0100335 def create_memoryview(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000336 (arena, start, stop), size = self._state
Richard Oudkerk26cdf1f2012-05-26 22:09:59 +0100337 return memoryview(arena.buffer)[start:start+size]