/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: Martin
  • Date: 2018-11-16 16:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 7172.
  • Revision ID: gzlist@googlemail.com-20181116163822-yg1h1cdng6w7w9kn
Make --profile-imports work on Python 3

Also tweak heading to line up correctly.

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