Mercurial > hg
annotate mercurial/lsprofcalltree.py @ 8093:70d8f70264c4
run-tests: move bits of main program so it's all at the bottom.
author | Greg Ward <greg-hg@gerg.ca> |
---|---|
date | Mon, 20 Apr 2009 21:10:39 -0400 |
parents | 9a1b86cfd29e |
children | beae42f3d93b |
rev | line source |
---|---|
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
1 """ |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
2 lsprofcalltree.py - lsprof output which is readable by kcachegrind |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
3 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
4 Authors: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
5 * David Allouche <david <at> allouche.net> |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
6 * Jp Calderone & Itamar Shtull-Trauring |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
7 * Johan Dahlin |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
8 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
9 This software may be used and distributed according to the terms |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
10 of the GNU General Public License, incorporated herein by reference. |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
11 """ |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
12 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
13 import optparse |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
14 import os |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
15 import sys |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
16 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
17 def label(code): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
18 if isinstance(code, str): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
19 return '~' + code # built-in functions ('~' sorts at the end) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
20 else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
21 return '%s %s:%d' % (code.co_name, |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
22 code.co_filename, |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
23 code.co_firstlineno) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
24 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
25 class KCacheGrind(object): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
26 def __init__(self, profiler): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
27 self.data = profiler.getstats() |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
28 self.out_file = None |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
29 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
30 def output(self, out_file): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
31 self.out_file = out_file |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
32 print >> out_file, 'events: Ticks' |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
33 self._print_summary() |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
34 for entry in self.data: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
35 self._entry(entry) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
36 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
37 def _print_summary(self): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
38 max_cost = 0 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
39 for entry in self.data: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
40 totaltime = int(entry.totaltime * 1000) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
41 max_cost = max(max_cost, totaltime) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
42 print >> self.out_file, 'summary: %d' % (max_cost,) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
43 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
44 def _entry(self, entry): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
45 out_file = self.out_file |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
46 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
47 code = entry.code |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
48 #print >> out_file, 'ob=%s' % (code.co_filename,) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
49 if isinstance(code, str): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
50 print >> out_file, 'fi=~' |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
51 else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
52 print >> out_file, 'fi=%s' % (code.co_filename,) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
53 print >> out_file, 'fn=%s' % (label(code),) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
54 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
55 inlinetime = int(entry.inlinetime * 1000) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
56 if isinstance(code, str): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
57 print >> out_file, '0 ', inlinetime |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
58 else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
59 print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
60 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
61 # recursive calls are counted in entry.calls |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
62 if entry.calls: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
63 calls = entry.calls |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
64 else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
65 calls = [] |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
66 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
67 if isinstance(code, str): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
68 lineno = 0 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
69 else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
70 lineno = code.co_firstlineno |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
71 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
72 for subentry in calls: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
73 self._subentry(lineno, subentry) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
74 print >> out_file |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
75 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
76 def _subentry(self, lineno, subentry): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
77 out_file = self.out_file |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
78 code = subentry.code |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
79 #print >> out_file, 'cob=%s' % (code.co_filename,) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
80 print >> out_file, 'cfn=%s' % (label(code),) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
81 if isinstance(code, str): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
82 print >> out_file, 'cfi=~' |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
83 print >> out_file, 'calls=%d 0' % (subentry.callcount,) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
84 else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
85 print >> out_file, 'cfi=%s' % (code.co_filename,) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
86 print >> out_file, 'calls=%d %d' % ( |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
87 subentry.callcount, code.co_firstlineno) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
88 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
89 totaltime = int(subentry.totaltime * 1000) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
90 print >> out_file, '%d %d' % (lineno, totaltime) |