Skip to content

Commit cfc6997

Browse files
committed
Add flag to open trace in perfetto
1 parent a7017bf commit cfc6997

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

preciceprofiling/perfetto.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from http.server import BaseHTTPRequestHandler
2+
from socketserver import TCPServer
3+
from webbrowser import open_new_tab
4+
import pathlib
5+
6+
7+
def open_in_perfetto(tracefile: pathlib.Path):
8+
print(f"Opening {str(tracefile)} on ui.perfetto.dev")
9+
content = tracefile.read_text()
10+
11+
PORT = 9001
12+
ORIGIN = "https://ui.perfetto.dev"
13+
14+
class TraceHandler(BaseHTTPRequestHandler):
15+
def do_GET(self):
16+
if self.path != "/trace.json":
17+
self.send_error(405)
18+
19+
self.server.trace_served = True
20+
self.send_response(200)
21+
self.send_header("Access-Control-Allow-Origin", ORIGIN)
22+
self.send_header("Cache-Control", "no-cache")
23+
self.send_header("Content-type", "text/json")
24+
self.send_header("Content-length", str(len(content)))
25+
self.end_headers()
26+
self.wfile.write(content.encode())
27+
28+
def do_POST(self):
29+
self.send_error(405)
30+
31+
def log_message(self, format, *args):
32+
pass
33+
34+
# We reuse the HTTP+RPC port because it's the only one allowed by the CSP.
35+
36+
TCPServer.allow_reuse_address = True
37+
with TCPServer(("127.0.0.1", PORT), TraceHandler) as httpd:
38+
address = f"{ORIGIN}/#!/?url=http://127.0.0.1:{PORT}/trace.json"
39+
open_new_tab(address)
40+
41+
httpd.trace_served = False
42+
httpd.allow_origin = ORIGIN
43+
while not httpd.trace_served:
44+
httpd.handle_request()

preciceprofiling/trace.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import orjson
33
import argparse
44
import sys
5+
import pathlib
6+
from preciceprofiling.perfetto import open_in_perfetto
57

68

79
def makeTraceParser(add_help: bool = True):
@@ -15,7 +17,17 @@ def makeTraceParser(add_help: bool = True):
1517
help="The profiling file to process",
1618
)
1719
trace.add_argument(
18-
"-o", "--output", default="trace.json", help="The resulting trace file"
20+
"-o",
21+
"--output",
22+
default="trace.json",
23+
type=pathlib.Path,
24+
help="The resulting trace file",
25+
)
26+
trace.add_argument(
27+
"-w",
28+
"--web",
29+
action="store_true",
30+
help="Open resulting trace in ui.perfetto.dev",
1931
)
2032
trace.add_argument(
2133
"-l", "--limit", type=int, metavar="n", help="Select the first n ranks"
@@ -27,10 +39,10 @@ def makeTraceParser(add_help: bool = True):
2739

2840

2941
def runTrace(ns):
30-
return traceCommand(ns.profilingfile, ns.output, ns.rank, ns.limit)
42+
return traceCommand(ns.profilingfile, ns.output, ns.rank, ns.limit, ns.web)
3143

3244

33-
def traceCommand(profilingfile, outfile, rankfilter, limit):
45+
def traceCommand(profilingfile, outfile, rankfilter, limit, web):
3446
run = Run(profilingfile)
3547
selection = (
3648
set()
@@ -39,8 +51,10 @@ def traceCommand(profilingfile, outfile, rankfilter, limit):
3951
)
4052
traces = run.toTrace(selection)
4153
print(f"Writing to {outfile}")
42-
with open(outfile, "wb") as outfile:
43-
outfile.write(orjson.dumps(traces))
54+
outfile.write_bytes(orjson.dumps(traces))
55+
56+
if web:
57+
open_in_perfetto(outfile)
4458
return 0
4559

4660

tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def run_case(case: pathlib.Path, cwd: pathlib.Path, useDir: bool):
3939
assert export.exists()
4040

4141
print("--- Trace")
42-
assert traceCommand(profiling, trace, unit, None) == 0
42+
assert traceCommand(profiling, trace, unit, None, False) == 0
4343
assert trace.exists()
4444

4545
participants = (

0 commit comments

Comments
 (0)