~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

Merge bzr.dev

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
7
 
import os
8
6
import sys
9
7
import thread
10
8
import threading
11
9
from _lsprof import Profiler, profiler_entry
12
10
 
13
 
 
14
11
__all__ = ['profile', 'Stats']
15
12
 
16
13
_g_threadmap = {}
27
24
 
28
25
 
29
26
def profile(f, *args, **kwds):
30
 
    """Run a function profile.
31
 
 
32
 
    Exceptions are not caught: If you need stats even when exceptions are to be
33
 
    raised, passing in a closure that will catch the exceptions and transform
34
 
    them appropriately for your driver function.
35
 
 
36
 
    :return: The functions return value and a stats object.
37
 
    """
 
27
    """XXX docstring"""
38
28
    global _g_threadmap
39
29
    p = Profiler()
40
30
    p.enable(subcalls=True)
46
36
        for pp in _g_threadmap.values():
47
37
            pp.disable()
48
38
        threading.setprofile(None)
49
 
 
 
39
    
50
40
    threads = {}
51
41
    for tid, pp in _g_threadmap.items():
52
42
        threads[tid] = Stats(pp.getstats(), {})
114
104
        """Output profiling data in calltree format (for KCacheGrind)."""
115
105
        _CallTreeFilter(self.data).output(file)
116
106
 
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
 
            basename = os.path.basename(filename)
130
 
            if basename.startswith('callgrind.out'):
131
 
                format = "callgrind"
132
 
            else:
133
 
                ext = os.path.splitext(filename)[1]
134
 
                if len(ext) > 1:
135
 
                    format = ext[1:]
136
 
        outfile = open(filename, 'wb')
137
 
        try:
138
 
            if format == "callgrind":
139
 
                self.calltree(outfile)
140
 
            elif format == "txt":
141
 
                self.pprint(file=outfile)
142
 
            else:
143
 
                self.freeze()
144
 
                cPickle.dump(self, outfile, 2)
145
 
        finally:
146
 
            outfile.close()
147
 
 
148
107
 
149
108
class _CallTreeFilter(object):
150
 
    """Converter of a Stats object to input suitable for KCacheGrind.
151
 
 
152
 
    This code is taken from http://ddaa.net/blog/python/lsprof-calltree
153
 
    with the changes made by J.P. Calderone and Itamar applied. Note that
154
 
    isinstance(code, str) needs to be used at times to determine if the code
155
 
    object is actually an external code object (with a filename, etc.) or
156
 
    a Python built-in.
157
 
    """
158
109
 
159
110
    def __init__(self, data):
160
111
        self.data = data
161
112
        self.out_file = None
162
113
 
163
114
    def output(self, out_file):
164
 
        self.out_file = out_file
165
 
        out_file.write('events: Ticks\n')
 
115
        self.out_file = out_file        
 
116
        print >> out_file, 'events: Ticks'
166
117
        self._print_summary()
167
118
        for entry in self.data:
168
119
            self._entry(entry)
172
123
        for entry in self.data:
173
124
            totaltime = int(entry.totaltime * 1000)
174
125
            max_cost = max(max_cost, totaltime)
175
 
        self.out_file.write('summary: %d\n' % (max_cost,))
 
126
        print >> self.out_file, 'summary: %d' % (max_cost,)
176
127
 
177
128
    def _entry(self, entry):
178
129
        out_file = self.out_file
179
130
        code = entry.code
180
131
        inlinetime = int(entry.inlinetime * 1000)
181
 
        #out_file.write('ob=%s\n' % (code.co_filename,))
182
 
        if isinstance(code, str):
183
 
            out_file.write('fi=~\n')
184
 
        else:
185
 
            out_file.write('fi=%s\n' % (code.co_filename,))
186
 
        out_file.write('fn=%s\n' % (label(code, True),))
187
 
        if isinstance(code, str):
188
 
            out_file.write('0  %s\n' % (inlinetime,))
189
 
        else:
190
 
            out_file.write('%d %d\n' % (code.co_firstlineno, inlinetime))
 
132
        #print >> out_file, 'ob=%s' % (code.co_filename,)
 
133
        print >> out_file, 'fi=%s' % (code.co_filename,)
 
134
        print >> out_file, 'fn=%s' % (label(code, True),)
 
135
        print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
191
136
        # recursive calls are counted in entry.calls
192
137
        if entry.calls:
193
138
            calls = entry.calls
194
139
        else:
195
140
            calls = []
196
 
        if isinstance(code, str):
197
 
            lineno = 0
198
 
        else:
199
 
            lineno = code.co_firstlineno
200
141
        for subentry in calls:
201
 
            self._subentry(lineno, subentry)
202
 
        out_file.write('\n')
 
142
            self._subentry(code.co_firstlineno, subentry)
 
143
        print >> out_file
203
144
 
204
145
    def _subentry(self, lineno, subentry):
205
146
        out_file = self.out_file
206
147
        code = subentry.code
207
148
        totaltime = int(subentry.totaltime * 1000)
208
 
        #out_file.write('cob=%s\n' % (code.co_filename,))
209
 
        out_file.write('cfn=%s\n' % (label(code, True),))
210
 
        if isinstance(code, str):
211
 
            out_file.write('cfi=~\n')
212
 
            out_file.write('calls=%d 0\n' % (subentry.callcount,))
213
 
        else:
214
 
            out_file.write('cfi=%s\n' % (code.co_filename,))
215
 
            out_file.write('calls=%d %d\n' % (
216
 
                subentry.callcount, code.co_firstlineno))
217
 
        out_file.write('%d %d\n' % (lineno, totaltime))
 
149
        #print >> out_file, 'cob=%s' % (code.co_filename,)
 
150
        print >> out_file, 'cfn=%s' % (label(code, True),)
 
151
        print >> out_file, 'cfi=%s' % (code.co_filename,)
 
152
        print >> out_file, 'calls=%d %d' % (
 
153
            subentry.callcount, code.co_firstlineno)
 
154
        print >> out_file, '%d %d' % (lineno, totaltime)
 
155
 
218
156
 
219
157
_fn2mod = {}
220
158
 
246
184
    import os
247
185
    sys.argv = sys.argv[1:]
248
186
    if not sys.argv:
249
 
        sys.stderr.write("usage: lsprof.py <script> <arguments...>\n")
 
187
        print >> sys.stderr, "usage: lsprof.py <script> <arguments...>"
250
188
        sys.exit(2)
251
189
    sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
252
190
    stats = profile(execfile, sys.argv[0], globals(), locals())