Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/pt_snap_cli/snapshot/PROVENANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ The upstream standalone split frontend is deliberately excluded, not deferred:
warn and stop later groups. `workspace_flag` remains a
fallback for unmatched workspace traces. Audited upstream source
mappings and license terms are unchanged.
- On 2026-08-19, issue #97 made OOM trace loading tolerate missing `addr`/`stream`,
read `device_free`, and mapped `"oom"` to action value `8` so import persists the
event as an integer. Replay still skips OOM. This is a local first-party bugfix;
audited upstream mappings and licensing evidence are unchanged.

## Migration toolchain

Expand Down
7 changes: 4 additions & 3 deletions src/pt_snap_cli/snapshot/base/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ def from_dict(cls, trace_dict: dict, _raw_frames: bool = False):
_validate_raw_frames(frame_dicts)
trace_entry = cls(
action=trace_dict["action"],
addr=int(trace_dict["addr"]),
size=int(trace_dict["size"]),
stream=int(trace_dict["stream"]),
addr=int(trace_dict.get("addr", -1)),
size=int(trace_dict.get("size", 0)),

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size 的默认值放宽得过头了。

addr/stream 缺失是 OOM 的已知特征,放宽合理;但 size 对所有 action 都是必需字段——OOM 事件里它恰恰是最关键的信息(请求了多少字节却失败了)。改成 .get("size", 0) 之后,一条畸形的 alloc(漏了 size)会静默变成 0 字节而不再抛 KeyError,掩盖数据损坏。

建议只对 OOM 放宽,或至少保留 size 为必需键:

is_oom = trace_dict["action"] == "oom"
...
addr=int(trace_dict["addr"]) if not is_oom else int(trace_dict.get("addr", -1)),
size=int(trace_dict["size"]),

stream=int(trace_dict.get("stream", 0)),
device_free=int(trace_dict.get("device_free", -1)),

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

device_free 解析出来后目前无人消费:全仓库除本行和新增测试外没有任何引用,snapshot2db 没有对应列,to_dict 的非 _origin 分支也不输出它。所以这个值只存在于内存对象里,测试断言的是一个暂时不可观测的字段。

本身不算错(补上了之前完全没解析的缺口),但如果后续要做 OOM 分析,值得单独开 issue 跟踪落库 + to_dict 输出。

_origin=trace_dict,
frames=[] if _raw_frames else [Frame.from_dict(frame) for frame in frame_dicts],
_raw_frames=frame_dicts if _raw_frames else None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"free_requested": 5,
"free_completed": 6,
"workspace_snapshot": 7,
"oom": 8,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

映射表仍缺 snapshot。

TraceEntry 的 docstring 列出了 snapshot action,但这张表里没有它。sqlite_meta.py 的转换是 value_map.get(value, value),未命中就把原始字符串直接写进 INTEGER 列——也就是本 PR 想消除的"写字符串而不是整数"的问题,在 snapshot 上依然存在,属于同一个 bug 的另一个入口。

建议在本 PR 顺手补一个值;或者更好的做法是给未命中 value_map 的 action 加一条显式告警/异常,避免下一次同类 issue 再以线上 import 失败的形式暴露。

}

BLOCK_STATE_VALUE_MAP = {
Expand Down
1 change: 1 addition & 0 deletions tests/snapshot/golden_observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"free_requested": 5,
"free_completed": 6,
"workspace_snapshot": 7,
"oom": 8,
}

BLOCK_STATE_VALUE_MAP = {
Expand Down
31 changes: 30 additions & 1 deletion tests/snapshot/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ def test_from_dict_without_frames(self):
self.assertEqual(trace.size, 2048)
self.assertEqual(len(trace.frames), 0)

def test_from_dict_loads_oom_without_addr_or_stream(self):
trace = TraceEntry.from_dict(
{"action": "oom", "size": 1024, "device_free": 2048, "frames": []}
)
self.assertEqual(trace.action, "oom")
self.assertEqual(trace.addr, -1)
self.assertEqual(trace.size, 1024)
self.assertEqual(trace.stream, 0)
self.assertEqual(trace.device_free, 2048)
self.assertEqual(trace.frames, [])

def test_from_dict_requires_action(self):
with self.assertRaises(KeyError):
TraceEntry.from_dict({"size": 1024, "frames": []})

def test_get_callstack(self):
trace = TraceEntry.from_dict(
{
Expand Down Expand Up @@ -395,7 +410,6 @@ def test_from_dict(self):
self.assertEqual(snapshot.total_activated, 2048)
self.assertEqual(snapshot.trace_entries[0].idx, 0)
self.assertEqual(snapshot.trace_entries[1].idx, 1)

self.assertTrue(

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:这里删掉了一个与本 PR 无关的空行,建议还原以保持 diff 干净。

all(
isinstance(frame, Frame)
Expand All @@ -404,6 +418,21 @@ def test_from_dict(self):
)
)

def test_from_dict_loads_oom_trace_without_addr(self):
snapshot = DeviceSnapshot.from_dict(
{
"segments": [],
"device_traces": [
[{"action": "oom", "size": 1024, "device_free": 2048, "frames": []}]
],
},
0,
)
self.assertEqual(len(snapshot.trace_entries), 1)
self.assertEqual(snapshot.trace_entries[0].action, "oom")
self.assertEqual(snapshot.trace_entries[0].addr, -1)
self.assertEqual(snapshot.trace_entries[0].device_free, 2048)

def test_find_overlapping_segment_returns_idx_and_matches_containing_segment(self):
snapshot = DeviceSnapshot()
snapshot.segments = [
Expand Down
15 changes: 15 additions & 0 deletions tests/snapshot/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,18 @@ def test_workspace_adapt_skips_when_segment_still_has_live_blocks(caplog):
assert segment.allocated_size == 1024
assert snapshot.device_snapshot.total_allocated == 1024
assert "still has 1 live block" in caplog.text


def test_simulate_replay_skips_oom_event_loaded_without_addr():
snapshot = SimulateDeviceSnapshot(
{
"segments": [],
"device_traces": [[{"action": "oom", "size": 1024, "device_free": 2048, "frames": []}]],
},
0,
)

assert [event.action for event in snapshot.device_snapshot.trace_entries] == ["oom"]
assert snapshot.device_snapshot.trace_entries[0].addr == -1
assert snapshot.replay() is True
assert snapshot.device_snapshot.trace_entries == []
30 changes: 30 additions & 0 deletions tests/snapshot/test_snapshot2db_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,36 @@ def test_empty_device_snapshot(tmp_path: Path):
assert not database.exists()


def test_snapshot2db_dumps_oom_event_without_addr(tmp_path: Path):
snapshot_file = tmp_path / "oom.pkl"
snapshot_file.write_bytes(
pickle.dumps(
{
"segments": [],
"device_traces": [
[{"action": "oom", "size": 1024, "device_free": 2048, "frames": []}]
],
}
)
)
database = tmp_path / "oom.db"
suppress_logs()
try:
result = snapshot2db.dump(snapshot_file, database, 0)
finally:
restore_logs()

assert result is True
with sqlite3.connect(database) as connection:
action, address, size = connection.execute(
"SELECT action, address, size FROM trace_entry_0"
).fetchone()

assert action == RUNTIME_ACTION_VALUE_MAP["oom"]
assert address == -1
assert size == 1024


def test_dump_all_multiple_device_snapshot(dump_database):
result, database = dump_database("snapshot_with_multi_devices.pkl")

Expand Down
Loading