/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: 2020-02-07 02:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200207021430-m49iq3x4x8xlib6x
Drop python2 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# this is copied from the lsprof distro because somehow
 
2
# it is not installed by distutils
 
3
# I made one modification to profile so that it returns a pair
 
4
# instead of just the Stats object
 
5
 
 
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
 
14
import os
 
15
import sys
 
16
import _thread
 
17
import threading
 
18
from _lsprof import Profiler, profiler_entry
 
19
 
 
20
from . import errors
 
21
 
 
22
__all__ = ['profile', 'Stats']
 
23
 
 
24
 
 
25
def profile(f, *args, **kwds):
 
26
    """Run a function profile.
 
27
 
 
28
    Exceptions are not caught: If you need stats even when exceptions are to be
 
29
    raised, pass in a closure that will catch the exceptions and transform them
 
30
    appropriately for your driver function.
 
31
 
 
32
    Important caveat: only one profile can execute at a time. See BzrProfiler
 
33
    for details.
 
34
 
 
35
    :return: The functions return value and a stats object.
 
36
    """
 
37
    profiler = BzrProfiler()
 
38
    profiler.start()
 
39
    try:
 
40
        ret = f(*args, **kwds)
 
41
    finally:
 
42
        stats = profiler.stop()
 
43
    return ret, stats
 
44
 
 
45
 
 
46
class BzrProfiler(object):
 
47
    """Bzr utility wrapper around Profiler.
 
48
 
 
49
    For most uses the module level 'profile()' function will be suitable.
 
50
    However profiling when a simple wrapped function isn't available may
 
51
    be easier to accomplish using this class.
 
52
 
 
53
    To use it, create a BzrProfiler and call start() on it. Some arbitrary
 
54
    time later call stop() to stop profiling and retrieve the statistics
 
55
    from the code executed in the interim.
 
56
 
 
57
    Note that profiling involves a threading.Lock around the actual profiling.
 
58
    This is needed because profiling involves global manipulation of the python
 
59
    interpreter state. As such you cannot perform multiple profiles at once.
 
60
    Trying to do so will lock out the second profiler unless the global
 
61
    breezy.lsprof.BzrProfiler.profiler_block is set to 0. Setting it to 0 will
 
62
    cause profiling to fail rather than blocking.
 
63
    """
 
64
 
 
65
    profiler_block = 1
 
66
    """Serialise rather than failing to profile concurrent profile requests."""
 
67
 
 
68
    profiler_lock = threading.Lock()
 
69
    """Global lock used to serialise profiles."""
 
70
 
 
71
    def start(self):
 
72
        """Start profiling.
 
73
 
 
74
        This hooks into threading and will record all calls made until
 
75
        stop() is called.
 
76
        """
 
77
        self._g_threadmap = {}
 
78
        self.p = Profiler()
 
79
        permitted = self.__class__.profiler_lock.acquire(
 
80
            self.__class__.profiler_block)
 
81
        if not permitted:
 
82
            raise errors.InternalBzrError(msg="Already profiling something")
 
83
        try:
 
84
            self.p.enable(subcalls=True)
 
85
            threading.setprofile(self._thread_profile)
 
86
        except BaseException:
 
87
            self.__class__.profiler_lock.release()
 
88
            raise
 
89
 
 
90
    def stop(self):
 
91
        """Stop profiling.
 
92
 
 
93
        This unhooks from threading and cleans up the profiler, returning
 
94
        the gathered Stats object.
 
95
 
 
96
        :return: A breezy.lsprof.Stats object.
 
97
        """
 
98
        try:
 
99
            self.p.disable()
 
100
            for pp in self._g_threadmap.values():
 
101
                pp.disable()
 
102
            threading.setprofile(None)
 
103
            p = self.p
 
104
            self.p = None
 
105
            threads = {}
 
106
            for tid, pp in self._g_threadmap.items():
 
107
                threads[tid] = Stats(pp.getstats(), {})
 
108
            self._g_threadmap = None
 
109
            return Stats(p.getstats(), threads)
 
110
        finally:
 
111
            self.__class__.profiler_lock.release()
 
112
 
 
113
    def _thread_profile(self, f, *args, **kwds):
 
114
        # we lose the first profile point for a new thread in order to
 
115
        # trampoline a new Profile object into place
 
116
        thr = _thread.get_ident()
 
117
        self._g_threadmap[thr] = p = Profiler()
 
118
        # this overrides our sys.setprofile hook:
 
119
        p.enable(subcalls=True, builtins=True)
 
120
 
 
121
 
 
122
class Stats(object):
 
123
    """Wrapper around the collected data.
 
124
 
 
125
    A Stats instance is created when the profiler finishes. Normal
 
126
    usage is to use save() to write out the data to a file, or pprint()
 
127
    to write human-readable information to the command line.
 
128
    """
 
129
 
 
130
    def __init__(self, data, threads):
 
131
        self.data = data
 
132
        self.threads = threads
 
133
 
 
134
    def sort(self, crit="inlinetime", reverse=True):
 
135
        """Sort the data by the supplied critera.
 
136
 
 
137
        :param crit: the data attribute used as the sort key."""
 
138
        if crit not in profiler_entry.__dict__ or crit == 'code':
 
139
            raise ValueError("Can't sort by %s" % crit)
 
140
 
 
141
        key_func = operator.attrgetter(crit)
 
142
        self.data.sort(key=key_func, reverse=reverse)
 
143
 
 
144
        for e in self.data:
 
145
            if e.calls:
 
146
                e.calls.sort(key=key_func, reverse=reverse)
 
147
 
 
148
    def pprint(self, top=None, file=None):
 
149
        """Pretty-print the data as plain text for human consumption.
 
150
 
 
151
        :param top: only output the top n entries.
 
152
            The default value of None means output all data.
 
153
        :param file: the output file; if None, output will
 
154
            default to stdout."""
 
155
        if file is None:
 
156
            file = sys.stdout
 
157
        d = self.data
 
158
        if top is not None:
 
159
            d = d[:top]
 
160
        cols = "% 12s %12s %11.4f %11.4f   %s\n"
 
161
        hcols = "% 12s %12s %12s %12s %s\n"
 
162
        file.write(hcols % ("CallCount", "Recursive", "Total(ms)",
 
163
                            "Inline(ms)", "module:lineno(function)"))
 
164
        for e in d:
 
165
            file.write(cols % (e.callcount, e.reccallcount, e.totaltime,
 
166
                               e.inlinetime, label(e.code)))
 
167
            if e.calls:
 
168
                for se in e.calls:
 
169
                    file.write(cols % ("+%s" % se.callcount, se.reccallcount,
 
170
                                       se.totaltime, se.inlinetime,
 
171
                                       "+%s" % label(se.code)))
 
172
 
 
173
    def freeze(self):
 
174
        """Replace all references to code objects with string
 
175
        descriptions; this makes it possible to pickle the instance."""
 
176
 
 
177
        # this code is probably rather ickier than it needs to be!
 
178
        for i in range(len(self.data)):
 
179
            e = self.data[i]
 
180
            if not isinstance(e.code, str):
 
181
                self.data[i] = type(e)((label(e.code),) + e[1:])
 
182
            if e.calls:
 
183
                for j in range(len(e.calls)):
 
184
                    se = e.calls[j]
 
185
                    if not isinstance(se.code, str):
 
186
                        e.calls[j] = type(se)((label(se.code),) + se[1:])
 
187
        for s in self.threads.values():
 
188
            s.freeze()
 
189
 
 
190
    def calltree(self, file):
 
191
        """Output profiling data in calltree format (for KCacheGrind)."""
 
192
        _CallTreeFilter(self.data).output(file)
 
193
 
 
194
    def save(self, filename, format=None):
 
195
        """Save profiling data to a file.
 
196
 
 
197
        :param filename: the name of the output file
 
198
        :param format: 'txt' for a text representation;
 
199
            'callgrind' for calltree format;
 
200
            otherwise a pickled Python object. A format of None indicates
 
201
            that the format to use is to be found from the filename. If
 
202
            the name starts with callgrind.out, callgrind format is used
 
203
            otherwise the format is given by the filename extension.
 
204
        """
 
205
        if format is None:
 
206
            basename = os.path.basename(filename)
 
207
            if basename.startswith('callgrind.out'):
 
208
                format = "callgrind"
 
209
            else:
 
210
                ext = os.path.splitext(filename)[1]
 
211
                if len(ext) > 1:
 
212
                    format = ext[1:]
 
213
        with open(filename, 'wb') as outfile:
 
214
            if format == "callgrind":
 
215
                # The callgrind format states it is 'ASCII based':
 
216
                # <http://valgrind.org/docs/manual/cl-format.html>
 
217
                # But includes filenames so lets ignore and use UTF-8.
 
218
                self.calltree(codecs.getwriter('utf-8')(outfile))
 
219
            elif format == "txt":
 
220
                self.pprint(file=codecs.getwriter('utf-8')(outfile))
 
221
            else:
 
222
                self.freeze()
 
223
                pickle.dump(self, outfile, 2)
 
224
 
 
225
 
 
226
class _CallTreeFilter(object):
 
227
    """Converter of a Stats object to input suitable for KCacheGrind.
 
228
 
 
229
    This code is taken from http://ddaa.net/blog/python/lsprof-calltree
 
230
    with the changes made by J.P. Calderone and Itamar applied. Note that
 
231
    isinstance(code, str) needs to be used at times to determine if the code
 
232
    object is actually an external code object (with a filename, etc.) or
 
233
    a Python built-in.
 
234
    """
 
235
 
 
236
    def __init__(self, data):
 
237
        self.data = data
 
238
        self.out_file = None
 
239
 
 
240
    def output(self, out_file):
 
241
        self.out_file = out_file
 
242
        out_file.write('events: Ticks\n')
 
243
        self._print_summary()
 
244
        for entry in self.data:
 
245
            self._entry(entry)
 
246
 
 
247
    def _print_summary(self):
 
248
        max_cost = 0
 
249
        for entry in self.data:
 
250
            totaltime = int(entry.totaltime * 1000)
 
251
            max_cost = max(max_cost, totaltime)
 
252
        self.out_file.write('summary: %d\n' % (max_cost,))
 
253
 
 
254
    def _entry(self, entry):
 
255
        out_file = self.out_file
 
256
        code = entry.code
 
257
        inlinetime = int(entry.inlinetime * 1000)
 
258
        if isinstance(code, str):
 
259
            out_file.write('fi=~\n')
 
260
        else:
 
261
            out_file.write('fi=%s\n' % (code.co_filename,))
 
262
        out_file.write('fn=%s\n' % (label(code, True),))
 
263
        if isinstance(code, str):
 
264
            out_file.write('0  %s\n' % (inlinetime,))
 
265
        else:
 
266
            out_file.write('%d %d\n' % (code.co_firstlineno, inlinetime))
 
267
        # recursive calls are counted in entry.calls
 
268
        if entry.calls:
 
269
            calls = entry.calls
 
270
        else:
 
271
            calls = []
 
272
        if isinstance(code, str):
 
273
            lineno = 0
 
274
        else:
 
275
            lineno = code.co_firstlineno
 
276
        for subentry in calls:
 
277
            self._subentry(lineno, subentry)
 
278
        out_file.write('\n')
 
279
 
 
280
    def _subentry(self, lineno, subentry):
 
281
        out_file = self.out_file
 
282
        code = subentry.code
 
283
        totaltime = int(subentry.totaltime * 1000)
 
284
        if isinstance(code, str):
 
285
            out_file.write('cfi=~\n')
 
286
            out_file.write('cfn=%s\n' % (label(code, True),))
 
287
            out_file.write('calls=%d 0\n' % (subentry.callcount,))
 
288
        else:
 
289
            out_file.write('cfi=%s\n' % (code.co_filename,))
 
290
            out_file.write('cfn=%s\n' % (label(code, True),))
 
291
            out_file.write('calls=%d %d\n' % (
 
292
                subentry.callcount, code.co_firstlineno))
 
293
        out_file.write('%d %d\n' % (lineno, totaltime))
 
294
 
 
295
 
 
296
_fn2mod = {}
 
297
 
 
298
 
 
299
def label(code, calltree=False):
 
300
    if isinstance(code, str):
 
301
        return code
 
302
    try:
 
303
        mname = _fn2mod[code.co_filename]
 
304
    except KeyError:
 
305
        for k, v in sys.modules.items():
 
306
            if v is None:
 
307
                continue
 
308
            if getattr(v, '__file__', None) is None:
 
309
                continue
 
310
            if not isinstance(v.__file__, str):
 
311
                continue
 
312
            if v.__file__.startswith(code.co_filename):
 
313
                mname = _fn2mod[code.co_filename] = k
 
314
                break
 
315
        else:
 
316
            mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename
 
317
    if calltree:
 
318
        return '%s %s:%d' % (code.co_name, mname, code.co_firstlineno)
 
319
    else:
 
320
        return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
 
321
 
 
322
 
 
323
def main():
 
324
    sys.argv = sys.argv[1:]
 
325
    if not sys.argv:
 
326
        sys.stderr.write("usage: lsprof.py <script> <arguments...>\n")
 
327
        sys.exit(2)
 
328
    import runpy
 
329
    result, stats = profile(runpy.run_path, sys.argv[0], run_name='__main__')
 
330
    stats.sort()
 
331
    stats.pprint()
 
332
 
 
333
 
 
334
if __name__ == '__main__':
 
335
    main()