Example #1
0
 def _thread_profile(self, f, *args, **kwds):
     # we lose the first profile point for a new thread in order to
     # trampoline a new Profile object into place
     thr = thread.get_ident()
     self._g_threadmap[thr] = p = Profiler()
     # this overrides our sys.setprofile hook:
     p.enable(subcalls=True, builtins=True)
Example #2
0
def profile(f, *args, **kwds):
    """XXX docstring"""
    global _g_threadmap
    p = Profiler()
    p.enable(subcalls=True)
    threading.setprofile(_thread_profile)
    # Note: The except clause is needed below so that profiling data still
    # gets dumped even when exceptions are encountered. The except clause code
    # is taken straight from run_bzr_catch_errrors() in commands.py and ought
    # to be kept in sync with it.
    try:
        try:
            ret = f(*args, **kwds)
        except (KeyboardInterrupt, Exception), e:
            import bzrlib.trace
            bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
            ret = 3
    finally:
        p.disable()
        for pp in _g_threadmap.values():
            pp.disable()
        threading.setprofile(None)
    
    threads = {}
    for tid, pp in _g_threadmap.items():
        threads[tid] = Stats(pp.getstats(), {})
    _g_threadmap = {}
    return ret, Stats(p.getstats(), threads)
Example #3
0
def profile(f, *args, **kwds):
    """XXX docstring"""
    p = Profiler()
    p.enable(subcalls=True, builtins=True)
    try:
        f(*args, **kwds)
    finally:
        p.disable()
    return Stats(p.getstats())
Example #4
0
 def start(self):
     """Start profiling.
     
     This hooks into threading and will record all calls made until
     stop() is called.
     """
     self._g_threadmap = {}
     self.p = Profiler()
     permitted = self.__class__.profiler_lock.acquire(
         self.__class__.profiler_block)
     if not permitted:
         raise errors.InternalBzrError(msg="Already profiling something")
     try:
         self.p.enable(subcalls=True)
         threading.setprofile(self._thread_profile)
     except:
         self.__class__.profiler_lock.release()
         raise
Example #5
0
def profile(f, *args, **kwds):
    """FIXME: docstring"""
    global _g_threadmap
    p = Profiler()
    p.enable(subcalls=True)
    threading.setprofile(_thread_profile)
    try:
        ret = f(*args, **kwds)
    finally:
        p.disable()
        for pp in _g_threadmap.values():
            pp.disable()
        threading.setprofile(None)

    threads = {}
    for tid, pp in _g_threadmap.items():
        threads[tid] = Stats(pp.getstats(), {})
    _g_threadmap = {}
    return ret, Stats(p.getstats(), threads)