Show More
@@ -1,86 +1,88 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | lsprofcalltree.py - lsprof output which is readable by kcachegrind |
|
3 | 3 | |
|
4 | 4 | Authors: |
|
5 | 5 | * David Allouche <david <at> allouche.net> |
|
6 | 6 | * Jp Calderone & Itamar Shtull-Trauring |
|
7 | 7 | * Johan Dahlin |
|
8 | 8 | |
|
9 | 9 | This software may be used and distributed according to the terms |
|
10 | 10 | of the GNU General Public License, incorporated herein by reference. |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 |
from __future__ import absolute_import |
|
|
13 | from __future__ import absolute_import | |
|
14 | 14 | |
|
15 | 15 | def label(code): |
|
16 | 16 | if isinstance(code, str): |
|
17 | 17 | return '~' + code # built-in functions ('~' sorts at the end) |
|
18 | 18 | else: |
|
19 | 19 | return '%s %s:%d' % (code.co_name, |
|
20 | 20 | code.co_filename, |
|
21 | 21 | code.co_firstlineno) |
|
22 | 22 | |
|
23 | 23 | class KCacheGrind(object): |
|
24 | 24 | def __init__(self, profiler): |
|
25 | 25 | self.data = profiler.getstats() |
|
26 | 26 | self.out_file = None |
|
27 | 27 | |
|
28 | 28 | def output(self, out_file): |
|
29 | 29 | self.out_file = out_file |
|
30 |
|
|
|
30 | out_file.write(b'events: Ticks\n') | |
|
31 | 31 | self._print_summary() |
|
32 | 32 | for entry in self.data: |
|
33 | 33 | self._entry(entry) |
|
34 | 34 | |
|
35 | 35 | def _print_summary(self): |
|
36 | 36 | max_cost = 0 |
|
37 | 37 | for entry in self.data: |
|
38 | 38 | totaltime = int(entry.totaltime * 1000) |
|
39 | 39 | max_cost = max(max_cost, totaltime) |
|
40 | print('summary: %d' % max_cost, file=self.out_file) | |
|
40 | self.out_file.write(b'summary: %d\n' % max_cost) | |
|
41 | 41 | |
|
42 | 42 | def _entry(self, entry): |
|
43 | 43 | out_file = self.out_file |
|
44 | 44 | |
|
45 | 45 | code = entry.code |
|
46 | 46 | if isinstance(code, str): |
|
47 | print('fi=~', file=out_file) | |
|
47 | out_file.write(b'fi=~\n') | |
|
48 | 48 | else: |
|
49 |
|
|
|
50 | print('fn=%s' % label(code), file=out_file) | |
|
49 | out_file.write(b'fi=%s\n' % code.co_filename) | |
|
50 | ||
|
51 | out_file.write(b'fn=%s\n' % label(code)) | |
|
51 | 52 | |
|
52 | 53 | inlinetime = int(entry.inlinetime * 1000) |
|
53 | 54 | if isinstance(code, str): |
|
54 |
|
|
|
55 | out_file.write(b'0 %d\n' % inlinetime) | |
|
55 | 56 | else: |
|
56 |
|
|
|
57 | out_file.write(b'%d %d\n' % (code.co_firstlineno, inlinetime)) | |
|
57 | 58 | |
|
58 | 59 | # recursive calls are counted in entry.calls |
|
59 | 60 | if entry.calls: |
|
60 | 61 | calls = entry.calls |
|
61 | 62 | else: |
|
62 | 63 | calls = [] |
|
63 | 64 | |
|
64 | 65 | if isinstance(code, str): |
|
65 | 66 | lineno = 0 |
|
66 | 67 | else: |
|
67 | 68 | lineno = code.co_firstlineno |
|
68 | 69 | |
|
69 | 70 | for subentry in calls: |
|
70 | 71 | self._subentry(lineno, subentry) |
|
71 | print(file=out_file) | |
|
72 | ||
|
73 | out_file.write(b'\n') | |
|
72 | 74 | |
|
73 | 75 | def _subentry(self, lineno, subentry): |
|
74 | 76 | out_file = self.out_file |
|
75 | 77 | code = subentry.code |
|
76 |
|
|
|
78 | out_file.write(b'cfn=%s\n' % label(code)) | |
|
77 | 79 | if isinstance(code, str): |
|
78 | print('cfi=~', file=out_file) | |
|
79 |
|
|
|
80 | out_file.write(b'cfi=~\n') | |
|
81 | out_file.write(b'calls=%d 0\n' % subentry.callcount) | |
|
80 | 82 | else: |
|
81 |
|
|
|
82 |
|
|
|
83 |
subentry.callcount, code.co_firstlineno) |
|
|
83 | out_file.write(b'cfi=%s\n' % code.co_filename) | |
|
84 | out_file.write(b'calls=%d %d\n' % ( | |
|
85 | subentry.callcount, code.co_firstlineno)) | |
|
84 | 86 | |
|
85 | 87 | totaltime = int(subentry.totaltime * 1000) |
|
86 |
|
|
|
88 | out_file.write(b'%d %d\n' % (lineno, totaltime)) |
General Comments 0
You need to be logged in to leave comments.
Login now