Esempio n. 1
0
def postprocess_memtrace_flat(fname, min_mem=1.0, show_colors=True, rank=0, stream=sys.stdout):
    from openmdao.utils.general_utils import simple_warning
    cprint, Fore, Back, Style = _get_color_printer(stream, show_colors, rank=rank)

    info = {}
    cache = {}

    top = None
    stack = []
    qual_cache = {}
    maxmem = 0.0

    if stream is None:
        stream = sys.stdout

    with open(fname, 'r') as f:
        for i, line in enumerate(f):
            parts = _process_parts(line.rstrip().split('|'), qual_cache)
            event, fpath, lineno, func, mem, elapsed, call = parts

            if mem > maxmem:
                maxmem = mem

            if event == 'c':

                key = call

                if key not in info:

                    info[key] = idict = {
                        'calls': 0,
                        'fpath': fpath,
                        'line': lineno,
                        'func': func,
                        'total_mem': 0.,
                        'time': elapsed,
                        'qualname': call,
                    }
                else:
                    idict = info[key]

                idict['calls'] += 1
                stack.append(parts)

            elif event == 'r':
                try:
                    c_parts = stack.pop()
                except IndexError:
                    continue  # last line is a return from funct called before we start recording

                c_event, c_fpath, c_lineno, c_func, c_mem, c_elapsed, c_call = c_parts

                assert c_fpath == fpath and c_lineno == lineno, "stack error: %s != %s" % (
                    [c_fpath, c_lineno], [fpath, lineno]
                )
                info[c_call]['total_mem'] += (mem - c_mem)

    # print out the final results
    # sort by total mem
    for val in sorted(info.values(), key=lambda x: x['total_mem']):

        if val['total_mem'] > min_mem:
            cprint("%7.2f " % val['total_mem'], color=Fore.GREEN + Style.BRIGHT)
            cprint(" (%d calls)  " % val['calls'])
            cprint("%s\n" % val['qualname'])

    cprint("\nMax mem usage: ")
    cprint("%7.2f MB\n" % maxmem, color=Fore.RED + Style.BRIGHT)
Esempio n. 2
0
def postprocess_memtrace_flat(fname, min_mem=1.0, show_colors=True, rank=0, stream=sys.stdout):
    from openmdao.utils.general_utils import simple_warning
    cprint, Fore, Back, Style = _get_color_printer(stream, show_colors, rank=rank)

    info = {}
    cache = {}

    top = None
    stack = []
    qual_cache = {}
    maxmem = 0.0

    if stream is None:
        stream = sys.stdout

    with open(fname, 'r') as f:
        for i, line in enumerate(f):
            parts = _process_parts(line.rstrip().split('|'), qual_cache)
            event, fpath, lineno, func, mem, elapsed, call = parts

            if mem > maxmem:
                maxmem = mem

            if event == 'c':

                key = call

                if key not in info:

                    info[key] = idict = {
                        'calls': 0,
                        'fpath': fpath,
                        'line': lineno,
                        'func': func,
                        'total_mem': 0.,
                        'time': elapsed,
                        'qualname': call,
                    }
                else:
                    idict = info[key]

                idict['calls'] += 1
                stack.append(parts)

            elif event == 'r':
                try:
                    c_parts = stack.pop()
                except IndexError:
                    continue  # last line is a return from funct called before we start recording

                c_event, c_fpath, c_lineno, c_func, c_mem, c_elapsed, c_call = c_parts

                assert c_fpath == fpath and c_lineno == lineno, "stack error: %s != %s" % (
                    [c_fpath, c_lineno], [fpath, lineno]
                )
                info[c_call]['total_mem'] += (mem - c_mem)

    # print out the final results
    # sort by total mem
    for val in sorted(info.values(), key=lambda x: x['total_mem']):

        if val['total_mem'] > min_mem:
            cprint("%7.2f " % val['total_mem'], color=Fore.GREEN + Style.BRIGHT)
            cprint(" (%d calls)  " % val['calls'])
            cprint("%s\n" % val['qualname'])

    cprint("\nMax mem usage: ")
    cprint("%7.2f MB\n" % maxmem, color=Fore.RED + Style.BRIGHT)
Esempio n. 3
0
def postprocess_memtrace_tree(fname, min_mem=1.0, show_colors=True, rank=0, stream=sys.stdout):
    from openmdao.utils.general_utils import simple_warning
    cprint, Fore, Back, Style = _get_color_printer(stream, show_colors, rank=rank)

    info = {}
    cache = {}

    top = None
    stack = []
    path_stack = []
    qual_cache = {}
    maxmem = 0.0

    if stream is None:
        stream = sys.stdout

    with open(fname, 'r') as f:
        for i, line in enumerate(f):
            parts = _process_parts(line.rstrip().split('|'), qual_cache)
            event, fpath, lineno, func, mem, elapsed, call = parts

            if mem > maxmem:
                maxmem = mem

            if event == 'c':

                path_stack.append(call)
                call_path = '|'.join(path_stack)

                key = call_path

                if key not in info:

                    info[key] = idict = {
                        'calls': 0,
                        'fpath': fpath,
                        'line': lineno,
                        'func': func,
                        'total_mem': 0.,
                        'time': elapsed,
                        'qualname': call,
                        'children': [],
                        'child_ids': set(),
                        'call_path': call_path,
                        'depth': len(path_stack),
                    }
                else:
                    idict = info[key]

                idict['calls'] += 1

                if stack:
                    pcall_path = '|'.join(path_stack[:-1])
                    parent = info[pcall_path]
                    ident = id(info[key])
                    if ident not in parent['child_ids']:
                        parent['children'].append(info[key])
                        parent['child_ids'].add(ident)

                stack.append(parts)

            elif event == 'r':
                try:
                    c_parts = stack.pop()
                except IndexError:
                    path_stack.pop()
                    continue  # last line is a return from funct called before we start recording

                c_event, c_fpath, c_lineno, c_func, c_mem, c_elapsed, c_call = c_parts

                assert c_fpath == fpath and c_lineno == lineno, "stack error: %s != %s" % (
                    [c_fpath, c_lineno], [fpath, lineno]
                )
                info['|'.join(path_stack)]['total_mem'] += (mem - c_mem)

                path_stack.pop()

    # print out the final results
    seen = set()
    # sort by depth first, then total mem
    for val in sorted(info.values(), key=lambda x: (-x['depth'], x['total_mem']), reverse=True):
        if id(val) in seen:
            continue
        stack = [('', iter([val]), 1.0e20)]
        while stack:
            indent, children, lastmem = stack[-1]
            try:
                val = next(children)
                if id(val) in seen:
                    continue
                seen.add(id(val))
                if val['total_mem'] > min_mem:
                    # if mem is more than the parent, highlight in red
                    if val['total_mem'] > lastmem:
                        fg = Fore.RED
                    else:
                        fg = Fore.GREEN
                    lastmem = val['total_mem']
                    cprint("%s%7.2f " % (indent, val['total_mem']), color=fg + Style.BRIGHT)
                    cprint(" (%d calls)  " % val['calls'])
                    cprint("%s\n" % val['qualname'])
                    stack.append((indent + '   |',
                                 iter(sorted(val['children'], key=lambda x: x['total_mem'],
                                             reverse=True)), lastmem))
            except StopIteration:
                stack.pop()

    cprint("\nMax mem usage: ")
    cprint("%7.2f MB\n" % maxmem, color=Fore.RED + Style.BRIGHT)
Esempio n. 4
0
def postprocess_memtrace_tree(fname, min_mem=1.0, show_colors=True, rank=0, stream=sys.stdout):
    from openmdao.utils.general_utils import simple_warning
    cprint, Fore, Back, Style = _get_color_printer(stream, show_colors, rank=rank)

    info = {}
    cache = {}

    top = None
    stack = []
    path_stack = []
    qual_cache = {}
    maxmem = 0.0

    if stream is None:
        stream = sys.stdout

    with open(fname, 'r') as f:
        for i, line in enumerate(f):
            parts = _process_parts(line.rstrip().split('|'), qual_cache)
            event, fpath, lineno, func, mem, elapsed, call = parts

            if mem > maxmem:
                maxmem = mem

            if event == 'c':

                path_stack.append(call)
                call_path = '|'.join(path_stack)

                key = call_path

                if key not in info:

                    info[key] = idict = {
                        'calls': 0,
                        'fpath': fpath,
                        'line': lineno,
                        'func': func,
                        'total_mem': 0.,
                        'time': elapsed,
                        'qualname': call,
                        'children': [],
                        'child_ids': set(),
                        'call_path': call_path,
                        'depth': len(path_stack),
                    }
                else:
                    idict = info[key]

                idict['calls'] += 1

                if stack:
                    pcall_path = '|'.join(path_stack[:-1])
                    parent = info[pcall_path]
                    ident = id(info[key])
                    if ident not in parent['child_ids']:
                        parent['children'].append(info[key])
                        parent['child_ids'].add(ident)

                stack.append(parts)

            elif event == 'r':
                try:
                    c_parts = stack.pop()
                except IndexError:
                    path_stack.pop()
                    continue  # last line is a return from funct called before we start recording

                c_event, c_fpath, c_lineno, c_func, c_mem, c_elapsed, c_call = c_parts

                assert c_fpath == fpath and c_lineno == lineno, "stack error: %s != %s" % (
                    [c_fpath, c_lineno], [fpath, lineno]
                )
                info['|'.join(path_stack)]['total_mem'] += (mem - c_mem)

                path_stack.pop()

    # print out the final results
    seen = set()
    # sort by depth first, then total mem
    for val in sorted(info.values(), key=lambda x: (-x['depth'], x['total_mem']), reverse=True):
        if id(val) in seen:
            continue
        stack = [('', iter([val]), 1.0e20)]
        while stack:
            indent, children, lastmem = stack[-1]
            try:
                val = next(children)
                if id(val) in seen:
                    continue
                seen.add(id(val))
                if val['total_mem'] > min_mem:
                    # if mem is more than the parent, highlight in red
                    if val['total_mem'] > lastmem:
                        fg = Fore.RED
                    else:
                        fg = Fore.GREEN
                    lastmem = val['total_mem']
                    cprint("%s%7.2f " % (indent, val['total_mem']), color=fg + Style.BRIGHT)
                    cprint(" (%d calls)  " % val['calls'])
                    cprint("%s\n" % val['qualname'])
                    stack.append((indent + '   |',
                                 iter(sorted(val['children'], key=lambda x: x['total_mem'],
                                             reverse=True)), lastmem))
            except StopIteration:
                stack.pop()

    cprint("\nMax mem usage: ")
    cprint("%7.2f MB\n" % maxmem, color=Fore.RED + Style.BRIGHT)