Skip to content

Commit 8840488

Browse files
winstonzhang-intelbb-ur
authored andcommitted
Fix incorrect profiling timestamps when events are reused (#21086)
Fix issue where urEventGetProfilingInfo returns stale timestamps when profiling events are reused from cache. zeEventHostReset() does not clear profiling data, causing zeEventQueryKernelTimestamp() to return old timestamps. This patch includes these changes: - Prevents reusing events with profiling enabled from the cache, ensuring each profiling measurement uses a fresh event. - Fix reset() to clear timestamp fields. - COMMAND_START case for timestamped events to return correct start timestamp. --------- Signed-off-by: Zhang, Winston <winston.zhang@intel.com>
1 parent cc21edc commit 8840488

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

source/adapters/level_zero/context.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,13 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
597597
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
598598
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
599599
bool CounterBasedEventEnabled, bool InterruptBasedEventEnabled) {
600+
// Don't reuse events with profiling enabled because zeEventHostReset
601+
// does not clear the profiling timestamps, causing stale timestamp data
602+
// to be returned by zeEventQueryKernelTimestamp after the event is reused.
603+
if (WithProfiling) {
604+
return nullptr;
605+
}
606+
600607
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
601608
auto Cache =
602609
getEventCache(HostVisible, WithProfiling, Device,

source/adapters/level_zero/event.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,15 @@ EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
14271427
}
14281428

14291429
ur_result_t ur_event_handle_t_::reset() {
1430+
// Clean up timestamp recording entry from queue if this was a timestamped
1431+
// event
1432+
if (IsTimestamped && UrQueue) {
1433+
auto Entry = UrQueue->EndTimeRecordings.find(this);
1434+
if (Entry != UrQueue->EndTimeRecordings.end()) {
1435+
UrQueue->EndTimeRecordings.erase(Entry);
1436+
}
1437+
}
1438+
14301439
UrQueue = nullptr;
14311440
CleanedUp = false;
14321441
Completed = false;
@@ -1439,6 +1448,8 @@ ur_result_t ur_event_handle_t_::reset() {
14391448
completionBatch = std::nullopt;
14401449
OriginAllocEvent = nullptr;
14411450
IsTimestamped = false;
1451+
RecordEventStartTimestamp = 0;
1452+
RecordEventEndTimestamp = 0;
14421453

14431454
if (!isHostVisible())
14441455
HostVisibleEvent = nullptr;

test/adapters/level_zero/event_cache_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ TEST_P(urEventCacheTest, eventsReuseWithVisibleEvent) {
142142
verifyData();
143143
}
144144

145-
ASSERT_LT(eventCreateCount, numIters * numEnqueues);
145+
if (flags & UR_QUEUE_FLAG_PROFILING_ENABLE) {
146+
ASSERT_GE(eventCreateCount, numIters * numEnqueues);
147+
} else {
148+
ASSERT_LT(eventCreateCount, numIters * numEnqueues);
149+
}
146150
}
147151

148152
TEST_P(urEventCacheTest, eventsReuseWithVisibleEventAndWait) {

0 commit comments

Comments
 (0)