33
30
p.enable(subcalls=True)
34
31
threading.setprofile(_thread_profile)
35
# Note: The except clause is needed below so that profiling data still
36
# gets dumped even when exceptions are encountered. The except clause code
37
# is taken straight from run_bzr_catch_errrors() in commands.py and ought
38
# to be kept in sync with it.
41
ret = f(*args, **kwds)
42
except (KeyboardInterrupt, Exception), e:
44
bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
33
ret = f(*args, **kwds)
48
36
for pp in _g_threadmap.values():
116
104
"""Output profiling data in calltree format (for KCacheGrind)."""
117
105
_CallTreeFilter(self.data).output(file)
119
def save(self, filename, format=None):
120
"""Save profiling data to a file.
122
:param filename: the name of the output file
123
:param format: 'txt' for a text representation;
124
'callgrind' for calltree format;
125
otherwise a pickled Python object. A format of None indicates
126
that the format to use is to be found from the filename. If
127
the name starts with callgrind.out, callgrind format is used
128
otherwise the format is given by the filename extension.
131
basename = os.path.basename(filename)
132
if basename.startswith('callgrind.out'):
135
ext = os.path.splitext(filename)[1]
138
outfile = open(filename, 'wb')
140
if format == "callgrind":
141
self.calltree(outfile)
142
elif format == "txt":
143
self.pprint(file=outfile)
146
cPickle.dump(self, outfile, 2)
151
108
class _CallTreeFilter(object):
152
"""Converter of a Stats object to input suitable for KCacheGrind.
154
This code is taken from http://ddaa.net/blog/python/lsprof-calltree
155
with the changes made by J.P. Calderone and Itamar applied. Note that
156
isinstance(code, str) needs to be used at times to determine if the code
157
object is actually an external code object (with a filename, etc.) or
161
110
def __init__(self, data):
174
123
for entry in self.data:
175
124
totaltime = int(entry.totaltime * 1000)
176
125
max_cost = max(max_cost, totaltime)
177
self.out_file.write('summary: %d\n' % (max_cost,))
126
print >> self.out_file, 'summary: %d' % (max_cost,)
179
128
def _entry(self, entry):
180
129
out_file = self.out_file
181
130
code = entry.code
182
131
inlinetime = int(entry.inlinetime * 1000)
183
#out_file.write('ob=%s\n' % (code.co_filename,))
184
if isinstance(code, str):
185
out_file.write('fi=~\n')
187
out_file.write('fi=%s\n' % (code.co_filename,))
188
out_file.write('fn=%s\n' % (label(code, True),))
189
if isinstance(code, str):
190
out_file.write('0 %s\n' % (inlinetime,))
192
out_file.write('%d %d\n' % (code.co_firstlineno, inlinetime))
132
#print >> out_file, 'ob=%s' % (code.co_filename,)
133
print >> out_file, 'fi=%s' % (code.co_filename,)
134
print >> out_file, 'fn=%s' % (label(code, True),)
135
print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
193
136
# recursive calls are counted in entry.calls
195
138
calls = entry.calls
198
if isinstance(code, str):
201
lineno = code.co_firstlineno
202
141
for subentry in calls:
203
self._subentry(lineno, subentry)
142
self._subentry(code.co_firstlineno, subentry)
206
145
def _subentry(self, lineno, subentry):
207
146
out_file = self.out_file
208
147
code = subentry.code
209
148
totaltime = int(subentry.totaltime * 1000)
210
#out_file.write('cob=%s\n' % (code.co_filename,))
211
out_file.write('cfn=%s\n' % (label(code, True),))
212
if isinstance(code, str):
213
out_file.write('cfi=~\n')
214
out_file.write('calls=%d 0\n' % (subentry.callcount,))
216
out_file.write('cfi=%s\n' % (code.co_filename,))
217
out_file.write('calls=%d %d\n' % (
218
subentry.callcount, code.co_firstlineno))
219
out_file.write('%d %d\n' % (lineno, totaltime))
149
#print >> out_file, 'cob=%s' % (code.co_filename,)
150
print >> out_file, 'cfn=%s' % (label(code, True),)
151
print >> out_file, 'cfi=%s' % (code.co_filename,)
152
print >> out_file, 'calls=%d %d' % (
153
subentry.callcount, code.co_firstlineno)
154
print >> out_file, '%d %d' % (lineno, totaltime)