@@ -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 ):
0 commit comments