Esempio 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
Esempio 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
Esempio 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))
Esempio 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))
Esempio n. 5
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
Esempio n. 6
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))