36
32
p.enable(subcalls=True)
37
33
threading.setprofile(_thread_profile)
38
# Note: The except clause is needed below so that profiling data still
39
# gets dumped even when exceptions are encountered. The except clause code
40
# is taken straight from run_bzr_catch_errrors() in commands.py and ought
41
# to be kept in sync with it.
44
ret = f(*args, **kwds)
45
except (KeyboardInterrupt, Exception), e:
47
bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
35
ret = f(*args, **kwds)
51
38
for pp in _g_threadmap.values():
131
118
otherwise the format is given by the filename extension.
133
120
if format is None:
134
basename = bzrlib.osutils.basename(filename)
135
if basename.startswith('callgrind.out'):
121
if filename.startswith('callgrind.out'):
136
122
format = "callgrind"
138
ext = bzrlib.osutils.splitext(filename)[1]
124
ext = os.path.splitext(filename)[1]
141
127
outfile = open(filename, 'wb')
177
163
for entry in self.data:
178
164
totaltime = int(entry.totaltime * 1000)
179
165
max_cost = max(max_cost, totaltime)
180
self.out_file.write('summary: %d\n' % (max_cost,))
166
print >> self.out_file, 'summary: %d' % (max_cost,)
182
168
def _entry(self, entry):
183
169
out_file = self.out_file
184
170
code = entry.code
185
171
inlinetime = int(entry.inlinetime * 1000)
186
#out_file.write('ob=%s\n' % (code.co_filename,))
187
if isinstance(code, str):
188
out_file.write('fi=~\n')
190
out_file.write('fi=%s\n' % (code.co_filename,))
191
out_file.write('fn=%s\n' % (label(code, True),))
192
if isinstance(code, str):
193
out_file.write('0 %s\n' % (inlinetime,))
195
out_file.write('%d %d\n' % (code.co_firstlineno, inlinetime))
172
#print >> out_file, 'ob=%s' % (code.co_filename,)
173
if isinstance(code, str):
174
print >> out_file, 'fi=~'
176
print >> out_file, 'fi=%s' % (code.co_filename,)
177
print >> out_file, 'fn=%s' % (label(code, True),)
178
if isinstance(code, str):
179
print >> out_file, '0 ', inlinetime
181
print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
196
182
# recursive calls are counted in entry.calls
198
184
calls = entry.calls
204
190
lineno = code.co_firstlineno
205
191
for subentry in calls:
206
192
self._subentry(lineno, subentry)
209
195
def _subentry(self, lineno, subentry):
210
196
out_file = self.out_file
211
197
code = subentry.code
212
198
totaltime = int(subentry.totaltime * 1000)
213
#out_file.write('cob=%s\n' % (code.co_filename,))
214
out_file.write('cfn=%s\n' % (label(code, True),))
199
#print >> out_file, 'cob=%s' % (code.co_filename,)
200
print >> out_file, 'cfn=%s' % (label(code, True),)
215
201
if isinstance(code, str):
216
out_file.write('cfi=~\n')
217
out_file.write('calls=%d 0\n' % (subentry.callcount,))
202
print >> out_file, 'cfi=~'
203
print >> out_file, 'calls=%d 0' % (subentry.callcount,)
219
out_file.write('cfi=%s\n' % (code.co_filename,))
220
out_file.write('calls=%d %d\n' % (
221
subentry.callcount, code.co_firstlineno))
222
out_file.write('%d %d\n' % (lineno, totaltime))
205
print >> out_file, 'cfi=%s' % (code.co_filename,)
206
print >> out_file, 'calls=%d %d' % (
207
subentry.callcount, code.co_firstlineno)
208
print >> out_file, '%d %d' % (lineno, totaltime)
252
238
sys.argv = sys.argv[1:]
254
sys.stderr.write("usage: lsprof.py <script> <arguments...>\n")
240
print >> sys.stderr, "usage: lsprof.py <script> <arguments...>"
256
242
sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
257
243
stats = profile(execfile, sys.argv[0], globals(), locals())