astrofrog / psrecord

Record the CPU and memory activity of a process :chart_with_upwards_trend:
BSD 2-Clause "Simplified" License
595 stars 77 forks source link

How to run on windows? #66

Open jfseneca opened 2 years ago

jfseneca commented 2 years ago

Installed PSrecord successfully, but can't invoke it from command prompt. Was using it in this manner on Linux, would like the same functionality for Windows:

[[Some Linux Command]] & psrecord $! --interval 0.1 --plot plot1.png

Thanks,

ansonnn07 commented 2 years ago

I just copied the main function out of the library, then run it as a python file. For example, for a script named "run_psrecord.py" as below, just run the command python run_psrecord.py <PID> --plot plot.png --log activity.txt to start it.

# 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()
SolomidHero commented 2 years ago

python module use is not possible too

(base) PS C:\Users\Administrator\models> python -m psrecord
C:\Users\Administrator\miniconda3\python.exe: No module named psrecord.__main__; 'psreco
rd' is a package and cannot be directly executed
BramVanroy commented 1 year ago

I also can't get it to run on Windows. The example commands just immediately exit and a system pop-up appears asking me how to open the file. When I open it, it is actually a shell script.

#!.venv\Scripts\python.exe

import sys
import psrecord

if __name__ == '__main__':
    sys.exit(psrecord.main())