1 | |
---|
2 | # Attempt to load guppy module, and only define Memdump class |
---|
3 | # if available |
---|
4 | |
---|
5 | try: |
---|
6 | |
---|
7 | import pkg_resources |
---|
8 | pkg_resources.require( "guppy" ) |
---|
9 | |
---|
10 | except: |
---|
11 | |
---|
12 | import sys |
---|
13 | print >> sys.stderr, "No guppy module, Memdump not available" |
---|
14 | Memdump = None |
---|
15 | |
---|
16 | else: |
---|
17 | |
---|
18 | import os, sys, signal, time, guppy |
---|
19 | |
---|
20 | class Memdump( object ): |
---|
21 | def __init__( self, signum=signal.SIGUSR1, fname="memdump.log" ): |
---|
22 | self.fname = fname |
---|
23 | signal.signal( signum, self.dump ) |
---|
24 | self.heapy = guppy.hpy() |
---|
25 | self.heap = None |
---|
26 | def dump( self, signum, stack ): |
---|
27 | file = open( self.fname, "a" ) |
---|
28 | print >> file, "Memdump for pid %d at %s" % ( os.getpid(), time.asctime() ) |
---|
29 | print >> file |
---|
30 | try: |
---|
31 | self.heap = self.heapy.heap() |
---|
32 | print >> file, "heap():" |
---|
33 | print >> file, self.heap |
---|
34 | print >> file, "\nbyrcs:" |
---|
35 | print >> file, self.heap.byrcs |
---|
36 | print >> file, "\nbyrcs[0].byid:" |
---|
37 | print >> file, self.heap.byrcs[0].byid |
---|
38 | print >> file, "\nget_rp():" |
---|
39 | print >> file, self.heap.get_rp() |
---|
40 | self.heapy.setref() |
---|
41 | except AssertionError: |
---|
42 | pass |
---|
43 | print >> file, "\nEnd dump\n" |
---|
44 | file.close() |
---|
45 | def setref( self ): |
---|
46 | self.heapy.setref() |
---|
47 | def get( self, update=False ): |
---|
48 | if update: |
---|
49 | self.heap = self.heapy.heap() |
---|
50 | return self.heap |
---|