Skip to content

Commit 2005753

Browse files
gh-159207: Bound Tachyon replay memory on Linux and Windows (#162580)
1 parent 0b6ead6 commit 2005753

5 files changed

Lines changed: 262 additions & 102 deletions

File tree

‎InternalDocs/profiling_binary_format.md‎

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ network streams.
119119

120120
## Sample Data
121121

122-
Sample data begins at offset 64 and extends to `string_table_offset`. Samples
123-
are grouped into blocks of at most 1 MiB of decoded data. Within a block,
124-
samples use delta compression to minimize redundancy when consecutive samples
125-
from the same thread have identical or similar call stacks. Each block can be
126-
decoded on its own once the string and frame tables are loaded, so a reader
127-
only needs to decode one block at a time.
122+
Sample data begins at offset 64 and extends to `string_table_offset`. Each
123+
block has a header followed by its columns. The columns together take at most
124+
1 MiB on disk and at most 1 MiB when decoded. Within a block, samples use
125+
delta compression to minimize redundancy when consecutive samples from the
126+
same thread have identical or similar call stacks. Once the reader loads the
127+
string and frame tables, it can decode each block on its own.
128128

129129
### Block Format
130130

@@ -659,16 +659,25 @@ patterns benefit from kernel read-ahead.
659659

660660
The implementation uses `madvise()` to hint the access pattern to the kernel:
661661
`MADV_SEQUENTIAL` indicates the file will be read linearly, enabling
662-
aggressive read-ahead. `MADV_WILLNEED` requests pre-faulting of pages.
663-
On Linux, `MAP_POPULATE` pre-faults all pages at mmap time rather than on
664-
first access, moving page fault overhead from the parsing loop to the
665-
initial mapping for more predictable performance. For large files (over
666-
32 MB), `MADV_HUGEPAGE` requests transparent huge pages (2 MB instead of
667-
4 KB) to reduce TLB pressure when accessing large amounts of data.
668-
669-
On Windows, the implementation falls back to standard file I/O with full
670-
file buffering. Profiling data files are typically small enough (tens to
671-
hundreds of megabytes) that this is acceptable.
662+
aggressive read-ahead. For large files (over 32 MB), `MADV_HUGEPAGE` requests
663+
transparent huge pages (2 MB instead of 4 KB) to reduce TLB pressure when
664+
accessing large amounts of data. Pages are not loaded up front, only when
665+
replay reaches them.
666+
667+
On Linux, the reader also gives back memory it no longer needs. Once the
668+
string and frame tables have been parsed, and after each sample block has
669+
been replayed, it uses `MADV_DONTNEED` to tell the kernel those pages can be
670+
dropped. The mapping is read-only, so if a dropped page is needed again, the
671+
kernel reads it back from the file. This keeps the memory used for sample
672+
data small, however large the file is.
673+
674+
On Windows, the reader uses standard file I/O instead. When a file is opened,
675+
it only reads the header and the part after the sample data, which holds the
676+
string and frame tables, the statistics and the footer. During replay,
677+
samples are read into a reusable buffer. The reader requests the smaller of
678+
2 MiB and the sample section size. A complete block, including its header,
679+
fits in this buffer. When the buffer runs low, the bytes not yet
680+
replayed are moved to the start and the rest is filled from the file.
672681

673682
The writer buffers samples in the current block and writes each completed
674683
block with a single call, so individual samples never cause a write.

‎Lib/test/test_profiling/test_sampling_profiler/test_binary_format.py‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ def write_binary_file(self, samples, interval=1000, compression="none",
207207
collector.export(None)
208208
return filename, collector
209209

210+
def _read_offset(self, filename, hdr_off):
211+
with open(filename, "rb") as raw:
212+
raw.seek(hdr_off)
213+
return struct.unpack("=Q", raw.read(8))[0]
214+
210215
def read_block_columns(self, filename):
211216
"""Return the header flags and (decoded size, encoding) per column.
212217
@@ -1255,6 +1260,36 @@ def collect(self, stack_frames, timestamps_us):
12551260
):
12561261
self.assertIs(first_frame, second_frame)
12571262

1263+
def test_reader_replays_large_file_twice(self):
1264+
"""Replaying more sample data than the reader buffers is exact."""
1265+
class StackCollector:
1266+
def __init__(self):
1267+
self.stacks = []
1268+
1269+
def collect(self, stack_frames, timestamps_us):
1270+
for interp in stack_frames:
1271+
for thread in interp.threads:
1272+
stack = list(thread.frame_info)
1273+
self.stacks.extend([stack] * len(timestamps_us))
1274+
1275+
# Deep, shifting stacks give about 2.5 MB of sample data, more than
1276+
# the reader reads at once on platforms without mmap.
1277+
frames = [make_frame("deep.py", line, "f") for line in range(1356)]
1278+
stacks = [frames[i % 1100:i % 1100 + 256] for i in range(5000)]
1279+
samples = [[make_interpreter(0, [make_thread(1, stack)])]
1280+
for stack in stacks]
1281+
filename = self.create_binary_file(samples)
1282+
sample_size = (self._read_offset(filename, self.HDR_OFF_STR_TABLE)
1283+
- self.FILE_HEADER_PLACEHOLDER_SIZE)
1284+
self.assertGreater(sample_size, 2 * 1024 * 1024)
1285+
with BinaryReader(filename) as reader:
1286+
first = StackCollector()
1287+
second = StackCollector()
1288+
self.assertEqual(reader.replay_samples(first), len(samples))
1289+
self.assertEqual(reader.replay_samples(second), len(samples))
1290+
self.assertEqual(first.stacks, stacks)
1291+
self.assertEqual(second.stacks, stacks)
1292+
12581293
def test_writer_repeat_run_spans_interleaved_streams(self):
12591294
"""A thread's repeat run continues across other threads' samples."""
12601295
filename = self.temp_filename()
@@ -1649,11 +1684,6 @@ def test_replay_rejects_trailing_partial_sample_header(self):
16491684
MIN_STRING_ENTRY_SIZE = 1
16501685
MIN_FRAME_ENTRY_SIZE = 7
16511686

1652-
def _read_offset(self, filename, hdr_off):
1653-
with open(filename, "rb") as raw:
1654-
raw.seek(hdr_off)
1655-
return struct.unpack("=Q", raw.read(8))[0]
1656-
16571687
def _patch_footer_count(self, filename, ftr_off, value):
16581688
size = os.path.getsize(filename)
16591689
with open(filename, "r+b") as raw:
@@ -2051,6 +2081,8 @@ def test_replay_rejects_invalid_zstd_column(self):
20512081
("zstd frame size does not match the decoded size",
20522082
frame, len(instructions) + 1),
20532083
("zstd decompression error: ", bad_checksum, len(instructions)),
2084+
("Invalid sample block: stored columns exceed the block limit",
2085+
b"\x00" * (self.MAX_BLOCK_SIZE + 1), len(instructions)),
20542086
]
20552087
for message, stored, decoded_size in cases:
20562088
with self.subTest(message):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Reduce the memory used by :mod:`profiling.sampling` when replaying binary
2+
profiles on Linux and Windows. Sample data is now read one block at a time
3+
instead of being loaded into memory in full, so the memory used for samples
4+
stays small even for very long captures.

‎Modules/_remote_debugging/binary_io.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,13 @@ typedef struct {
459459
#if USE_MMAP
460460
uint8_t *mapped_data;
461461
size_t mapped_size;
462+
size_t page_size;
462463
#else
463464
FILE *fp;
464-
uint8_t *file_data;
465465
size_t file_size;
466+
uint8_t *table_data;
467+
ByteBuffer sample_window;
468+
size_t sample_window_start;
466469
#endif
467470

468471
/* Decompression state */

0 commit comments

Comments
 (0)