Debian Codegraph @

rpython/tool/lsprofcalltree.py

raw symbols SCIP
1 # lsprofcalltree.py: lsprof output which is readable by kcachegrind
2 # David Allouche
3 # Jp Calderone & Itamar Shtull-Trauring
4 # Johan Dahlin
5 from __future__ import print_function
6
7 import optparse
8 import os
9 import sys
10
11 try:
12 import cProfile
13 except ImportError:
14 raise SystemExit("This script requires cProfile from Python 2.5")
15
16 def label(code):
17 if isinstance(code, str):
18 return ('~', 0, code) # built-in functions ('~' sorts at the end)
19 else:
20 return '%s %s:%d' % (code.co_name,
21 code.co_filename,
22 code.co_firstlineno)
23
24 class KCacheGrind(object):
25 def __init__(self, profiler):
26 self.data = profiler.getstats()
27 self.out_file = None
28
29 def output(self, out_file):
30 self.out_file = out_file
31 print('events: Ticks', file=out_file)
32 self._print_summary()
33 for entry in self.data:
34 self._entry(entry)
35
36 def _print_summary(self):
37 max_cost = 0
38 for entry in self.data:
39 totaltime = int(entry.totaltime * 1000)
40 max_cost = max(max_cost, totaltime)
41 print('summary: %d' % (max_cost,), file=self.out_file)
42
43 def _entry(self, entry):
44 out_file = self.out_file
45
46 code = entry.code
47 #print >> out_file, 'ob=%s' % (code.co_filename,)
48 if isinstance(code, str):
49 print('fi=~', file=out_file)
50 else:
51 print('fi=%s' % (code.co_filename,), file=out_file)
52 print('fn=%s' % (label(code),), file=out_file)
53
54 inlinetime = int(entry.inlinetime * 1000)
55 if isinstance(code, str):
56 print('0 ', inlinetime, file=out_file)
57 else:
58 print('%d %d' % (code.co_firstlineno, inlinetime), file=out_file)
59
60 # recursive calls are counted in entry.calls
61 if entry.calls:
62 calls = entry.calls
63 else:
64 calls = []
65
66 if isinstance(code, str):
67 lineno = 0
68 else:
69 lineno = code.co_firstlineno
70
71 for subentry in calls:
72 self._subentry(lineno, subentry)
73 print(file=out_file)
74
75 def _subentry(self, lineno, subentry):
76 out_file = self.out_file
77 code = subentry.code
78 #print >> out_file, 'cob=%s' % (code.co_filename,)
79 print('cfn=%s' % (label(code),), file=out_file)
80 if isinstance(code, str):
81 print('cfi=~', file=out_file)
82 print('calls=%d 0' % (subentry.callcount,), file=out_file)
83 else:
84 print('cfi=%s' % (code.co_filename,), file=out_file)
85 print('calls=%d %d' % (
86 subentry.callcount, code.co_firstlineno), file=out_file)
87
88 totaltime = int(subentry.totaltime * 1000)
89 print('%d %d' % (lineno, totaltime), file=out_file)
90
91 def main(args):
92 usage = "%s [-o output_file_path] scriptfile [arg] ..."
93 parser = optparse.OptionParser(usage=usage % sys.argv[0])
94 parser.allow_interspersed_args = False
95 parser.add_option('-o', '--outfile', dest="outfile",
96 help="Save stats to <outfile>", default=None)
97
98 if not sys.argv[1:]:
99 parser.print_usage()
100 sys.exit(2)
101
102 options, args = parser.parse_args()
103
104 if not options.outfile:
105 options.outfile = '%s.log' % os.path.basename(args[0])
106
107 sys.argv[:] = args
108
109 prof = cProfile.Profile()
110 try:
111 try:
112 prof = prof.run('execfile(%r)' % (sys.argv[0],))
113 except SystemExit:
114 pass
115 finally:
116 kg = KCacheGrind(prof)
117 kg.output(file(options.outfile, 'w'))
118
119 if __name__ == '__main__':
120 sys.exit(main(sys.argv))