/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/lsprof.py

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# I made one modification to profile so that it returns a pair
4
4
# instead of just the Stats object
5
5
 
 
6
from __future__ import absolute_import
 
7
 
6
8
import codecs
7
9
try:
8
10
    import cPickle as pickle
11
13
import operator
12
14
import os
13
15
import sys
14
 
import _thread
 
16
try:
 
17
    import _thread
 
18
except ImportError:
 
19
    import thread as _thread
15
20
import threading
16
21
from _lsprof import Profiler, profiler_entry
17
22
 
19
24
 
20
25
__all__ = ['profile', 'Stats']
21
26
 
22
 
 
23
27
def profile(f, *args, **kwds):
24
28
    """Run a function profile.
25
29
 
43
47
 
44
48
class BzrProfiler(object):
45
49
    """Bzr utility wrapper around Profiler.
46
 
 
 
50
    
47
51
    For most uses the module level 'profile()' function will be suitable.
48
52
    However profiling when a simple wrapped function isn't available may
49
53
    be easier to accomplish using this class.
55
59
    Note that profiling involves a threading.Lock around the actual profiling.
56
60
    This is needed because profiling involves global manipulation of the python
57
61
    interpreter state. As such you cannot perform multiple profiles at once.
58
 
    Trying to do so will lock out the second profiler unless the global
 
62
    Trying to do so will lock out the second profiler unless the global 
59
63
    breezy.lsprof.BzrProfiler.profiler_block is set to 0. Setting it to 0 will
60
64
    cause profiling to fail rather than blocking.
61
65
    """
68
72
 
69
73
    def start(self):
70
74
        """Start profiling.
71
 
 
 
75
        
72
76
        This hooks into threading and will record all calls made until
73
77
        stop() is called.
74
78
        """
81
85
        try:
82
86
            self.p.enable(subcalls=True)
83
87
            threading.setprofile(self._thread_profile)
84
 
        except BaseException:
 
88
        except:
85
89
            self.__class__.profiler_lock.release()
86
90
            raise
87
91
 
157
161
            d = d[:top]
158
162
        cols = "% 12s %12s %11.4f %11.4f   %s\n"
159
163
        hcols = "% 12s %12s %12s %12s %s\n"
 
164
        cols2 = "+%12s %12s %11.4f %11.4f +  %s\n"
160
165
        file.write(hcols % ("CallCount", "Recursive", "Total(ms)",
161
166
                            "Inline(ms)", "module:lineno(function)"))
162
167
        for e in d:
253
258
        out_file = self.out_file
254
259
        code = entry.code
255
260
        inlinetime = int(entry.inlinetime * 1000)
 
261
        #out_file.write('ob=%s\n' % (code.co_filename,))
256
262
        if isinstance(code, str):
257
263
            out_file.write('fi=~\n')
258
264
        else:
279
285
        out_file = self.out_file
280
286
        code = subentry.code
281
287
        totaltime = int(subentry.totaltime * 1000)
 
288
        #out_file.write('cob=%s\n' % (code.co_filename,))
282
289
        if isinstance(code, str):
283
290
            out_file.write('cfi=~\n')
284
291
            out_file.write('cfn=%s\n' % (label(code, True),))
290
297
                subentry.callcount, code.co_firstlineno))
291
298
        out_file.write('%d %d\n' % (lineno, totaltime))
292
299
 
293
 
 
294
300
_fn2mod = {}
295
301
 
296
 
 
297
302
def label(code, calltree=False):
298
303
    if isinstance(code, str):
299
304
        return code
311
316
                mname = _fn2mod[code.co_filename] = k
312
317
                break
313
318
        else:
314
 
            mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename
 
319
            mname = _fn2mod[code.co_filename] = '<%s>'%code.co_filename
315
320
    if calltree:
316
321
        return '%s %s:%d' % (code.co_name, mname, code.co_firstlineno)
317
322
    else: