Example #1
0
 def profiler(self,frame,event,arg):
     """
     This is the profile routine. It creates the instances of ForthonTimings for each
     routine called and starts and stops the timers.
     """
     # --- Get the name of the routine
     name = frame.f_code.co_name
     # --- If name is not known, this could mean that this was called from
     # --- inside the sys.setprofile routine, or from some other odd place,
     # --- like from fortran. Skip those cases.
     if name == '?': return
     # --- Turn the profiler off during these operations (though this is
     # --- probably unecessary since the sys package should already do this).
     sys.setprofile(None)
     # --- Create an instance of the timer for the toplevel if it hasn't
     # --- already been done.
     if self.root is None:
         self.root = ForthonTimings(0,"toplevel")
         self.timer = self.root
     if event == 'return' and self.level > 0:
         self.level = self.level - 1
         self.timer = self.timer.stoptimer()
     if self.trace:
         if self.tracelevel is None or self.tracelevel > self.level:
             # --- The flush is added so that anything that was printed to stdout
             # --- or stderr get outputed now so that the print outs occur
             # --- at the proper time relative to the trace.
             sys.stdout.flush()
             sys.stderr.flush()
             print "%s %s %s"%(self.level*'  ',event,name)
     if event == 'call':
         self.level = self.level + 1
         self.timer = self.timer.newtimer(name)
     # --- Turn the profiler back on
     sys.setprofile(self.profiler)
Example #2
0
def stop():
    """
    Turn off memory profiling.
    """
    global _out_stream
    sys.setprofile(None)
    _out_stream.close()
Example #3
0
    def __call__(self, func, *args, **kwargs):
        global _converting
        if _converting:
            return func(*args, **kwargs) # skip
        else:
            # clean start
            sys.setprofile(None)
        from myhdl import _traceSignals
        if _traceSignals._tracing:
            raise ToVerilogError("Cannot use toVerilog while tracing signals")
        if not callable(func):
            raise ToVerilogError(_error.FirstArgType, "got %s" % type(func))

        _converting = 1
        if self.name is None:
            name = func.func_name
        else:
            name = str(self.name)
        try:
            h = _HierExtr(name, func, *args, **kwargs)
        finally:
            _converting = 0

        vpath = name + ".v"
        vfile = open(vpath, 'w')

        ### initialize properly ###
        _genUniqueSuffix.reset()

        arglist = _flatten(h.top)
        # print h.top
        _checkArgs(arglist)
        genlist = _analyzeGens(arglist, h.absnames)
        siglist, memlist = _analyzeSigs(h.hierarchy)
        _annotateTypes(genlist)
        top_inst = h.hierarchy[0]
        intf = _analyzeTopFunc(top_inst, func, *args, **kwargs)
        doc = _makeDoc(inspect.getdoc(func))

        self._convert_filter(h, intf, siglist, memlist, genlist)

        _writeFileHeader(vfile, vpath, self.timescale)
        _writeModuleHeader(vfile, intf, doc)
        _writeSigDecls(vfile, intf, siglist, memlist)
        _convertGens(genlist, vfile)
        _writeModuleFooter(vfile)

        vfile.close()

        # don't write testbench if module has no ports
        if len(intf.argnames) > 0 and not toVerilog.no_testbench:
            tbpath = "tb_" + vpath
            tbfile = open(tbpath, 'w')
            _writeTestBench(tbfile, intf)
            tbfile.close()

        ### clean-up properly ###
        self._cleanup(siglist)

        return h.top
Example #4
0
	def runctx(self, cmd, globals, locals):
		self.set_cmd(cmd)
		sys.setprofile(self.dispatcher)
		try:
			exec cmd in globals, locals
		finally:
			sys.setprofile(None)
Example #5
0
 def start(self, name = 'start'):
     if getattr(self, 'running', False):
         return
     self._setup()
     self.simulate_call('start')
     self.running = True
     sys.setprofile(self.dispatcher)
Example #6
0
def set_profile(active=False):
    if active:
        log_start("Profiling init", bookmark=True)
        sys.setprofile(_profile)
    else:
        sys.setprofile(None)
        log_endok("Profiling exit")
Example #7
0
 def run(self, profiler):
     profile = functools.partial(self._profile, profiler)
     sys.setprofile(profile)
     threading.setprofile(profile)
     yield
     threading.setprofile(None)
     sys.setprofile(None)
Example #8
0
    def Tasklet(self, do_profile=False, do_trace=False, disable_trace_after_schedule=False):
        if do_profile:
            sys.setprofile(self.Callback)
            self.addCleanup(sys.setprofile, None)
        if do_trace:
            sys.settrace(self.Callback)
            self.addCleanup(sys.settrace, None)

        self.foo()
        n = len(self.trace)
        self.foo()
        n2 = len(self.trace)
        if do_profile or do_trace:
            self.assertGreater(n2, n)
        else:
            self.assertEqual(n2, n)

        schedule()

        self.foo()
        n = len(self.trace)
        self.foo()
        n2 = len(self.trace)
        if (do_profile or do_trace) and not disable_trace_after_schedule:
            self.assertGreater(n2, n)
        else:
            self.assertEqual(n2, n)
Example #9
0
	def runctx(self, cmd, globals, locals):
		self.set_cmd(cmd)
		sys.setprofile(self.trace_dispatch)
		try:
			exec(cmd, globals, locals)
		finally:
			sys.setprofile(None)
Example #10
0
 def runcall(self, func, *args):
     self.set_cmd(`func`)
     sys.setprofile(self.dispatcher)
     try:
         return apply(func, args)
     finally:
         sys.setprofile(None)
Example #11
0
    def get_info_on_next_invocation(self, func, callback, needed):
        """
        func is a tuple (filename, funcname)
        needed is a list/tuple of NeededInfo objects
        """

        if isinstance(needed, NeededInfo):
            needed = (needed,)

        request = WatchRequest(needed, {}, callback)
        if func in self.watching_funcs:
            self.watching_funcs[func].requests.append(request)
        else:
            self.watching_funcs[func] = FuncWatchStatus(func, requests=[request])
        fws = self.watching_funcs[func]

        for ni in needed:
            hsh = 0
            for i in range(ni.stmt_idx+1):
                hsh += hash(ni.stmt_sequence[i])
                #cprint ("adding to hash %s: %i"%(str(ni.stmt_sequence[i]), hsh), 'green')
            if hsh in fws.ni_hashmap:
                fws.ni_hashmap[hsh].append((request,ni))
            else:
                fws.ni_hashmap[hsh] = [(request,ni)]

        cprint("finishing up get_info_on_next_invocation", 'red')

        # XXX Why do I need to use both settrace and setprofile?  settrace doesn't
        # XXX trigger on return events, and setprofile doesn't trigger on line events...
        self.old_trace, self.old_profile = sys.gettrace(), sys.getprofile()
        sys.settrace(self.trace_cb)
        sys.setprofile(self.profile_cb)
Example #12
0
    def notify_event(self, frame, event, arg):
        if event in ["call", "return"]:

            fname = self._getfilename(frame)
            func = frame.f_code.co_name

            if event == "return":
                self.frameno -= 1
                if not self.enabled and self.frameno == self.depth:
                    self.enabled = True
                if self.frameno < 0:
                    sys.setprofile(None)
                    return

            if self.enabled:
                lineno = frame.f_lineno
                prefix  = "    " * self.frameno
                buff = "%s%s %s:%d:%s()\n" % (prefix,event,fname,lineno,func)
                sys.stderr.write(buff)

            if event == "call":
                if self.enabled and fname.startswith("logging/"):
                    self.enabled = False
                    self.depth = self.frameno
                self.frameno += 1
Example #13
0
 def runcall(self, func, *args, **kw):
     self.set_cmd(repr(func))
     sys.setprofile(self.dispatcher)
     try:
         return func(*args, **kw)
     finally:
         sys.setprofile(None)
Example #14
0
def run_toplevel(f, *fargs, **fkwds):
    """Calls f() and handles all OperationErrors.
    Intended use is to run the main program or one interactive statement.
    run_protected() handles details like forwarding exceptions to
    sys.excepthook(), catching SystemExit, printing a newline after
    sys.stdout if needed, etc.
    """
    try:
        # run it
        try:
            f(*fargs, **fkwds)
        finally:
            sys.settrace(None)
            sys.setprofile(None)

        # we arrive here if no exception is raised.  stdout cosmetics...
        try:
            stdout = sys.stdout
            softspace = stdout.softspace
        except AttributeError:
            pass
            # Don't crash if user defined stdout doesn't have softspace
        else:
            if softspace:
                stdout.write('\n')

    except SystemExit as e:
        handle_sys_exit(e)
    except BaseException as e:
        display_exception(e)
        return False
    return True   # success
Example #15
0
def profile_on(*filenames):
    global p_stats, p_start_time, files
    p_stats = []
    p_start_time = time.time()
    files = filenames
    threading.setprofile(profiler)
    sys.setprofile(profiler)
Example #16
0
 def event(self, frame, event, arg):
     currentTime = time()
     code = frame.f_code
     if (not code.co_filename.endswith('threading.py')) or code.co_name == 'acquire' or code.co_name == 'release':
         func = (code.co_filename, code.co_firstlineno, code.co_name)
         thread = threading.currentThread().getName()
         if not self.active:
             #no longer active, lets try to teach that to the thread ;)
             sys.setprofile(None)
         else:
             #still active
             if thread not in self.callStack:
                 sys.setprofile(self.event)
             
             if event == 'call':
                 #entered func
                 self.callStack[thread].append((time(), func))
                 
             elif event == 'return':
                 #returned from func
                 if len(self.callStack[thread]) > 0:
                     startTime, oldFunc = self.callStack[thread].pop()
                     assert oldFunc == func
                     self.funcStats[func][0] += currentTime - startTime
                     self.funcStats[func][1] += 1
Example #17
0
def setprofile(include='', exclude=None, output=sys.stdout):
    """
    Print a message on method call/return/exception.

    @param include: A regular expression for output filtering. The regular
    expression is applied to the output string, which is on the format 'event
    filename(line): classname.methodname'.

    For example, setprofile(os.getcwd()) prints only methods that are defined
    in python files in this directory and subdirectories. setprofile('^c_')
    prints only C invocations. And so on.

    @param exclude: Another regular expression.
    """
    include = re.compile(include)
    exclude = exclude and re.compile(exclude)
    def prof(frame, event, arg):
        meth = frame.f_code.co_name
        try:
            cls = frame.f_locals['self']
            if isinstance(cls, object):
                cls = cls.__class__
            meth = '.'.join([cls.__name__, meth])
        except KeyError:
            pass
        s = '%s: %s(%s): %s\n' % (event, frame.f_code.co_filename,
                                  frame.f_lineno, meth)
        if include.search(s) and not (exclude and exclude.search(s)):
            output.write(s)
    sys.setprofile(prof)
Example #18
0
 def attachThread(self, target=None, args=None, kwargs=None, mainThread=0):
     """
     Public method to setup a thread for DebugClient to debug.
     
     If mainThread is non-zero, then we are attaching to the already
     started mainthread of the app and the rest of the args are ignored.
     
     @param target the start function of the target thread (i.e. the
         user code)
     @param args arguments to pass to target
     @param kwargs keyword arguments to pass to target
     @param mainThread non-zero, if we are attaching to the already
           started mainthread of the app
     @return The identifier of the created thread
     """
     try:
         self.lockClient()
         newThread = DebugThread(self, target, args, kwargs, mainThread)
         ident = -1
         if mainThread:
             ident = thread.get_ident()
             self.mainThread = newThread
             if self.debugging:
                 sys.setprofile(newThread.profile)
         else:
             ident = _original_start_thread(newThread.bootstrap, ())
             if self.mainThread is not None:
                 self.tracePython = self.mainThread.tracePython
         newThread.set_ident(ident)
         self.threads[newThread.get_ident()] = newThread
     finally:
         self.unlockClient()
     return ident
Example #19
0
 def start(self, name="start"):
     if getattr(self, "running", False):
         return
     self._setup()
     self.simulate_call("start")
     self.running = True
     sys.setprofile(self.dispatcher)
Example #20
0
 def run(self, cmd):
     sys.setprofile(self.profile)
     try:
         exec(cmd)
     finally:
         sys.setprofile(None)
     return self
Example #21
0
    def run(self):
        if _settrace_func:
            _sys.settrace(_settrace_func)
        if _setprofile_func:
            _sys.setprofile(_setprofile_func)

        self.__target(*self.__args, **self.__kwargs)
def capture_events(callable, p=None):
    if p is None:
        p = HookWatcher()
    sys.setprofile(p.callback)
    protect(callable, p)
    sys.setprofile(None)
    return p.get_events()[1:-1]
Example #23
0
    def platform_specific(self, context, *args, **kwargs):

        def tracer(frame, event, arg):
            if event == 'return':
                self._locals = frame.f_locals.copy()

        # tracer is activated on next call, return or exception
        sys.setprofile(tracer)
        try:
            # trace the function call
            self.func(context, *args, **kwargs)
        finally:
            # disable tracer and replace with old one
            sys.setprofile(None)

        if hasattr(context, 'browser') and 'browser' in self._locals:
            return self._locals['browser'](context, *args, **kwargs)
        elif hasattr(context, 'device'):
            if context.device.capabilities['platformName'] == 'iOS' and 'ios' in self._locals:
                return self._locals['ios'](context, *args, **kwargs)
            elif context.device.capabilities['platformName'] == 'Android' and 'android' in self._locals:
                return self._locals['android'](context, *args, **kwargs)
            elif 'mobile' in self._locals:
                return self._locals['mobile'](context, *args, **kwargs)

        raise MultiplatformException(
            "Function %s was decorated with @multiplatform but could not find appropriate context" % self.func)
Example #24
0
 def trace_dispatch(self, frame, event, arg):
     """
     Public method wrapping the trace_dispatch of bdb.py.
     
     It wraps the call to dispatch tracing into
     bdb to make sure we have locked the client to prevent multiple
     threads from entering the client event loop.
     
     @param frame The current stack frame.
     @param event The trace event (string)
     @param arg The arguments
     @return local trace function
     """
     try:
         self._dbgClient.lockClient()
         # if this thread came out of a lock, and we are quitting
         # and we are still running, then get rid of tracing for this thread
         if self.quitting and self._threadRunning:
             sys.settrace(None)
             sys.setprofile(None)
         import threading
         self.__name = threading.currentThread().getName()
         retval = DebugBase.trace_dispatch(self, frame, event, arg)
     finally:
         self._dbgClient.unlockClient()
     
     return retval
Example #25
0
    def __init__(self, filename=None, timer=time.time, fudge=5e-6):
        if Profiler.profiler:
            Profiler.profiler.stop()
        Profiler.profiler = self
        self.filename = filename
        self.timer = timer
        self.funcdict = {}
        self.stack = []
        self.fudge = fudge
        self.overhead = 0.
        # time is measured in elapsed time since the program began,
        # not counting time spent in the profiler.
        self.lasteventtime = 0.
        self.ncalls = 0
        # Put a dummy function in the dictionary.  It will pretend to
        # be the calling function when we don't know the real one.
        dummyid = ('?', 0, '?')
        self.dummyfunc = Function(dummyid)
        self.funcdict[dummyid] = self.dummyfunc

        atexit.register(_stopprof)

        # starttime is the time the Profiler was started PLUS an
        # estimate of all of the time spent running the Profiler
        # itself.  That is, it's an estimate of when a non-profiled
        # program would have been started if it were at the same point
        # that the profiled program is now.  starttime is continually
        # updated.
        self.starttime = timer()
        # Ok, here we go...
        sys.setprofile(self.handler)
Example #26
0
  def __bootstrap(self):
    try:
      self._set_ident()
      self._Thread__started.set()
      threading._active_limbo_lock.acquire()
      threading._active[self._Thread__ident] = self
      del threading._limbo[self]
      threading._active_limbo_lock.release()

      if threading._trace_hook:
        sys.settrace(threading._trace_hook)
      if threading._profile_hook:
        sys.setprofile(threading._profile_hook)

      try:
        self.run()
      finally:
        self._Thread__exc_clear()
    finally:
      with threading._active_limbo_lock:
        self._Thread__stop()
        try:
          del threading._active[threading._get_ident()]
        except:
          pass
Example #27
0
 def start(self):
     if sys.getprofile() is not None:
         raise RuntimeError('Another profiler already registered.')
     self._running = True
     sys.setprofile(self._profile)
     threading.setprofile(self._profile)
     self.timer.start()
     self.stats.record_starting(time.clock())
Example #28
0
 def set_trace(self, frame=None):
     """
     Public method reimplemented from bdb.py to do some special setup.
     
     @param frame frame to start debugging from
     """
     bdb.Bdb.set_trace(self, frame)
     sys.setprofile(self.profile)
Example #29
0
	def runcall(self, func, *args):
		self.set_cmd(func.__name__)
		sys.setprofile(self.trace_dispatch)
		try:
			apply(func, args)
		finally:
			sys.setprofile(None)
		return self
Example #30
0
 def run(self, profiler):
     profile = functools.partial(self._profile, profiler)
     with deferral() as defer:
         sys.setprofile(profile)
         defer(sys.setprofile, None)
         threading.setprofile(profile)
         defer(threading.setprofile, None)
         yield
 def _unsetprofile():
     sys.setprofile(None)
Example #32
0
 def _setprofile(func):
     threading.setprofile(func)
     sys.setprofile(func)
Example #33
0
 def setUp(self):
     sys.setprofile(None)
Example #34
0
def trace_mpi(fname='mpi_trace', skip=(), flush=True):
    """
    Dump traces to the specified filename<.rank> showing openmdao and mpi/petsc calls.

    Parameters
    ----------
    fname : str
        Name of the trace file(s).  <.rank> will be appended to the name on each rank.
    skip : set-like
        Collection of function names to skip.
    flush : bool
        If True, flush print buffer after every print call.
    """
    if MPI is None:
        issue_warning("MPI is not active.  Trace aborted.", category=MPIWarning)
        return
    if sys.getprofile() is not None:
        raise RuntimeError("another profile function is already active.")

    my_fname = fname + '.' + str(MPI.COMM_WORLD.rank)

    outfile = open(my_fname, 'w')

    stack = []

    _c_map = {
        'c_call': '(c) -->',
        'c_return': '(c) <--',
        'c_exception': '(c_exception)',
    }


    def _print_c_func(frame, arg, typestr):
        s = str(arg)
        if 'mpi4py' in s or 'petsc4py' in s:
            c = arg.__self__.__class__
            print('   ' * len(stack), typestr, "%s.%s.%s" %
                    (c.__module__, c.__name__, arg.__name__),
                    "%s:%d" % (frame.f_code.co_filename, frame.f_code.co_firstlineno),
                    file=outfile, flush=True)


    def _mpi_trace_callback(frame, event, arg):
        pname = None
        commsize = ''
        if event == 'call':
            if 'openmdao' in frame.f_code.co_filename:
                if frame.f_code.co_name in skip:
                    return
                if 'self' in frame.f_locals:
                    try:
                        pname = frame.f_locals['self'].msginfo
                    except:
                        pass
                    try:
                        commsize = frame.f_locals['self'].comm.size
                    except:
                        pass
                if pname is not None:
                    if not stack or pname != stack[-1][0]:
                        stack.append([pname, 1])
                        print('   ' * len(stack), commsize, pname, file=outfile, flush=flush)
                    else:
                        stack[-1][1] += 1
                print('   ' * len(stack), '-->', frame.f_code.co_name, "%s:%d" %
                      (frame.f_code.co_filename, frame.f_code.co_firstlineno),
                      file=outfile, flush=flush)
        elif event == 'return':
            if 'openmdao' in frame.f_code.co_filename:
                if frame.f_code.co_name in skip:
                    return
                if 'self' in frame.f_locals:
                    try:
                        pname = frame.f_locals['self'].msginfo
                    except:
                        pass
                    try:
                        commsize = frame.f_locals['self'].comm.size
                    except:
                        pass
                print('   ' * len(stack), '<--', frame.f_code.co_name, "%s:%d" %
                      (frame.f_code.co_filename, frame.f_code.co_firstlineno),
                      file=outfile, flush=flush)
                if pname is not None and stack and pname == stack[-1][0]:
                    stack[-1][1] -= 1
                    if stack[-1][1] < 1:
                        stack.pop()
                        if stack:
                            print('   ' * len(stack), commsize, stack[-1][0], file=outfile,
                                  flush=flush)
        else:
            _print_c_func(frame, arg, _c_map[event])

    sys.setprofile(_mpi_trace_callback)
Example #35
0
 def __exit__(self, *args):
     sys.setprofile(self.oldtrace)
Example #36
0
def stop():
    """
    Turn off memory profiling.
    """
    sys.setprofile(None)
Example #37
0
import os
import time
import shutil
import functools
import re
import sys
import TraceFuncCalls as trc
import ClearScreen

'''
#Checking a Specific Function is Being Called.
sys.setprofile(trc.tracefunc)
trc.keyword = 'copyfile, copytree, memoryview'
'''

def gen_call_counter(func):
    def helper(*args, **kwargs):
        helper.calls += 1
        printable = format_time(helper.calls)
        sys.stdout.write('\r'+ ' '*50)
        sys.stdout.write('\rGeneral Backup Time Count: '+printable)
        return func(*args, **kwargs)

    helper.calls = 0
    helper.__name__ = func.__name__
    return helper

@gen_call_counter
def gen_callback(val):
    pass
Example #38
0
def _install_trace():
    sys.setprofile(_trace_func)
Example #39
0
 def start(self):
     sys.setprofile(self._profile)
     if not self.started:
         self.started = timeit.default_timer()
Example #40
0
 def stop(self):
     sys.setprofile(None)
Example #41
0
 def stop(self):
     sys.setprofile(None)
     self.running = False
     self.TallyTimings()
Example #42
0
 def pytest_runtest_teardown(self):
     """Stop collection by disabling profiling and flushing the logger."""
     sys.setprofile(None)
     self.trace_logger.flush()
Example #43
0
 def pytest_runtest_call(self):
     """Start collection by installing the CallTracer as profiler."""
     sys.setprofile(self.tracer)
Example #44
0
def profile_off():
    threading.setprofile(None)
    sys.setprofile(None)
Example #45
0
def profile_on():
    global p_stats, p_start_time
    p_stats = {}
    p_start_time = time.time()
    threading.setprofile(profiler)
    sys.setprofile(profiler)
Example #46
0
 def teardown_method(self, method):
     self.artist.remove()
     sys.setprofile(None)
Example #47
0
    def run(self):
        sys.settrace(None)
        sys.setprofile(None)

        threading.Thread.run(self)
Example #48
0
 def __enter__(self):
     self.oldtrace = sys.setprofile(self)
     return self.actions
Example #49
0
 def stop():
     sys.setprofile(None)
Example #50
0
 def stop():
     sys.setprofile(None)
     print("# Profile mode stopped. #")
Example #51
0
    def __bootstrap_inner(self):
        try:
            self._set_ident()
            self.__started.set()
            with _active_limbo_lock:
                _active[self.__ident] = self
                del _limbo[self]
            if __debug__:
                self._note("%s.__bootstrap(): thread started", self)

            if _trace_hook:
                self._note("%s.__bootstrap(): registering trace hook", self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note("%s.__bootstrap(): registering profile hook", self)
                _sys.setprofile(_profile_hook)

            try:
                self.run()
            except SystemExit:
                if __debug__:
                    self._note("%s.__bootstrap(): raised SystemExit", self)
            except:
                if __debug__:
                    self._note("%s.__bootstrap(): unhandled exception", self)
                # If sys.stderr is no more (most likely from interpreter
                # shutdown) use self.__stderr.  Otherwise still use sys (as in
                # _sys) in case sys.stderr was redefined since the creation of
                # self.
                if _sys:
                    _sys.stderr.write("Exception in thread %s:\n%s\n" %
                                      (self.name, _format_exc()))
                else:
                    # Do the best job possible w/o a huge amt. of code to
                    # approximate a traceback (code ideas from
                    # Lib/traceback.py)
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print >> self.__stderr, (
                            "Exception in thread " + self.name +
                            " (most likely raised during interpreter shutdown):"
                        )
                        print >> self.__stderr, (
                            "Traceback (most recent call last):")
                        while exc_tb:
                            print >> self.__stderr, (
                                '  File "%s", line %s, in %s' %
                                (exc_tb.tb_frame.f_code.co_filename,
                                 exc_tb.tb_lineno,
                                 exc_tb.tb_frame.f_code.co_name))
                            exc_tb = exc_tb.tb_next
                        print >> self.__stderr, ("%s: %s" %
                                                 (exc_type, exc_value))
                    # Make sure that exc_tb gets deleted since it is a memory
                    # hog; deleting everything else is just for thoroughness
                    finally:
                        del exc_type, exc_value, exc_tb
            else:
                if __debug__:
                    self._note("%s.__bootstrap(): normal return", self)
            finally:
                # Prevent a race in
                # test_threading.test_no_refcycle_through_target when
                # the exception keeps the target alive past when we
                # assert that it's dead.
                self.__exc_clear()
        finally:
            with _active_limbo_lock:
                self.__stop()
                try:
                    # We don't call self.__delete() because it also
                    # grabs _active_limbo_lock.
                    del _active[_get_ident()]
                except:
                    pass
Example #52
0
 def _unsetprofile():
     sys.setprofile(None)
     threading.setprofile(None)
Example #53
0
from ._scripts.converter import _main

# expose unified converter in coremltools package level
from .converters import convert
from .converters import (
    ClassifierConfig,
    TensorType,
    ImageType,
    RangeDim,
    Shape,
    EnumeratedShapes,
)
from .converters.mil._deployment_compatibility import AvailableTarget as target

# Time profiling for functions in coremltools package, decorated with @profile
import os as _os
import sys as _sys
from .converters._profile_utils import _profiler

_ENABLE_PROFILING = _os.environ.get("ENABLE_PROFILING", False)

if _ENABLE_PROFILING:
    _sys.setprofile(_profiler)

# Restore root logger handlers
_root_logger = _getLogger()
_coreml_logger = _getLogger(__name__)
_coreml_logger.handlers = _root_logger.handlers.copy()
_root_logger.handlers = _root_logger_handlers_backup
    def __bootstrap(self):
        try:
            self.__started = True
            _active_limbo_lock.acquire()
            _active[_get_ident()] = self
            del _limbo[self]
            _active_limbo_lock.release()
            if __debug__:
                self._note("%s.__bootstrap(): thread started", self)

            if _trace_hook:
                self._note("%s.__bootstrap(): registering trace hook", self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note("%s.__bootstrap(): registering profile hook", self)
                _sys.setprofile(_profile_hook)

            try:
                self.run()
            except SystemExit:
                if __debug__:
                    self._note("%s.__bootstrap(): raised SystemExit", self)
            except:
                if __debug__:
                    self._note("%s.__bootstrap(): unhandled exception", self)
                # If sys.stderr is no more (most likely from interpreter
                # shutdown) use self.__stderr.  Otherwise still use sys (as in
                # _sys) in case sys.stderr was redefined.
                if _sys:
                    _sys.stderr.write("Exception in thread %s:" %
                            self.getName())
                    _print_exc(file=_sys.stderr)
                else:
                    # Do the best job possible w/o a huge amt. of code to
                    # approx. a traceback stack trace
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print>>self.__stderr, (
                            "Exception in thread " + self.getName() +
                            " (most likely raised during interpreter shutdown):")
                        print>>self.__stderr, (
                            "Traceback (most recent call last):")
                        while exc_tb:
                            print>>self.__stderr, (
                                '  File "%s", line %s, in %s' %
                                (exc_tb.tb_frame.f_code.co_filename,
                                    exc_tb.tb_lineno,
                                    exc_tb.tb_frame.f_code.co_name))
                            exc_tb = exc_tb.tb_next
                        print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
                    # Make sure that exc_tb gets deleted since it is a memory
                    # hog; deleting everything else is just for thoroughness
                    finally:
                        del exc_type, exc_value, exc_tb
            else:
                if __debug__:
                    self._note("%s.__bootstrap(): normal return", self)
        finally:
            self.__stop()
            try:
                self.__delete()
            except:
                pass
Example #55
0
 def start(self):
     sys.setprofile(self._profile)
     self.started = timeit.default_timer()
     print("# Running in profile mode. #")
Example #56
0
 def __enter__(self):
     if self.tracer.enable_cpatching:
         sys.setprofile(self.func)
Example #57
0
 def __exit__(self, type, value, tb):
     sys.setprofile(None)
Example #58
0
def trace_execution(fn, args, save_to=None):
    import inspect
    if save_to:
        os.environ['INTEL_SEA_SAVE_TO'] = save_to
    itt = ITT("python")

    if itt.lib:
        file_id = itt.get_string_id('__FILE__')
        line_id = itt.get_string_id('__LINE__')
        module_id = itt.get_string_id('__MODULE__')
        trace_execution.frames = {}
        trace_execution.recurrent = False
        high_part = 2**32

        def profiler(frame, event,
                     arg):  # https://pymotw.com/2/sys/tracing.html
            if trace_execution.recurrent:
                return
            trace_execution.recurrent = True
            task_id = id(frame.f_code)
            if 'call' in event:
                if task_id in trace_execution.frames:
                    trace_execution.frames[task_id] += 1
                else:
                    trace_execution.frames[task_id] = 1
                task_id += trace_execution.frames[task_id] * high_part
                name = frame.f_code.co_name + (
                    (' (%s)' % arg.__name__) if arg else '')
                if 'self' in frame.f_locals:
                    cls = frame.f_locals['self'].__class__.__name__
                    name = cls + "." + name
                # print event, name, task_id, arg
                mdl = inspect.getmodule(frame)
                itt.lib.itt_task_begin_overlapped(itt.domain, task_id, 0,
                                                  itt.get_string_id(name), 0)
                itt.lib.itt_metadata_add_str(itt.domain, task_id, file_id,
                                             frame.f_code.co_filename)
                itt.lib.itt_metadata_add(itt.domain, task_id, line_id,
                                         frame.f_code.co_firstlineno)
                if mdl:
                    itt.lib.itt_metadata_add_str(itt.domain, task_id,
                                                 module_id, mdl.__name__)
            elif 'return' in event:
                # print event, frame.f_code.co_name, task_id + trace_execution.frames[task_id] * high_part
                if task_id in trace_execution.frames:
                    itt.lib.itt_task_end_overlapped(
                        itt.domain, 0,
                        task_id + trace_execution.frames[task_id] * high_part)
                    if trace_execution.frames[task_id] > 1:
                        trace_execution.frames[task_id] -= 1
                    else:
                        del trace_execution.frames[task_id]
                itt.counter('MEMORY_USAGE', get_memory_usage())
            trace_execution.recurrent = False

        old_profiler = sys.getprofile()
        sys.setprofile(profiler)
        old_threading_profiler = threading.setprofile(profiler)
        if fn:
            fn(*args)
            sys.setprofile(old_profiler)
            threading.setprofile(old_threading_profiler)
    elif fn:
        fn(*args)
Example #59
0
 def pytest_unconfigure(self, config):
     """Unconfigure by disabling profiling and flushing the logger."""
     sys.setprofile(None)
     self.trace_logger.flush()
Example #60
0
def _install_trace():
    global _trace_thread_count
    sys.setprofile(_thread_trace_func(_trace_thread_count))
    _trace_thread_count += 1