Skip to content

Commit 2e156f7

Browse files
mfrancepilloiskbenzie
authored andcommitted
[EXP][CMDBUF] Add extra event to get CommandBuffer start time
Adds an extra event in the first command list associated to the CommandBuffer execution to obtain the start time of the graph execution.
1 parent 12a67f5 commit 2e156f7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

source/adapters/level_zero/command_buffer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,25 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
941941
ZE2UR_CALL(zeCommandListAppendBarrier,
942942
(SignalCommandList->first, RetEvent->ZeEvent, 1,
943943
&(CommandBuffer->SignalEvent->ZeEvent)));
944+
945+
if ((Queue->Properties & UR_QUEUE_FLAG_PROFILING_ENABLE)) {
946+
// We create an additional signal specific to the current execution of the
947+
// CommandBuffer. This signal is needed for profiling the execution time
948+
// of the CommandBuffer. It waits for the WaitEvent to be signaled
949+
// which indicates the start of the CommandBuffer actual execution.
950+
// This event is embedded into the Event return to the user to allow
951+
// the profiling engine to retrieve it.
952+
ur_event_handle_t StartEvent{};
953+
UR_CALL(createEventAndAssociateQueue(
954+
Queue, &StartEvent, UR_COMMAND_COMMAND_BUFFER_ENQUEUE_EXP,
955+
WaitCommandList, false));
956+
957+
ZE2UR_CALL(zeCommandListAppendBarrier,
958+
(WaitCommandList->first, StartEvent->ZeEvent, 1,
959+
&(CommandBuffer->WaitEvent->ZeEvent)));
960+
961+
RetEvent->CommandData = StartEvent;
962+
}
944963
}
945964

946965
// Execution our command-lists asynchronously

source/adapters/level_zero/event.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <mutex>
1414
#include <string.h>
1515

16+
#include "command_buffer.hpp"
1617
#include "common.hpp"
1718
#include "event.hpp"
1819
#include "ur_level_zero.hpp"
@@ -454,6 +455,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetProfilingInfo(
454455
///< bytes returned in propValue
455456
) {
456457
std::shared_lock<ur_shared_mutex> EventLock(Event->Mutex);
458+
459+
// A Command-buffer consists of three command-lists.
460+
// The start time should therefore be taken from an event associated
461+
// to the first command-list.
462+
if ((Event->CommandType == UR_COMMAND_COMMAND_BUFFER_ENQUEUE_EXP) &&
463+
(PropName == UR_PROFILING_INFO_COMMAND_START) && (Event->CommandData)) {
464+
auto StartEvent = static_cast<ur_event_handle_t>(Event->CommandData);
465+
return urEventGetProfilingInfo(StartEvent, UR_PROFILING_INFO_COMMAND_END,
466+
PropValueSize, PropValue, PropValueSizeRet);
467+
}
468+
457469
if (Event->UrQueue &&
458470
(Event->UrQueue->Properties & UR_QUEUE_FLAG_PROFILING_ENABLE) == 0) {
459471
return UR_RESULT_ERROR_PROFILING_INFO_NOT_AVAILABLE;
@@ -763,6 +775,13 @@ ur_result_t urEventReleaseInternal(ur_event_handle_t Event) {
763775
return Res;
764776
Event->CommandData = nullptr;
765777
}
778+
if (Event->CommandType == UR_COMMAND_COMMAND_BUFFER_ENQUEUE_EXP &&
779+
Event->CommandData) {
780+
// Free the memory extra event allocated for profiling purposed.
781+
auto AssociateEvent = static_cast<ur_event_handle_t>(Event->CommandData);
782+
urEventRelease(AssociateEvent);
783+
Event->CommandData = nullptr;
784+
}
766785
if (Event->OwnNativeHandle) {
767786
if (DisableEventsCaching) {
768787
auto ZeResult = ZE_CALL_NOCHECK(zeEventDestroy, (Event->ZeEvent));

0 commit comments

Comments
 (0)