Debian Codegraph @

pypy/tool/gcdump.py

raw symbols SCIP
1 #! /usr/bin/env python
2 """
3 Prints a human-readable total out of a dumpfile produced
4 by gc.dump_rpy_heap(), and optionally a typeids.txt.
5
6 Syntax: dump.py <dumpfile> [<typeids.txt>]
7
8 By default, typeids.txt is loaded from the same dir as dumpfile.
9 """
10 from __future__ import print_function
11 import sys, array, struct, os
12
13
14 class Stat(object):
15 summary = {}
16 typeids = {0: '<GCROOT>'}
17 BIGOBJ = 65536 # bytes
18
19 def summarize(self, filename):
20 a = self.load_dump_file(filename)
21 self.summary = {} # {typenum: [count, totalsize]}
22 self.bigobjs = [] # list of individual (size, typenum)
23 for obj in self.walk(a):
24 self.add_object_summary(obj[2], obj[3])
25
26 def load_typeids(self, filename_or_iter):
27 self.typeids = Stat.typeids.copy()
28 if isinstance(filename_or_iter, str):
29 iter = open(filename_or_iter)
30 else:
31 iter = filename_or_iter
32 for num, line in enumerate(iter):
33 if num == 0:
34 continue
35 if not line:
36 continue
37 words = line.split()
38 if words[0].startswith('member'):
39 del words[0]
40 if words[0] == 'GcStruct':
41 del words[0]
42 self.typeids[num] = ' '.join(words)
43
44 def get_type_name(self, num):
45 return self.typeids.get(num, '<typenum %d>' % num)
46
47 def print_summary(self):
48 items = list(self.summary.items())
49 items.sort(key=lambda element: element[1][1]) # sort by totalsize
50 totalsize = 0
51 for typenum, stat in items:
52 totalsize += stat[1]
53 print('%8d %8.2fM %s' % (stat[0], stat[1] / (1024.0*1024.0),
54 self.get_type_name(typenum)))
55 print('total %.1fM' % (totalsize / (1024.0*1024.0),))
56 print
57 lst = sorted(self.bigobjs)[-10:]
58 if lst:
59 if len(lst) == len(self.bigobjs):
60 print('%d objects take at least %d bytes each:' % (len(lst), self.BIGOBJ))
61 else:
62 print('%d largest single objects:' % (len(lst),))
63 for size, typenum in lst:
64 print('%8s %8.2fM %s' % ('', size / (1024.0*1024.0),
65 self.get_type_name(typenum)))
66 else:
67 print('No object takes at least %d bytes on its own.' % (self.BIGOBJ,))
68
69 def load_dump_file(self, filename):
70 f = open(filename, 'rb')
71 f.seek(0, 2)
72 end = f.tell()
73 f.seek(0)
74 a = array.array('l')
75 a.fromfile(f, end // struct.calcsize('l'))
76 f.close()
77 return a
78
79 def add_object_summary(self, typenum, sizeobj):
80 if sizeobj >= self.BIGOBJ:
81 self.bigobjs.append((sizeobj, typenum))
82 try:
83 stat = self.summary[typenum]
84 except KeyError:
85 stat = self.summary[typenum] = [0, 0]
86 stat[0] += 1
87 stat[1] += sizeobj
88
89 def walk(self, a, start=0, stop=None):
90 assert a[-1] == -1, "invalid or truncated dump file (or 32/64-bit mix)"
91 assert a[-2] != -1, "invalid or truncated dump file (or 32/64-bit mix)"
92 print('walking...', file=sys.stderr)
93 i = start
94 if stop is None:
95 stop = len(a)
96 while i < stop:
97 j = i + 3
98 while a[j] != -1:
99 j += 1
100 yield (i, a[i], a[i+1], a[i+2], a[i+3:j])
101 i = j + 1
102 print('done', file=sys.stderr)
103
104
105 if __name__ == '__main__':
106 if len(sys.argv) <= 1:
107 print(__doc__, file=sys.stderr)
108 sys.exit(2)
109 stat = Stat()
110 stat.summarize(sys.argv[1])
111 #
112 if len(sys.argv) > 2:
113 typeid_name = sys.argv[2]
114 else:
115 typeid_name = os.path.join(os.path.dirname(sys.argv[1]), 'typeids.txt')
116 if os.path.isfile(typeid_name):
117 stat.load_typeids(typeid_name)
118 else:
119 import zlib, gc
120 stat.load_typeids(zlib.decompress(gc.get_typeids_z()).split("\n"))
121 #
122 stat.print_summary()