/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 bzrlib/lsprof.py

  • Committer: Ian Clatworthy
  • Date: 2007-09-10 02:40:07 UTC
  • mto: (2818.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2819.
  • Revision ID: ian.clatworthy@internode.on.net-20070910024007-qeqapxc91bqwhe0q
Dump profiling data even when exceptions are encountered

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
import cPickle
 
7
import os
 
8
import sys
 
9
import thread
 
10
import threading
 
11
from _lsprof import Profiler, profiler_entry
 
12
 
 
13
__all__ = ['profile', 'Stats']
 
14
 
 
15
_g_threadmap = {}
 
16
 
 
17
 
 
18
def _thread_profile(f, *args, **kwds):
 
19
    # we lose the first profile point for a new thread in order to trampoline
 
20
    # a new Profile object into place
 
21
    global _g_threadmap
 
22
    thr = thread.get_ident()
 
23
    _g_threadmap[thr] = p = Profiler()
 
24
    # this overrides our sys.setprofile hook:
 
25
    p.enable(subcalls=True, builtins=True)
 
26
 
 
27
 
 
28
def profile(f, *args, **kwds):
 
29
    """XXX docstring"""
 
30
    global _g_threadmap
 
31
    p = Profiler()
 
32
    p.enable(subcalls=True)
 
33
    threading.setprofile(_thread_profile)
 
34
    # Note: The except clause is needed below so that profiling data still
 
35
    # gets dumped even when exceptions are encountered. The except clause code
 
36
    # is taken straight from run_bzr_catch_errrors() in commands.py and ought
 
37
    # to be kept in sync with it.
 
38
    try:
 
39
        ret = f(*args, **kwds)
 
40
    except (KeyboardInterrupt, Exception), e:
 
41
        import bzrlib.trace
 
42
        bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
 
43
        ret = 3
 
44
    finally:
 
45
        p.disable()
 
46
        for pp in _g_threadmap.values():
 
47
            pp.disable()
 
48
        threading.setprofile(None)
 
49
    
 
50
    threads = {}
 
51
    for tid, pp in _g_threadmap.items():
 
52
        threads[tid] = Stats(pp.getstats(), {})
 
53
    _g_threadmap = {}
 
54
    return ret, Stats(p.getstats(), threads)
 
55
 
 
56
 
 
57
class Stats(object):
 
58
    """XXX docstring"""
 
59
 
 
60
    def __init__(self, data, threads):
 
61
        self.data = data
 
62
        self.threads = threads
 
63
 
 
64
    def sort(self, crit="inlinetime"):
 
65
        """XXX docstring"""
 
66
        if crit not in profiler_entry.__dict__:
 
67
            raise ValueError, "Can't sort by %s" % crit
 
68
        self.data.sort(lambda b, a: cmp(getattr(a, crit),
 
69
                                        getattr(b, crit)))
 
70
        for e in self.data:
 
71
            if e.calls:
 
72
                e.calls.sort(lambda b, a: cmp(getattr(a, crit),
 
73
                                              getattr(b, crit)))
 
74
 
 
75
    def pprint(self, top=None, file=None):
 
76
        """XXX docstring"""
 
77
        if file is None:
 
78
            file = sys.stdout
 
79
        d = self.data
 
80
        if top is not None:
 
81
            d = d[:top]
 
82
        cols = "% 12s %12s %11.4f %11.4f   %s\n"
 
83
        hcols = "% 12s %12s %12s %12s %s\n"
 
84
        cols2 = "+%12s %12s %11.4f %11.4f +  %s\n"
 
85
        file.write(hcols % ("CallCount", "Recursive", "Total(ms)",
 
86
                            "Inline(ms)", "module:lineno(function)"))
 
87
        for e in d:
 
88
            file.write(cols % (e.callcount, e.reccallcount, e.totaltime,
 
89
                               e.inlinetime, label(e.code)))
 
90
            if e.calls:
 
91
                for se in e.calls:
 
92
                    file.write(cols % ("+%s" % se.callcount, se.reccallcount,
 
93
                                       se.totaltime, se.inlinetime,
 
94
                                       "+%s" % label(se.code)))
 
95
 
 
96
    def freeze(self):
 
97
        """Replace all references to code objects with string
 
98
        descriptions; this makes it possible to pickle the instance."""
 
99
 
 
100
        # this code is probably rather ickier than it needs to be!
 
101
        for i in range(len(self.data)):
 
102
            e = self.data[i]
 
103
            if not isinstance(e.code, str):
 
104
                self.data[i] = type(e)((label(e.code),) + e[1:])
 
105
            if e.calls:
 
106
                for j in range(len(e.calls)):
 
107
                    se = e.calls[j]
 
108
                    if not isinstance(se.code, str):
 
109
                        e.calls[j] = type(se)((label(se.code),) + se[1:])
 
110
        for s in self.threads.values():
 
111
            s.freeze()
 
112
 
 
113
    def calltree(self, file):
 
114
        """Output profiling data in calltree format (for KCacheGrind)."""
 
115
        _CallTreeFilter(self.data).output(file)
 
116
 
 
117
    def save(self, filename, format=None):
 
118
        """Save profiling data to a file.
 
119
 
 
120
        :param filename: the name of the output file
 
121
        :param format: 'txt' for a text representation;
 
122
            'callgrind' for calltree format;
 
123
            otherwise a pickled Python object. A format of None indicates
 
124
            that the format to use is to be found from the filename. If
 
125
            the name starts with callgrind.out, callgrind format is used
 
126
            otherwise the format is given by the filename extension.
 
127
        """
 
128
        if format is None:
 
129
            if filename.startswith('callgrind.out'):
 
130
                format = "callgrind"
 
131
            else:
 
132
                ext = os.path.splitext(filename)[1]
 
133
                if len(ext) > 1:
 
134
                    format = ext[1:]
 
135
        outfile = open(filename, 'wb')
 
136
        try:
 
137
            if format == "callgrind":
 
138
                self.calltree(outfile)
 
139
            elif format == "txt":
 
140
                self.pprint(file=outfile)
 
141
            else:
 
142
                self.freeze()
 
143
                cPickle.dump(self, outfile, 2)
 
144
        finally:
 
145
            outfile.close()
 
146
 
 
147
 
 
148
class _CallTreeFilter(object):
 
149
    """Converter of a Stats object to input suitable for KCacheGrind.
 
150
 
 
151
    This code is taken from http://ddaa.net/blog/python/lsprof-calltree
 
152
    with the changes made by J.P. Calderone and Itamar applied. Note that
 
153
    isinstance(code, str) needs to be used at times to determine if the code 
 
154
    object is actually an external code object (with a filename, etc.) or
 
155
    a Python built-in.
 
156
    """
 
157
 
 
158
    def __init__(self, data):
 
159
        self.data = data
 
160
        self.out_file = None
 
161
 
 
162
    def output(self, out_file):
 
163
        self.out_file = out_file        
 
164
        print >> out_file, 'events: Ticks'
 
165
        self._print_summary()
 
166
        for entry in self.data:
 
167
            self._entry(entry)
 
168
 
 
169
    def _print_summary(self):
 
170
        max_cost = 0
 
171
        for entry in self.data:
 
172
            totaltime = int(entry.totaltime * 1000)
 
173
            max_cost = max(max_cost, totaltime)
 
174
        print >> self.out_file, 'summary: %d' % (max_cost,)
 
175
 
 
176
    def _entry(self, entry):
 
177
        out_file = self.out_file
 
178
        code = entry.code
 
179
        inlinetime = int(entry.inlinetime * 1000)
 
180
        #print >> out_file, 'ob=%s' % (code.co_filename,)
 
181
        if isinstance(code, str):
 
182
            print >> out_file, 'fi=~'
 
183
        else:
 
184
            print >> out_file, 'fi=%s' % (code.co_filename,)
 
185
        print >> out_file, 'fn=%s' % (label(code, True),)
 
186
        if isinstance(code, str):
 
187
            print >> out_file, '0 ', inlinetime
 
188
        else:
 
189
            print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
 
190
        # recursive calls are counted in entry.calls
 
191
        if entry.calls:
 
192
            calls = entry.calls
 
193
        else:
 
194
            calls = []
 
195
        if isinstance(code, str):
 
196
            lineno = 0
 
197
        else:
 
198
            lineno = code.co_firstlineno
 
199
        for subentry in calls:
 
200
            self._subentry(lineno, subentry)
 
201
        print >> out_file
 
202
 
 
203
    def _subentry(self, lineno, subentry):
 
204
        out_file = self.out_file
 
205
        code = subentry.code
 
206
        totaltime = int(subentry.totaltime * 1000)
 
207
        #print >> out_file, 'cob=%s' % (code.co_filename,)
 
208
        print >> out_file, 'cfn=%s' % (label(code, True),)
 
209
        if isinstance(code, str):
 
210
            print >> out_file, 'cfi=~'
 
211
            print >> out_file, 'calls=%d 0' % (subentry.callcount,)
 
212
        else:
 
213
            print >> out_file, 'cfi=%s' % (code.co_filename,)
 
214
            print >> out_file, 'calls=%d %d' % (
 
215
                subentry.callcount, code.co_firstlineno)
 
216
        print >> out_file, '%d %d' % (lineno, totaltime)
 
217
 
 
218
_fn2mod = {}
 
219
 
 
220
def label(code, calltree=False):
 
221
    if isinstance(code, str):
 
222
        return code
 
223
    try:
 
224
        mname = _fn2mod[code.co_filename]
 
225
    except KeyError:
 
226
        for k, v in sys.modules.items():
 
227
            if v is None:
 
228
                continue
 
229
            if getattr(v, '__file__', None) is None:
 
230
                continue
 
231
            if not isinstance(v.__file__, str):
 
232
                continue
 
233
            if v.__file__.startswith(code.co_filename):
 
234
                mname = _fn2mod[code.co_filename] = k
 
235
                break
 
236
        else:
 
237
            mname = _fn2mod[code.co_filename] = '<%s>'%code.co_filename
 
238
    if calltree:
 
239
        return '%s %s:%d' % (code.co_name, mname, code.co_firstlineno)
 
240
    else:
 
241
        return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
 
242
 
 
243
 
 
244
if __name__ == '__main__':
 
245
    import os
 
246
    sys.argv = sys.argv[1:]
 
247
    if not sys.argv:
 
248
        print >> sys.stderr, "usage: lsprof.py <script> <arguments...>"
 
249
        sys.exit(2)
 
250
    sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
 
251
    stats = profile(execfile, sys.argv[0], globals(), locals())
 
252
    stats.sort()
 
253
    stats.pprint()