1 year ago

#337105

test-img

David Owens

How to kill perf record with python subprocess?

I am attempting to use the perf utility to monitor my system.

It will be started and killed within a python script. I have created a sandbox like so:

extra_params = ["-F", "99", "-g", "-a", "--"]
record_args = ["sudo", "perf", "record", "-o", "/path/perf.data", *extra_params]
print(f"Starting perf w/ args: {record_args}")
proc = subprocess.Popen(
    args=record_args,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
)
print(proc.pid)
time.sleep(2)
subprocess.check_output(["sudo", "kill", "-2", str(proc.pid)])

This fails to properly kill the process resulting in perf not correctly flushing its data. I do not get the Captured and wrote success message. Eventually my Flamegraph states:

Unrecognized line: Was the 'perf record' command properly terminated? at stackcollapse-perf.pl line 424, <> line 

If I open another terminal and run the exact same sudo kill -2 $pid command, perf exits correctly and the downstream FlameGraph executables work correctly.

I have referenced the following resources, all which fail to fix the issue:

Thank you!

python

performance

monitoring

perf

flamegraph

0 Answers

Your Answer

Accepted video resources