~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Robert Collins
  • Date: 2007-08-02 03:17:46 UTC
  • mto: (2592.3.65 repository)
  • mto: This revision was merged to the branch mainline in revision 2667.
  • Revision ID: robertc@robertcollins.net-20070802031746-mpnoaxym829719w6
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
  files that are stored on a transport. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import threading
11
11
from _lsprof import Profiler, profiler_entry
12
12
 
13
 
 
14
13
__all__ = ['profile', 'Stats']
15
14
 
16
15
_g_threadmap = {}
27
26
 
28
27
 
29
28
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
 
    """
 
29
    """XXX docstring"""
38
30
    global _g_threadmap
39
31
    p = Profiler()
40
32
    p.enable(subcalls=True)
46
38
        for pp in _g_threadmap.values():
47
39
            pp.disable()
48
40
        threading.setprofile(None)
49
 
 
 
41
    
50
42
    threads = {}
51
43
    for tid, pp in _g_threadmap.items():
52
44
        threads[tid] = Stats(pp.getstats(), {})
126
118
            otherwise the format is given by the filename extension.
127
119
        """
128
120
        if format is None:
129
 
            basename = os.path.basename(filename)
130
 
            if basename.startswith('callgrind.out'):
 
121
            if filename.startswith('callgrind.out'):
131
122
                format = "callgrind"
132
123
            else:
133
124
                ext = os.path.splitext(filename)[1]
151
142
 
152
143
    This code is taken from http://ddaa.net/blog/python/lsprof-calltree
153
144
    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
 
145
    isinstance(code, str) needs to be used at times to determine if the code 
155
146
    object is actually an external code object (with a filename, etc.) or
156
147
    a Python built-in.
157
148
    """
161
152
        self.out_file = None
162
153
 
163
154
    def output(self, out_file):
164
 
        self.out_file = out_file
165
 
        out_file.write('events: Ticks\n')
 
155
        self.out_file = out_file        
 
156
        print >> out_file, 'events: Ticks'
166
157
        self._print_summary()
167
158
        for entry in self.data:
168
159
            self._entry(entry)
172
163
        for entry in self.data:
173
164
            totaltime = int(entry.totaltime * 1000)
174
165
            max_cost = max(max_cost, totaltime)
175
 
        self.out_file.write('summary: %d\n' % (max_cost,))
 
166
        print >> self.out_file, 'summary: %d' % (max_cost,)
176
167
 
177
168
    def _entry(self, entry):
178
169
        out_file = self.out_file
179
170
        code = entry.code
180
171
        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))
 
172
        #print >> out_file, 'ob=%s' % (code.co_filename,)
 
173
        if isinstance(code, str):
 
174
            print >> out_file, 'fi=~'
 
175
        else:
 
176
            print >> out_file, 'fi=%s' % (code.co_filename,)
 
177
        print >> out_file, 'fn=%s' % (label(code, True),)
 
178
        if isinstance(code, str):
 
179
            print >> out_file, '0 ', inlinetime
 
180
        else:
 
181
            print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
191
182
        # recursive calls are counted in entry.calls
192
183
        if entry.calls:
193
184
            calls = entry.calls
199
190
            lineno = code.co_firstlineno
200
191
        for subentry in calls:
201
192
            self._subentry(lineno, subentry)
202
 
        out_file.write('\n')
 
193
        print >> out_file
203
194
 
204
195
    def _subentry(self, lineno, subentry):
205
196
        out_file = self.out_file
206
197
        code = subentry.code
207
198
        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),))
 
199
        #print >> out_file, 'cob=%s' % (code.co_filename,)
 
200
        print >> out_file, 'cfn=%s' % (label(code, True),)
210
201
        if isinstance(code, str):
211
 
            out_file.write('cfi=~\n')
212
 
            out_file.write('calls=%d 0\n' % (subentry.callcount,))
 
202
            print >> out_file, 'cfi=~'
 
203
            print >> out_file, 'calls=%d 0' % (subentry.callcount,)
213
204
        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))
 
205
            print >> out_file, 'cfi=%s' % (code.co_filename,)
 
206
            print >> out_file, 'calls=%d %d' % (
 
207
                subentry.callcount, code.co_firstlineno)
 
208
        print >> out_file, '%d %d' % (lineno, totaltime)
218
209
 
219
210
_fn2mod = {}
220
211
 
246
237
    import os
247
238
    sys.argv = sys.argv[1:]
248
239
    if not sys.argv:
249
 
        sys.stderr.write("usage: lsprof.py <script> <arguments...>\n")
 
240
        print >> sys.stderr, "usage: lsprof.py <script> <arguments...>"
250
241
        sys.exit(2)
251
242
    sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
252
243
    stats = profile(execfile, sys.argv[0], globals(), locals())