~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Robey Pointer
  • Date: 2006-02-02 01:42:06 UTC
  • mto: (1706.2.1 bzr.dev.lsprof)
  • mto: This revision was merged to the branch mainline in revision 1830.
  • Revision ID: robey@dhcp-192-168-1-88.danger.com-20060202014206-bf6c5b6d352b93dd
add threading support -- each thread created during an lsprof session gets its own profile object, and the Stats objects are attached to a 'threads' member of the final Stats object

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, profiler_subentry
8
10
 
9
11
__all__ = ['profile', 'Stats']
10
12
 
 
13
_g_threadmap = {}
 
14
 
 
15
def _thread_profile(f, *args, **kwds):
 
16
    # we lose the first profile point for a new thread in order to trampoline
 
17
    # a new Profile object into place
 
18
    global _g_threadmap
 
19
    thr = thread.get_ident()
 
20
    _g_threadmap[thr] = p = Profiler()
 
21
    # this overrides our sys.setprofile hook:
 
22
    p.enable(subcalls=True)
 
23
 
 
24
 
11
25
def profile(f, *args, **kwds):
12
26
    """XXX docstring"""
 
27
    global _g_threadmap
13
28
    p = Profiler()
14
29
    p.enable(subcalls=True)
 
30
    threading.setprofile(_thread_profile)
15
31
    try:
16
32
        ret = f(*args, **kwds)
17
33
    finally:
18
34
        p.disable()
19
 
    return ret,Stats(p.getstats())
 
35
        for pp in _g_threadmap.values():
 
36
            pp.disable()
 
37
        threading.setprofile(None)
 
38
    
 
39
    threads = {}
 
40
    for tid, pp in _g_threadmap.items():
 
41
        threads[tid] = Stats(pp.getstats(), {})
 
42
    _g_threadmap = {}
 
43
    return ret, Stats(p.getstats(), threads)
20
44
 
21
45
 
22
46
class Stats(object):
23
47
    """XXX docstring"""
24
48
 
25
 
    def __init__(self, data):
 
49
    def __init__(self, data, threads):
26
50
        self.data = data
 
51
        self.threads = threads
27
52
 
28
53
    def sort(self, crit="inlinetime"):
29
54
        """XXX docstring"""
71
96
                        se = e.calls[j]
72
97
                        if not isinstance(se.code, str):
73
98
                            e.calls[j] = type(se)((label(se.code),) + se[1:])
 
99
        for s in self.threads.values():
 
100
            s.freeze()
 
101
 
74
102
 
75
103
_fn2mod = {}
76
104