pypy / pypy / tool /

#! /usr/bin/env python
Prints a human-readable total out of a dumpfile produced
by gc.dump_rpy_heap(), and optionally a typeids.txt.

Syntax:  <dumpfile>  [<typeids.txt>]

By default, typeids.txt is loaded from the same dir as dumpfile.
import sys, array, struct, os

class Stat(object):
    summary = {}
    typeids = {0: '<GCROOT>'}

    def summarize(self, filename):
        a = self.load_dump_file(filename)
        self.summary = {}     # {typenum: [count, totalsize]}
        for obj in self.walk(a):
            self.add_object_summary(obj[2], obj[3])

    def load_typeids(self, filename):
        self.typeids = Stat.typeids.copy()
        for num, line in enumerate(open(filename)):
            if num == 0:
            words = line.split()
            if words[0].startswith('member'):
                del words[0]
            if words[0] == 'GcStruct':
                del words[0]
            self.typeids[num] = ' '.join(words)

    def get_type_name(self, num):
        return self.typeids.get(num, '<typenum %d>' % num)

    def print_summary(self):
        items = self.summary.items()
        items.sort(key=lambda(typenum, stat): stat[1])    # sort by totalsize
        totalsize = 0
        for typenum, stat in items:
            totalsize += stat[1]
            print '%8d %8.2fM  %s' % (stat[0], stat[1] / (1024.0*1024.0),
        print 'total %.1fM' % (totalsize / (1024.0*1024.0),)

    def load_dump_file(self, filename):
        f = open(filename, 'rb'), 2)
        end = f.tell()
        a = array.array('l')
        a.fromfile(f, end / struct.calcsize('l'))
        return a

    def add_object_summary(self, typenum, sizeobj):
            stat = self.summary[typenum]
        except KeyError:
            stat = self.summary[typenum] = [0, 0]
        stat[0] += 1
        stat[1] += sizeobj

    def walk(self, a, start=0, stop=None):
        assert a[-1] == -1, "invalid or truncated dump file (or 32/64-bit mix)"
        assert a[-2] != -1, "invalid or truncated dump file (or 32/64-bit mix)"
        print >> sys.stderr, 'walking...',
        i = start
        if stop is None:
            stop = len(a)
        while i < stop:
            j = i + 3
            while a[j] != -1:
                j += 1
            yield (i, a[i], a[i+1], a[i+2], a[i+3:j])
            i = j + 1
        print >> sys.stderr, 'done'

if __name__ == '__main__':
    if len(sys.argv) <= 1:
        print >> sys.stderr, __doc__
    stat = Stat()
    if len(sys.argv) > 2:
        typeid_name = sys.argv[2]
        typeid_name = os.path.join(os.path.dirname(sys.argv[1]), 'typeids.txt')
    if os.path.isfile(typeid_name):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.