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
22 changes: 17 additions & 5 deletions src/pt_snap_cli/core/dataset_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,20 @@ def _scope(
}


def _traces(sources: DatasetSourceResolver, low: int, high: int, items: list[SliceRecord]):
def _traces(
sources: DatasetSourceResolver,
low: int,
high: int,
items: list[SliceRecord],
columns: tuple[str, ...],
):
# Each built-in supplies only the fields it actually consumes. In particular,
# inline stack text must not spend the byte budget of a counter-only query.
projection = ", ".join(f'"{column}"' for column in columns)
for item in items:
rows = sources.read(
sources.dataset.root / item.file,
f'SELECT * FROM "trace_entry_{sources.device.device_id}" WHERE id>=0 AND id>=? AND id<=? ORDER BY id',
f'SELECT {projection} FROM "trace_entry_{sources.device.device_id}" WHERE id>=0 AND id>=? AND id<=? ORDER BY id',
[low, high],
)
yield item, rows
Expand All @@ -81,7 +90,7 @@ def _traces(sources: DatasetSourceResolver, low: int, high: int, items: list[Sli
def _peaks(sources, low, high, items, template, scope):
chosen: dict[str, dict[str, object]] = {}
count, first, last = 0, None, None
for _item, rows in _traces(sources, low, high, items):
for _item, rows in _traces(sources, low, high, items, ("id", *METRICS)):
for row in rows:
sources.budget.remaining()
event = cast(int, row["id"])
Expand Down Expand Up @@ -149,7 +158,10 @@ def _sort(rows: list[dict[str, object]], key: str, descending: bool) -> None:
def _events(sources, low, high, items, params, template, scope):
rows = []
first, last, count = None, None, 0
for _item, batch in _traces(sources, low, high, items):
columns = ("id", *METRICS)
if template == "event":
columns += ("action", "address", "size", "stream")
for _item, batch in _traces(sources, low, high, items, columns):
for row in batch:
sources.budget.remaining()
event = cast(int, row["id"])
Expand Down Expand Up @@ -287,7 +299,7 @@ def _lifecycles(sources, items, scope):
def _stacks(sources, low, high, items, params, scope):
grouped = {}
events = ordered = 0
for _item, batch in _traces(sources, low, high, items):
for _item, batch in _traces(sources, low, high, items, ("id",)):
refs = sources.events(cast(int, r["id"]) for r in batch)
for source in refs.values():
sources.budget.remaining()
Expand Down
54 changes: 54 additions & 0 deletions tests/core/test_dataset_projections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Per-template scan projections must not charge unrelated inline stack text."""

import sqlite3
import time
from contextlib import closing

import pytest

from pt_snap_cli.core.dataset_sources import QueryBudget
from pt_snap_cli.core.query_service import QueryService
from tests.test_dataset_focus import make_dataset


@pytest.mark.parametrize("template", ["memory_peak", "allocator_gap", "allocation"])
def test_counter_query_budget_is_independent_of_inline_stack_length(tmp_path, template):
root = make_dataset(tmp_path / "dataset", devices=(0,), slices=3)
observations = []
for stack in ("x" * 16, "x" * 8192, "x" * 32768, "栈🙂" * 8192):
for path in root.glob("device_0/*.db"):
with closing(sqlite3.connect(path)) as conn, conn:
conn.execute("UPDATE trace_entry_0 SET callstack=?", (stack,))
service = QueryService()
try:
budget = QueryBudget(None, time.monotonic(), max_work_bytes=8192)
result = service.execute_query(template, db_path=root, _budget=budget)
observations.append((result.rows, budget.work_rows, budget.work_bytes))
finally:
service.close()
assert all(item == observations[0] for item in observations)


@pytest.mark.parametrize("direction", ["ASC", "DESC"])
def test_event_projection_preserves_callstack_sort_and_paging(tmp_path, direction):
root = make_dataset(tmp_path / "dataset", devices=(0,), slices=3)
stacks = ["z" * 8192, None, "栈🙂" * 8192, "a" * 16384, "", "a" * 16384]
for index, stack in enumerate(stacks):
path = root / f"device_0/slice_{index // 2:05d}.db"
with closing(sqlite3.connect(path)) as conn, conn:
conn.execute("UPDATE trace_entry_0 SET callstack=? WHERE id=?", (stack, index))
service = QueryService()
try:
params = {"order_by": "callstack", "order_dir": direction, "offset": 1, "limit": 3}
result = service.execute_query("event", params, db_path=root, exact_total=True)
expected = sorted(
range(len(stacks)),
key=lambda event: (stacks[event] is not None, stacks[event], event),
reverse=direction == "DESC",
)[1:4]
assert [row["id"] for row in result.rows] == expected
assert [row["callstack"] for row in result.rows] == [stacks[event] for event in expected]
assert result.total == 6 and result.has_more and result.truncated
assert all(len(row) == 9 for row in result.rows)
finally:
service.close()
Loading