Skip to content

Commit

Permalink
fixup! Fix debugger output parsing in attach command
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal committed Nov 3, 2023
1 parent 42d27b4 commit 9cc9eb1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/memray/_memray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,9 @@ cdef class FileReader:
cdef HighWatermark _high_watermark
cdef object _header
cdef bool _report_progress
cdef size_t _memory_snapshot_bucket

def __cinit__(self, object file_name, *, bool report_progress=False):
def __cinit__(self, object file_name, *, bool report_progress=False, max_memory_records=10000):
try:
self._file = open(file_name)
except OSError as exc:
Expand All @@ -879,6 +880,9 @@ cdef class FileReader:
n_memory_snapshots_approx = 2048
if 0 < stats["start_time"] < stats["end_time"]:
n_memory_snapshots_approx = (stats["end_time"] - stats["start_time"]) / 10

if n_memory_snapshots_approx > max_memory_records:
n_memory_snapshots_approx = max_memory_records
self._memory_snapshots.reserve(n_memory_snapshots_approx)

cdef object total = stats['n_allocations'] or None
Expand All @@ -890,6 +894,7 @@ cdef class FileReader:
report_progress=self._report_progress
)
cdef MemoryRecord memory_record
cdef size_t snapshots_seen = 0
with progress_indicator:
while True:
PyErr_CheckSignals()
Expand All @@ -915,6 +920,10 @@ cdef class FileReader:
self._memory_snapshots.push_back(reader.getLatestMemorySnapshot())
else:
break

if len(self._memory_snapshots) > max_memory_records:
self._memory_snapshot_bucket = len(self._memory_snapshots) // max_memory_records
self._memory_snapshots = self._memory_snapshots[::self._memory_snapshot_bucket]
self._high_watermark = finder.getHighWatermark()
stats["n_allocations"] = progress_indicator.num_processed

Expand Down Expand Up @@ -1099,6 +1108,7 @@ cdef class FileReader:
cdef AllocationLifetimeAggregator aggregator
cdef _Allocation allocation

cdef size_t snapshots_seen = 0
with progress_indicator:
while records_to_process > 0:
PyErr_CheckSignals()
Expand All @@ -1111,6 +1121,8 @@ cdef class FileReader:
records_to_process -= 1
progress_indicator.update(1)
elif ret == RecordResult.RecordResultMemoryRecord:
if self._memory_snapshot_bucket and snapshots_seen % self._memory_snapshot_bucket != 0:
continue
aggregator.captureSnapshot()
else:
assert ret != RecordResult.RecordResultMemorySnapshot
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/test_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,30 @@ def test_memory_snapshots_tick_interval(self, tmp_path):
for prev, _next in zip(memory_snapshots, memory_snapshots[1:])
)

def test_memory_snapshots_limit_when_reading(self, tmp_path):
# GIVEN
allocator = MemoryAllocator()
output = tmp_path / "test.bin"

# WHEN
with Tracker(output):
allocator.valloc(ALLOC_SIZE)
time.sleep(0.11)
allocator.free()

memory_snapshots = list(FileReader(output).get_memory_snapshots())

assert memory_snapshots
assert all(record.rss > 0 for record in memory_snapshots)
assert any(record.heap >= ALLOC_SIZE for record in memory_snapshots)
assert sorted(memory_snapshots, key=lambda r: r.time) == memory_snapshots
assert all(
_next.time - prev.time >= 10
for prev, _next in zip(memory_snapshots, memory_snapshots[1:])
)

memory_snapshots = list(FileReader(output).get_memory_snapshots())

def test_temporary_allocations_when_filling_vector_without_preallocating(
self, tmp_path
):
Expand Down

0 comments on commit 9cc9eb1

Please sign in to comment.