| 1 |
|
| 2 |
|
| 3 |
import gc |
| 4 |
|
| 5 |
def dump_rpy_heap(file): |
| 6 |
"""Write a full dump of the objects in the heap to the given file |
| 7 |
(which can be a file, a file name, or a file descritor). |
| 8 |
Format for each object (each item is one machine word): |
| 9 |
|
| 10 |
[addr] [typeindex] [size] [addr1]..[addrn] [-1] |
| 11 |
|
| 12 |
where [addr] is the address of the object, [typeindex] and [size] |
| 13 |
are as get_rpy_type_index() and get_rpy_memory_usage() would return, |
| 14 |
and [addr1]..[addrn] are addresses of other objects that this object |
| 15 |
points to. The full dump is a list of such objects, with a marker |
| 16 |
[0][0][0][-1] inserted after all GC roots, before all non-roots. |
| 17 |
|
| 18 |
If the argument is a filename and the 'zlib' module is available, |
| 19 |
we also write 'typeids.txt' and 'typeids.lst' in the same directory, |
| 20 |
if they don't already exist. |
| 21 |
""" |
| 22 |
if isinstance(file, str): |
| 23 |
f = open(file, 'wb') |
| 24 |
gc._dump_rpy_heap(f.fileno()) |
| 25 |
f.close() |
| 26 |
try: |
| 27 |
import zlib, os |
| 28 |
except ImportError: |
| 29 |
pass |
| 30 |
else: |
| 31 |
filename2 = os.path.join(os.path.dirname(file), 'typeids.txt') |
| 32 |
if not os.path.exists(filename2): |
| 33 |
data = zlib.decompress(gc.get_typeids_z()) |
| 34 |
f = open(filename2, 'wb') |
| 35 |
f.write(data) |
| 36 |
f.close() |
| 37 |
filename2 = os.path.join(os.path.dirname(file), 'typeids.lst') |
| 38 |
if not os.path.exists(filename2): |
| 39 |
data = ''.join(['%d\n' % n for n in gc.get_typeids_list()]) |
| 40 |
f = open(filename2, 'w') |
| 41 |
f.write(data) |
| 42 |
f.close() |
| 43 |
else: |
| 44 |
if isinstance(file, int): |
| 45 |
fd = file |
| 46 |
else: |
| 47 |
if hasattr(file, 'flush'): |
| 48 |
file.flush() |
| 49 |
fd = file.fileno() |
| 50 |
gc._dump_rpy_heap(fd) |
| 51 |
|
| 52 |
class GcStats(object): |
| 53 |
def __init__(self, s): |
| 54 |
self._s = s |
| 55 |
for item in ('total_gc_memory', 'jit_backend_used', |
| 56 |
'total_memory_pressure', |
| 57 |
'total_allocated_memory', 'jit_backend_allocated', |
| 58 |
'peak_memory', 'peak_allocated_memory', 'total_arena_memory', |
| 59 |
'total_rawmalloced_memory', 'nursery_size', |
| 60 |
'peak_arena_memory', 'peak_rawmalloced_memory', |
| 61 |
): |
| 62 |
setattr(self, item, self._format(getattr(self._s, item))) |
| 63 |
memory_pressure = 0 |
| 64 |
if self._s.total_memory_pressure != -1: |
| 65 |
memory_pressure = self._s.total_memory_pressure |
| 66 |
self.memory_used_sum = self._format(self._s.total_gc_memory + memory_pressure + |
| 67 |
self._s.jit_backend_used) |
| 68 |
self.memory_allocated_sum = self._format(self._s.total_allocated_memory + memory_pressure + |
| 69 |
self._s.jit_backend_allocated) |
| 70 |
self.total_gc_time = self._s.total_gc_time |
| 71 |
|
| 72 |
def _format(self, v): |
| 73 |
if v < 1000000: |
| 74 |
|
| 75 |
return "%.1fkB" % (v / 1024.) |
| 76 |
return "%.1fMB" % (v / 1024. / 1024.) |
| 77 |
|
| 78 |
def __repr__(self): |
| 79 |
|
| 80 |
if self._s.total_memory_pressure != -1: |
| 81 |
extra = "\n memory pressure: %s" % self.total_memory_pressure |
| 82 |
else: |
| 83 |
extra = "" |
| 84 |
total_arena_allocated = self._format(self._s.total_allocated_memory - self._s.total_rawmalloced_memory - self._s.nursery_size) |
| 85 |
return """Total memory consumed: |
| 86 |
GC used: %s (peak: %s) |
| 87 |
in arenas: %s (peak: %s) |
| 88 |
rawmalloced: %s (peak: %s) |
| 89 |
nursery: %s |
| 90 |
raw assembler used: %s%s |
| 91 |
----------------------------- |
| 92 |
Total: %s |
| 93 |
|
| 94 |
Total memory allocated (includes freelists): |
| 95 |
GC allocated: %s (peak: %s) |
| 96 |
in arenas: %s |
| 97 |
rawmalloced: %s |
| 98 |
nursery: %s |
| 99 |
raw assembler allocated: %s%s |
| 100 |
----------------------------- |
| 101 |
Total: %s |
| 102 |
|
| 103 |
Total time spent in GC: %s |
| 104 |
""" % (self.total_gc_memory, self.peak_memory, |
| 105 |
self.total_arena_memory, self.peak_arena_memory, |
| 106 |
self.total_rawmalloced_memory, self.peak_rawmalloced_memory, |
| 107 |
self.nursery_size, |
| 108 |
self.jit_backend_used, |
| 109 |
extra, |
| 110 |
self.memory_used_sum, |
| 111 |
|
| 112 |
self.total_allocated_memory, self.peak_allocated_memory, |
| 113 |
total_arena_allocated, |
| 114 |
self.total_rawmalloced_memory, |
| 115 |
self.nursery_size, |
| 116 |
self.jit_backend_allocated, |
| 117 |
extra, |
| 118 |
self.memory_allocated_sum, |
| 119 |
self.total_gc_time / 1000.0) |
| 120 |
|
| 121 |
|
| 122 |
def get_stats(memory_pressure=False): |
| 123 |
return GcStats(gc._get_stats(memory_pressure=memory_pressure)) |