Skip to content

Commit d708b94

Browse files
sxufacebook-github-bot
authored andcommitted
Allow Inspector to accept ETDump bytes directly (#5657)
Summary: Pull Request resolved: #5657 Reviewed By: tarun292 Differential Revision: D63400412 fbshipit-source-id: 194feedb1d7e8f140b659d63de240a14a2a9a6f2
1 parent 5877c2a commit d708b94

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

devtools/inspector/_inspector.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ class Inspector:
967967
def __init__(
968968
self,
969969
etdump_path: Optional[str] = None,
970+
etdump_data: Optional[bytes] = None,
970971
etrecord: Optional[Union[ETRecord, str]] = None,
971972
source_time_scale: TimeScale = TimeScale.NS,
972973
target_time_scale: TimeScale = TimeScale.MS,
@@ -980,11 +981,12 @@ def __init__(
980981
enable_module_hierarchy: bool = False,
981982
) -> None:
982983
r"""
983-
Initialize an `Inspector` instance with the underlying `EventBlock`\ s populated with data from the provided ETDump path
984+
Initialize an `Inspector` instance with the underlying `EventBlock`\ s populated with data from the provided ETDump path or binary,
984985
and optional ETRecord path.
985986
986987
Args:
987-
etdump_path: Path to the ETDump file.
988+
etdump_path: Path to the ETDump file. Either this parameter or etdump_data should be provided.
989+
etdump_data: ETDump binary. Either this parameter or etdump_path should be provided.
988990
etrecord: Optional ETRecord object or path to the ETRecord file.
989991
source_time_scale: The time scale of the performance data retrieved from the runtime. The default time hook implentation in the runtime returns NS.
990992
target_time_scale: The target time scale to which the users want their performance data converted to. Defaults to MS.
@@ -1025,8 +1027,13 @@ def __init__(
10251027
else:
10261028
raise TypeError("Unsupported ETRecord type")
10271029

1030+
if (etdump_path is None) == (etdump_data is None):
1031+
raise ValueError(
1032+
"Expecting exactly one of etdump_path or etdump_data to be specified."
1033+
)
1034+
10281035
# Create EventBlocks from ETDump
1029-
etdump = gen_etdump_object(etdump_path=etdump_path)
1036+
etdump = gen_etdump_object(etdump_path=etdump_path, etdump_data=etdump_data)
10301037
if debug_buffer_path is not None:
10311038
with open(debug_buffer_path, "rb") as f:
10321039
output_buffer = f.read()

devtools/inspector/_inspector_utils.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,20 @@ def _extract_debug_handles(graph: OperatorGraph):
279279
return debug_handle_to_op_node_map
280280

281281

282-
def gen_etdump_object(etdump_path: Optional[str] = None) -> ETDumpFlatCC:
282+
def gen_etdump_object(
283+
etdump_path: Optional[str] = None, etdump_data: Optional[bytes] = None
284+
) -> ETDumpFlatCC:
283285
# Gen event blocks from etdump
284-
if etdump_path is None:
285-
raise ValueError("Etdump_path must be specified.")
286-
with open(etdump_path, "rb") as buff:
287-
etdump = deserialize_from_etdump_flatcc(buff.read())
288-
return etdump
286+
if etdump_data is None and etdump_path is not None:
287+
with open(etdump_path, "rb") as buff:
288+
etdump_data = buff.read()
289+
290+
if etdump_data is None:
291+
raise ValueError(
292+
"Unable to get ETDump data. One and only one of etdump_path and etdump_data must be specified."
293+
)
294+
295+
return deserialize_from_etdump_flatcc(etdump_data)
289296

290297

291298
def plot_metric(result: List[float], metric_name: str):

devtools/inspector/tests/inspector_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ def test_inspector_constructor(self):
8686

8787
# Assert that expected functions are called
8888
mock_parse_etrecord.assert_called_once_with(etrecord_path=ETRECORD_PATH)
89-
mock_gen_etdump.assert_called_once_with(etdump_path=ETDUMP_PATH)
89+
mock_gen_etdump.assert_called_once_with(
90+
etdump_path=ETDUMP_PATH, etdump_data=None
91+
)
9092
mock_gen_from_etdump.assert_called_once()
9193
# Because we mocked parse_etrecord() to return None, this method shouldn't be called
9294
mock_gen_graphs_from_etrecord.assert_not_called()

0 commit comments

Comments
 (0)