Spaces:
Running
Running
| # USAGE: python run_psrecord.py <PID> --plot plot.png --log activity.txt | |
| from psrecord.main import monitor | |
| import argparse | |
| def main(): | |
| # copied from C:\Users\user\anaconda3\envs\tfod\Lib\site-packages\psrecord\main.py | |
| parser = argparse.ArgumentParser( | |
| description='Record CPU and memory usage for a process') | |
| parser.add_argument('process_id_or_command', type=str, | |
| help='the process id or command') | |
| parser.add_argument('--log', type=str, | |
| help='output the statistics to a file') | |
| parser.add_argument('--plot', type=str, | |
| help='output the statistics to a plot') | |
| parser.add_argument('--duration', type=float, | |
| help='how long to record for (in seconds). If not ' | |
| 'specified, the recording is continuous until ' | |
| 'the job exits.') | |
| parser.add_argument('--interval', type=float, | |
| help='how long to wait between each sample (in ' | |
| 'seconds). By default the process is sampled ' | |
| 'as often as possible.') | |
| parser.add_argument('--include-children', | |
| help='include sub-processes in statistics (results ' | |
| 'in a slower maximum sampling rate).', | |
| action='store_true') | |
| args = parser.parse_args() | |
| # Attach to process | |
| try: | |
| pid = int(args.process_id_or_command) | |
| print("Attaching to process {0}".format(pid)) | |
| sprocess = None | |
| except Exception: | |
| import subprocess | |
| command = args.process_id_or_command | |
| print("Starting up command '{0}' and attaching to process" | |
| .format(command)) | |
| sprocess = subprocess.Popen(command, shell=True) | |
| pid = sprocess.pid | |
| monitor(pid, logfile=args.log, plot=args.plot, duration=args.duration, | |
| interval=args.interval, include_children=args.include_children) | |
| if sprocess is not None: | |
| sprocess.kill() | |
| if __name__ == '__main__': | |
| main() |