25
26
import_logfile = sys.stderr
26
27
compile_logfile = sys.stderr
36
def stack_add(name, frame_name, frame_lineno, scope_name=None):
37
"""Start a new record on the stack"""
40
this_stack = (_cur_id, name)
43
_total_stack[_parent_stack[-1]].append(this_stack)
44
_total_stack[this_stack] = []
45
_parent_stack.append(this_stack)
46
_info[this_stack] = [len(_parent_stack)-1, frame_name, frame_lineno, scope_name]
51
def stack_finish(this, cost):
52
"""Finish a given entry, and record its cost in time"""
55
if _parent_stack[-1] != this:
56
# This should only happen if there is a bug
57
# (used to happen if an import failed)
58
if this not in _parent_stack:
59
import_logfile.write('could not find %s in callstat: %s\n'
60
% (this, pprint.pformat(_parent_stack)))
62
idx = _parent_stack.index(this)
63
import_logfile.write('stripping off extra children: %s\n'
64
% (_parent_stack[idx:],))
65
_parent_stack = _parent_stack[:idx]
68
_info[this].append(cost)
71
def log_stack_info(out_file):
72
# Find all of the roots with import = 0
73
out_file.write('cum_time\tmod_time\tname\tscope\tframe\n')
74
todo = [key for key,value in _info.iteritems() if value[0] == 0]
78
children = _total_stack[cur]
81
# Compute the module time by removing the children times
83
for child in children:
85
mod_time -= c_info[-1]
88
if scope_name is None:
89
txt = '%-48s' % (cur[1],)
91
txt = '%-24s\tfor %-24s' % (cur[1], scope_name)
92
# indent, cum_time, mod_time, name,
93
# scope_name, frame_name, frame_lineno
94
out_file.write('%s%5.1f %5.1f %s\t@ %s:%d\n'
95
% (info[0]*'+', info[-1]*1000., mod_time, txt, info[1], info[2]))
97
todo.extend(reversed(children))
28
100
_real_import = __import__
30
102
def timed_import(name, globals, locals, fromlist):
31
103
"""Wrap around standard importer to log import time"""
32
105
if import_logfile is None:
33
106
return _real_import(name, globals, locals, fromlist)
65
133
frame_name = frame.f_globals.get('__name__', '<unknown>')
66
134
frame_lineno = frame.f_lineno
69
import_logfile.write('%3.0fms %-24s\tfor %-24s\t@ %s:%d%s\n'
70
% ((time.time()-tstart)*1000, name, scope_name,
71
frame_name, frame_lineno, extra))
73
# If the import took a long time, log the stack that generated
74
# this import. Especially necessary for demandloaded code
77
for fnum in range(cur_frame+1, cur_frame+10):
79
f = sys._getframe(fnum)
83
% (f.f_globals.get('__name__', '<unknown>'),
87
import_logfile.write('\t' + ' '.join(stack) + '\n')
136
this = stack_add(name+extra, frame_name, frame_lineno, scope_name)
141
mod = _real_import(name, globals, locals, fromlist)
143
tload = time.time()-tstart
144
stack_finish(this, tload)
95
153
if compile_logfile is None:
96
154
return _real_compile(*args, **kwargs)
98
# Measure the compile time
100
comp = _real_compile(*args, **kwargs)
102
156
# And who is requesting this?
103
157
frame = sys._getframe(2)
104
158
frame_name = frame.f_globals.get('__name__', '<unknown>')
105
159
frame_lineno = frame.f_lineno
106
compile_logfile.write('%3.0fms %-40r\t@ %s:%d\n'
107
% ((time.time()-tstart)*1000, args[0][:40],
108
frame_name, frame_lineno))
161
this = stack_add(repr(args[0][:40]), frame_name, frame_lineno)
165
# Measure the compile time
166
comp = _real_compile(*args, **kwargs)
168
tcompile = time.time() - tstart
169
stack_finish(this, tcompile)