Ejemplo n.º 1
0
def _setup(options):
    global _registered, _trace_memory, mem_usage
    if not _registered:
        from openmdao.devtools.memory import mem_usage

        mem_changes = defaultdict(lambda: [0., 0, set()])
        memstack = []
        callstack = []
        _trace_memory = _create_profile_callback(
            callstack,
            _collect_methods(_get_methods(options, default='openmdao_all')),
            do_call=_trace_mem_call,
            do_ret=_trace_mem_ret,
            context=(memstack, mem_changes))

        def print_totals():
            print("Memory (MB)   Calls  File:Line:Function")
            print("---------------------------------------")
            for key, (delta, ncalls, parents) in sorted(mem_changes.items(),
                                                        key=lambda x: x[1]):
                if delta != 0.0:
                    print("%10.4g  %7d  %s:%d:%s" %
                          (delta, ncalls, key[0], key[1], key[2]))
            print("---------------------------------------")
            print("Memory (MB)   Calls  File:Line:Function")

        atexit.register(print_totals)
        _registered = True
Ejemplo n.º 2
0
def _setup(options):
    global _registered, _trace_memory, mem_usage
    if not _registered:
        from openmdao.devtools.debug import mem_usage

        mem_changes = defaultdict(lambda: [0., 0, set()])
        memstack = []
        callstack = []
        _trace_memory = _create_profile_callback(callstack,
                                                 _collect_methods(
                                                    _get_methods(options, default='openmdao_all')),
                                                 do_call=_trace_mem_call, do_ret=_trace_mem_ret,
                                                 context=(memstack, mem_changes))

        def print_totals():
            print("Memory (MB)   Calls  File:Line:Function")
            print("---------------------------------------")
            for key, (delta, ncalls, parents) in sorted(mem_changes.items(), key=lambda x: x[1]):
                if delta != 0.0:
                    print("%10.4g  %7d  %s:%d:%s" % (delta, ncalls, key[0], key[1], key[2]))
            print("---------------------------------------")
            print("Memory (MB)   Calls  File:Line:Function")

        atexit.register(print_totals)
        _registered = True
Ejemplo n.º 3
0
def setup(methods=None, verbose=False):
    """
    Setup call tracing.

    Parameters
    ----------
    methods : list of (glob, (classes...)) or None
        Methods to be traced, based on glob patterns and isinstance checks.
    verbose : bool
        If True, show function locals and return values.
    """
    global _registered, _trace_calls
    if not _registered:
        if methods is None:
            methods = func_group['openmdao']
        elif isinstance(methods, string_types):
            methods = func_group.get(methods)
            if methods is None:
                methods = func_group['openmdao']

        call_stack = []
        qual_cache = {}
        method_counts = defaultdict(int)
        class_counts = defaultdict(set)
        if verbose:
            do_ret = _trace_return
        else:
            do_ret = None
        _trace_calls = _create_profile_callback(
            call_stack,
            _collect_methods(methods),
            do_call=_trace_call,
            do_ret=do_ret,
            context=(qual_cache, method_counts, class_counts, verbose))
Ejemplo n.º 4
0
def _setup(options):
    if not func_group:
        _setup_func_group()

    global _registered, _trace_calls, _printer

    verbose = options.verbose
    memory = options.memory
    leaks = options.leaks

    if not _registered:
        methods = _get_methods(options, default='openmdao')

        call_stack = []
        qual_cache = {}
        method_counts = defaultdict(int)
        class_counts = defaultdict(lambda: -1)
        id2count = {}
        if verbose or memory or leaks:
            do_ret = _trace_return
        else:
            do_ret = None

        if memory:
            if psutil is None:
                raise RuntimeError(
                    "Memory tracing requires the 'psutil' package.  "
                    "Install it using 'pip install psutil'.")
            memory = []
        else:
            memory = None

        if leaks:
            if objgraph is None:
                raise RuntimeError(
                    "Leak detection requires the 'objgraph' package. "
                    "Install it using 'pip install objgraph'.")
            leaks = []
        else:
            leaks = None

        if options.outfile == 'stdout':
            stream = sys.stdout
        elif options.outfile == 'stderr':
            stream = sys.stderr
        else:
            stream = open(options.outfile, 'w')

        _printer = _get_printer(stream, options.rank)

        _trace_calls = _create_profile_callback(
            call_stack,
            _collect_methods(methods),
            do_call=_trace_call,
            do_ret=do_ret,
            context=(qual_cache, method_counts, class_counts, id2count,
                     verbose, memory, leaks, stream))
Ejemplo n.º 5
0
def setup(prefix='iprof', methods=None, prof_dir=None, finalize=True):
    """
    Instruments certain important openmdao methods for profiling.

    Parameters
    ----------

    prefix : str ('iprof')
        Prefix used for the raw profile data. Process rank will be appended
        to it to get the actual filename.  When not using MPI, rank=0.

    methods : list, optional
        A list of tuples of profiled methods to override the default set.  The first
        entry is the method name or glob pattern and the second is a tuple of class
        objects used for isinstance checking.  The default set of methods is:

        ::

            [
                "*": (System, Jacobian, Matrix, Solver, Driver, Problem),
            ]

    prof_dir : str
        Directory where the profile files will be written. Defaults to the
        current directory.

    finallize : bool
        If True, register a function to finalize the profile before exit.

    """

    global _profile_prefix, _matches
    global _profile_setup, _profile_total, _profile_out

    if _profile_setup:
        raise RuntimeError("profiling is already set up.")

    if prof_dir is None:
        _profile_prefix = os.path.join(os.getcwd(), prefix)
    else:
        _profile_prefix = os.path.join(os.path.abspath(prof_dir), prefix)

    _profile_setup = True

    if methods is None:
        methods = func_group['openmdao']

    rank = MPI.COMM_WORLD.rank if MPI else 0
    _profile_out = open("%s.%s" % (_profile_prefix, rank), 'wb')

    if finalize:
        atexit.register(_finalize_profile)

    _matches = _collect_methods(methods)
Ejemplo n.º 6
0
def _setup(options, finalize=True):

    global _profile_prefix, _matches
    global _profile_setup, _profile_total, _profile_out

    if _profile_setup:
        raise RuntimeError("profiling is already set up.")

    _profile_prefix = os.path.join(os.getcwd(), 'iprof')
    _profile_setup = True

    methods = _get_methods(options, default='openmdao')

    rank = MPI.COMM_WORLD.rank if MPI else 0
    _profile_out = open("%s.%s" % (_profile_prefix, rank), 'wb')

    if finalize:
        atexit.register(_finalize_profile)

    _matches = _collect_methods(methods)
Ejemplo n.º 7
0
def _setup(options, finalize=True):

    global _profile_prefix, _matches
    global _profile_setup, _profile_total, _profile_out

    if _profile_setup:
        raise RuntimeError("profiling is already set up.")

    _profile_prefix = os.path.join(os.getcwd(), 'iprof')
    _profile_setup = True

    methods = _get_methods(options, default='openmdao')

    rank = MPI.COMM_WORLD.rank if MPI else 0
    _profile_out = open("%s.%s" % (_profile_prefix, rank), 'wb')

    if finalize:
        atexit.register(_finalize_profile)

    _matches = _collect_methods(methods)
Ejemplo n.º 8
0
def setup(methods=None):
    """
    Setup memory profiling.

    Parameters
    ----------
    methods : list of (glob, (classes...)) or None
        Methods to be profiled, based on glob patterns and isinstance checks.
    """
    global _registered, _trace_memory, mem_usage
    if not _registered:
        from openmdao.devtools.debug import mem_usage
        if methods is None:
            methods = func_group['openmdao_all']

        mem_changes = defaultdict(lambda: [0., 0, set()])
        memstack = []
        callstack = []
        _trace_memory = _create_profile_callback(callstack,
                                                 _collect_methods(methods),
                                                 do_call=_trace_mem_call,
                                                 do_ret=_trace_mem_ret,
                                                 context=(memstack,
                                                          mem_changes))

        def print_totals():
            print("Memory (MB)   Calls  File:Line:Function")
            print("---------------------------------------")
            for key, (delta, ncalls, parents) in sorted(mem_changes.items(),
                                                        key=lambda x: x[1]):
                if delta != 0.0:
                    print("%10.4g  %7d  %s:%d:%s" %
                          (delta, ncalls, key[0], key[1], key[2]))
            print("---------------------------------------")
            print("Memory (MB)   Calls  File:Line:Function")

        atexit.register(print_totals)
        _registered = True
Ejemplo n.º 9
0
def _setup(options):
    if not func_group:
        _setup_func_group()

    global _registered, _trace_calls

    verbose=options.verbose
    if not _registered:
        methods = _get_methods(options, default='openmdao')

        call_stack = []
        qual_cache = {}
        method_counts = defaultdict(int)
        class_counts = defaultdict(set)
        if verbose:
            do_ret = _trace_return
        else:
            do_ret = None
        _trace_calls = _create_profile_callback(call_stack, _collect_methods(methods),
                                                do_call=_trace_call,
                                                do_ret=do_ret,
                                                context=(qual_cache, method_counts,
                                                         class_counts, verbose))