Пример #1
0
def _iprof_py_file(options):
    """
    Run instance-based profiling on the given python script.

    Parameters
    ----------
    options : argparse Namespace
        Command line options.
    """
    if not func_group:
        _setup_func_group()

    progname = options.file[0]
    sys.path.insert(0, os.path.dirname(progname))

    with open(progname, 'rb') as fp:
        code = compile(fp.read(), progname, 'exec')

    globals_dict = {
        '__file__': progname,
        '__name__': '__main__',
        '__package__': None,
        '__cached__': None,
    }

    _setup(options, finalize=False)
    start()
    exec(code, globals_dict)
    _finalize_profile()
Пример #2
0
def setup(methods=None, finalize=True):
    """
    Instruments certain important openmdao methods for profiling.

    Parameters
    ----------

    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:

        .. code-block:: python

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

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

    """
    if not func_group:
        _setup_func_group()
    _setup(_Options(methods=methods), finalize=finalize)
Пример #3
0
def setup(methods=None, finalize=True):
    """
    Instruments certain important openmdao methods for profiling.

    Parameters
    ----------

    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:

        .. code-block:: python

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

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

    """
    if not func_group:
        _setup_func_group()
    _setup(_Options(methods=methods), finalize=finalize)
Пример #4
0
def _iprof_py_file(options, user_args):
    """
    Run instance-based profiling on the given python script.

    Parameters
    ----------
    options : argparse Namespace
        Command line options.
    user_args : list of str
        Command line options after '--' (if any).  Passed to user script.
    """
    if not func_group:
        _setup_func_group()

    progname = options.file[0]
    sys.path.insert(0, os.path.dirname(progname))

    # update sys.argv in case python script takes cmd line args
    sys.argv[:] = [progname] + user_args

    with open(progname, 'rb') as fp:
        code = compile(fp.read(), progname, 'exec')

    globals_dict = {
        '__file__': progname,
        '__name__': '__main__',
        '__package__': None,
        '__cached__': None,
    }

    _setup(options, finalize=False)
    start()
    exec(code, globals_dict)
    _finalize_profile()
Пример #5
0
def _iprof_py_file(options):
    """
    Run instance-based profiling on the given python script.

    Parameters
    ----------
    options : argparse Namespace
        Command line options.
    """
    if not func_group:
        _setup_func_group()

    progname = options.file[0]
    sys.path.insert(0, os.path.dirname(progname))

    with open(progname, 'rb') as fp:
        code = compile(fp.read(), progname, 'exec')

    globals_dict = {
        '__file__': progname,
        '__name__': '__main__',
        '__package__': None,
        '__cached__': None,
    }

    _setup(options, finalize=False)
    start()
    exec (code, globals_dict)
    _finalize_profile()
Пример #6
0
def _itrace_setup_parser(parser):
    """
    Set up the command line options for the 'openmdao trace' command line tool.
    """
    if not func_group:
        _setup_func_group()

    parser.add_argument('file', nargs=1, help='Python file to be traced.')
    parser.add_argument('-g', '--group', action='store', dest='methods',
                        default='openmdao',
                        help='Determines which group of methods will be traced. Default is "openmdao".'
                              ' Options are: %s' % sorted(func_group.keys()))
    parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
                        help="Show function locals and return values.")
    parser.add_argument('--ptrs', action='store_true', dest='show_ptrs',
                        help="Show addresses of printed objects.")
    parser.add_argument('-m', '--memory', action='store_true', dest='memory',
                        help="Show memory usage.")
    parser.add_argument('-l', '--leaks', action='store_true', dest='leaks',
                        help="Show objects that are not garbage collected after each function call.")
    parser.add_argument('-r', '--rank', action='store', dest='rank', type=int,
                        default=-1, help='MPI rank where output is desired.  Default is all ranks.')
    parser.add_argument('-o', '--outfile', action='store', dest='outfile',
                        default='stdout', help='Output file.  Defaults to stdout.')
    parser.add_argument('-f', '--filter', action='append', dest='filters',
                        default=[],
                        help='An expression.  If it evaluates to True for any matching trace '
                             'function, that function will be displayed in the trace. One '
                             'expression can be added for each class.')
Пример #7
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))
Пример #8
0
def _mem_prof_setup_parser(parser):
    if not func_group:
        _setup_func_group()

    parser.add_argument('-g', '--group', action='store', dest='methods',
                        default='openmdao_all',
                        help='Determines which group of methods will be tracked. Options are %s' %
                             sorted(func_group.keys()))
    parser.add_argument('file', metavar='file', nargs=1,
                        help='Python file to profile.')
Пример #9
0
def _itrace_setup_parser(parser):
    if not func_group:
        _setup_func_group()

    parser.add_argument('file', nargs=1, help='Python file to be traced.')
    parser.add_argument('-g', '--group', action='store', dest='methods',
                        default='openmdao',
                        help='Determines which group of methods will be traced. Default is "openmdao".'
                              ' Options are: %s' % sorted(func_group.keys()))
    parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
                        help="Show function locals and return values.")
Пример #10
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.
    """
    if not func_group:
        _setup_func_group()
    _setup(_Options(methods=methods))
Пример #11
0
def _itrace_setup_parser(parser):
    """
    Set up the command line options for the 'openmdao trace' command line tool.
    """
    if not func_group:
        _setup_func_group()

    parser.add_argument('file', nargs=1, help='Python file to be traced.')
    parser.add_argument(
        '-g',
        '--group',
        action='store',
        dest='methods',
        default='openmdao',
        help=
        'Determines which group of methods will be traced. Default is "openmdao".'
        ' Options are: %s' % sorted(func_group.keys()))
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        dest='verbose',
                        help="Show function locals and return values.")
    parser.add_argument('-m',
                        '--memory',
                        action='store_true',
                        dest='memory',
                        help="Show memory usage.")
    parser.add_argument(
        '-l',
        '--leaks',
        action='store_true',
        dest='leaks',
        help=
        "Show objects that are not garbage collected after each function call."
    )
    parser.add_argument(
        '-r',
        '--rank',
        action='store',
        dest='rank',
        type=int,
        default=-1,
        help='MPI rank where output is desired.  Default is all ranks.')
    parser.add_argument('-o',
                        '--outfile',
                        action='store',
                        dest='outfile',
                        default='stdout',
                        help='Output file.  Defaults to stdout.')
Пример #12
0
def _mem_prof_setup_parser(parser):
    if not func_group:
        _setup_func_group()

    parser.add_argument(
        '-g',
        '--group',
        action='store',
        dest='methods',
        default='openmdao_all',
        help='Determines which group of methods will be tracked. Options are %s'
        % sorted(func_group.keys()))
    parser.add_argument('file',
                        metavar='file',
                        nargs=1,
                        help='Python file to profile.')
Пример #13
0
def _iprof_setup_parser(parser):
    if not func_group:
        _setup_func_group()

    parser.add_argument('-p',
                        '--port',
                        action='store',
                        dest='port',
                        default=8009,
                        type=int,
                        help='port used for web server')
    parser.add_argument('--no_browser',
                        action='store_true',
                        dest='noshow',
                        help="Don't pop up a browser to view the data.")
    parser.add_argument('-t',
                        '--title',
                        action='store',
                        dest='title',
                        default='Profile of Method Calls by Instance',
                        help='Title to be displayed above profiling view.')
    parser.add_argument(
        '-g',
        '--group',
        action='store',
        dest='methods',
        default='openmdao',
        help='Determines which group of methods will be tracked. Current '
        'options are: %s and "openmdao" is the default' %
        sorted(func_group.keys()))
    parser.add_argument(
        '-m',
        '--maxcalls',
        action='store',
        dest='maxcalls',
        default=15000,
        type=int,
        help='Maximum number of calls displayed at one time.  Default=15000.')
    parser.add_argument('file',
                        metavar='file',
                        nargs='+',
                        help='Raw profile data files or a python file.')
Пример #14
0
def _iprof_setup_parser(parser):
    if not func_group:
        _setup_func_group()

    parser.add_argument('-p', '--port', action='store', dest='port',
                        default=8009, type=int,
                        help='port used for web server')
    parser.add_argument('--no_browser', action='store_true', dest='noshow',
                        help="Don't pop up a browser to view the data.")
    parser.add_argument('-t', '--title', action='store', dest='title',
                        default='Profile of Method Calls by Instance',
                        help='Title to be displayed above profiling view.')
    parser.add_argument('-g', '--group', action='store', dest='methods',
                        default='openmdao',
                        help='Determines which group of methods will be tracked. Current '
                             'options are: %s and "openmdao" is the default' %
                              sorted(func_group.keys()))
    parser.add_argument('-m', '--maxcalls', action='store', dest='maxcalls',
                        default=15000, type=int,
                        help='Maximum number of calls displayed at one time.  Default=15000.')
    parser.add_argument('file', metavar='file', nargs='+',
                        help='Raw profile data files or a python file.')
Пример #15
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))