Skip to content

Commit dc8ec9f

Browse files
gh-160735: Fix Gecko marker end times during binary replay (#160738)
1 parent 922413e commit dc8ec9f

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎Lib/profiling/sampling/gecko_collector.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,9 @@ def collect(self, stack_frames, timestamps_us=None):
245245

246246
first_time = times[0]
247247

248-
# Update interval calculation
249-
if self.sample_count > 0 and self.last_sample_time > 0:
250-
self.interval = (times[-1] - self.last_sample_time) / self.sample_count
251-
self.last_sample_time = times[-1]
248+
# Binary replay can deliver batches from different threads out of
249+
# order, so keep the latest time seen rather than the last one.
250+
self.last_sample_time = max(self.last_sample_time, times[-1])
252251

253252
# Process async tasks
254253
if stack_frames and hasattr(stack_frames[0], "awaited_by"):
@@ -727,9 +726,6 @@ def _finalize_markers(self):
727726

728727
def export(self, filename):
729728
"""Export the profile to a Gecko JSON file."""
730-
if self.sample_count > 0 and self.last_sample_time > 0:
731-
self.interval = self.last_sample_time / self.sample_count
732-
733729
# Spinner for progress indication
734730
spinner = itertools.cycle(['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'])
735731
stop_spinner = threading.Event()
@@ -859,6 +855,8 @@ def _profile_tail(self):
859855
def _prepare_for_serialization(self):
860856
if self.exported:
861857
raise RuntimeError("GeckoCollector has already been exported")
858+
if self.sample_count > 0 and self.last_sample_time > 0:
859+
self.interval = self.last_sample_time / self.sample_count
862860
self._finalize_markers()
863861
for thread_data in self.threads.values():
864862
thread_data["_spill"].prepare_read()

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,36 @@ def test_gecko_collector_markers(self):
11761176
f"Marker {i} should have valid category",
11771177
)
11781178

1179+
def test_gecko_collector_replay_out_of_order_batches(self):
1180+
"""Open markers are closed at the latest replayed sample."""
1181+
collector = GeckoCollector(1000)
1182+
# Binary replay can send thread 2's last batch after thread 1's later
1183+
# samples
1184+
batches = [
1185+
(1, [1000], THREAD_STATUS_ON_CPU),
1186+
(2, [1500], 0),
1187+
(1, [2000], THREAD_STATUS_ON_CPU),
1188+
(1, [3000, 4000], 0),
1189+
(2, [2500], 0),
1190+
]
1191+
for tid, timestamps, status in batches:
1192+
frames = [MockFrameInfo(f"t{tid}.py", 10, f"func{tid}")]
1193+
collector.collect(
1194+
[MockInterpreterInfo(0, [MockThreadInfo(tid, frames, status=status)])],
1195+
timestamps_us=timestamps,
1196+
)
1197+
1198+
profile_data = collector._build_profile()
1199+
1200+
end_times = []
1201+
for thread_data in profile_data["threads"]:
1202+
markers = thread_data["markers"]
1203+
for start_time, end_time in zip(markers["startTime"],
1204+
markers["endTime"]):
1205+
self.assertLessEqual(start_time, end_time)
1206+
end_times.append(end_time)
1207+
self.assertEqual(max(end_times), 3.0)
1208+
11791209
def test_pstats_collector_export(self):
11801210
collector = PstatsCollector(
11811211
sample_interval_usec=1000000
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix Tachyon's Gecko export closing open markers too early when replaying a
2+
binary profile whose thread samples arrive out of time order.

0 commit comments

Comments
 (0)