~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Alexander Belchenko
  • Date: 2007-08-10 09:04:38 UTC
  • mto: This revision was merged to the branch mainline in revision 2694.
  • Revision ID: bialix@ukr.net-20070810090438-0835xdz0rl8825qv
fixes after Ian's review

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import threading
11
11
from _lsprof import Profiler, profiler_entry
12
12
 
13
 
 
14
 
import bzrlib.osutils
15
 
 
16
 
 
17
13
__all__ = ['profile', 'Stats']
18
14
 
19
15
_g_threadmap = {}
35
31
    p = Profiler()
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.
42
34
    try:
43
 
        try:
44
 
            ret = f(*args, **kwds)
45
 
        except (KeyboardInterrupt, Exception), e:
46
 
            import bzrlib.trace
47
 
            bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
48
 
            ret = 3
 
35
        ret = f(*args, **kwds)
49
36
    finally:
50
37
        p.disable()
51
38
        for pp in _g_threadmap.values():
131
118
            otherwise the format is given by the filename extension.
132
119
        """
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"
137
123
            else:
138
 
                ext = bzrlib.osutils.splitext(filename)[1]
 
124
                ext = os.path.splitext(filename)[1]
139
125
                if len(ext) > 1:
140
126
                    format = ext[1:]
141
127
        outfile = open(filename, 'wb')
167
153
 
168
154
    def output(self, out_file):
169
155
        self.out_file = out_file        
170
 
        out_file.write('events: Ticks\n')
 
156
        print >> out_file, 'events: Ticks'
171
157
        self._print_summary()
172
158
        for entry in self.data:
173
159
            self._entry(entry)
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,)
181
167
 
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')
189
 
        else:
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,))
194
 
        else:
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=~'
 
175
        else:
 
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
 
180
        else:
 
181
            print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
196
182
        # recursive calls are counted in entry.calls
197
183
        if 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)
207
 
        out_file.write('\n')
 
193
        print >> out_file
208
194
 
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,)
218
204
        else:
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)
223
209
 
224
210
_fn2mod = {}
225
211
 
251
237
    import os
252
238
    sys.argv = sys.argv[1:]
253
239
    if not sys.argv:
254
 
        sys.stderr.write("usage: lsprof.py <script> <arguments...>\n")
 
240
        print >> sys.stderr, "usage: lsprof.py <script> <arguments...>"
255
241
        sys.exit(2)
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())