From 2adeb116dff6f9e25d806f5e887b585f4e77a387 Mon Sep 17 00:00:00 2001 From: codeasier Date: Fri, 7 Aug 2026 17:32:17 +0800 Subject: [PATCH 1/3] perf: skip Frame reconstruction during database import (#84) Database import now keeps raw trace/segment/block frame dictionaries instead of building a Frame object per frame reference, and formats callstacks with reversed iterator and list comprehension. Public snapshot replay and split keep eager Frame construction unchanged. Baseline benchmark on 131k/628k sanitized samples (same machine, before on dafaf4f, after on this change): | sample | metric | before | after | change | |--------|----------|--------|-------|--------| | 131k | wall | 9.600s | 3.510s | -63% | | 131k | peak RSS | 1415.6MB | 233.0MB | -84% | | 628k | wall | 43.000s | 17.930s | -58% | | 628k | peak RSS | 5615.8MB | 706.5MB | -87% | DB output is byte-for-byte identical (same row counts, callstacks, representative queries, quick_check ok). --- benchmarks/baseline_import.py | 62 ++++++++++++-- src/pt_snap_cli/snapshot/PROVENANCE.md | 6 ++ src/pt_snap_cli/snapshot/base/entities.py | 83 +++++++++++++++---- src/pt_snap_cli/snapshot/representation.py | 3 +- src/pt_snap_cli/snapshot/simulate/simulate.py | 7 +- .../snapshot/simulate/snapshot_mutator.py | 2 + .../snapshot/tools/adaptors/snapshot2db.py | 2 + tests/snapshot/test_entities.py | 57 +++++++++++++ tests/snapshot/test_snapshot2db_runtime.py | 76 ++++++++++++++++- tests/snapshot/test_snapshot_mutator.py | 16 ++++ 10 files changed, 283 insertions(+), 31 deletions(-) diff --git a/benchmarks/baseline_import.py b/benchmarks/baseline_import.py index 97b558e..075a2f5 100644 --- a/benchmarks/baseline_import.py +++ b/benchmarks/baseline_import.py @@ -46,12 +46,21 @@ class RunResult: db_size_mb: float +@dataclass +class FrameMetrics: + events: int + total_frames: int + average_depth: float + max_depth: int + + @dataclass class SampleReport: name: str path: Path formal_runs: int = 0 runs: list[RunResult] = field(default_factory=list) + frames: FrameMetrics | None = None @property def median_wall(self) -> float: @@ -63,6 +72,13 @@ def format_table(self) -> str: "", f"正式测量 {self.formal_runs} 次,另有 1 次 warmup", "", + ( + f"Events: {self.frames.events}; frame references: {self.frames.total_frames}; " + f"average/max depth: {self.frames.average_depth:.1f}/{self.frames.max_depth}" + if self.frames + else "Frame depth metrics unavailable" + ), + "", "| Run | Wall (s) | User (s) | Sys (s) | Max RSS (MB) | DB (MB) |", "|-----|----------|----------|---------|--------------|---------|", ] @@ -77,16 +93,16 @@ def format_table(self) -> str: def _parse_time_output(stderr: str) -> dict[str, float]: - wall_match = re.search(r"(\d+\.\d+) real", stderr) - user_match = re.search(r"(\d+\.\d+) user", stderr) - sys_match = re.search(r"(\d+\.\d+) sys", stderr) - rss_match = re.search(r"(\d+)\s+maximum resident set size", stderr) + wall_match = re.search(r"PT_SNAP_TIME wall=(\d+\.\d+)", stderr) + user_match = re.search(r"\buser=(\d+\.\d+)", stderr) + sys_match = re.search(r"\bsys=(\d+\.\d+)", stderr) + rss_match = re.search(r"\bmax_rss_kb=(\d+)", stderr) return { "wall_s": float(wall_match.group(1)) if wall_match else 0.0, "user_s": float(user_match.group(1)) if user_match else 0.0, "sys_s": float(sys_match.group(1)) if sys_match else 0.0, - "max_rss_bytes": int(rss_match.group(1)) if rss_match else 0, + "max_rss_kb": int(rss_match.group(1)) if rss_match else 0, } @@ -95,13 +111,17 @@ def _run_import_once(snapshot: Path, output_dir: Path, device: int) -> RunResult result = subprocess.run( [ "/usr/bin/time", - "-l", + "-f", + "PT_SNAP_TIME wall=%e user=%U sys=%S max_rss_kb=%M", sys.executable, "-c", f""" import logging; logging.disable(logging.CRITICAL) +import pt_snap_cli.core.import_metadata as import_metadata from pt_snap_cli.core.import_service import ImportOptions, ImportService from pathlib import Path +if not import_metadata.__version__: + import_metadata.__version__ = "benchmark" options = ImportOptions( snapshot_file=Path({str(snapshot)!r}), output_dir=Path({str(output_dir)!r}), @@ -131,7 +151,7 @@ def _run_import_once(snapshot: Path, output_dir: Path, device: int) -> RunResult wall_s=metrics["wall_s"], user_s=metrics["user_s"], sys_s=metrics["sys_s"], - max_rss_mb=metrics["max_rss_bytes"] / (1024 * 1024), + max_rss_mb=metrics["max_rss_kb"] / 1024, db_size_mb=db_size / (1024 * 1024), ) @@ -184,6 +204,22 @@ def _representative_queries(db_path: Path, device: int) -> dict: return results +def _frame_metrics(snapshot: Path, device: int) -> FrameMetrics: + from pt_snap_cli.snapshot.representation import load_snapshot_representation + + representation = load_snapshot_representation(snapshot) + device_traces = representation.get("device_traces", []) + traces = device_traces[device] if 0 <= device < len(device_traces) else [] + depths = [len(trace.get("frames", [])) for trace in traces] + total = sum(depths) + return FrameMetrics( + events=len(depths), + total_frames=total, + average_depth=total / len(depths) if depths else 0.0, + max_depth=max(depths, default=0), + ) + + def _correctness_summary(output_dir: Path, device: int) -> dict: db_files = list(output_dir.glob("*.db")) if not db_files: @@ -239,6 +275,7 @@ def _profile_import(snapshot: Path, output_dir: Path, device: int, top_n: int = stats = pstats.Stats(profiler, stream=stream) stats.sort_stats("cumulative") stats.print_stats(top_n) + stats.print_stats("from_dict|get_callstack|replay|_do_insert") return stream.getvalue() @@ -270,11 +307,20 @@ def main() -> None: continue n_runs = run_counts.get(name, 3) - report = SampleReport(name=name, path=snapshot, formal_runs=n_runs) + report = SampleReport( + name=name, + path=snapshot, + formal_runs=n_runs, + frames=_frame_metrics(snapshot, DEVICE), + ) print(f"\n{'='*60}") print(f"Sample: {name} ({snapshot.name})") print(f"{'='*60}") + print( + f" Frames: total={report.frames.total_frames} " + f"avg_depth={report.frames.average_depth:.1f} max_depth={report.frames.max_depth}" + ) print(" Warmup run...") with tempfile.TemporaryDirectory(prefix=f"baseline_{name}_warmup_") as tmp: diff --git a/src/pt_snap_cli/snapshot/PROVENANCE.md b/src/pt_snap_cli/snapshot/PROVENANCE.md index 38a77dc..8afbec5 100644 --- a/src/pt_snap_cli/snapshot/PROVENANCE.md +++ b/src/pt_snap_cli/snapshot/PROVENANCE.md @@ -117,6 +117,12 @@ The upstream standalone split frontend is deliberately excluded, not deferred: segment lookup by reusing discovered indices and standard-library keyed binary search. This is a local performance change against the first-party runtime; audited upstream source mappings and license terms are unchanged. +- On 2026-08-07, issue #84 added an internal database-import-only raw frame + path and generator-based callstack formatting. Public snapshot construction + remains eager, while database replay avoids reconstructing per-event `Frame` + objects and preserves source frame dictionaries through synthetic segments. + This is a local performance change; audited upstream mappings and licensing + evidence are unchanged. ## Migration toolchain diff --git a/src/pt_snap_cli/snapshot/base/entities.py b/src/pt_snap_cli/snapshot/base/entities.py index c59b48b..cbc4510 100644 --- a/src/pt_snap_cli/snapshot/base/entities.py +++ b/src/pt_snap_cli/snapshot/base/entities.py @@ -26,6 +26,18 @@ def to_dict(self): ) +def _format_callstack(frames: list[Frame] | list[dict]) -> str: + if not frames: + return "" + if isinstance(frames[0], Frame): + return "\n".join( + [f"{frame.filename}:{frame.line} {frame.name}" for frame in reversed(frames)] + ) + return "\n".join( + [f"{frame['filename']}:{frame['line']} {frame['name']}" for frame in reversed(frames)] + ) + + @dataclass class TraceEntry: # When `torch.npu.memory._record_memory_history()` is enabled, @@ -59,26 +71,27 @@ class TraceEntry: _origin: dict = None # Readonly idx: int = -1 # 索引,全局唯一 + _raw_frames: list[dict] | None = field(default=None, repr=False) @classmethod - def from_dict(cls, trace_dict: dict): + def from_dict(cls, trace_dict: dict, _raw_frames: bool = False): + frame_dicts = trace_dict.get("frames", []) + if _raw_frames: + iter(frame_dicts) trace_entry = cls( action=trace_dict["action"], addr=int(trace_dict["addr"]), size=int(trace_dict["size"]), stream=int(trace_dict["stream"]), _origin=trace_dict, - frames=[Frame.from_dict(_frame_dict) for _frame_dict in trace_dict.get("frames", [])], + frames=[] if _raw_frames else [Frame.from_dict(frame) for frame in frame_dicts], + _raw_frames=frame_dicts if _raw_frames else None, idx=int(trace_dict["id"]) if "id" in trace_dict else -1, ) return trace_entry def get_callstack(self): - if not self.frames: - return "" - return "\n".join( - [f"{frame.filename}:{frame.line} {frame.name}" for frame in self.frames[::-1]] - ) + return _format_callstack(self._raw_frames if self._raw_frames is not None else self.frames) def to_dict(self, include_id: bool = False): if self._origin and not include_id: @@ -91,7 +104,11 @@ def to_dict(self, include_id: bool = False): "addr": self.addr, "size": self.size, "stream": self.stream, - "frames": [frame.to_dict() for frame in self.frames], + "frames": ( + self._raw_frames + if self._raw_frames is not None + else [frame.to_dict() for frame in self.frames] + ), } ) if include_id: @@ -126,15 +143,20 @@ class Block: segment_ptr: Any = None free_event_idx: int = None alloc_event_idx: int = None + _raw_frames: list[dict] | None = field(default=None, repr=False) @classmethod - def from_dict(cls, block_dict: dict): + def from_dict(cls, block_dict: dict, _raw_frames: bool = False): + frame_dicts = block_dict.get("frames", []) + if _raw_frames: + iter(frame_dicts) block = cls( size=block_dict["size"], requested_size=block_dict["requested_size"], address=block_dict["address"], state=block_dict["state"], - frames=[Frame.from_dict(frame) for frame in block_dict.get("frames", [])], + frames=[] if _raw_frames else [Frame.from_dict(frame) for frame in frame_dicts], + _raw_frames=frame_dicts if _raw_frames else None, ) return block @@ -145,6 +167,7 @@ def build_from_event(cls, event: TraceEntry): requested_size=event.size, address=event.addr, frames=event.frames, + _raw_frames=event._raw_frames, ) return block @@ -154,7 +177,11 @@ def to_dict(self): "requested_size": self.requested_size, "address": self.address, "state": self.state, - "frames": [frame.to_dict() for frame in self.frames], + "frames": ( + self._raw_frames + if self._raw_frames is not None + else [frame.to_dict() for frame in self.frames] + ), } @@ -179,9 +206,15 @@ class Segment: _origin: dict = None # Readonly free_or_unmap_event_idx: int = None alloc_or_map_event_idx: int = None + _raw_frames: list[dict] | None = field(default=None, repr=False) @classmethod - def from_dict(cls, segment_dict: dict, ignore_inactive_blocks: bool = False): + def from_dict( + cls, segment_dict: dict, ignore_inactive_blocks: bool = False, _raw_frames: bool = False + ): + frame_dicts = segment_dict.get("frames", []) + if _raw_frames: + iter(frame_dicts) segment = cls( address=segment_dict["address"], total_size=segment_dict["total_size"], @@ -189,7 +222,8 @@ def from_dict(cls, segment_dict: dict, ignore_inactive_blocks: bool = False): segment_type=segment_dict["segment_type"], allocated_size=segment_dict["allocated_size"], active_size=segment_dict["active_size"], - frames=[Frame.from_dict(_frame) for _frame in segment_dict.get("frames", [])], + frames=[] if _raw_frames else [Frame.from_dict(frame) for frame in frame_dicts], + _raw_frames=frame_dicts if _raw_frames else None, device=segment_dict.get("device", 0), _origin=segment_dict, is_expandable=segment_dict.get("is_expandable", False), @@ -197,7 +231,7 @@ def from_dict(cls, segment_dict: dict, ignore_inactive_blocks: bool = False): for block in segment_dict["blocks"]: if ignore_inactive_blocks and block["state"] == BlockState.INACTIVE: continue - _block = Block.from_dict(block) + _block = Block.from_dict(block, _raw_frames=_raw_frames) _block.segment_ptr = segment segment.blocks.append(_block) return segment @@ -209,6 +243,7 @@ def build_from_event(cls, event: TraceEntry, with_inactive_block: bool = False): total_size=event.size, stream=event.stream, frames=event.frames, + _raw_frames=event._raw_frames, device=event.device if hasattr(event, "device") else 0, allocated_size=0, active_size=0, @@ -239,7 +274,11 @@ def to_dict(self): "active_size": self.active_size, "device": self.device, "is_expandable": self.is_expandable, - "frames": [frame.to_dict() for frame in self.frames], + "frames": ( + self._raw_frames + if self._raw_frames is not None + else [frame.to_dict() for frame in self.frames] + ), "blocks": [block.to_dict() for block in self.blocks], } @@ -255,7 +294,13 @@ class DeviceSnapshot: device: int @classmethod - def from_dict(cls, snapshot_dict: dict, device: int, ignore_inactive_blocks: bool = False): + def from_dict( + cls, + snapshot_dict: dict, + device: int, + ignore_inactive_blocks: bool = False, + _raw_frames: bool = False, + ): segments_dict = snapshot_dict.get("segments", []) device_traces = snapshot_dict.get("device_traces", []) device_trace_list = device_traces[device] if 0 <= device < len(device_traces) else [] @@ -272,7 +317,9 @@ def from_dict(cls, snapshot_dict: dict, device: int, ignore_inactive_blocks: boo if segment_dict.get("device", 0) != device: continue _segment = Segment.from_dict( - segment_dict, ignore_inactive_blocks=ignore_inactive_blocks + segment_dict, + ignore_inactive_blocks=ignore_inactive_blocks, + _raw_frames=_raw_frames, ) snapshot.segments.append(_segment) snapshot.total_allocated += _segment.allocated_size @@ -281,7 +328,7 @@ def from_dict(cls, snapshot_dict: dict, device: int, ignore_inactive_blocks: boo snapshot.segments.sort(key=lambda segment: (segment.address, segment.stream)) # 读取事件序列 for idx, trace_entry_dict in enumerate(device_trace_list): - trace_entry = TraceEntry.from_dict(trace_entry_dict) + trace_entry = TraceEntry.from_dict(trace_entry_dict, _raw_frames=_raw_frames) if "id" not in trace_entry_dict: trace_entry.idx = idx snapshot.trace_entries.append(trace_entry) diff --git a/src/pt_snap_cli/snapshot/representation.py b/src/pt_snap_cli/snapshot/representation.py index 63dd25a..2b4dcef 100644 --- a/src/pt_snap_cli/snapshot/representation.py +++ b/src/pt_snap_cli/snapshot/representation.py @@ -127,9 +127,10 @@ def replay_snapshot( *, hooker: SimulateHooker | None = None, allocator_hooker: AllocatorHooker | None = None, + _raw_frames: bool = False, ) -> tuple[SimulateDeviceSnapshot, bool]: """Construct and replay one device through the shared snapshot runtime.""" - snapshot = SimulateDeviceSnapshot(representation, device) + snapshot = SimulateDeviceSnapshot(representation, device, _raw_frames=_raw_frames) if hooker is not None: snapshot.register_hooker(hooker) if allocator_hooker is not None: diff --git a/src/pt_snap_cli/snapshot/simulate/simulate.py b/src/pt_snap_cli/snapshot/simulate/simulate.py index dbbee65..1cb591e 100644 --- a/src/pt_snap_cli/snapshot/simulate/simulate.py +++ b/src/pt_snap_cli/snapshot/simulate/simulate.py @@ -19,7 +19,7 @@ class SimulateDeviceSnapshot: _loading_logger: Logger _replay_logger: Logger - def __init__(self, snapshot_dict: dict, device: int): + def __init__(self, snapshot_dict: dict, device: int, *, _raw_frames: bool = False): # 基于device初始化logger self._loading_logger = loading_logger.getChild(f"{device}") self._replay_logger = replay_logger.getChild(f"{device}") @@ -27,7 +27,10 @@ def __init__(self, snapshot_dict: dict, device: int): raise RuntimeError("Cannot init snapshot from empty data.") self._loading_logger.info("Loading snapshot data...") self.device_snapshot = DeviceSnapshot.from_dict( - snapshot_dict, device, ignore_inactive_blocks=True + snapshot_dict, + device, + ignore_inactive_blocks=True, + _raw_frames=_raw_frames, ) self._loading_logger.info( f"Finished to load snapshot data: total of {len(self.device_snapshot.trace_entries)} " diff --git a/src/pt_snap_cli/snapshot/simulate/snapshot_mutator.py b/src/pt_snap_cli/snapshot/simulate/snapshot_mutator.py index d8ffd6b..2a2badb 100644 --- a/src/pt_snap_cli/snapshot/simulate/snapshot_mutator.py +++ b/src/pt_snap_cli/snapshot/simulate/snapshot_mutator.py @@ -274,6 +274,7 @@ def _split_segment_at(snapshot: DeviceSnapshot, seg_idx: int, cut_addr: int, cut blocks=[], device=original_segment.device, frames=original_segment.frames, + _raw_frames=original_segment._raw_frames, is_expandable=original_segment.is_expandable, free_or_unmap_event_idx=original_segment.free_or_unmap_event_idx, alloc_or_map_event_idx=original_segment.alloc_or_map_event_idx, @@ -288,6 +289,7 @@ def _split_segment_at(snapshot: DeviceSnapshot, seg_idx: int, cut_addr: int, cut blocks=[], device=original_segment.device, frames=original_segment.frames, + _raw_frames=original_segment._raw_frames, is_expandable=original_segment.is_expandable, free_or_unmap_event_idx=original_segment.free_or_unmap_event_idx, alloc_or_map_event_idx=original_segment.alloc_or_map_event_idx, diff --git a/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py b/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py index c66cfd4..7a57f63 100644 --- a/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py +++ b/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py @@ -85,6 +85,7 @@ def post_undo_event( action="segment_map" if seg.is_expandable else "segment_alloc", addr=seg.address, frames=seg.frames, + _raw_frames=seg._raw_frames, size=seg.total_size, stream=seg.stream, ) @@ -145,6 +146,7 @@ def dump(pickle_file: str, dump_file: str, device=None) -> bool: device, hooker=hooker, allocator_hooker=hooker, + _raw_frames=True, ) if not replayed: dump_logger.error(f"Failed to dump the snapshot to database for device {device}.") diff --git a/tests/snapshot/test_entities.py b/tests/snapshot/test_entities.py index 589c536..a496c56 100644 --- a/tests/snapshot/test_entities.py +++ b/tests/snapshot/test_entities.py @@ -96,6 +96,59 @@ def test_get_callstack_empty_frames(self): trace = TraceEntry(action="alloc") self.assertEqual(trace.get_callstack(), "") + def test_raw_frames_match_eager_format_without_mutating_source(self): + trace_dict = { + "action": "alloc", + "addr": 0x1000, + "size": "1024", + "stream": "0", + "id": 27, + "frames": [ + {"filename": "inner.py", "line": 10, "name": "inner"}, + {"filename": "outer.py", "line": 20, "name": "outer"}, + ], + } + original = {**trace_dict, "frames": [dict(frame) for frame in trace_dict["frames"]]} + + eager = TraceEntry.from_dict(trace_dict) + raw = TraceEntry.from_dict(trace_dict, _raw_frames=True) + + self.assertTrue(all(isinstance(frame, Frame) for frame in eager.frames)) + self.assertEqual(raw.frames, []) + self.assertIs(raw._raw_frames, trace_dict["frames"]) + self.assertEqual(raw.get_callstack(), eager.get_callstack()) + self.assertEqual(raw.get_callstack(), "outer.py:20 outer\ninner.py:10 inner") + self.assertEqual(raw.to_dict(include_id=True), eager.to_dict(include_id=True)) + self.assertEqual(raw.idx, 27) + self.assertEqual(trace_dict, original) + + def test_raw_frames_preserve_empty_missing_and_incomplete_behavior(self): + base = {"action": "alloc", "addr": 1, "size": 2, "stream": 0} + + self.assertEqual(TraceEntry.from_dict(base, _raw_frames=True).get_callstack(), "") + self.assertEqual( + TraceEntry.from_dict({**base, "frames": []}, _raw_frames=True).get_callstack(), + "", + ) + incomplete = {**base, "frames": [{"filename": "broken.py", "line": 3}]} + with self.assertRaises(KeyError): + TraceEntry.from_dict(incomplete) + with self.assertRaises(KeyError): + TraceEntry.from_dict(incomplete, _raw_frames=True).get_callstack() + with self.assertRaises(TypeError): + TraceEntry.from_dict({**base, "frames": None}, _raw_frames=True) + + def test_raw_frames_preserve_positional_constructor_and_synthetic_serialization(self): + origin = {"action": "alloc"} + trace = TraceEntry("alloc", 1, [], 2, 3, 4, origin, 5) + raw_frames = [{"filename": "raw.py", "line": 6, "name": "run"}] + synthetic = TraceEntry(action="alloc", _raw_frames=raw_frames) + + self.assertIs(trace._origin, origin) + self.assertEqual(trace.idx, 5) + self.assertIsNone(trace._raw_frames) + self.assertIs(synthetic.to_dict()["frames"], raw_frames) + def test_to_dict(self): trace_dict = { "action": "alloc", @@ -343,6 +396,10 @@ def test_from_dict(self): self.assertEqual(snapshot.trace_entries[0].idx, 0) self.assertEqual(snapshot.trace_entries[1].idx, 1) + self.assertTrue( + all(isinstance(frame, Frame) for trace in snapshot.trace_entries for frame in trace.frames) + ) + def test_find_overlapping_segment_returns_idx_and_matches_containing_segment(self): snapshot = DeviceSnapshot() snapshot.segments = [ diff --git a/tests/snapshot/test_snapshot2db_runtime.py b/tests/snapshot/test_snapshot2db_runtime.py index c849b63..5ff4e7c 100644 --- a/tests/snapshot/test_snapshot2db_runtime.py +++ b/tests/snapshot/test_snapshot2db_runtime.py @@ -1,3 +1,4 @@ +import pickle import sqlite3 from pathlib import Path @@ -10,6 +11,7 @@ from pt_snap_cli.snapshot.tools.adaptors.database import ( TRACE_ENTRY_ACTION_VALUE_MAP as RUNTIME_ACTION_VALUE_MAP, ) +from pt_snap_cli.snapshot.tools.adaptors.database import entity2record from pt_snap_cli.snapshot.util.logger import restore_logs, suppress_logs from .golden_observations import ( @@ -57,7 +59,7 @@ def test_snapshot2db_uses_shared_load_and_replay_entrypoints(monkeypatch, tmp_pa database = tmp_path / "shared-entrypoints.db" original_load = snapshot2db.load_snapshot_representation original_replay = snapshot2db.replay_snapshot - calls = {"load": 0, "replay": 0} + calls = {"load": 0, "replay": 0, "raw_frames": False} def load(snapshot_file): calls["load"] += 1 @@ -65,13 +67,83 @@ def load(snapshot_file): def replay(representation, device, **kwargs): calls["replay"] += 1 + calls["raw_frames"] = kwargs.get("_raw_frames", False) return original_replay(representation, device, **kwargs) monkeypatch.setattr(snapshot2db, "load_snapshot_representation", load) monkeypatch.setattr(snapshot2db, "replay_snapshot", replay) assert snapshot2db.dump(FIXTURE_DIR / "snapshot_with_empty_cache.pkl", database, 0) - assert calls == {"load": 1, "replay": 1} + assert calls == {"load": 1, "replay": 1, "raw_frames": True} + + +def test_fast_frame_import_matches_eager_database_exactly(monkeypatch, tmp_path): + representation = { + "segments": [], + "device_traces": [ + [ + { + "action": "segment_free", + "addr": 0x1000, + "size": 64, + "stream": 3, + "id": 17, + "frames": [ + {"filename": "inner.py", "line": 10, "name": "inner"}, + {"filename": "outer.py", "line": 20, "name": "outer"}, + ], + }, + { + "action": "segment_free", + "addr": 0x2000, + "size": 32, + "stream": 3, + "frames": [], + }, + ] + ], + } + snapshot = tmp_path / "frames.pkl" + with snapshot.open("wb") as stream: + pickle.dump(representation, stream) + + original_replay = snapshot2db.replay_snapshot + + def dump_rows(database, *, eager): + entity2record.next_default_event_id = entity2record.make_default_id_counter() + entity2record.next_default_block_id = entity2record.make_default_id_counter() + if eager: + monkeypatch.setattr( + snapshot2db, + "replay_snapshot", + lambda data, device, **kwargs: original_replay( + data, + device, + hooker=kwargs["hooker"], + allocator_hooker=kwargs["allocator_hooker"], + ), + ) + else: + monkeypatch.setattr(snapshot2db, "replay_snapshot", original_replay) + assert snapshot2db.dump(snapshot, database, 0) + with sqlite3.connect(database) as connection: + return connection.execute( + "SELECT id, action, address, size, stream, allocated, active, reserved, callstack " + "FROM trace_entry_0 ORDER BY id" + ).fetchall() + + eager_rows = dump_rows(tmp_path / "eager.db", eager=True) + fast_rows = dump_rows(tmp_path / "fast.db", eager=False) + + assert fast_rows == eager_rows + explicit = next(row for row in fast_rows if row[0] == 17) + assert explicit[1:5] == (3, 0x1000, 64, 3) + assert explicit[-1] == "outer.py:20 outer\ninner.py:10 inner" + assert any(row[0] == 1 and row[2] == 0x2000 for row in fast_rows) + assert sum(row[-1] == "" for row in fast_rows) == 2 + synthetic = [row for row in fast_rows if row[0] < 0] + assert len(synthetic) == 2 + assert any(row[-1] == "outer.py:20 outer\ninner.py:10 inner" for row in synthetic) def test_expandable_snapshot2db(dump_database): diff --git a/tests/snapshot/test_snapshot_mutator.py b/tests/snapshot/test_snapshot_mutator.py index 28ce958..28ee2ff 100644 --- a/tests/snapshot/test_snapshot_mutator.py +++ b/tests/snapshot/test_snapshot_mutator.py @@ -4,6 +4,7 @@ Block, BlockState, DeviceSnapshot, + Frame, Segment, TraceEntry, ) @@ -150,6 +151,7 @@ def test_merge_mapped_segment_returns_false_when_no_adjacent_segments_exist(): def test_split_or_shrink_segment_uses_middle_split_branch(): segment = make_segment(0x1000, 0x1000) + segment.frames = [Frame.from_dict({"filename": "alloc.py", "line": 4, "name": "run"})] left_block = make_block(0x1000, 0x100) right_block = make_block(0x1800, 0x100) left_block.segment_ptr = segment @@ -161,9 +163,23 @@ def test_split_or_shrink_segment_uses_middle_split_branch(): assert snapshot_mutator.split_or_shrink_segment(snapshot, 0, 0x1400, 0x100) is True assert len(snapshot.segments) == 2 + assert all(part.frames is segment.frames for part in snapshot.segments) + assert all(isinstance(part.frames[0], Frame) for part in snapshot.segments) assert_valid_segments(snapshot.segments) +def test_split_or_shrink_segment_preserves_raw_frames(): + raw_frames = [{"filename": "alloc.py", "line": 4, "name": "run"}] + segment = make_segment(0x1000, 0x1000) + segment._raw_frames = raw_frames + snapshot = make_snapshot([segment]) + + assert snapshot_mutator.split_or_shrink_segment(snapshot, 0, 0x1400, 0x100) is True + assert len(snapshot.segments) == 2 + assert all(part._raw_frames is raw_frames for part in snapshot.segments) + assert all(part.to_dict()["frames"] is raw_frames for part in snapshot.segments) + + def test_increase_and_decrease_reserved_update_snapshot_totals(): snapshot = make_snapshot() From 4ac6e3424c45103a6eb5a259c6804a56c90b3e18 Mon Sep 17 00:00:00 2001 From: codeasier Date: Fri, 7 Aug 2026 18:00:17 +0800 Subject: [PATCH 2/3] style: apply black formatting to entity tests (#84) --- tests/snapshot/test_entities.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/snapshot/test_entities.py b/tests/snapshot/test_entities.py index a496c56..76d3aea 100644 --- a/tests/snapshot/test_entities.py +++ b/tests/snapshot/test_entities.py @@ -397,7 +397,11 @@ def test_from_dict(self): self.assertEqual(snapshot.trace_entries[1].idx, 1) self.assertTrue( - all(isinstance(frame, Frame) for trace in snapshot.trace_entries for frame in trace.frames) + all( + isinstance(frame, Frame) + for trace in snapshot.trace_entries + for frame in trace.frames + ) ) def test_find_overlapping_segment_returns_idx_and_matches_containing_segment(self): From 4072930dd0db66448b4695e0cf11aa174bda9218 Mon Sep 17 00:00:00 2001 From: codeasier Date: Fri, 7 Aug 2026 20:59:26 +0800 Subject: [PATCH 3/3] fix: address import benchmark review feedback (#84) --- benchmarks/baseline_import.py | 52 +++++--- src/pt_snap_cli/snapshot/base/entities.py | 13 +- .../snapshot/tools/adaptors/snapshot2db.py | 69 ++++++++--- tests/snapshot/test_entities.py | 2 +- tests/snapshot/test_snapshot2db_runtime.py | 117 ++++++++++++++++++ tests/test_baseline_import.py | 24 ++++ 6 files changed, 238 insertions(+), 39 deletions(-) create mode 100644 tests/test_baseline_import.py diff --git a/benchmarks/baseline_import.py b/benchmarks/baseline_import.py index 075a2f5..4230039 100644 --- a/benchmarks/baseline_import.py +++ b/benchmarks/baseline_import.py @@ -17,12 +17,12 @@ import logging import os import pstats -import re import sqlite3 import statistics import subprocess import sys import tempfile +import time from dataclasses import dataclass, field from pathlib import Path @@ -92,32 +92,42 @@ def format_table(self) -> str: return "\n".join(lines) -def _parse_time_output(stderr: str) -> dict[str, float]: - wall_match = re.search(r"PT_SNAP_TIME wall=(\d+\.\d+)", stderr) - user_match = re.search(r"\buser=(\d+\.\d+)", stderr) - sys_match = re.search(r"\bsys=(\d+\.\d+)", stderr) - rss_match = re.search(r"\bmax_rss_kb=(\d+)", stderr) +def _parse_resource_output(stderr: str) -> dict[str, float]: + prefix = "PT_SNAP_RESOURCE " + line = next((line for line in stderr.splitlines() if line.startswith(prefix)), None) + if line is None: + raise RuntimeError("benchmark subprocess did not report resource usage") + try: + values = { + key: float(value) + for key, value in (part.split("=", 1) for part in line.removeprefix(prefix).split()) + } + return { + "user_s": values["user"], + "sys_s": values["sys"], + "max_rss_kb": values["max_rss_kb"], + } + except (KeyError, ValueError) as exc: + raise RuntimeError(f"invalid benchmark resource output: {line}") from exc - return { - "wall_s": float(wall_match.group(1)) if wall_match else 0.0, - "user_s": float(user_match.group(1)) if user_match else 0.0, - "sys_s": float(sys_match.group(1)) if sys_match else 0.0, - "max_rss_kb": int(rss_match.group(1)) if rss_match else 0, - } + +def _max_rss_kb(max_rss: float, platform: str) -> float: + return max_rss / 1024 if platform == "darwin" else max_rss def _run_import_once(snapshot: Path, output_dir: Path, device: int) -> RunResult: env = {**os.environ, "PYTHONPATH": "src"} + started_at = time.perf_counter() result = subprocess.run( [ - "/usr/bin/time", - "-f", - "PT_SNAP_TIME wall=%e user=%U sys=%S max_rss_kb=%M", sys.executable, "-c", f""" import logging; logging.disable(logging.CRITICAL) import pt_snap_cli.core.import_metadata as import_metadata +import resource +import sys +from benchmarks.baseline_import import _max_rss_kb from pt_snap_cli.core.import_service import ImportOptions, ImportService from pathlib import Path if not import_metadata.__version__: @@ -130,25 +140,33 @@ def _run_import_once(snapshot: Path, output_dir: Path, device: int) -> RunResult force=True, ) ImportService().import_snapshot(options) +usage = resource.getrusage(resource.RUSAGE_SELF) +max_rss_kb = _max_rss_kb(usage.ru_maxrss, sys.platform) +print( + f"PT_SNAP_RESOURCE user={{usage.ru_utime}} sys={{usage.ru_stime}} " + f"max_rss_kb={{max_rss_kb}}", + file=sys.stderr, +) """, ], capture_output=True, text=True, env=env, ) + wall_s = time.perf_counter() - started_at if result.returncode != 0: print(f"STDOUT: {result.stdout}", file=sys.stderr) print(f"STDERR: {result.stderr}", file=sys.stderr) raise RuntimeError(f"import failed: {result.returncode}") - metrics = _parse_time_output(result.stderr) + metrics = _parse_resource_output(result.stderr) db_files = list(output_dir.glob("*.db")) db_size = sum(f.stat().st_size for f in db_files) if db_files else 0 return RunResult( - wall_s=metrics["wall_s"], + wall_s=wall_s, user_s=metrics["user_s"], sys_s=metrics["sys_s"], max_rss_mb=metrics["max_rss_kb"] / 1024, diff --git a/src/pt_snap_cli/snapshot/base/entities.py b/src/pt_snap_cli/snapshot/base/entities.py index cbc4510..04facaf 100644 --- a/src/pt_snap_cli/snapshot/base/entities.py +++ b/src/pt_snap_cli/snapshot/base/entities.py @@ -38,6 +38,13 @@ def _format_callstack(frames: list[Frame] | list[dict]) -> str: ) +def _validate_raw_frames(frames: list[dict]) -> None: + for frame in frames: + _ = frame["filename"] + _ = frame["line"] + _ = frame["name"] + + @dataclass class TraceEntry: # When `torch.npu.memory._record_memory_history()` is enabled, @@ -77,7 +84,7 @@ class TraceEntry: def from_dict(cls, trace_dict: dict, _raw_frames: bool = False): frame_dicts = trace_dict.get("frames", []) if _raw_frames: - iter(frame_dicts) + _validate_raw_frames(frame_dicts) trace_entry = cls( action=trace_dict["action"], addr=int(trace_dict["addr"]), @@ -149,7 +156,7 @@ class Block: def from_dict(cls, block_dict: dict, _raw_frames: bool = False): frame_dicts = block_dict.get("frames", []) if _raw_frames: - iter(frame_dicts) + _validate_raw_frames(frame_dicts) block = cls( size=block_dict["size"], requested_size=block_dict["requested_size"], @@ -214,7 +221,7 @@ def from_dict( ): frame_dicts = segment_dict.get("frames", []) if _raw_frames: - iter(frame_dicts) + _validate_raw_frames(frame_dicts) segment = cls( address=segment_dict["address"], total_size=segment_dict["total_size"], diff --git a/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py b/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py index 7a57f63..6d4cd69 100644 --- a/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py +++ b/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py @@ -1,3 +1,5 @@ +import os +import tempfile from pathlib import Path from ...base import Block, BlockState, DeviceSnapshot, TraceEntry @@ -11,6 +13,7 @@ class SnapshotDbHandler: def __init__(self, db_path: str, devices: list[int], insert_cache_size: int = 1000): + self._closed = False self.db_path = db_path self.db = SnapshotDb(db_path) self._device_event_cache = {} @@ -61,9 +64,18 @@ def _do_insert_blocks(self, device: int = 0): self.db.conn.commit() self._device_block_cache[device].clear() - def __del__(self): - self.db.conn.commit() + def close(self, *, commit: bool = True): + if getattr(self, "_closed", True): + return + if commit: + self.db.conn.commit() + else: + self.db.conn.rollback() self.db.conn.close() + self._closed = True + + def __del__(self): + self.close() class DumpEventHooker(SimulateHooker, AllocatorHooker): @@ -119,6 +131,9 @@ def post_replay_free_block(self, released_block: Block, current_snapshot: Device def flush(self, device: int = 0): self.db_handler.flush(device) + def close(self, *, commit: bool = True): + self.db_handler.close(commit=commit) + def dump(pickle_file: str, dump_file: str, device=None) -> bool: try: @@ -138,23 +153,41 @@ def dump(pickle_file: str, dump_file: str, device=None) -> bool: if device is not None: need_dump_devices = [device] dump_logger.info(f"Recognized need to dump devices {need_dump_devices}.") - hooker = DumpEventHooker(dump_file, need_dump_devices) - for device in need_dump_devices: - dump_logger.info(f"Start to dump the snapshot to database for device {device}.") - _, replayed = replay_snapshot( - data, - device, - hooker=hooker, - allocator_hooker=hooker, - _raw_frames=True, + dump_path = Path(dump_file) + dump_path.parent.mkdir(parents=True, exist_ok=True) + fd, temporary_name = tempfile.mkstemp( + prefix=f".{dump_path.name}.", suffix=".tmp", dir=dump_path.parent + ) + os.close(fd) + temporary_path = Path(temporary_name) + temporary_path.unlink() + hooker = DumpEventHooker(str(temporary_path), need_dump_devices) + try: + for device in need_dump_devices: + dump_logger.info(f"Start to dump the snapshot to database for device {device}.") + _, replayed = replay_snapshot( + data, + device, + hooker=hooker, + allocator_hooker=hooker, + _raw_frames=True, + ) + if not replayed: + dump_logger.error(f"Failed to dump the snapshot to database for device {device}.") + return False + dump_logger.info(f"Finished dump the snapshot to database for device {device}.") + hooker.flush(device) + dump_logger.info( + f"Successfully dump the snapshot to database for devices {need_dump_devices}." ) - if not replayed: - dump_logger.error(f"Failed to dump the snapshot to database for device {device}.") - return False - dump_logger.info(f"Finished dump the snapshot to database for device {device}.") - hooker.flush(device) - dump_logger.info(f"Successfully dump the snapshot to database for devices {need_dump_devices}.") - return True + hooker.close() + os.replace(temporary_path, dump_path) + return True + finally: + try: + hooker.close(commit=False) + finally: + temporary_path.unlink(missing_ok=True) def run_dump_to_db( diff --git a/tests/snapshot/test_entities.py b/tests/snapshot/test_entities.py index 76d3aea..d152d41 100644 --- a/tests/snapshot/test_entities.py +++ b/tests/snapshot/test_entities.py @@ -134,7 +134,7 @@ def test_raw_frames_preserve_empty_missing_and_incomplete_behavior(self): with self.assertRaises(KeyError): TraceEntry.from_dict(incomplete) with self.assertRaises(KeyError): - TraceEntry.from_dict(incomplete, _raw_frames=True).get_callstack() + TraceEntry.from_dict(incomplete, _raw_frames=True) with self.assertRaises(TypeError): TraceEntry.from_dict({**base, "frames": None}, _raw_frames=True) diff --git a/tests/snapshot/test_snapshot2db_runtime.py b/tests/snapshot/test_snapshot2db_runtime.py index 5ff4e7c..67e13f5 100644 --- a/tests/snapshot/test_snapshot2db_runtime.py +++ b/tests/snapshot/test_snapshot2db_runtime.py @@ -146,6 +146,123 @@ def dump_rows(database, *, eager): assert any(row[-1] == "outer.py:20 outer\ninner.py:10 inner" for row in synthetic) +def test_fast_frame_import_rejects_malformed_frames_before_replay(monkeypatch, tmp_path): + representation = { + "segments": [], + "device_traces": [ + [ + { + "action": "segment_free", + "addr": 0x1000, + "size": 64, + "stream": 0, + "frames": [], + }, + { + "action": "segment_free", + "addr": 0x2000, + "size": 32, + "stream": 0, + "frames": [{"filename": "broken.py", "line": 3}], + }, + ] + ], + } + snapshot = tmp_path / "malformed.pkl" + database = tmp_path / "malformed.db" + with snapshot.open("wb") as stream: + pickle.dump(representation, stream) + + replayed_events = 0 + original_pre_undo_event = snapshot2db.DumpEventHooker.pre_undo_event + + def pre_undo_event(self, event, current_snapshot): + nonlocal replayed_events + replayed_events += 1 + return original_pre_undo_event(self, event, current_snapshot) + + monkeypatch.setattr(snapshot2db.DumpEventHooker, "pre_undo_event", pre_undo_event) + + with pytest.raises(KeyError, match="name"): + snapshot2db.dump(snapshot, database, 0) + + assert replayed_events == 0 + assert not database.exists() + + +def test_dump_all_removes_committed_devices_when_later_device_is_malformed(tmp_path): + representation = { + "segments": [], + "device_traces": [ + [ + { + "action": "segment_free", + "addr": 0x1000, + "size": 64, + "stream": 0, + "frames": [], + } + ], + [ + { + "action": "segment_free", + "addr": 0x2000, + "size": 32, + "stream": 0, + "frames": [{"filename": "broken.py", "line": 3}], + } + ], + ], + } + snapshot = tmp_path / "malformed-multi.pkl" + database = tmp_path / "malformed-multi.db" + with snapshot.open("wb") as stream: + pickle.dump(representation, stream) + + with pytest.raises(KeyError, match="name"): + snapshot2db.dump(snapshot, database) + + assert not database.exists() + + +def test_failed_dump_preserves_existing_database(tmp_path): + representation = { + "segments": [], + "device_traces": [ + [ + { + "action": "segment_free", + "addr": 0x1000, + "size": 64, + "stream": 0, + "frames": [{"filename": "broken.py", "line": 3}], + } + ] + ], + } + snapshot = tmp_path / "malformed-existing.pkl" + database = tmp_path / "existing.db" + with snapshot.open("wb") as stream: + pickle.dump(representation, stream) + with sqlite3.connect(database) as connection: + connection.execute("CREATE TABLE marker (value TEXT)") + connection.execute("INSERT INTO marker VALUES ('preserved')") + connection.commit() + + with pytest.raises(KeyError, match="name"): + snapshot2db.dump(snapshot, database, 0) + + with sqlite3.connect(database) as connection: + assert connection.execute("SELECT value FROM marker").fetchone() == ("preserved",) + + +def test_dump_creates_missing_destination_directories(tmp_path): + database = tmp_path / "missing" / "nested" / "snapshot.db" + + assert snapshot2db.dump(FIXTURE_DIR / "snapshot_with_empty_cache.pkl", database, 0) + assert database.is_file() + + def test_expandable_snapshot2db(dump_database): result, database = dump_database("snapshot_with_empty_cache_expandable.pkl", 0) diff --git a/tests/test_baseline_import.py b/tests/test_baseline_import.py new file mode 100644 index 0000000..2c88c96 --- /dev/null +++ b/tests/test_baseline_import.py @@ -0,0 +1,24 @@ +import pytest + +from benchmarks.baseline_import import _max_rss_kb, _parse_resource_output + + +def test_parse_resource_output_ignores_other_stderr_lines(): + metrics = _parse_resource_output( + "warning\nPT_SNAP_RESOURCE user=1.25 sys=0.5 max_rss_kb=2048.0\n" + ) + + assert metrics == {"user_s": 1.25, "sys_s": 0.5, "max_rss_kb": 2048.0} + + +def test_parse_resource_output_rejects_missing_metrics(): + with pytest.raises(RuntimeError, match="did not report"): + _parse_resource_output("warning only") + + with pytest.raises(RuntimeError, match="invalid benchmark resource output"): + _parse_resource_output("PT_SNAP_RESOURCE user=1.25 max_rss_kb=2048") + + +def test_max_rss_kb_normalizes_darwin_bytes_and_keeps_linux_kibibytes(): + assert _max_rss_kb(2 * 1024 * 1024, "darwin") == 2048 + assert _max_rss_kb(2048, "linux") == 2048