From e13a3ada215cb732ead5ae7a31ff732eb19c78c9 Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Tue, 15 Dec 2020 12:46:49 +0000 Subject: [PATCH] chore: better logging/error msg --- per4m/giltracer.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/per4m/giltracer.py b/per4m/giltracer.py index 3ff6d9e..b8675e1 100644 --- a/per4m/giltracer.py +++ b/per4m/giltracer.py @@ -34,6 +34,8 @@ class GilTracer: def __enter__(self): pid = os.getpid() cmd = f"perf record -e 'sched:*' --call-graph dwarf -k CLOCK_MONOTONIC --pid {pid} -o {self.perf_output}" + if self.verbose >= 2: + print(f"Running: {cmd}") args = shlex.split(cmd) self.perf = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) start_time = time.time() @@ -45,6 +47,7 @@ class GilTracer: # we need to wait till perf creates the file time.sleep(0.1) else: + self.finish() raise OSError(f'perf did not create {self.perf_output}') start_size = os.path.getsize(self.perf_output) for _ in range(RETRIES): @@ -54,12 +57,13 @@ class GilTracer: # we need to wait till perf writes time.sleep(0.1) else: + self.finish() raise OSError(f'perf did not write to {self.perf_output}') # and give perf a bit more time time.sleep(0.05) return self - def __exit__(self, *args): + def finish(self): self.perf.terminate() outs, errs = self.perf.communicate(timeout=5) if self.verbose >= 1: @@ -70,7 +74,9 @@ class GilTracer: if self.perf.returncode not in [0, -signal.SIGTERM.value]: print(signal.SIGTERM) raise OSError(f'perf record fails, got exit code {self.perf.returncode}') - # import pdb; pdb.set_trace() + + def __exit__(self, *args): + self.finish() if self.verbose >= 1: print('Wait for perf to finish...') self.perf.wait() @@ -79,7 +85,7 @@ class GilTracer: verbose = '-q ' + '-v ' * self.verbose cmd = f"perf script -i {self.perf_output} --no-inline | per4m perf2trace -o {self.trace_output} {verbose}" if os.system(cmd) != 0: - raise OSError('Failed to run perf or per4m perf2trace') + raise OSError(f'Failed to run perf or per4m perf2trace, command:\n$ {cmd}') def main(argv=sys.argv):