Skip to content

Record a high resolution timestamp for each allocation - #965

Closed
godlygeek wants to merge 6 commits into
bloomberg:mainfrom
godlygeek:record_allocation_timestamps
Closed

Record a high resolution timestamp for each allocation#965
godlygeek wants to merge 6 commits into
bloomberg:mainfrom
godlygeek:record_allocation_timestamps

Conversation

@godlygeek

Copy link
Copy Markdown
Contributor

This was originally included in #899, but was dropped when we realized these timestamps weren't actually necessary for the report we wanted to generate. Since we had already put in all the work to get the timestamp recording into a production-worthy state, I'm putting up this PR so that we can resurrect the timestamps if we ever do need them.

pablogsal and others added 6 commits July 20, 2026 01:16
Record monotonic timestamps for allocation and deallocation events in
all-allocation capture files, surface the data through the reader
metadata and Python API, and add a CLI flag to enable the feature.

The CLI rejects --allocation-timestamps with aggregated captures since
that format does not preserve per-event ordering.

Signed-off-by: Pablo Galindo Salgado <pablogsal@gmail.com>
Vacates record-type value 2 so that FRAME_PUSH can be relocated there in a
following change. Value 4 was previously unused, and the writer and reader
refer to the enumerator symbolically, so this is a pure renumbering with no
behavioral change.

Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
FRAME_PUSH only ever used a single flag bit (is_entry_frame), so it does not
need the 64-127 range with 6 flag bits. Relocate it to the 2-3 range (1 flag
bit) to free up 64-127 for a future record type.

The reader can no longer identify FRAME_PUSH by just a high bit, so
extractRecordTypeAndFlags now matches it with an exact (value & ~1) == 2 check,
placed after the higher-bit record types. Comments describing the encoding are
updated accordingly.

Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
Instead of appending a delta-encoded timestamp to every allocation record, the
streaming writer now emits a separate CLOCK_ADVANCED record whenever an
allocation's timestamp is higher than the last one emitted. The reader
accumulates these advances into a running clock and stamps each subsequent
allocation with the clock's current value.

CLOCK_ADVANCED occupies record-type values 64-127 (freed up by the earlier
FRAME_PUSH relocation). The 6 flag bits carry the advance in microseconds:
values 1-63 encode the advance directly with no extra bytes (the common case),
while 0 is a sentinel indicating the advance follows as an unsigned varint.

Allocations that share a microsecond now cost zero timestamp bytes, and small
advances cost a single byte. The Allocation/AllocationRecord structures are
unchanged.

Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
@godlygeek godlygeek closed this Jul 21, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.03540% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.23%. Comparing base (776fe15) to head (c3b938e).

Files with missing lines Patch % Lines
src/memray/_memray/record_writer.cpp 78.94% 4 Missing ⚠️
src/memray/_memray/record_reader.cpp 90.32% 3 Missing ⚠️
src/memray/_memray/tracking_api.cpp 86.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #965      +/-   ##
==========================================
+ Coverage   92.22%   92.23%   +0.01%     
==========================================
  Files          99       99              
  Lines       12556    12651      +95     
  Branches      432      434       +2     
==========================================
+ Hits        11580    11669      +89     
- Misses        976      982       +6     
Flag Coverage Δ
cpp 92.23% <92.03%> (+0.01%) ⬆️
python_and_cython 92.23% <92.03%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants