/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-11-11 04:08:32 UTC
  • mto: (7143.16.20 even-more-cleanups)
  • mto: This revision was merged to the branch mainline in revision 7175.
  • Revision ID: jelmer@jelmer.uk-20181111040832-nsljjynzzwmznf3h
Run autopep8.

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
 
import cPickle
 
6
from __future__ import absolute_import
 
7
 
 
8
import codecs
 
9
try:
 
10
    import cPickle as pickle
 
11
except ImportError:
 
12
    import pickle
 
13
import operator
7
14
import os
8
15
import sys
9
 
import thread
 
16
try:
 
17
    import _thread
 
18
except ImportError:
 
19
    import thread as _thread
10
20
import threading
11
21
from _lsprof import Profiler, profiler_entry
12
22
 
 
23
from . import errors
13
24
 
14
25
__all__ = ['profile', 'Stats']
15
26
 
 
27
 
16
28
def profile(f, *args, **kwds):
17
29
    """Run a function profile.
18
30
 
20
32
    raised, pass in a closure that will catch the exceptions and transform them
21
33
    appropriately for your driver function.
22
34
 
 
35
    Important caveat: only one profile can execute at a time. See BzrProfiler
 
36
    for details.
 
37
 
23
38
    :return: The functions return value and a stats object.
24
39
    """
25
40
    profiler = BzrProfiler()
33
48
 
34
49
class BzrProfiler(object):
35
50
    """Bzr utility wrapper around Profiler.
36
 
    
 
51
 
37
52
    For most uses the module level 'profile()' function will be suitable.
38
53
    However profiling when a simple wrapped function isn't available may
39
54
    be easier to accomplish using this class.
41
56
    To use it, create a BzrProfiler and call start() on it. Some arbitrary
42
57
    time later call stop() to stop profiling and retrieve the statistics
43
58
    from the code executed in the interim.
 
59
 
 
60
    Note that profiling involves a threading.Lock around the actual profiling.
 
61
    This is needed because profiling involves global manipulation of the python
 
62
    interpreter state. As such you cannot perform multiple profiles at once.
 
63
    Trying to do so will lock out the second profiler unless the global 
 
64
    breezy.lsprof.BzrProfiler.profiler_block is set to 0. Setting it to 0 will
 
65
    cause profiling to fail rather than blocking.
44
66
    """
45
67
 
 
68
    profiler_block = 1
 
69
    """Serialise rather than failing to profile concurrent profile requests."""
 
70
 
 
71
    profiler_lock = threading.Lock()
 
72
    """Global lock used to serialise profiles."""
 
73
 
46
74
    def start(self):
47
75
        """Start profiling.
48
 
        
 
76
 
49
77
        This hooks into threading and will record all calls made until
50
78
        stop() is called.
51
79
        """
52
80
        self._g_threadmap = {}
53
81
        self.p = Profiler()
54
 
        self.p.enable(subcalls=True)
55
 
        threading.setprofile(self._thread_profile)
 
82
        permitted = self.__class__.profiler_lock.acquire(
 
83
            self.__class__.profiler_block)
 
84
        if not permitted:
 
85
            raise errors.InternalBzrError(msg="Already profiling something")
 
86
        try:
 
87
            self.p.enable(subcalls=True)
 
88
            threading.setprofile(self._thread_profile)
 
89
        except:
 
90
            self.__class__.profiler_lock.release()
 
91
            raise
56
92
 
57
93
    def stop(self):
58
94
        """Stop profiling.
60
96
        This unhooks from threading and cleans up the profiler, returning
61
97
        the gathered Stats object.
62
98
 
63
 
        :return: A bzrlib.lsprof.Stats object.
 
99
        :return: A breezy.lsprof.Stats object.
64
100
        """
65
 
        self.p.disable()
66
 
        for pp in self._g_threadmap.values():
67
 
            pp.disable()
68
 
        threading.setprofile(None)
69
 
        p = self.p
70
 
        self.p = None
71
 
        threads = {}
72
 
        for tid, pp in self._g_threadmap.items():
73
 
            threads[tid] = Stats(pp.getstats(), {})
74
 
        self._g_threadmap = None
75
 
        return Stats(p.getstats(), threads)
 
101
        try:
 
102
            self.p.disable()
 
103
            for pp in self._g_threadmap.values():
 
104
                pp.disable()
 
105
            threading.setprofile(None)
 
106
            p = self.p
 
107
            self.p = None
 
108
            threads = {}
 
109
            for tid, pp in self._g_threadmap.items():
 
110
                threads[tid] = Stats(pp.getstats(), {})
 
111
            self._g_threadmap = None
 
112
            return Stats(p.getstats(), threads)
 
113
        finally:
 
114
            self.__class__.profiler_lock.release()
76
115
 
77
116
    def _thread_profile(self, f, *args, **kwds):
78
117
        # we lose the first profile point for a new thread in order to
79
118
        # trampoline a new Profile object into place
80
 
        thr = thread.get_ident()
 
119
        thr = _thread.get_ident()
81
120
        self._g_threadmap[thr] = p = Profiler()
82
121
        # this overrides our sys.setprofile hook:
83
122
        p.enable(subcalls=True, builtins=True)
84
123
 
85
124
 
86
125
class Stats(object):
87
 
    """XXX docstring"""
 
126
    """Wrapper around the collected data.
 
127
 
 
128
    A Stats instance is created when the profiler finishes. Normal
 
129
    usage is to use save() to write out the data to a file, or pprint()
 
130
    to write human-readable information to the command line.
 
131
    """
88
132
 
89
133
    def __init__(self, data, threads):
90
134
        self.data = data
91
135
        self.threads = threads
92
136
 
93
 
    def sort(self, crit="inlinetime"):
94
 
        """XXX docstring"""
95
 
        if crit not in profiler_entry.__dict__:
96
 
            raise ValueError, "Can't sort by %s" % crit
97
 
        self.data.sort(lambda b, a: cmp(getattr(a, crit),
98
 
                                        getattr(b, crit)))
 
137
    def sort(self, crit="inlinetime", reverse=True):
 
138
        """Sort the data by the supplied critera.
 
139
 
 
140
        :param crit: the data attribute used as the sort key."""
 
141
        if crit not in profiler_entry.__dict__ or crit == 'code':
 
142
            raise ValueError("Can't sort by %s" % crit)
 
143
 
 
144
        key_func = operator.attrgetter(crit)
 
145
        self.data.sort(key=key_func, reverse=reverse)
 
146
 
99
147
        for e in self.data:
100
148
            if e.calls:
101
 
                e.calls.sort(lambda b, a: cmp(getattr(a, crit),
102
 
                                              getattr(b, crit)))
 
149
                e.calls.sort(key=key_func, reverse=reverse)
103
150
 
104
151
    def pprint(self, top=None, file=None):
105
 
        """XXX docstring"""
 
152
        """Pretty-print the data as plain text for human consumption.
 
153
 
 
154
        :param top: only output the top n entries.
 
155
            The default value of None means output all data.
 
156
        :param file: the output file; if None, output will
 
157
            default to stdout."""
106
158
        if file is None:
107
159
            file = sys.stdout
108
160
        d = self.data
162
214
                ext = os.path.splitext(filename)[1]
163
215
                if len(ext) > 1:
164
216
                    format = ext[1:]
165
 
        outfile = open(filename, 'wb')
166
 
        try:
 
217
        with open(filename, 'wb') as outfile:
167
218
            if format == "callgrind":
168
 
                self.calltree(outfile)
 
219
                # The callgrind format states it is 'ASCII based':
 
220
                # <http://valgrind.org/docs/manual/cl-format.html>
 
221
                # But includes filenames so lets ignore and use UTF-8.
 
222
                self.calltree(codecs.getwriter('utf-8')(outfile))
169
223
            elif format == "txt":
170
 
                self.pprint(file=outfile)
 
224
                self.pprint(file=codecs.getwriter('utf-8')(outfile))
171
225
            else:
172
226
                self.freeze()
173
 
                cPickle.dump(self, outfile, 2)
174
 
        finally:
175
 
            outfile.close()
 
227
                pickle.dump(self, outfile, 2)
176
228
 
177
229
 
178
230
class _CallTreeFilter(object):
235
287
        code = subentry.code
236
288
        totaltime = int(subentry.totaltime * 1000)
237
289
        #out_file.write('cob=%s\n' % (code.co_filename,))
238
 
        out_file.write('cfn=%s\n' % (label(code, True),))
239
290
        if isinstance(code, str):
240
291
            out_file.write('cfi=~\n')
 
292
            out_file.write('cfn=%s\n' % (label(code, True),))
241
293
            out_file.write('calls=%d 0\n' % (subentry.callcount,))
242
294
        else:
243
295
            out_file.write('cfi=%s\n' % (code.co_filename,))
 
296
            out_file.write('cfn=%s\n' % (label(code, True),))
244
297
            out_file.write('calls=%d %d\n' % (
245
298
                subentry.callcount, code.co_firstlineno))
246
299
        out_file.write('%d %d\n' % (lineno, totaltime))
247
300
 
 
301
 
248
302
_fn2mod = {}
249
303
 
 
304
 
250
305
def label(code, calltree=False):
251
306
    if isinstance(code, str):
252
307
        return code
264
319
                mname = _fn2mod[code.co_filename] = k
265
320
                break
266
321
        else:
267
 
            mname = _fn2mod[code.co_filename] = '<%s>'%code.co_filename
 
322
            mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename
268
323
    if calltree:
269
324
        return '%s %s:%d' % (code.co_name, mname, code.co_firstlineno)
270
325
    else:
271
326
        return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
272
327
 
273
328
 
274
 
if __name__ == '__main__':
275
 
    import os
 
329
def main():
276
330
    sys.argv = sys.argv[1:]
277
331
    if not sys.argv:
278
332
        sys.stderr.write("usage: lsprof.py <script> <arguments...>\n")
279
333
        sys.exit(2)
280
 
    sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
281
 
    stats = profile(execfile, sys.argv[0], globals(), locals())
 
334
    import runpy
 
335
    result, stats = profile(runpy.run_path, sys.argv[0], run_name='__main__')
282
336
    stats.sort()
283
337
    stats.pprint()
 
338
 
 
339
 
 
340
if __name__ == '__main__':
 
341
    main()