chore: better logging/error msg
This commit is contained in:
parent
d6796cec11
commit
e13a3ada21
@ -34,6 +34,8 @@ class GilTracer:
|
|||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
pid = os.getpid()
|
pid = os.getpid()
|
||||||
cmd = f"perf record -e 'sched:*' --call-graph dwarf -k CLOCK_MONOTONIC --pid {pid} -o {self.perf_output}"
|
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)
|
args = shlex.split(cmd)
|
||||||
self.perf = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
self.perf = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
@ -45,6 +47,7 @@ class GilTracer:
|
|||||||
# we need to wait till perf creates the file
|
# we need to wait till perf creates the file
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
else:
|
else:
|
||||||
|
self.finish()
|
||||||
raise OSError(f'perf did not create {self.perf_output}')
|
raise OSError(f'perf did not create {self.perf_output}')
|
||||||
start_size = os.path.getsize(self.perf_output)
|
start_size = os.path.getsize(self.perf_output)
|
||||||
for _ in range(RETRIES):
|
for _ in range(RETRIES):
|
||||||
@ -54,12 +57,13 @@ class GilTracer:
|
|||||||
# we need to wait till perf writes
|
# we need to wait till perf writes
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
else:
|
else:
|
||||||
|
self.finish()
|
||||||
raise OSError(f'perf did not write to {self.perf_output}')
|
raise OSError(f'perf did not write to {self.perf_output}')
|
||||||
# and give perf a bit more time
|
# and give perf a bit more time
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def finish(self):
|
||||||
self.perf.terminate()
|
self.perf.terminate()
|
||||||
outs, errs = self.perf.communicate(timeout=5)
|
outs, errs = self.perf.communicate(timeout=5)
|
||||||
if self.verbose >= 1:
|
if self.verbose >= 1:
|
||||||
@ -70,7 +74,9 @@ class GilTracer:
|
|||||||
if self.perf.returncode not in [0, -signal.SIGTERM.value]:
|
if self.perf.returncode not in [0, -signal.SIGTERM.value]:
|
||||||
print(signal.SIGTERM)
|
print(signal.SIGTERM)
|
||||||
raise OSError(f'perf record fails, got exit code {self.perf.returncode}')
|
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:
|
if self.verbose >= 1:
|
||||||
print('Wait for perf to finish...')
|
print('Wait for perf to finish...')
|
||||||
self.perf.wait()
|
self.perf.wait()
|
||||||
@ -79,7 +85,7 @@ class GilTracer:
|
|||||||
verbose = '-q ' + '-v ' * self.verbose
|
verbose = '-q ' + '-v ' * self.verbose
|
||||||
cmd = f"perf script -i {self.perf_output} --no-inline | per4m perf2trace -o {self.trace_output} {verbose}"
|
cmd = f"perf script -i {self.perf_output} --no-inline | per4m perf2trace -o {self.trace_output} {verbose}"
|
||||||
if os.system(cmd) != 0:
|
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):
|
def main(argv=sys.argv):
|
||||||
|
Loading…
Reference in New Issue
Block a user