| 1 |
import sys |
| 2 |
from rpython.rlib.rarithmetic import intmask, r_uint, LONG_BIT |
| 3 |
from rpython.rlib.objectmodel import we_are_translated |
| 4 |
from rpython.rlib import rmmap |
| 5 |
from rpython.rlib.debug import debug_start, debug_print, debug_stop |
| 6 |
from rpython.rlib.debug import have_debug_prints |
| 7 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 8 |
|
| 9 |
|
| 10 |
class AsmMemoryManager(object): |
| 11 |
LARGE_ALLOC_SIZE = 1024 * 1024 |
| 12 |
MIN_FRAGMENT = 64 |
| 13 |
NUM_INDICES = 32 |
| 14 |
_allocated = None |
| 15 |
|
| 16 |
def __init__(self, large_alloc_size = LARGE_ALLOC_SIZE, |
| 17 |
min_fragment = MIN_FRAGMENT, |
| 18 |
num_indices = NUM_INDICES): |
| 19 |
self.total_memory_allocated = r_uint(0) |
| 20 |
self.total_mallocs = r_uint(0) |
| 21 |
self.large_alloc_size = large_alloc_size |
| 22 |
self.min_fragment = min_fragment |
| 23 |
self.num_indices = num_indices |
| 24 |
self.free_blocks = {} |
| 25 |
self.free_blocks_end = {} |
| 26 |
self.blocks_by_size = [[] for i in range(self.num_indices)] |
| 27 |
|
| 28 |
def get_stats(self): |
| 29 |
"""Returns stats for rlib.jit.jit_hooks.stats_asmmemmgr_*().""" |
| 30 |
return (self.total_memory_allocated, self.total_mallocs) |
| 31 |
|
| 32 |
def malloc(self, minsize, maxsize): |
| 33 |
"""Allocate executable memory, between minsize and maxsize bytes, |
| 34 |
and return a pair (start, stop). Does not perform any rounding |
| 35 |
of minsize and maxsize. |
| 36 |
""" |
| 37 |
result = self._allocate_block(minsize) |
| 38 |
(start, stop) = result |
| 39 |
if maxsize <= stop - start - self.min_fragment: |
| 40 |
smaller_stop = start + maxsize |
| 41 |
self._add_free_block(smaller_stop, stop) |
| 42 |
stop = smaller_stop |
| 43 |
result = (start, stop) |
| 44 |
self.total_mallocs += r_uint(stop - start) |
| 45 |
return result |
| 46 |
|
| 47 |
def free(self, start, stop): |
| 48 |
"""Free a block (start, stop) returned by a previous malloc().""" |
| 49 |
if r_uint is not None: |
| 50 |
self.total_mallocs -= r_uint(stop - start) |
| 51 |
self._add_free_block(start, stop) |
| 52 |
|
| 53 |
def open_malloc(self, minsize): |
| 54 |
"""Allocate at least minsize bytes. Returns (start, stop).""" |
| 55 |
result = self._allocate_block(minsize) |
| 56 |
(start, stop) = result |
| 57 |
self.total_mallocs += r_uint(stop - start) |
| 58 |
return result |
| 59 |
|
| 60 |
def open_free(self, middle, stop): |
| 61 |
"""Used for freeing the end of an open-allocated block of memory.""" |
| 62 |
if stop - middle >= self.min_fragment: |
| 63 |
self.total_mallocs -= r_uint(stop - middle) |
| 64 |
self._add_free_block(middle, stop) |
| 65 |
return True |
| 66 |
else: |
| 67 |
return False |
| 68 |
|
| 69 |
def _mmap_alloc(self, size): |
| 70 |
|
| 71 |
data = rmmap.alloc(size) |
| 72 |
if not we_are_translated(): |
| 73 |
if self._allocated is None: |
| 74 |
self._allocated = [] |
| 75 |
self._allocated.append((data, size)) |
| 76 |
if sys.maxint > 2147483647: |
| 77 |
|
| 78 |
|
| 79 |
rmmap.hint.pos += 0x80000000 - size |
| 80 |
return data |
| 81 |
|
| 82 |
def _allocate_large_block(self, minsize): |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
minsize = max(minsize, intmask(self.total_memory_allocated >> 4)) |
| 87 |
size = minsize + self.large_alloc_size - 1 |
| 88 |
size = (size // self.large_alloc_size) * self.large_alloc_size |
| 89 |
data = self._mmap_alloc(size) |
| 90 |
self.total_memory_allocated += r_uint(size) |
| 91 |
data = rffi.cast(lltype.Signed, data) |
| 92 |
return self._add_free_block(data, data + size) |
| 93 |
|
| 94 |
def _get_index(self, length): |
| 95 |
i = 0 |
| 96 |
while length > self.min_fragment: |
| 97 |
length = (length * 3) >> 2 |
| 98 |
i += 1 |
| 99 |
if i == self.num_indices - 1: |
| 100 |
break |
| 101 |
return i |
| 102 |
|
| 103 |
def _add_free_block(self, start, stop): |
| 104 |
|
| 105 |
if start in self.free_blocks_end: |
| 106 |
left_start = self.free_blocks_end[start] |
| 107 |
self._del_free_block(left_start, start) |
| 108 |
start = left_start |
| 109 |
assert start not in self.free_blocks_end |
| 110 |
|
| 111 |
if stop in self.free_blocks: |
| 112 |
right_stop = self.free_blocks[stop] |
| 113 |
self._del_free_block(stop, right_stop) |
| 114 |
stop = right_stop |
| 115 |
assert stop not in self.free_blocks |
| 116 |
|
| 117 |
assert start not in self.free_blocks |
| 118 |
self.free_blocks[start] = stop |
| 119 |
assert stop not in self.free_blocks_end |
| 120 |
self.free_blocks_end[stop] = start |
| 121 |
i = self._get_index(stop - start) |
| 122 |
self.blocks_by_size[i].append(start) |
| 123 |
return start |
| 124 |
|
| 125 |
def _del_free_block(self, start, stop): |
| 126 |
del self.free_blocks[start] |
| 127 |
del self.free_blocks_end[stop] |
| 128 |
i = self._get_index(stop - start) |
| 129 |
self.blocks_by_size[i].remove(start) |
| 130 |
|
| 131 |
def _allocate_block(self, length): |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
i0 = self._get_index(length) |
| 138 |
bbs = self.blocks_by_size[i0] |
| 139 |
for j in range(len(bbs)): |
| 140 |
start = bbs[j] |
| 141 |
stop = self.free_blocks[start] |
| 142 |
if start + length <= stop: |
| 143 |
del bbs[j] |
| 144 |
break |
| 145 |
else: |
| 146 |
|
| 147 |
i = i0 + 1 |
| 148 |
while i < self.num_indices: |
| 149 |
if len(self.blocks_by_size[i]) > 0: |
| 150 |
|
| 151 |
start = self.blocks_by_size[i].pop(0) |
| 152 |
stop = self.free_blocks[start] |
| 153 |
assert start + length <= stop |
| 154 |
break |
| 155 |
i += 1 |
| 156 |
else: |
| 157 |
|
| 158 |
start = self._allocate_large_block(length) |
| 159 |
stop = self.free_blocks[start] |
| 160 |
i = self._get_index(stop - start) |
| 161 |
assert self.blocks_by_size[i][-1] == start |
| 162 |
self.blocks_by_size[i].pop() |
| 163 |
|
| 164 |
del self.free_blocks[start] |
| 165 |
del self.free_blocks_end[stop] |
| 166 |
return (start, stop) |
| 167 |
|
| 168 |
def _delete(self): |
| 169 |
"NOT_RPYTHON" |
| 170 |
if self._allocated: |
| 171 |
for data, size in self._allocated: |
| 172 |
rmmap.free(data, size) |
| 173 |
self._allocated = None |
| 174 |
|
| 175 |
|
| 176 |
class MachineDataBlockWrapper(object): |
| 177 |
def __init__(self, asmmemmgr, allblocks): |
| 178 |
self.asmmemmgr = asmmemmgr |
| 179 |
self.allblocks = allblocks |
| 180 |
self.rawstart = 0 |
| 181 |
self.rawposition = 0 |
| 182 |
self.rawstop = 0 |
| 183 |
|
| 184 |
def done(self): |
| 185 |
if self.rawstart != 0: |
| 186 |
if self.asmmemmgr.open_free(self.rawposition, self.rawstop): |
| 187 |
self.rawstop = self.rawposition |
| 188 |
self.allblocks.append((self.rawstart, self.rawstop)) |
| 189 |
self.rawstart = 0 |
| 190 |
self.rawposition = 0 |
| 191 |
self.rawstop = 0 |
| 192 |
|
| 193 |
def _allocate_next_block(self, minsize): |
| 194 |
self.done() |
| 195 |
self.rawstart, self.rawstop = self.asmmemmgr.open_malloc(minsize) |
| 196 |
self.rawposition = self.rawstart |
| 197 |
|
| 198 |
def malloc_aligned(self, size, alignment): |
| 199 |
p = self.rawposition |
| 200 |
p = (p + alignment - 1) & (-alignment) |
| 201 |
if p + size > self.rawstop: |
| 202 |
self._allocate_next_block(size + alignment - 1) |
| 203 |
p = self.rawposition |
| 204 |
p = (p + alignment - 1) & (-alignment) |
| 205 |
assert p + size <= self.rawstop |
| 206 |
self.rawposition = p + size |
| 207 |
return p |
| 208 |
|
| 209 |
|
| 210 |
class BlockBuilderMixin(object): |
| 211 |
_mixin_ = True |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
WORD = LONG_BIT // 8 |
| 218 |
SUBBLOCK_SIZE = 32 * WORD |
| 219 |
SUBBLOCK_PTR = lltype.Ptr(lltype.GcForwardReference()) |
| 220 |
SUBBLOCK = lltype.GcStruct('SUBBLOCK', |
| 221 |
('prev', SUBBLOCK_PTR), |
| 222 |
('data', lltype.FixedSizeArray(lltype.Char, SUBBLOCK_SIZE))) |
| 223 |
SUBBLOCK_PTR.TO.become(SUBBLOCK) |
| 224 |
|
| 225 |
ALIGN_MATERIALIZE = 16 |
| 226 |
|
| 227 |
gcroot_markers = None |
| 228 |
|
| 229 |
def __init__(self, translated=None): |
| 230 |
if translated is None: |
| 231 |
translated = we_are_translated() |
| 232 |
if translated: |
| 233 |
self.init_block_builder() |
| 234 |
else: |
| 235 |
self._become_a_plain_block_builder() |
| 236 |
self.rawstart = 0 |
| 237 |
|
| 238 |
def init_block_builder(self): |
| 239 |
self._cursubblock = lltype.nullptr(self.SUBBLOCK) |
| 240 |
self._baserelpos = -self.SUBBLOCK_SIZE |
| 241 |
self._make_new_subblock() |
| 242 |
|
| 243 |
def _make_new_subblock(self): |
| 244 |
nextsubblock = lltype.malloc(self.SUBBLOCK) |
| 245 |
nextsubblock.prev = self._cursubblock |
| 246 |
self._cursubblock = nextsubblock |
| 247 |
self._cursubindex = 0 |
| 248 |
self._baserelpos += self.SUBBLOCK_SIZE |
| 249 |
_make_new_subblock._dont_inline_ = True |
| 250 |
|
| 251 |
def writechar(self, char): |
| 252 |
index = self._cursubindex |
| 253 |
if index == self.SUBBLOCK_SIZE: |
| 254 |
self._make_new_subblock() |
| 255 |
index = 0 |
| 256 |
self._cursubblock.data[index] = char |
| 257 |
self._cursubindex = index + 1 |
| 258 |
|
| 259 |
def absolute_addr(self): |
| 260 |
return self.rawstart |
| 261 |
|
| 262 |
def overwrite(self, index, char): |
| 263 |
assert 0 <= index < self.get_relative_pos(break_basic_block=False) |
| 264 |
block = self._cursubblock |
| 265 |
index -= self._baserelpos |
| 266 |
while index < 0: |
| 267 |
block = block.prev |
| 268 |
index += self.SUBBLOCK_SIZE |
| 269 |
block.data[index] = char |
| 270 |
|
| 271 |
def overwrite32(self, index, val): |
| 272 |
self.overwrite(index, chr(val & 0xff)) |
| 273 |
self.overwrite(index + 1, chr((val >> 8) & 0xff)) |
| 274 |
self.overwrite(index + 2, chr((val >> 16) & 0xff)) |
| 275 |
self.overwrite(index + 3, chr((val >> 24) & 0xff)) |
| 276 |
|
| 277 |
def get_relative_pos(self, break_basic_block=True): |
| 278 |
|
| 279 |
return self._baserelpos + self._cursubindex |
| 280 |
|
| 281 |
def copy_to_raw_memory(self, addr): |
| 282 |
|
| 283 |
self._copy_to_raw_memory(addr) |
| 284 |
|
| 285 |
def _copy_to_raw_memory(self, addr): |
| 286 |
block = self._cursubblock |
| 287 |
blocksize = self._cursubindex |
| 288 |
targetindex = self._baserelpos |
| 289 |
while targetindex >= 0: |
| 290 |
dst = rffi.cast(rffi.CCHARP, addr + targetindex) |
| 291 |
for j in range(blocksize): |
| 292 |
dst[j] = block.data[j] |
| 293 |
block = block.prev |
| 294 |
blocksize = self.SUBBLOCK_SIZE |
| 295 |
targetindex -= self.SUBBLOCK_SIZE |
| 296 |
assert not block |
| 297 |
|
| 298 |
def copy_core_dump(self, addr, offset=0, count=-1): |
| 299 |
HEX = '0123456789ABCDEF' |
| 300 |
dump = [] |
| 301 |
src = rffi.cast(rffi.CCHARP, addr) |
| 302 |
end = self.get_relative_pos(break_basic_block=False) |
| 303 |
if count != -1: |
| 304 |
end = offset + count |
| 305 |
for p in range(offset, end): |
| 306 |
o = ord(src[p]) |
| 307 |
dump.append(HEX[o >> 4]) |
| 308 |
dump.append(HEX[o & 15]) |
| 309 |
return ''.join(dump) |
| 310 |
|
| 311 |
def _dump(self, addr, logname, backend=None): |
| 312 |
debug_start(logname) |
| 313 |
if have_debug_prints(): |
| 314 |
|
| 315 |
if backend is not None: |
| 316 |
debug_print('BACKEND', backend) |
| 317 |
|
| 318 |
from rpython.jit.backend.hlinfo import highleveljitinfo |
| 319 |
if highleveljitinfo.sys_executable: |
| 320 |
debug_print('SYS_EXECUTABLE', highleveljitinfo.sys_executable) |
| 321 |
else: |
| 322 |
debug_print('SYS_EXECUTABLE', '??') |
| 323 |
|
| 324 |
dump = self.copy_core_dump(addr) |
| 325 |
debug_print('CODE_DUMP', |
| 326 |
'@%x' % addr, |
| 327 |
'+0 ', |
| 328 |
dump) |
| 329 |
|
| 330 |
debug_stop(logname) |
| 331 |
|
| 332 |
def materialize(self, cpu, allblocks, gcrootmap=None): |
| 333 |
size = self.get_relative_pos() |
| 334 |
align = self.ALIGN_MATERIALIZE |
| 335 |
size += align - 1 |
| 336 |
malloced = cpu.asmmemmgr.malloc(size, size) |
| 337 |
allblocks.append(malloced) |
| 338 |
rawstart = malloced[0] |
| 339 |
rawstart = (rawstart + align - 1) & (-align) |
| 340 |
self.rawstart = rawstart |
| 341 |
self.copy_to_raw_memory(rawstart) |
| 342 |
if self.gcroot_markers is not None: |
| 343 |
assert gcrootmap is not None |
| 344 |
for pos, mark in self.gcroot_markers: |
| 345 |
gcrootmap.register_asm_addr(rawstart + pos, mark) |
| 346 |
return rawstart |
| 347 |
|
| 348 |
def _become_a_plain_block_builder(self): |
| 349 |
|
| 350 |
self._data = _data = [] |
| 351 |
self.writechar = _data.append |
| 352 |
self.overwrite = _data.__setitem__ |
| 353 |
def get_relative_pos(break_basic_block=True): |
| 354 |
return len(_data) |
| 355 |
self.get_relative_pos = get_relative_pos |
| 356 |
def plain_copy_to_raw_memory(addr): |
| 357 |
dst = rffi.cast(rffi.CCHARP, addr) |
| 358 |
for i, c in enumerate(_data): |
| 359 |
dst[i] = c |
| 360 |
self._copy_to_raw_memory = plain_copy_to_raw_memory |
| 361 |
|
| 362 |
def insert_gcroot_marker(self, mark): |
| 363 |
if self.gcroot_markers is None: |
| 364 |
self.gcroot_markers = [] |
| 365 |
self.gcroot_markers.append( |
| 366 |
(self.get_relative_pos(break_basic_block=False), mark)) |