author | Gregory Szorc <gregory.szorc@gmail.com> |
Sat, 26 Jan 2019 10:53:10 -0800 | |
changeset 41406 | 2bf689b13a17 |
parent 40195 | 720355c7b7c9 |
child 43076 | 2372284d9457 |
permissions | -rw-r--r-- |
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 |
|
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
13 |
from __future__ import absolute_import |
27505
071af8d385a9
lsprofcalltree: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
8390
diff
changeset
|
14 |
|
40195
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
15 |
from . import ( |
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
16 |
pycompat, |
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
17 |
) |
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
18 |
|
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
19 |
def label(code): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
20 |
if isinstance(code, str): |
40195
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
21 |
# built-in functions ('~' sorts at the end) |
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
22 |
return '~' + pycompat.sysbytes(code) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
23 |
else: |
40195
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
24 |
return '%s %s:%d' % (pycompat.sysbytes(code.co_name), |
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
25 |
pycompat.sysbytes(code.co_filename), |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
26 |
code.co_firstlineno) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
27 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
28 |
class KCacheGrind(object): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
29 |
def __init__(self, profiler): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
30 |
self.data = profiler.getstats() |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
31 |
self.out_file = None |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
32 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
33 |
def output(self, out_file): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
34 |
self.out_file = out_file |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
35 |
out_file.write(b'events: Ticks\n') |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
36 |
self._print_summary() |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
37 |
for entry in self.data: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
38 |
self._entry(entry) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
39 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
40 |
def _print_summary(self): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
41 |
max_cost = 0 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
42 |
for entry in self.data: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
43 |
totaltime = int(entry.totaltime * 1000) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
44 |
max_cost = max(max_cost, totaltime) |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
45 |
self.out_file.write(b'summary: %d\n' % max_cost) |
8024
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 |
def _entry(self, entry): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
48 |
out_file = self.out_file |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
49 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
50 |
code = entry.code |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
51 |
if isinstance(code, str): |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
52 |
out_file.write(b'fi=~\n') |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
53 |
else: |
40195
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
54 |
out_file.write(b'fi=%s\n' % pycompat.sysbytes(code.co_filename)) |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
55 |
|
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
56 |
out_file.write(b'fn=%s\n' % label(code)) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
57 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
58 |
inlinetime = int(entry.inlinetime * 1000) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
59 |
if isinstance(code, str): |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
60 |
out_file.write(b'0 %d\n' % inlinetime) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
61 |
else: |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
62 |
out_file.write(b'%d %d\n' % (code.co_firstlineno, inlinetime)) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
63 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
64 |
# 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
|
65 |
if entry.calls: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
66 |
calls = entry.calls |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
67 |
else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
68 |
calls = [] |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
69 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
70 |
if isinstance(code, str): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
71 |
lineno = 0 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
72 |
else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
73 |
lineno = code.co_firstlineno |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
74 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
75 |
for subentry in calls: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
76 |
self._subentry(lineno, subentry) |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
77 |
|
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
78 |
out_file.write(b'\n') |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
79 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
80 |
def _subentry(self, lineno, subentry): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
81 |
out_file = self.out_file |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
82 |
code = subentry.code |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
83 |
out_file.write(b'cfn=%s\n' % label(code)) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
84 |
if isinstance(code, str): |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
85 |
out_file.write(b'cfi=~\n') |
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
86 |
out_file.write(b'calls=%d 0\n' % subentry.callcount) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
87 |
else: |
40195
720355c7b7c9
py3: use sysbytes for converting code attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40194
diff
changeset
|
88 |
out_file.write(b'cfi=%s\n' % pycompat.sysbytes(code.co_filename)) |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
89 |
out_file.write(b'calls=%d %d\n' % ( |
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
90 |
subentry.callcount, code.co_firstlineno)) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
91 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
92 |
totaltime = int(subentry.totaltime * 1000) |
40194
1ae0faa14797
py3: use write() instead of print()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27618
diff
changeset
|
93 |
out_file.write(b'%d %d\n' % (lineno, totaltime)) |