/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: 2019-03-04 00:16:27 UTC
  • mfrom: (7293 work)
  • mto: This revision was merged to the branch mainline in revision 7318.
  • Revision ID: jelmer@jelmer.uk-20190304001627-v6u7o6pf97tukhek
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
__all__ = ['profile', 'Stats']
26
26
 
 
27
 
27
28
def profile(f, *args, **kwds):
28
29
    """Run a function profile.
29
30
 
47
48
 
48
49
class BzrProfiler(object):
49
50
    """Bzr utility wrapper around Profiler.
50
 
    
 
51
 
51
52
    For most uses the module level 'profile()' function will be suitable.
52
53
    However profiling when a simple wrapped function isn't available may
53
54
    be easier to accomplish using this class.
59
60
    Note that profiling involves a threading.Lock around the actual profiling.
60
61
    This is needed because profiling involves global manipulation of the python
61
62
    interpreter state. As such you cannot perform multiple profiles at once.
62
 
    Trying to do so will lock out the second profiler unless the global 
 
63
    Trying to do so will lock out the second profiler unless the global
63
64
    breezy.lsprof.BzrProfiler.profiler_block is set to 0. Setting it to 0 will
64
65
    cause profiling to fail rather than blocking.
65
66
    """
72
73
 
73
74
    def start(self):
74
75
        """Start profiling.
75
 
        
 
76
 
76
77
        This hooks into threading and will record all calls made until
77
78
        stop() is called.
78
79
        """
85
86
        try:
86
87
            self.p.enable(subcalls=True)
87
88
            threading.setprofile(self._thread_profile)
88
 
        except:
 
89
        except BaseException:
89
90
            self.__class__.profiler_lock.release()
90
91
            raise
91
92
 
161
162
            d = d[:top]
162
163
        cols = "% 12s %12s %11.4f %11.4f   %s\n"
163
164
        hcols = "% 12s %12s %12s %12s %s\n"
164
 
        cols2 = "+%12s %12s %11.4f %11.4f +  %s\n"
165
165
        file.write(hcols % ("CallCount", "Recursive", "Total(ms)",
166
166
                            "Inline(ms)", "module:lineno(function)"))
167
167
        for e in d:
258
258
        out_file = self.out_file
259
259
        code = entry.code
260
260
        inlinetime = int(entry.inlinetime * 1000)
261
 
        #out_file.write('ob=%s\n' % (code.co_filename,))
262
261
        if isinstance(code, str):
263
262
            out_file.write('fi=~\n')
264
263
        else:
285
284
        out_file = self.out_file
286
285
        code = subentry.code
287
286
        totaltime = int(subentry.totaltime * 1000)
288
 
        #out_file.write('cob=%s\n' % (code.co_filename,))
289
287
        if isinstance(code, str):
290
288
            out_file.write('cfi=~\n')
291
289
            out_file.write('cfn=%s\n' % (label(code, True),))
297
295
                subentry.callcount, code.co_firstlineno))
298
296
        out_file.write('%d %d\n' % (lineno, totaltime))
299
297
 
 
298
 
300
299
_fn2mod = {}
301
300
 
 
301
 
302
302
def label(code, calltree=False):
303
303
    if isinstance(code, str):
304
304
        return code
316
316
                mname = _fn2mod[code.co_filename] = k
317
317
                break
318
318
        else:
319
 
            mname = _fn2mod[code.co_filename] = '<%s>'%code.co_filename
 
319
            mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename
320
320
    if calltree:
321
321
        return '%s %s:%d' % (code.co_name, mname, code.co_firstlineno)
322
322
    else: