Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#34) Add support for logging unix Timestamp #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 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',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it would be a little more verbose, I think I'd prefer --include-timestamp and make it so that it simply adds a column (e.g. at the start) rather than replace the relative time.

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,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()
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand Down