Skip to content

Commit

Permalink
fix(astrofrog#34) Add support for logging unix Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
tonythomas01 committed Jun 10, 2018
1 parent be9581f commit ba1e5f8
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions psrecord/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -142,7 +147,8 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None,

# Find current time
current_time = time.time()

logged_time = current_time if not use_unix_ts else \
(current_time - start_time)
try:
pr_status = pr.status()
except TypeError: # psutil < 2.0
Expand All @@ -153,7 +159,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
Expand Down Expand Up @@ -182,7 +188,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))
Expand All @@ -193,7 +199,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)
Expand Down

0 comments on commit ba1e5f8

Please sign in to comment.