def test_enable(self):
        import _jitlog, struct
        tmpfile = open(self.tmpfilename, 'wb')
        fileno = tmpfile.fileno()
        _jitlog.enable(fileno)
        _jitlog.disable()
        # no need to clsoe tmpfile, it is done by jitlog

        with open(self.tmpfilename, 'rb') as fd:
            assert fd.read(1) == self.mark_header
            assert fd.read(2) == self.version
            assert bool(ord(fd.read(1))) == self.is_32bit
            strcount, = struct.unpack('<i', fd.read(4))
            machine = fd.read(strcount)
            assert machine == self.machine
            # resoperations
            count, = struct.unpack('<h', fd.read(2))
            opnames = set()
            for i in range(count):
                opnum = struct.unpack('<h', fd.read(2))
                strcount, = struct.unpack('<i', fd.read(4))
                opname = fd.read(strcount)
                opnames.append((opnum, opname))

            for opnum, opname in opnames:
                # must be known resoperation
                assert opnum in self.resops
                # the name must equal
                assert self.resops[opnum] == opname
Example #2
0
def main():
    parser = build_argparser()
    args = parser.parse_args(sys.argv[1:])
    web = args.web

    if args.input:
        assert args.query is not None, "Using -i requires you to specify -q"
        forest = parse_jitlog(args.input)
        q = query.new_unsafe_query(args.query)
        objs = q(forest)
        pretty_printer.write(sys.stdout, objs)
        sys.exit(0)

    if args.upload:
        # parse_jitlog will append source code to the binary
        forest = parse_jitlog(args.program)
        if forest.exception_raised():
            print("ERROR:", forest.exception_raised())
            sys.exit(1)
        if forest.extract_source_code_lines():
            # only copy the tags if the jitlog has no source code yet!
            forest.copy_and_add_source_code_tags()
        jitlog_upload(forest.filepath, get_url(args.web_url, "api/jitlog//"))
        sys.exit(0)

    if not _jitlog:
        if '__pypy__' in sys.builtin_module_names:
            sys.stderr.write(
                "No _jitlog module. This PyPy version is too old!\n")
        else:
            sys.stderr.write(
                "No _jitlog module. Use PyPy instead of CPython!\n")

    if not web:
        prof_file = args.output
    else:
        prof_file = tempfile.NamedTemporaryFile(delete=False)
    prof_name = prof_file.name

    fd = os.open(prof_name, os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
    _jitlog.enable(fd)

    try:
        sys.argv = [args.program] + args.args
        sys.path.insert(0, os.path.dirname(args.program))
        runpy.run_path(args.program, run_name='__main__')
    except BaseException as e:
        if not isinstance(e, (KeyboardInterrupt, SystemExit)):
            raise
    # not need to close fd, will be here
    _jitlog.disable()

    if web:
        forest = parse_jitlog(prof_name)
        if forest.extract_source_code_lines():
            # only copy the tags if the jitlog has no source code yet!
            forest.copy_and_add_source_code_tags()
        jitlog_upload(forest.filepath, get_url(args.web_url, "api/jitlog//"))
        forest.unlink_jitlog()  # free space!
Example #3
0
def main():
    parser = build_argparser()
    args = parser.parse_args(sys.argv[1:])
    web = args.web

    if args.input:
        assert args.query is not None, "Using -i requires you to specify -q"
        forest = parse_jitlog(args.input)
        q = query.new_unsafe_query(args.query)
        objs = q(forest)
        pretty_printer.write(sys.stdout, objs)
        sys.exit(0)

    if args.upload:
        # parse_jitlog will append source code to the binary
        forest = parse_jitlog(args.program)
        if forest.exception_raised():
            print("ERROR:", forest.exception_raised())
            sys.exit(1)
        if forest.extract_source_code_lines():
            # only copy the tags if the jitlog has no source code yet!
            forest.copy_and_add_source_code_tags()
        jitlog_upload(forest.filepath, get_url(args.web_url, "api/jitlog//"))
        sys.exit(0)

    if not _jitlog:
        if '__pypy__' in sys.builtin_module_names:
            sys.stderr.write("No _jitlog module. This PyPy version is too old!\n")
        else:
            sys.stderr.write("No _jitlog module. Use PyPy instead of CPython!\n")

    if not web:
        prof_file = args.output
    else:
        prof_file = tempfile.NamedTemporaryFile(delete=False)
    prof_name = prof_file.name


    fd = os.open(prof_name, os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
    _jitlog.enable(fd)

    try:
        sys.argv = [args.program] + args.args
        sys.path.insert(0, os.path.dirname(args.program))
        runpy.run_path(args.program, run_name='__main__')
    except BaseException as e:
        if not isinstance(e, (KeyboardInterrupt, SystemExit)):
            raise
    # not need to close fd, will be here
    _jitlog.disable()

    if web:
        forest = parse_jitlog(prof_name)
        if forest.extract_source_code_lines():
            # only copy the tags if the jitlog has no source code yet!
            forest.copy_and_add_source_code_tags()
        jitlog_upload(forest.filepath, get_url(args.web_url, "api/jitlog//"))
        forest.unlink_jitlog() # free space!
Example #4
0
    def start(self):
        if self.output_mode == OUTPUT_FILE:
            self.prof_file = open(self.prof_name, "w+b")
        else:
            self.prof_file = tempfile.NamedTemporaryFile(delete=False)
            self.prof_name = self.prof_file.name

        if self.jitlog and _jitlog:
            self.jitlog_fd = os.open(self.prof_file + ".jitlog",
                                     os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
            _jitlog.enable(self.jitlog_fd)
        vmprof.enable(self.prof_file.fileno(), self.period, self.mem,
                      self.lines)
Example #5
0
    def __init__(self,
                 app,
                 period=0.001,
                 web_url='http://vmprof.com',
                 web_auth=None,
                 mem=False,
                 lines=False,
                 jitlog=False,
                 web=None,
                 output=None):
        """
        Parameter details are here:
            https://github.com/vmprof/vmprof-python/blob/master/vmprof/cli.py#L7-L72

        :param app: Your WSGI application object.
        :param float period: Sampling period (in microseconds)
        :param str web_auth: Authtoken for your acount on the server, works only when --web is used
        :param str web_url: Provide URL instead of the default vmprof.com)
        :param bool mem: Do memory profiling as well
        :param bool lines: Store lines execution stats
        :param bool jitlog: Upload the jitlog to remote server (defaults to vmprof.com)
        :param bool web: Upload profiling stats to a remote server (defaults to vmprof.com)
        :param output: Save profiling data to file
        """
        self.app = app
        self.web_url = web_url
        self.web_auth = web_auth
        self.period = period
        self.mem = mem
        self.lines = lines

        if web:
            self.output_mode = OUTPUT_WEB
        elif output:
            self.output_mode = OUTPUT_FILE
        else:
            self.output_mode = OUTPUT_CLI

        if self.output_mode == OUTPUT_FILE:
            self.prof_name = output
            self.prof_file = open(self.prof_name, 'w+b')
        else:
            self.prof_file = tempfile.NamedTemporaryFile(delete=False)
            self.prof_name = self.prof_file.name

        if jitlog and _jitlog:
            fd = os.open(self.prof_file + '.jitlog',
                         os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
            _jitlog.enable(fd)
Example #6
0
def main():
    args = vmprof.cli.parse_args(sys.argv[1:])

    # None means default on this platform
    native = None
    if args.no_native:
        native = False
    if args.web:
        output_mode = OUTPUT_WEB
    elif args.output:
        output_mode = OUTPUT_FILE
    else:
        output_mode = OUTPUT_CLI

    if output_mode == OUTPUT_FILE:
        prof_file = args.output
        prof_name = prof_file.name
    else:
        prof_file = tempfile.NamedTemporaryFile(delete=False)
        prof_name = prof_file.name

    vmprof.enable(prof_file.fileno(),
                  args.period,
                  args.mem,
                  args.lines,
                  native=native)
    if args.jitlog and _jitlog:
        fd = os.open(prof_name + '.jit', os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
        _jitlog.enable(fd)
    # invoke the user program:
    try:
        sys.argv = [args.program] + args.args
        sys.path.insert(0, os.path.dirname(args.program))
        runpy.run_path(args.program, run_name='__main__')
    except BaseException as e:
        if not isinstance(e, (KeyboardInterrupt, SystemExit)):
            raise
    #
    vmprof.disable()
    if args.jitlog and _jitlog:
        _jitlog.disable()

    prof_file.close()
    show_stats(prof_name, output_mode, args)
    if output_mode != OUTPUT_FILE:
        os.unlink(prof_name)
Example #7
0
def main():
    args = vmprof.cli.parse_args(sys.argv[1:])

    # None means default on this platform
    native = None
    if args.no_native:
        native = False
    if args.web:
        output_mode = OUTPUT_WEB
    elif args.output:
        output_mode = OUTPUT_FILE
    else:
        output_mode = OUTPUT_CLI

    if output_mode == OUTPUT_FILE:
        prof_file = args.output
        prof_name = prof_file.name
    else:
        prof_file = tempfile.NamedTemporaryFile(delete=False)
        prof_name = prof_file.name


    vmprof.enable(prof_file.fileno(), args.period, args.mem,
                  args.lines, native=native)
    if args.jitlog and _jitlog:
        fd = os.open(prof_name + '.jit', os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
        _jitlog.enable(fd)
    # invoke the user program:
    try:
        sys.argv = [args.program] + args.args
        sys.path.insert(0, os.path.dirname(args.program))
        runpy.run_path(args.program, run_name='__main__')
    except BaseException as e:
        if not isinstance(e, (KeyboardInterrupt, SystemExit)):
            raise
    #
    vmprof.disable()
    if args.jitlog and _jitlog:
        _jitlog.disable()

    prof_file.close()
    show_stats(prof_name, output_mode, args)
    if output_mode != OUTPUT_FILE:
        os.unlink(prof_name)
Example #8
0
def main():
    parser = build_argparser()
    args = parser.parse_args(sys.argv[1:])
    web = args.web

    if args.query is not None:
        from jitlog import prettyprinter as pp
        sys.stderr.write("Parsing jitlog...")
        sys.stderr.flush()
        forest = parse_jitlog(args.program)
        sys.stderr.write("done\n")
        query_str = args.query or "traces()"
        q = query.new_unsafe_query(query_str)
        objs = q(forest)
        color = True
        pp_clazz = pp.ColoredPrettyPrinter if color else pp.PrettyPrinter
        with pp_clazz() as ppinst:
            for trace in objs:
                ppinst.trace(sys.stdout, trace)
        if args.query is None:
            sys.stderr.write("-" * 10 + '\n')
            sys.stderr.write("Display the jitlog with an empty query (defaults to -q 'traces()'). "
                    "Add -q 'your query' if you want to narrow down the output\n")
        sys.exit(0)

    if args.upload:
        host, auth = args.web_url, args.web_auth
        service = Service(host, auth)
        service.post({ Service.FILE_JIT_PROFILE: args.program })
        sys.exit(0)

    if not _jitlog:
        if '__pypy__' in sys.builtin_module_names:
            sys.stderr.write("No _jitlog module. This PyPy version is too old!\n")
        else:
            sys.stderr.write("No _jitlog module. Use PyPy instead of CPython!\n")

    if not web:
        prof_file = args.output
    else:
        prof_file = tempfile.NamedTemporaryFile(delete=False)
    prof_name = prof_file.name


    fd = os.open(prof_name, os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
    _jitlog.enable(fd)

    try:
        sys.argv = [args.program] + args.args
        sys.path.insert(0, os.path.dirname(args.program))
        runpy.run_path(args.program, run_name='__main__')
    except BaseException as e:
        if not isinstance(e, (KeyboardInterrupt, SystemExit)):
            raise
    # not need to close fd, will be here
    _jitlog.disable()

    if web:
        forest = parse_jitlog(prof_name)
        if forest.extract_source_code_lines():
            # only copy the tags if the jitlog has no source code yet!
            forest.copy_and_add_source_code_tags()
        host, auth = args.web_url, args.web_auth
        service = Service(host, auth)
        service.post({ Service.FILE_JIT_PROFILE: forest.filepath })
        forest.unlink_jitlog() # free space!