From 8d38ba4b305580c1b5ed9199bde1c31ff0b3db68 Mon Sep 17 00:00:00 2001 From: Tony Thomas <01tonythomas@gmail.com> Date: Sun, 10 Jun 2018 20:08:10 +0200 Subject: [PATCH 1/2] fix(#34) Add support for logging unix Timestamp --- psrecord/main.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/psrecord/main.py b/psrecord/main.py index 9beccec..a17d116 100644 --- a/psrecord/main.py +++ b/psrecord/main.py @@ -90,6 +90,10 @@ def main(): 'in a slower maximum sampling rate).', action='store_true') + parser.add_argument('--use-unix-ts', + help='When used, time would be logged in unix timestamp', + action='store_true') + args = parser.parse_args() # Attach to process @@ -106,14 +110,15 @@ def main(): pid = sprocess.pid monitor(pid, logfile=args.log, plot=args.plot, duration=args.duration, - interval=args.interval, include_children=args.include_children) + interval=args.interval, include_children=args.include_children, + use_unix_ts=args.use_unix_ts) if sprocess is not None: sprocess.kill() def monitor(pid, logfile=None, plot=None, duration=None, interval=None, - include_children=False): + include_children=False, use_unix_ts=False): pr = psutil.Process(pid) @@ -142,6 +147,8 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None, # Find current time current_time = time.time() + logged_time = (current_time - start_time) if not use_unix_ts \ + else current_time try: pr_status = pr.status() @@ -153,7 +160,7 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None, # Check if process status indicates we should exit if pr_status in [psutil.STATUS_ZOMBIE, psutil.STATUS_DEAD]: print("Process finished ({0:.2f} seconds)" - .format(current_time - start_time)) + .format(logged_time)) break # Check if we have reached the maximum time @@ -182,7 +189,7 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None, if logfile: f.write("{0:12.3f} {1:12.3f} {2:12.3f} {3:12.3f}\n".format( - current_time - start_time, + logged_time, current_cpu, current_mem_real, current_mem_virtual)) @@ -193,7 +200,7 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None, # If plotting, record the values if plot: - log['times'].append(current_time - start_time) + log['times'].append(logged_time) log['cpu'].append(current_cpu) log['mem_real'].append(current_mem_real) log['mem_virtual'].append(current_mem_virtual) From 5bb4dab9926f039a9b8bf57aec751ee4a2736e22 Mon Sep 17 00:00:00 2001 From: Geoff Von Allmen Date: Thu, 18 Apr 2019 12:22:42 -0400 Subject: [PATCH 2/2] Remove add-unix-ts flag and just include it - might as well just include it by default. No harm and more usful for synchronization --- psrecord/main.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/psrecord/main.py b/psrecord/main.py index a17d116..8843c50 100644 --- a/psrecord/main.py +++ b/psrecord/main.py @@ -90,10 +90,6 @@ def main(): 'in a slower maximum sampling rate).', action='store_true') - parser.add_argument('--use-unix-ts', - help='When used, time would be logged in unix timestamp', - action='store_true') - args = parser.parse_args() # Attach to process @@ -110,15 +106,14 @@ def main(): pid = sprocess.pid monitor(pid, logfile=args.log, plot=args.plot, duration=args.duration, - interval=args.interval, include_children=args.include_children, - use_unix_ts=args.use_unix_ts) + interval=args.interval, include_children=args.include_children) if sprocess is not None: sprocess.kill() def monitor(pid, logfile=None, plot=None, duration=None, interval=None, - include_children=False, use_unix_ts=False): + include_children=False): pr = psutil.Process(pid) @@ -127,14 +122,16 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None, if logfile: f = open(logfile, 'w') - f.write("# {0:12s} {1:12s} {2:12s} {3:12s}\n".format( - 'Elapsed time'.center(12), + f.write("{0:12s} {1:12s} {2:12s} {3:12s} {4:12s}\n".format( + 'Current Time'.center(12), + 'Elapsed Time'.center(12), 'CPU (%)'.center(12), 'Real (MB)'.center(12), 'Virtual (MB)'.center(12)) ) log = {} + log['current_time'] = [] log['times'] = [] log['cpu'] = [] log['mem_real'] = [] @@ -147,8 +144,7 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None, # Find current time current_time = time.time() - logged_time = (current_time - start_time) if not use_unix_ts \ - else current_time + logged_time = (current_time - start_time) try: pr_status = pr.status() @@ -188,7 +184,8 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None, current_mem_virtual += current_mem.vms / 1024. ** 2 if logfile: - f.write("{0:12.3f} {1:12.3f} {2:12.3f} {3:12.3f}\n".format( + f.write("{0:12.3f} {1:12.3f} {2:12.3f} {3:12.3f} {4:12.3f}\n".format( + current_time, logged_time, current_cpu, current_mem_real, @@ -200,6 +197,7 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None, # If plotting, record the values if plot: + log['current_time'].append(current_time) log['times'].append(logged_time) log['cpu'].append(current_cpu) log['mem_real'].append(current_mem_real)