Skip to content

Commit

Permalink
Merge "[Python API] Improve TraceProcessor error reporting" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Khokhlov authored and Gerrit Code Review committed Oct 14, 2024
2 parents 372001e + 5056b35 commit 84c1b09
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
19 changes: 12 additions & 7 deletions python/perfetto/trace_processor/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import subprocess
import sys
import tempfile
import time
from typing import List, Optional
from urllib import request, error
Expand Down Expand Up @@ -55,11 +56,13 @@ def load_shell(bin_path: str,
if extra_flags:
args.extend(extra_flags)

temp_stdout = tempfile.TemporaryFile()
temp_stderr = tempfile.TemporaryFile()
p = subprocess.Popen(
tp_exec + args,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=None if verbose else subprocess.DEVNULL)
stdout=temp_stdout,
stderr=None if verbose else temp_stderr)

success = False
for _ in range(load_timeout + 1):
Expand All @@ -72,10 +75,12 @@ def load_shell(bin_path: str,
time.sleep(1)

if not success:
raise PerfettoException(
"Trace processor failed to start. Try rerunning with "
"verbose=True in TraceProcessorConfig for more detailed "
"information and file a bug at https://goto.google.com/perfetto-bug "
"or https://github.com/google/perfetto/issues if necessary.")
p.kill()
temp_stdout.seek(0)
stdout = temp_stdout.read().decode("utf-8")
temp_stderr.seek(0)
stderr = temp_stderr.read().decode("utf-8")
raise PerfettoException("Trace processor failed to start.\n"
f"stdout: {stdout}\nstderr: {stderr}\n")

return url, p
11 changes: 11 additions & 0 deletions python/test/api_integrationtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ def test_invalid_trace(self):
with self.assertRaises(TraceProcessorException):
_ = create_tp(trace=f)

def test_runtime_error(self):
# We emulate a situation when TP returns an error by passing the --version
# flag. This makes TP output version information and exit, instead of
# starting an http server.
config = TraceProcessorConfig(
bin_path=os.environ["SHELL_PATH"], extra_flags=["--version"])
with self.assertRaisesRegex(
TraceProcessorException,
expected_regex='.*Trace Processor RPC API version:.*'):
TraceProcessor(trace=io.BytesIO(b''), config=config)

def test_trace_path(self):
# Get path to trace_processor_shell and construct TraceProcessor
tp = create_tp(trace=example_android_trace_path())
Expand Down

0 comments on commit 84c1b09

Please sign in to comment.