~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-07-02 00:49:15 UTC
  • mfrom: (1706.2.8 bzr.dev.lsprof)
  • Revision ID: pqm@pqm.ubuntu.com-20060702004915-501855cc9fc14e10
(robey) updates for lsprof

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
# instead of just the Stats object
5
5
 
6
6
import sys
 
7
import thread
 
8
import threading
7
9
from _lsprof import Profiler, profiler_entry
8
10
 
9
11
__all__ = ['profile', 'Stats']
10
12
 
 
13
_g_threadmap = {}
 
14
 
 
15
 
 
16
def _thread_profile(f, *args, **kwds):
 
17
    # we lose the first profile point for a new thread in order to trampoline
 
18
    # a new Profile object into place
 
19
    global _g_threadmap
 
20
    thr = thread.get_ident()
 
21
    _g_threadmap[thr] = p = Profiler()
 
22
    # this overrides our sys.setprofile hook:
 
23
    p.enable(subcalls=True, builtins=True)
 
24
 
 
25
 
11
26
def profile(f, *args, **kwds):
12
27
    """XXX docstring"""
 
28
    global _g_threadmap
13
29
    p = Profiler()
14
30
    p.enable(subcalls=True)
 
31
    threading.setprofile(_thread_profile)
15
32
    try:
16
33
        ret = f(*args, **kwds)
17
34
    finally:
18
35
        p.disable()
19
 
    return ret,Stats(p.getstats())
 
36
        for pp in _g_threadmap.values():
 
37
            pp.disable()
 
38
        threading.setprofile(None)
 
39
    
 
40
    threads = {}
 
41
    for tid, pp in _g_threadmap.items():
 
42
        threads[tid] = Stats(pp.getstats(), {})
 
43
    _g_threadmap = {}
 
44
    return ret, Stats(p.getstats(), threads)
20
45
 
21
46
 
22
47
class Stats(object):
23
48
    """XXX docstring"""
24
49
 
25
 
    def __init__(self, data):
 
50
    def __init__(self, data, threads):
26
51
        self.data = data
 
52
        self.threads = threads
27
53
 
28
54
    def sort(self, crit="inlinetime"):
29
55
        """XXX docstring"""
66
92
            e = self.data[i]
67
93
            if not isinstance(e.code, str):
68
94
                self.data[i] = type(e)((label(e.code),) + e[1:])
69
 
                if e.calls:
70
 
                    for j in range(len(e.calls)):
71
 
                        se = e.calls[j]
72
 
                        if not isinstance(se.code, str):
73
 
                            e.calls[j] = type(se)((label(se.code),) + se[1:])
 
95
            if e.calls:
 
96
                for j in range(len(e.calls)):
 
97
                    se = e.calls[j]
 
98
                    if not isinstance(se.code, str):
 
99
                        e.calls[j] = type(se)((label(se.code),) + se[1:])
 
100
        for s in self.threads.values():
 
101
            s.freeze()
 
102
 
 
103
    def calltree(self, file):
 
104
        """Output profiling data in calltree format (for KCacheGrind)."""
 
105
        _CallTreeFilter(self.data).output(file)
 
106
 
 
107
 
 
108
class _CallTreeFilter(object):
 
109
 
 
110
    def __init__(self, data):
 
111
        self.data = data
 
112
        self.out_file = None
 
113
 
 
114
    def output(self, out_file):
 
115
        self.out_file = out_file        
 
116
        print >> out_file, 'events: Ticks'
 
117
        self._print_summary()
 
118
        for entry in self.data:
 
119
            self._entry(entry)
 
120
 
 
121
    def _print_summary(self):
 
122
        max_cost = 0
 
123
        for entry in self.data:
 
124
            totaltime = int(entry.totaltime * 1000)
 
125
            max_cost = max(max_cost, totaltime)
 
126
        print >> self.out_file, 'summary: %d' % (max_cost,)
 
127
 
 
128
    def _entry(self, entry):
 
129
        out_file = self.out_file
 
130
        code = entry.code
 
131
        inlinetime = int(entry.inlinetime * 1000)
 
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)
 
136
        # recursive calls are counted in entry.calls
 
137
        if entry.calls:
 
138
            calls = entry.calls
 
139
        else:
 
140
            calls = []
 
141
        for subentry in calls:
 
142
            self._subentry(code.co_firstlineno, subentry)
 
143
        print >> out_file
 
144
 
 
145
    def _subentry(self, lineno, subentry):
 
146
        out_file = self.out_file
 
147
        code = subentry.code
 
148
        totaltime = int(subentry.totaltime * 1000)
 
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
 
74
156
 
75
157
_fn2mod = {}
76
158
 
77
 
def label(code):
 
159
def label(code, calltree=False):
78
160
    if isinstance(code, str):
79
161
        return code
80
162
    try:
92
174
                break
93
175
        else:
94
176
            mname = _fn2mod[code.co_filename] = '<%s>'%code.co_filename
95
 
    
96
 
    return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
 
177
    if calltree:
 
178
        return '%s %s:%d' % (code.co_name, mname, code.co_firstlineno)
 
179
    else:
 
180
        return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
97
181
 
98
182
 
99
183
if __name__ == '__main__':