author | Augie Fackler <augie@google.com> |
Sat, 14 Oct 2017 11:59:51 -0400 | |
changeset 34724 | 9c3dcaf648ef |
parent 27618 | 5a988b3c9645 |
child 40194 | 1ae0faa14797 |
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 |
|
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
13 |
from __future__ import absolute_import, print_function |
27505
071af8d385a9
lsprofcalltree: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
8390
diff
changeset
|
14 |
|
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
15 |
def label(code): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
16 |
if isinstance(code, str): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
17 |
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
|
18 |
else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
19 |
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
|
20 |
code.co_filename, |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
21 |
code.co_firstlineno) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
22 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
23 |
class KCacheGrind(object): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
24 |
def __init__(self, profiler): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
25 |
self.data = profiler.getstats() |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
26 |
self.out_file = None |
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 |
def output(self, out_file): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
29 |
self.out_file = out_file |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
30 |
print('events: Ticks', file=out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
31 |
self._print_summary() |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
32 |
for entry in self.data: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
33 |
self._entry(entry) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
34 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
35 |
def _print_summary(self): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
36 |
max_cost = 0 |
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 |
totaltime = int(entry.totaltime * 1000) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
39 |
max_cost = max(max_cost, totaltime) |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
40 |
print('summary: %d' % max_cost, file=self.out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
41 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
42 |
def _entry(self, entry): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
43 |
out_file = self.out_file |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
44 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
45 |
code = entry.code |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
46 |
if isinstance(code, str): |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
47 |
print('fi=~', file=out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
48 |
else: |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
49 |
print('fi=%s' % code.co_filename, file=out_file) |
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
50 |
print('fn=%s' % label(code), file=out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
51 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
52 |
inlinetime = int(entry.inlinetime * 1000) |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
53 |
if isinstance(code, str): |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
54 |
print('0 ', inlinetime, file=out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
55 |
else: |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
56 |
print('%d %d' % (code.co_firstlineno, inlinetime), file=out_file) |
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 |
# 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
|
59 |
if entry.calls: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
60 |
calls = entry.calls |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
61 |
else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
62 |
calls = [] |
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 |
if isinstance(code, str): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
65 |
lineno = 0 |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
66 |
else: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
67 |
lineno = code.co_firstlineno |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
68 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
69 |
for subentry in calls: |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
70 |
self._subentry(lineno, subentry) |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
71 |
print(file=out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
72 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
73 |
def _subentry(self, lineno, subentry): |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
74 |
out_file = self.out_file |
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
75 |
code = subentry.code |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
76 |
print('cfn=%s' % label(code), file=out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
77 |
if isinstance(code, str): |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
78 |
print('cfi=~', file=out_file) |
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
79 |
print('calls=%d 0' % subentry.callcount, file=out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
80 |
else: |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
81 |
print('cfi=%s' % code.co_filename, file=out_file) |
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
82 |
print('calls=%d %d' % ( |
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
83 |
subentry.callcount, code.co_firstlineno), file=out_file) |
8024
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
84 |
|
9a1b86cfd29e
profiling: Adding support for kcachegrind output format, using lsprofcalltree
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff
changeset
|
85 |
totaltime = int(subentry.totaltime * 1000) |
27618
5a988b3c9645
lsprofcalltree: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27505
diff
changeset
|
86 |
print('%d %d' % (lineno, totaltime), file=out_file) |