Repository navigation
Expand file tree
/
Copy pathshow.py
More file actions
265 lines (208 loc) · 8.74 KB
/
Copy pathshow.py
File metadata and controls
265 lines (208 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from __future__ import absolute_import
import inspect
import linecache
import os
import six
import sys
import vmprof
import argparse
class color(six.text_type):
RED = '\033[31m'
WHITE = '\033[37m'
BLUE = '\033[94m'
BOLD = '\033[1m'
END = '\033[0m'
def __new__(cls, content, color, bold=False):
return six.text_type.__new__(
cls, "%s%s%s%s" % (color, cls.BOLD if bold else "", content, cls.END))
class PrettyPrinter(object):
"""
A pretty print for vmprof profile files.
"""
def __init__(self, prune_percent=None, prune_level=None, indent=None):
"""
:param prune_percent: Prune output of a profile stats node when CPU consumed
is less than this value for the node and everything below.
:type prune_percent: None or float
:param prune_level: Prune output of a profile stats node when the node is deeper
than this level down the call graph from the very top.
:type prune_level: None or int
:param indent: The indention per level within the call graph.
:type indent: None or int
"""
assert(prune_percent is None or (prune_percent >= 0 and prune_percent <= 100))
assert(prune_level is None or (prune_level >= 0 and prune_level <= 1000))
assert(indent is None or (indent >= 0 and indent <= 4))
self._prune_percent = prune_percent or 0.
self._prune_level = prune_level or 1000
self._indent = indent or 2
def show(self, profile):
"""
Read and display a vmprof profile file.
:param profile: The filename of the vmprof profile file to display.
:type profile: str
"""
try:
stats = vmprof.read_profile(profile)
except Exception as e:
print("Fatal: could not read vmprof profile file '{}': {}".format(profile, e))
return
#vmprof.cli.show(stats)
tree = stats.get_tree()
self._print_tree(tree)
def _walk_tree(self, parent, node, level, callback):
callback(parent, node, level)
level += 1
if level > self._prune_level:
return
for c in six.itervalues(node.children):
self._walk_tree(node, c, level, callback)
def _print_tree(self, tree):
total = float(tree.count)
def print_node(parent, node, level):
parent_name = parent.name if parent else None
perc = round(100. * float(node.count) / total, 1)
if parent and parent.count:
perc_of_parent = round(100. * float(node.count) / float(parent.count), 1)
else:
perc_of_parent = 100.
if perc >= self._prune_percent:
parts = node.name.count(':')
if parts == 3:
block_type, funname, funline, filename = node.name.split(':')
p2 = color(funname, color.BLUE, bold=True)
p2b = color(('.' * level * self._indent), color.BLUE)
p3 = []
if os.path.dirname(filename):
p3.append(color(os.path.dirname(filename) + '/', color.WHITE))
p3.append(color(os.path.basename(filename), color.WHITE, bold=True) + ":")
p3.append(color("{}".format(funline), color.WHITE))
p3 = ''.join(p3)
elif parts == 1:
block_type, funname = node.name.split(':')
p2 = color("JIT code", color.RED, bold=True)
p2b = color('.' * level * self._indent, color.RED, bold=False)
p3 = color(funname, color.WHITE, bold=False)
else:
p2 = color(node.name, color.WHITE)
p2b = color(('.' * level * self._indent), color.WHITE)
p3 = "<unknown>"
p1 = color("{:>5}%".format(perc), color.WHITE, bold=True)
p4 = color("{}%".format(perc_of_parent), color.WHITE, bold=True)
print("{} {} {} {} {}".format(p1, p2b, p2, p4, p3))
self._walk_tree(None, tree, 0, print_node)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("profile")
parser.add_argument(
'--prune_percent',
type=float,
default=0,
help="The indention per level within the call graph.")
parser.add_argument(
'--prune_level',
type=int,
default=None,
help='Prune output of a profile stats node when CPU.')
parser.add_argument(
'--indent',
type=int,
default=2,
help='The indention per level within the call graph.')
parser.add_argument('--lines', dest='lines', action='store_true')
parser.set_defaults(lines=False)
parser.add_argument('--filter', dest='filter', type=str, default=None)
args = parser.parse_args()
if args.lines:
pp = LinesPrinter(filter=args.filter)
else:
pp = PrettyPrinter(
prune_percent=args.prune_percent,
prune_level=args.prune_level,
indent=args.indent)
pp.show(args.profile)
class LinesPrinter(object):
def __init__(self, filter=None):
self.filter = filter
def show(self, profile):
"""
Read and display a vmprof profile file.
:param profile: The filename of the vmprof profile file to display.
:type profile: str
"""
try:
stats = vmprof.read_profile(profile)
except Exception as e:
print("Fatal: could not read vmprof profile file '{}': {}".format(profile, e))
return
tree = stats.get_tree()
for (filename, funline, funname), line_stats in self.lines_stat(tree):
if self.filter is None or funname.find(self.filter) != -1:
self.show_func(filename, funline, funname, line_stats)
def lines_stat(self, tree):
funcs = {}
def walk(node, d):
if node is None:
return
parts = node.name.count(':')
if parts == 3:
block_type, funname, funline, filename = node.name.split(':')
lines = d.setdefault((filename, int(funline), funname), {})
for l, cnt in six.iteritems(node.lines):
lines[l] = lines.get(l, 0) + cnt
for c in six.itervalues(node.children):
walk(c, d)
walk(tree, funcs)
return six.iteritems(funcs)
def show_func(self, filename, start_lineno, func_name, timings, stream=None, stripzeros=False):
""" Show results for a single function.
"""
if stream is None:
stream = sys.stdout
template = '%6s %8s %8s %-s'
d = {}
total_hits = 0.0
linenos = []
for lineno, nhits in six.iteritems(timings):
total_hits += nhits
linenos.append(lineno)
if stripzeros and total_hits == 0:
return
stream.write("Total hits: %g s\n" % total_hits)
if os.path.exists(filename) or filename.startswith("<ipython-input-"):
stream.write("File: %s\n" % filename)
stream.write("Function: %s at line %s\n" % (func_name, start_lineno))
if os.path.exists(filename):
# Clear the cache to ensure that we get up-to-date results.
linecache.clearcache()
all_lines = linecache.getlines(filename)
sublines = inspect.getblock(all_lines[start_lineno-1:])
else:
stream.write("\n")
stream.write("Could not find file %s\n" % filename)
stream.write("Are you sure you are running this program from the same directory\n")
stream.write("that you ran the profiler from?\n")
stream.write("Continuing without the function's contents.\n")
# Fake empty lines so we can see the timings, if not the code.
nlines = max(linenos) - min(min(linenos), start_lineno) + 1
sublines = [''] * nlines
for lineno, nhits in six.iteritems(timings):
d[lineno] = (nhits, '%5.1f' % (100* nhits / total_hits))
linenos = range(start_lineno, start_lineno + len(sublines))
empty = ('', '')
header = template % ('Line #', 'Hits', '% Hits',
'Line Contents')
stream.write("\n")
stream.write(header)
stream.write("\n")
stream.write('=' * len(header))
stream.write("\n")
for lineno, line in zip(linenos, sublines):
nhits, percent = d.get(lineno, empty)
txt = template % (lineno, nhits, percent,
line.rstrip('\n').rstrip('\r'))
stream.write(txt)
stream.write("\n")
stream.write("\n")
if __name__ == '__main__':
main()