-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathstats.py
More file actions
271 lines (233 loc) · 8.58 KB
/
stats.py
File metadata and controls
271 lines (233 loc) · 8.58 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
266
267
268
269
270
271
import six
from vmprof.reader import AssemblerCode, JittedCode, NativeCode
class EmptyProfileFile(Exception):
pass
class Stats(object):
def __init__(self, profiles, adr_dict=None, jit_frames=None, interp=None,
meta=None, start_time=None, end_time=None, state=None):
self.profiles = profiles
self.adr_dict = adr_dict
self.functions = {}
# kludgy, state is optional. stats should only take state as input
if state:
self.profile_lines = state.profile_lines
self.profile_memory = state.profile_memory
self.adr_size = len(state.virtual_ips)
else:
# unkown, for tests only
self.profile_lines = None
self.profile_memory = None
self.adr_size = None
self.generate_top()
if jit_frames is None:
jit_frames = set()
if interp is None:
interp = 'pypy' # why not
self.interp = interp
self.jit_frames = jit_frames
self.meta = meta or {}
self.start_time = start_time
self.end_time = end_time
def get_runtime_in_microseconds(self):
ts = self.end_time - self.start_time
return ts.total_seconds() * 1000000
def get_name(self, addr):
if addr not in self.adr_dict:
return "unknown"
line = self.adr_dict[addr]
return line.split(":")[1]
def find_addrs_containing_name(self, part):
for adr, name in self.adr_dict.items():
n, symbol, _, _ = name.split(':')
if part in symbol:
yield adr
def get_addr_info(self, addr):
name = self.adr_dict.get(addr, None)
if not name:
return None
lang, symbol, line, file = name.split(':')
return lang, symbol, line, file
def getargv(self):
return self.meta.get('argv', '')
def getmeta(self, key, default):
return self.meta.get(key, default)
def display(self, no):
prof = self.profiles[no][0]
return [self._get_name(elem) for elem in prof]
def generate_top(self):
for profile in self.profiles:
current_iter = {}
for i, addr in enumerate(profile[0]):
if self.profile_lines and i % 2 == 1:
# this entry in the profile is a negative number indicating a line
assert addr <= 0
continue
if addr not in current_iter: # count only topmost
self.functions[addr] = self.functions.get(addr, 0) + 1
current_iter[addr] = None
def top_profile(self):
return [(self._get_name(k), v) for (k, v) in six.iteritems(self.functions)]
def _get_name(self, addr):
if self.adr_dict is not None:
return self.adr_dict.get(addr, '<unknown code>')
return addr
def function_profile(self, top_function):
""" Show functions that we call (directly or indirectly) under
a given addr
"""
result = {}
total = 0
for profile in self.profiles:
current_iter = {} # don't count twice
counting = False
for addr in profile[0]:
if counting:
if addr in current_iter:
continue
current_iter[addr] = None
result[addr] = result.get(addr, 0) + 1
else:
if addr == top_function:
counting = True
total += 1
result = sorted(result.items(), key=lambda a: a[1])
return result, total
def get_top(self, profiles):
for prof in profiles:
if prof[0]:
break
else:
raise EmptyProfileFile()
top_addr = prof[0][0]
top = Node(top_addr, self._get_name(top_addr))
top.count = len(self.profiles)
return top
def get_tree(self):
# fine the first non-empty profile
top = self.get_top(self.profiles)
addr = None
for profile in self.profiles:
last_addr = top.addr
cur = top
for i in range(0, len(profile[0])):
if isinstance(profile[0][i], AssemblerCode):
continue # just ignore it for now
addr = profile[0][i]
if addr <= 0:
# negative address means line number
cur.lines[-addr] = cur.lines.get(-addr, 0) + 1
else:
if addr == last_addr:
continue # ignore duplicates
last_addr = addr
name = self._get_name(addr)
cur = cur.add_child(addr, name)
if isinstance(addr, JittedCode):
cur.meta['jit'] = cur.meta.get('jit', 0) + 1
if isinstance(addr, NativeCode):
cur.meta['native'] = cur.meta.get('native', 0) + 1
# get the first "interesting" node, that is after vmprof and pypy
# mess
return self.filter_top(top)
def filter_top(self, top):
first_top = top
while True:
if top.name.startswith('py:<module>') and 'vmprof/__main__.py' not in top.name:
return top
if len(top.children) > 1:
# pick the biggest one in case of branches in vmprof
next = None
count = -1
for c in top.children.values():
if c.count > count:
count = c.count
next = c
top = next
else:
try:
top = top['']
except KeyError:
break
return first_top
class Node(object):
""" children is a dict of addr -> Node
"""
_self_count = None
flat = False
def __init__(self, addr, name, count=1, children=None):
if children is None:
children = {}
self.children = children
self.name = name
self.addr = addr
self.count = count # starts at 1
self.jitcodes = {}
self.meta = {}
self.lines = {}
def __getitem__(self, item):
if isinstance(item, int):
return self.children[item]
for v in self.children.values():
if item in v.name:
return v
raise KeyError
def as_json(self):
import json
return json.dumps(self._serialize())
def _serialize(self):
chld = [ch._serialize() for ch in six.itervalues(self.children)]
# if we don't make str() of addr here, JS does its
# int -> float -> int losy convertion without
# any warning
return [self.name, str(self.addr), self.count, self.meta, chld]
def _rec_count(self):
c = 1
for x in six.itervalues(self.children):
c += x._rec_count()
return c
def walk(self, callback):
callback(self)
for c in six.itervalues(self.children):
c.walk(callback)
def cumulative_meta(self, d=None):
if d is None:
d = {}
for c in six.itervalues(self.children):
c.cumulative_meta(d)
for k, v in six.iteritems(self.meta):
d[k] = d.get(k, 0) + v
return d
def _filter(self, count):
# XXX make a copy
for key, c in self.children.items():
if c.count < count:
del self.children[key]
else:
c._filter(count)
def get_self_count(self):
if self._self_count is not None:
return self._self_count
self._self_count = self.count
for elem in six.itervalues(self.children):
self._self_count -= elem.count
return self._self_count
self_count = property(get_self_count)
def add_child(self, addr, name):
try:
next = self.children[addr]
next.count += 1
except KeyError:
next = Node(addr, name)
self.children[addr] = next
return next
def __eq__(self, other):
if not isinstance(other, Node):
return False
return self.name == other.name and self.addr == other.addr and self.count == other.count and self.children == other.children
def __ne__(self, other):
return not self == other
def __repr__(self):
items = sorted(self.children.items())
child_str = ", ".join([("(%d, %s)" % (v.count, v.name))
for k, v in items])
return '<Node: %s (%d) [%s]>' % (self.name, self.count, child_str)