~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/lsprof.py

  • Committer: Michael Hudson
  • Date: 2007-11-19 08:48:54 UTC
  • mfrom: (128.2.34 loggerhead.robey)
  • mto: This revision was merged to the branch mainline in revision 142.
  • Revision ID: michael.hudson@canonical.com-20071119084854-b1r1fqwzwve1hcgt
[merge conflict] from robey

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
#
3
1
# this is copied from the lsprof distro because somehow
4
2
# it is not installed by distutils
5
3
# I made one modification to profile so that it returns a pair
26
24
 
27
25
 
28
26
def profile(f, *args, **kwds):
29
 
    """FIXME: docstring"""
 
27
    """XXX docstring"""
30
28
    global _g_threadmap
31
29
    p = Profiler()
32
30
    p.enable(subcalls=True)
38
36
        for pp in _g_threadmap.values():
39
37
            pp.disable()
40
38
        threading.setprofile(None)
41
 
 
 
39
    
42
40
    threads = {}
43
41
    for tid, pp in _g_threadmap.items():
44
42
        threads[tid] = Stats(pp.getstats(), {})
47
45
 
48
46
 
49
47
class Stats(object):
50
 
    """FIXME: docstring"""
 
48
    """XXX docstring"""
51
49
 
52
50
    def __init__(self, data, threads):
53
51
        self.data = data
54
52
        self.threads = threads
55
53
 
56
54
    def sort(self, crit="inlinetime"):
57
 
        """FIXME: docstring"""
 
55
        """XXX docstring"""
58
56
        if crit not in profiler_entry.__dict__:
59
 
            raise ValueError("Can't sort by %s" % crit)
 
57
            raise ValueError, "Can't sort by %s" % crit
60
58
        self.data.sort(lambda b, a: cmp(getattr(a, crit),
61
59
                                        getattr(b, crit)))
62
60
        for e in self.data:
65
63
                                              getattr(b, crit)))
66
64
 
67
65
    def pprint(self, top=None, file=None):
68
 
        """FIXME: docstring"""
 
66
        """XXX docstring"""
69
67
        if file is None:
70
68
            file = sys.stdout
71
69
        d = self.data
73
71
            d = d[:top]
74
72
        cols = "% 12s %12s %11.4f %11.4f   %s\n"
75
73
        hcols = "% 12s %12s %12s %12s %s\n"
 
74
        cols2 = "+%12s %12s %11.4f %11.4f +  %s\n"
76
75
        file.write(hcols % ("CallCount", "Recursive", "Total(ms)",
77
76
                            "Inline(ms)", "module:lineno(function)"))
78
77
        for e in d:
92
91
        for i in range(len(self.data)):
93
92
            e = self.data[i]
94
93
            if not isinstance(e.code, str):
95
 
                self.data[i] = type(e)((label(e.code)) + e[1:])
 
94
                self.data[i] = type(e)((label(e.code),) + e[1:])
96
95
            if e.calls:
97
96
                for j in range(len(e.calls)):
98
97
                    se = e.calls[j]
99
98
                    if not isinstance(se.code, str):
100
 
                        e.calls[j] = type(se)((label(se.code)) + se[1:])
 
99
                        e.calls[j] = type(se)((label(se.code),) + se[1:])
101
100
        for s in self.threads.values():
102
101
            s.freeze()
103
102
 
113
112
        self.out_file = None
114
113
 
115
114
    def output(self, out_file):
116
 
        self.out_file = out_file
 
115
        self.out_file = out_file        
117
116
        print >> out_file, 'events: Ticks'
118
117
        self._print_summary()
119
118
        for entry in self.data:
124
123
        for entry in self.data:
125
124
            totaltime = int(entry.totaltime * 1000)
126
125
            max_cost = max(max_cost, totaltime)
127
 
        print >> self.out_file, 'summary: %d' % (max_cost)
 
126
        print >> self.out_file, 'summary: %d' % (max_cost,)
128
127
 
129
128
    def _entry(self, entry):
130
129
        out_file = self.out_file
131
130
        code = entry.code
132
131
        inlinetime = int(entry.inlinetime * 1000)
133
132
        #print >> out_file, 'ob=%s' % (code.co_filename,)
134
 
        print >> out_file, 'fi=%s' % (code.co_filename)
135
 
        print >> out_file, 'fn=%s' % (label(code, True))
 
133
        print >> out_file, 'fi=%s' % (code.co_filename,)
 
134
        print >> out_file, 'fn=%s' % (label(code, True),)
136
135
        print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
137
136
        # recursive calls are counted in entry.calls
138
137
        if entry.calls:
148
147
        code = subentry.code
149
148
        totaltime = int(subentry.totaltime * 1000)
150
149
        #print >> out_file, 'cob=%s' % (code.co_filename,)
151
 
        print >> out_file, 'cfn=%s' % (label(code, True))
152
 
        print >> out_file, 'cfi=%s' % (code.co_filename)
 
150
        print >> out_file, 'cfn=%s' % (label(code, True),)
 
151
        print >> out_file, 'cfi=%s' % (code.co_filename,)
153
152
        print >> out_file, 'calls=%d %d' % (
154
153
            subentry.callcount, code.co_firstlineno)
155
154
        print >> out_file, '%d %d' % (lineno, totaltime)
157
156
 
158
157
_fn2mod = {}
159
158
 
160
 
 
161
159
def label(code, calltree=False):
162
160
    if isinstance(code, str):
163
161
        return code