chore: better logging/error msg

This commit is contained in:
Maarten A. Breddels 2020-12-15 12:46:49 +00:00
parent d6796cec11
commit e13a3ada21

View File

@ -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):