| 1 |
""" |
| 2 |
Utility RPython functions to inspect objects in the GC. |
| 3 |
""" |
| 4 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi, llgroup |
| 5 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 6 |
from rpython.rlib.objectmodel import free_non_gc_object |
| 7 |
from rpython.rlib import rposix, rgc, jit |
| 8 |
|
| 9 |
from rpython.memory.support import AddressDict, get_address_stack |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
def _append_rpy_root(obj, gc): |
| 15 |
|
| 16 |
|
| 17 |
lst = gc._list_rpy |
| 18 |
index = gc._count_rpy |
| 19 |
gc._count_rpy = index + 1 |
| 20 |
if index < len(lst): |
| 21 |
lst[index] = llmemory.cast_adr_to_ptr(obj, llmemory.GCREF) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
def _do_append_rpy_roots(gc, lst): |
| 26 |
gc._count_rpy = 0 |
| 27 |
gc._list_rpy = lst |
| 28 |
gc.enumerate_all_roots(_append_rpy_root, gc) |
| 29 |
gc._list_rpy = None |
| 30 |
return gc._count_rpy |
| 31 |
|
| 32 |
def get_rpy_roots(gc): |
| 33 |
|
| 34 |
while True: |
| 35 |
result = [lltype.nullptr(llmemory.GCREF.TO)] * gc._totalroots_rpy |
| 36 |
count = _do_append_rpy_roots(gc, result) |
| 37 |
if count <= len(result): |
| 38 |
return result |
| 39 |
count += (count // 8) |
| 40 |
gc._totalroots_rpy = count + 10 |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
def _append_rpy_referent(pointer, gc, ignored): |
| 45 |
|
| 46 |
|
| 47 |
lst = gc._list_rpy |
| 48 |
index = gc._count_rpy |
| 49 |
gc._count_rpy = index + 1 |
| 50 |
if index < len(lst): |
| 51 |
lst[index] = llmemory.cast_adr_to_ptr(pointer.address[0], |
| 52 |
llmemory.GCREF) |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
def _do_append_rpy_referents(gc, gcref, lst): |
| 57 |
gc._count_rpy = 0 |
| 58 |
gc._list_rpy = lst |
| 59 |
gc.trace(llmemory.cast_ptr_to_adr(gcref), _append_rpy_referent, gc, None) |
| 60 |
gc._list_rpy = None |
| 61 |
return gc._count_rpy |
| 62 |
|
| 63 |
def get_rpy_referents(gc, gcref): |
| 64 |
|
| 65 |
result = [] |
| 66 |
while True: |
| 67 |
count = _do_append_rpy_referents(gc, gcref, result) |
| 68 |
if count <= len(result): |
| 69 |
if count < len(result): |
| 70 |
result = result[:count] |
| 71 |
return result |
| 72 |
result = [lltype.nullptr(llmemory.GCREF.TO)] * count |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
def get_rpy_memory_usage(gc, gcref): |
| 77 |
return gc.get_size_incl_hash(llmemory.cast_ptr_to_adr(gcref)) |
| 78 |
|
| 79 |
def get_rpy_type_index(gc, gcref): |
| 80 |
typeid = gc.get_type_id(llmemory.cast_ptr_to_adr(gcref)) |
| 81 |
return gc.get_member_index(typeid) |
| 82 |
|
| 83 |
def is_rpy_instance(gc, gcref): |
| 84 |
typeid = gc.get_type_id(llmemory.cast_ptr_to_adr(gcref)) |
| 85 |
return gc.is_rpython_class(typeid) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
raw_os_write = rffi.llexternal(rposix.UNDERSCORE_ON_WIN32 + 'write', |
| 90 |
[rffi.INT, llmemory.Address, rffi.SIZE_T], |
| 91 |
rffi.SIZE_T, |
| 92 |
sandboxsafe=True, _nowrapper=True) |
| 93 |
|
| 94 |
AddressStack = get_address_stack() |
| 95 |
|
| 96 |
class BaseWalker(object): |
| 97 |
_alloc_flavor_ = 'raw' |
| 98 |
|
| 99 |
def __init__(self, gc): |
| 100 |
self.gc = gc |
| 101 |
self.gcflag = gc.gcflag_extra |
| 102 |
if self.gcflag == 0: |
| 103 |
self.seen = AddressDict() |
| 104 |
self.pending = AddressStack() |
| 105 |
|
| 106 |
def delete(self): |
| 107 |
if self.gcflag == 0: |
| 108 |
self.seen.delete() |
| 109 |
self.pending.delete() |
| 110 |
free_non_gc_object(self) |
| 111 |
|
| 112 |
def add_roots(self): |
| 113 |
self.gc.enumerate_all_roots(_hd_add_root, self) |
| 114 |
pendingroots = self.pending |
| 115 |
self.pending = AddressStack() |
| 116 |
self.walk(pendingroots) |
| 117 |
pendingroots.delete() |
| 118 |
self.end_add_roots_marker() |
| 119 |
|
| 120 |
def end_add_roots_marker(self): |
| 121 |
pass |
| 122 |
|
| 123 |
def add(self, obj): |
| 124 |
if self.gcflag == 0: |
| 125 |
if not self.seen.contains(obj): |
| 126 |
self.seen.setitem(obj, obj) |
| 127 |
self.pending.append(obj) |
| 128 |
else: |
| 129 |
hdr = self.gc.header(obj) |
| 130 |
if (hdr.tid & self.gcflag) == 0: |
| 131 |
hdr.tid |= self.gcflag |
| 132 |
self.pending.append(obj) |
| 133 |
|
| 134 |
def walk(self, pending): |
| 135 |
while pending.non_empty(): |
| 136 |
self.processobj(pending.pop()) |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
def unobj(self, obj): |
| 143 |
gc = self.gc |
| 144 |
gc.trace(obj, gc.make_callback('_unref'), self, None) |
| 145 |
|
| 146 |
def _unref(self, pointer, ignored): |
| 147 |
obj = pointer.address[0] |
| 148 |
self.unadd(obj) |
| 149 |
|
| 150 |
def unadd(self, obj): |
| 151 |
assert self.gcflag != 0 |
| 152 |
hdr = self.gc.header(obj) |
| 153 |
if (hdr.tid & self.gcflag) != 0: |
| 154 |
hdr.tid &= ~self.gcflag |
| 155 |
self.pending.append(obj) |
| 156 |
|
| 157 |
def clear_gcflag_again(self): |
| 158 |
self.gc.enumerate_all_roots(_hd_unadd_root, self) |
| 159 |
pendingroots = self.pending |
| 160 |
self.pending = AddressStack() |
| 161 |
self.unwalk(pendingroots) |
| 162 |
pendingroots.delete() |
| 163 |
|
| 164 |
def unwalk(self, pending): |
| 165 |
while pending.non_empty(): |
| 166 |
self.unobj(pending.pop()) |
| 167 |
|
| 168 |
def finish_processing(self): |
| 169 |
if self.gcflag != 0: |
| 170 |
self.clear_gcflag_again() |
| 171 |
self.unwalk(self.pending) |
| 172 |
|
| 173 |
def process(self): |
| 174 |
self.add_roots() |
| 175 |
self.walk(self.pending) |
| 176 |
|
| 177 |
|
| 178 |
class MemoryPressureCounter(BaseWalker): |
| 179 |
|
| 180 |
def __init__(self, gc): |
| 181 |
self.count = 0 |
| 182 |
BaseWalker.__init__(self, gc) |
| 183 |
|
| 184 |
def processobj(self, obj): |
| 185 |
gc = self.gc |
| 186 |
typeid = gc.get_type_id(obj) |
| 187 |
if gc.has_memory_pressure(typeid): |
| 188 |
ofs = gc.get_memory_pressure_ofs(typeid) |
| 189 |
val = (obj + ofs).signed[0] |
| 190 |
self.count += val |
| 191 |
gc.trace(obj, gc.make_callback('_ref'), self, None) |
| 192 |
|
| 193 |
def _ref(self, pointer, ignored): |
| 194 |
obj = pointer.address[0] |
| 195 |
self.add(obj) |
| 196 |
|
| 197 |
|
| 198 |
class HeapDumper(BaseWalker): |
| 199 |
BUFSIZE = 8192 |
| 200 |
|
| 201 |
def __init__(self, gc, fd): |
| 202 |
BaseWalker.__init__(self, gc) |
| 203 |
self.fd = rffi.cast(rffi.INT, fd) |
| 204 |
self.writebuffer = lltype.malloc(rffi.SIGNEDP.TO, self.BUFSIZE, |
| 205 |
flavor='raw') |
| 206 |
self.buf_count = 0 |
| 207 |
|
| 208 |
def delete(self): |
| 209 |
lltype.free(self.writebuffer, flavor='raw') |
| 210 |
BaseWalker.delete(self) |
| 211 |
|
| 212 |
@jit.dont_look_inside |
| 213 |
def flush(self): |
| 214 |
if self.buf_count > 0: |
| 215 |
bytes = self.buf_count * rffi.sizeof(rffi.SIGNED) |
| 216 |
count = raw_os_write(self.fd, |
| 217 |
rffi.cast(llmemory.Address, self.writebuffer), |
| 218 |
rffi.cast(rffi.SIZE_T, bytes)) |
| 219 |
if rffi.cast(lltype.Signed, count) != bytes: |
| 220 |
raise OSError(rffi.cast(lltype.Signed, rposix._get_errno()), |
| 221 |
"raw_os_write failed") |
| 222 |
self.buf_count = 0 |
| 223 |
flush._dont_inline_ = True |
| 224 |
|
| 225 |
def write(self, value): |
| 226 |
x = self.buf_count |
| 227 |
self.writebuffer[x] = value |
| 228 |
x += 1 |
| 229 |
self.buf_count = x |
| 230 |
if x == self.BUFSIZE: |
| 231 |
self.flush() |
| 232 |
write._always_inline_ = True |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
def write_marker(self): |
| 237 |
self.write(0) |
| 238 |
self.write(0) |
| 239 |
self.write(0) |
| 240 |
self.write(-1) |
| 241 |
end_add_roots_marker = write_marker |
| 242 |
|
| 243 |
def writeobj(self, obj): |
| 244 |
gc = self.gc |
| 245 |
typeid = gc.get_type_id(obj) |
| 246 |
self.write(llmemory.cast_adr_to_int(obj)) |
| 247 |
self.write(gc.get_member_index(typeid)) |
| 248 |
self.write(gc.get_size_incl_hash(obj)) |
| 249 |
gc.trace(obj, gc.make_callback('_writeref'), self, None) |
| 250 |
self.write(-1) |
| 251 |
processobj = writeobj |
| 252 |
|
| 253 |
def _writeref(self, pointer, ignored): |
| 254 |
obj = pointer.address[0] |
| 255 |
self.write(llmemory.cast_adr_to_int(obj)) |
| 256 |
self.add(obj) |
| 257 |
|
| 258 |
|
| 259 |
def _hd_add_root(obj, heap_dumper): |
| 260 |
heap_dumper.add(obj) |
| 261 |
|
| 262 |
def _hd_unadd_root(obj, heap_dumper): |
| 263 |
heap_dumper.unadd(obj) |
| 264 |
|
| 265 |
def dump_rpy_heap(gc, fd): |
| 266 |
heapdumper = HeapDumper(gc, fd) |
| 267 |
heapdumper.process() |
| 268 |
heapdumper.flush() |
| 269 |
heapdumper.finish_processing() |
| 270 |
heapdumper.delete() |
| 271 |
return True |
| 272 |
|
| 273 |
def count_memory_pressure(gc): |
| 274 |
counter = MemoryPressureCounter(gc) |
| 275 |
counter.process() |
| 276 |
counter.finish_processing() |
| 277 |
res = counter.count |
| 278 |
counter.delete() |
| 279 |
return res |
| 280 |
|
| 281 |
def get_typeids_z(gc): |
| 282 |
srcaddress = gc.root_walker.gcdata.typeids_z |
| 283 |
return llmemory.cast_adr_to_ptr(srcaddress, lltype.Ptr(rgc.ARRAY_OF_CHAR)) |
| 284 |
|
| 285 |
def get_typeids_list(gc): |
| 286 |
srcaddress = gc.root_walker.gcdata.typeids_list |
| 287 |
return llmemory.cast_adr_to_ptr(srcaddress, lltype.Ptr(ARRAY_OF_HALFWORDS)) |
| 288 |
ARRAY_OF_HALFWORDS = lltype.Array(llgroup.HALFWORD) |