|
| 1 | +import runpy |
| 2 | +import sys, os |
| 3 | +import tempfile |
| 4 | + |
| 5 | +try: |
| 6 | + import _jitlog |
| 7 | +except ImportError: |
| 8 | + _jitlog = None |
| 9 | + |
| 10 | +def upload_stats(stats, forest, args): |
| 11 | + import vmprof.upload |
| 12 | + name = args.program |
| 13 | + argv = " ".join(args.args) |
| 14 | + host = args.web_url |
| 15 | + auth = args.web_auth |
| 16 | + # |
| 17 | + sys.stderr.write("Compiling and uploading to %s...\n" % (args.web_url,)) |
| 18 | + vmprof.upload.upload(stats, name, argv, host, auth, forest) |
| 19 | + |
| 20 | +def build_argparser(): |
| 21 | + parser = argparse.ArgumentParser( |
| 22 | + description='Jitlog', |
| 23 | + prog="jitlog" |
| 24 | + ) |
| 25 | + parser.add_argument( |
| 26 | + 'program', |
| 27 | + help='program' |
| 28 | + ) |
| 29 | + parser.add_argument( |
| 30 | + 'args', |
| 31 | + nargs=argparse.REMAINDER, |
| 32 | + help='program arguments' |
| 33 | + ) |
| 34 | + |
| 35 | + parser.add_argument( |
| 36 | + '--web-auth', |
| 37 | + help='Authtoken for your acount on the server, works only when --web is used' |
| 38 | + ) |
| 39 | + |
| 40 | + parser.add_argument( |
| 41 | + '--web-url', |
| 42 | + metavar='url', |
| 43 | + default='http://vmprof.com', |
| 44 | + help='Provide URL instead of the default vmprof.com)' |
| 45 | + ) |
| 46 | + output_mode_args = parser.add_mutually_exclusive_group() |
| 47 | + output_mode_args.add_argument( |
| 48 | + '--web', |
| 49 | + action='store_true', |
| 50 | + help='Upload profiling stats to a remote server (defaults to vmprof.com)' |
| 51 | + ) |
| 52 | + output_mode_args.add_argument( |
| 53 | + '--output', '-o', |
| 54 | + metavar='file.prof', |
| 55 | + type=argparse.FileType('w+b'), |
| 56 | + help='Save profiling data to file' |
| 57 | + ) |
| 58 | + |
| 59 | + return parser |
| 60 | + |
| 61 | +def main(): |
| 62 | + parser = build_argparser() |
| 63 | + args = parser.parse_args(sys.argv[1:]) |
| 64 | + |
| 65 | + if args.web: |
| 66 | + output_mode = OUTPUT_WEB |
| 67 | + elif args.output: |
| 68 | + output_mode = OUTPUT_FILE |
| 69 | + |
| 70 | + if output_mode == OUTPUT_FILE: |
| 71 | + prof_file = args.output |
| 72 | + prof_name = prof_file.name |
| 73 | + else: |
| 74 | + prof_file = tempfile.NamedTemporaryFile(delete=False) |
| 75 | + prof_name = prof_file.name |
| 76 | + |
| 77 | + |
| 78 | + vmprof.enable(prof_file.fileno(), args.period, args.mem, args.lines) |
| 79 | + fd = os.open(prof_name, os.O_WRONLY | os.O_TRUNC | os.O_CREAT) |
| 80 | + _jitlog.enable(fd) |
| 81 | + # not need to close fd, will be closed in _jitlog.disable() |
| 82 | + |
| 83 | + try: |
| 84 | + sys.argv = [args.program] + args.args |
| 85 | + sys.path.insert(0, os.path.dirname(args.program)) |
| 86 | + runpy.run_path(args.program, run_name='__main__') |
| 87 | + except BaseException as e: |
| 88 | + if not isinstance(e, (KeyboardInterrupt, SystemExit)): |
| 89 | + raise |
| 90 | + _jitlog.disable() |
| 91 | + if output_mode == OUTPUT_WEB: |
| 92 | + upload(prof_name, output_mode, args) |
| 93 | + if output_mode != OUTPUT_FILE: |
| 94 | + os.unlink(prof_name) |
| 95 | + |
| 96 | +main() |
0 commit comments