Skip to content

Commit bd5d8e3

Browse files
committed
Add view command
1 parent c642e94 commit bd5d8e3

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

preciceprofiling/perfetto.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,34 @@
22
from socketserver import TCPServer
33
from webbrowser import open_new_tab
44
import pathlib
5+
import json
6+
import urllib.parse
57

68

7-
def open_in_perfetto(tracefile: pathlib.Path):
9+
def open_in_perfetto(tracefile: pathlib.Path, startupCommands=[]):
810
print(f"Opening {str(tracefile)} on ui.perfetto.dev")
9-
content = tracefile.read_text()
11+
12+
content = tracefile.read_bytes()
13+
contentType = (
14+
"text/json" if tracefile.suffix == ".json" else "application/x-protobuf"
15+
)
1016

1117
PORT = 9001
1218
ORIGIN = "https://ui.perfetto.dev"
1319

1420
class TraceHandler(BaseHTTPRequestHandler):
1521
def do_GET(self):
16-
if self.path != "/trace.json":
22+
if self.path != "/trace/":
1723
self.send_error(405)
1824

1925
self.server.trace_served = True
2026
self.send_response(200)
2127
self.send_header("Access-Control-Allow-Origin", ORIGIN)
2228
self.send_header("Cache-Control", "no-cache")
23-
self.send_header("Content-type", "text/json")
29+
self.send_header("Content-type", contentType)
2430
self.send_header("Content-length", str(len(content)))
2531
self.end_headers()
26-
self.wfile.write(content.encode())
32+
self.wfile.write(content)
2733

2834
def do_POST(self):
2935
self.send_error(405)
@@ -35,7 +41,13 @@ def log_message(self, format, *args):
3541

3642
TCPServer.allow_reuse_address = True
3743
with TCPServer(("127.0.0.1", PORT), TraceHandler) as httpd:
38-
address = f"{ORIGIN}/#!/?url=http://127.0.0.1:{PORT}/trace.json"
44+
address = f"{ORIGIN}/#!/?url=http://127.0.0.1:{PORT}/trace/"
45+
46+
if startupCommands:
47+
print(json.dumps(startupCommands))
48+
encoded = urllib.parse.quote(json.dumps(startupCommands))
49+
address += f"&startupCommands={encoded}"
50+
3951
open_new_tab(address)
4052

4153
httpd.trace_served = False

preciceprofiling/view.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from preciceprofiling.parsers import addInputArgument
2+
from preciceprofiling.perfetto import open_in_perfetto
3+
import argparse
4+
import sys
5+
import pathlib
6+
import json
7+
8+
9+
def makeViewParser(add_help: bool = True):
10+
view_help = "Open the trace in ui.perfetto.dev"
11+
view = argparse.ArgumentParser(description=view_help, add_help=add_help)
12+
view.add_argument(
13+
"pftrace",
14+
nargs="?",
15+
type=pathlib.Path,
16+
default=pathlib.Path("profiling.pftrace"),
17+
help="The perfetto trace file to view",
18+
)
19+
view.add_argument(
20+
"-c",
21+
"--commands",
22+
type=pathlib.Path,
23+
help="File to read startup commands from",
24+
)
25+
view.add_argument(
26+
"-i",
27+
"--isolate",
28+
type=int,
29+
nargs="+",
30+
help="Isolate ranks in new workspace",
31+
)
32+
return view
33+
34+
35+
def runView(ns):
36+
return viewCommand(ns.pftrace, ns.commands, ns.isolate)
37+
38+
39+
def viewCommand(tracefile, commandsFile, isolate):
40+
cmds = []
41+
if isolate:
42+
matcher = "|".join(map(str, isolate))
43+
WORKSPACE = "preCICE"
44+
cmds.append({"id": "dev.perfetto.CreateWorkspace", "args": [WORKSPACE]})
45+
cmds.append(
46+
{
47+
"id": "dev.perfetto.CopyTracksToWorkspaceByRegexWithAncestors",
48+
"args": [f".*Rank ({matcher})$", WORKSPACE, "path"],
49+
}
50+
)
51+
cmds.append({"id": "dev.perfetto.SwitchWorkspace", "args": [WORKSPACE]})
52+
if commandsFile:
53+
assert commandsFile.exists()
54+
cmds.extend(json.loads(commandsFile.read_bytes()))
55+
open_in_perfetto(tracefile, startupCommands=cmds)
56+
return 0
57+
58+
59+
def main():
60+
parser = makeViewParser()
61+
ns = parser.parse_args()
62+
return runView(ns)
63+
64+
65+
if __name__ == "__main__":
66+
sys.exit(main())

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ precice-profiling-trace = "preciceprofiling.trace:main"
4141
precice-profiling-export = "preciceprofiling.export:main"
4242
precice-profiling-histogram = "preciceprofiling.histogram:main"
4343
precice-profiling-pftrace = "preciceprofiling.pftrace:main"
44+
precice-profiling-view = "preciceprofiling.view:main"
4445

4546
[tool.setuptools]
4647
packages=["preciceprofiling"]

0 commit comments

Comments
 (0)