()
| 191 | run_action() |
| 192 | |
| 193 | def run_action(): |
| 194 | import time, subprocess |
| 195 | parser = ArgumentParser(usage="mprof run [options] program", formatter_class=RawTextHelpFormatter) |
| 196 | parser.add_argument('--version', action='version', version=mp.__version__) |
| 197 | parser.add_argument("--python", dest="python", action="store_true", |
| 198 | help="""Activates extra features when the profiling executable is a Python program (currently: function timestamping.)""") |
| 199 | parser.add_argument("--nopython", dest="nopython", action="store_true", |
| 200 | help="""Disables extra features when the profiled executable is a Python program (currently: function timestamping.)""") |
| 201 | parser.add_argument("--interval", "-T", dest="interval", default="0.1", type=float, action="store", |
| 202 | help="Sampling period (in seconds), defaults to 0.1") |
| 203 | parser.add_argument("--include-children", "-C", dest="include_children", action="store_true", |
| 204 | help="""Monitors forked processes as well (sum up all process memory)""") |
| 205 | parser.add_argument("--multiprocess", "-M", dest="multiprocess", action="store_true", |
| 206 | help="""Monitors forked processes creating individual plots for each child (disables --python features)""") |
| 207 | parser.add_argument("--exit-code", "-E", dest="exit_code", action="store_true", help="""Propagate the exit code""") |
| 208 | attach_arg = parser.add_argument("--attach", "-a", dest="attach_existing", action="store_true", |
| 209 | help="Attach to an existing process, by process name or by pid") |
| 210 | parser.add_argument("--timeout", "-t", dest="timeout", action="store", type=int, |
| 211 | help="timeout in seconds for the profiling, default new process has no timeout, attach existing is 1 hour") |
| 212 | parser.add_argument("--output", "-o", dest="filename", |
| 213 | default="mprofile_%s.dat" % time.strftime("%Y%m%d%H%M%S", time.localtime()), |
| 214 | help="""File to store results in, defaults to 'mprofile_<YYYYMMDDhhmmss>.dat' in the current directory, |
| 215 | (where <YYYYMMDDhhmmss> is the date-time of the program start). |
| 216 | This file contains the process memory consumption, in Mb (one value per line).""") |
| 217 | parser.add_argument("--backend", dest="backend", choices=["psutil", "psutil_pss", "psutil_uss", "posix", "tracemalloc"], |
| 218 | default="psutil", |
| 219 | help="Current supported backends: 'psutil', 'psutil_pss', 'psutil_uss', 'posix', 'tracemalloc'. Defaults to 'psutil'.") |
| 220 | parser.add_argument("program", nargs=REMAINDER, |
| 221 | help='Option 1: "<EXECUTABLE> <ARG1> <ARG2>..." - profile executable\n' |
| 222 | 'Option 2: "<PYTHON_SCRIPT> <ARG1> <ARG2>..." - profile python script\n' |
| 223 | 'Option 3: (--python flag present) "<PYTHON_EXECUTABLE> <PYTHON_SCRIPT> <ARG1> <ARG2>..." - profile python script with specified interpreter\n' |
| 224 | 'Option 4: (--python flag present) "<PYTHON_MODULE> <ARG1> <ARG2>..." - profile python module\n' |
| 225 | ) |
| 226 | args = parser.parse_args() |
| 227 | |
| 228 | if len(args.program) == 0: |
| 229 | print("A program to run must be provided. Use -h for help") |
| 230 | sys.exit(1) |
| 231 | |
| 232 | print("{1}: Sampling memory every {0}s".format( |
| 233 | args.interval, osp.basename(sys.argv[0]))) |
| 234 | |
| 235 | mprofile_output = args.filename |
| 236 | |
| 237 | program = args.program |
| 238 | if args.attach_existing: |
| 239 | print('attaching to existing process, using hint: {}'.format(program[0])) |
| 240 | if program[0].isdigit(): |
| 241 | p = literal_eval(program[0]) |
| 242 | cmd_line = get_cmd_line(program) |
| 243 | else: |
| 244 | proc = find_first_process(program[0]) |
| 245 | if proc is None: |
| 246 | raise ArgumentError(attach_arg, '\nWhen attaching, program should be process name or pid.\nFailed to find a process using hint: {}'.format(program[0])) |
| 247 | |
| 248 | p = proc.pid |
| 249 | try: |
| 250 | cmd_line = proc.cmdline() |
no test coverage detected