@@ -60,6 +60,21 @@ uint64_t event_profiling_data_t::getEventEndTimestamp() {
6060 return adjustedEventEndTimestamp;
6161}
6262
63+ void event_profiling_data_t::reset () {
64+ // This ensures that the event is consider as not timestamped.
65+ // We can't touch the recordEventEndTimestamp
66+ // as it may still be overwritten by the driver.
67+ // In case event is resued and recordStartTimestamp
68+ // is called again, adjustedEventEndTimestamp will always be updated correctly
69+ // to the new value as we wait for the event to be signaled.
70+ // If the event is reused on another queue, this means that the original
71+ // queue must have been destroyed (and the even pool released back to the
72+ // context) and the timstamp is already wrriten, so there's no race-condition
73+ // possible.
74+ adjustedEventStartTimestamp = 0 ;
75+ adjustedEventEndTimestamp = 0 ;
76+ }
77+
6378void event_profiling_data_t::recordStartTimestamp (ur_device_handle_t hDevice) {
6479 zeTimerResolution = hDevice->ZeDeviceProperties ->timerResolution ;
6580 timestampMaxValue = hDevice->getTimestampMask ();
@@ -98,16 +113,22 @@ void ur_event_handle_t_::resetQueueAndCommand(ur_queue_t_ *hQueue,
98113 ur_command_t commandType) {
99114 this ->hQueue = hQueue;
100115 this ->commandType = commandType;
101- profilingData = event_profiling_data_t (getZeEvent ());
116+
117+ if (hQueue) {
118+ UR_CALL_THROWS (hQueue->queueGetInfo (UR_QUEUE_INFO_DEVICE, sizeof (hDevice),
119+ reinterpret_cast <void *>(&hDevice),
120+ nullptr ));
121+ } else {
122+ hDevice = nullptr ;
123+ }
124+
125+ profilingData.reset ();
102126}
103127
104128void ur_event_handle_t_::recordStartTimestamp () {
105- assert (hQueue); // queue must be set before calling this
106-
107- ur_device_handle_t hDevice;
108- UR_CALL_THROWS (hQueue->queueGetInfo (UR_QUEUE_INFO_DEVICE, sizeof (hDevice),
109- reinterpret_cast <void *>(&hDevice),
110- nullptr ));
129+ // queue and device must be set before calling this
130+ assert (hQueue);
131+ assert (hDevice);
111132
112133 profilingData.recordStartTimestamp (hDevice);
113134}
@@ -141,33 +162,17 @@ ur_result_t ur_event_handle_t_::retain() {
141162 return UR_RESULT_SUCCESS;
142163}
143164
144- ur_result_t ur_event_handle_t_::releaseDeferred () {
145- assert (zeEventQueryStatus (getZeEvent ()) == ZE_RESULT_SUCCESS);
146- assert (RefCount.load () == 0 );
147-
148- return this ->forceRelease ();
149- }
150-
151165ur_result_t ur_event_handle_t_::release () {
152166 if (!RefCount.decrementAndTest ())
153167 return UR_RESULT_SUCCESS;
154168
155- // Need to take a lock before checking if the event is timestamped.
156- std::unique_lock<ur_shared_mutex> lock (Mutex);
157-
158- if (isTimestamped () && !getEventEndTimestamp ()) {
159- // L0 will write end timestamp to this event some time in the future,
160- // so we can't release it yet.
161- assert (hQueue);
162- hQueue->deferEventFree (this );
163- return UR_RESULT_SUCCESS;
169+ if (event_pool) {
170+ event_pool->free (this );
171+ } else {
172+ std::get<v2::raii::ze_event_handle_t >(hZeEvent).release ();
173+ delete this ;
164174 }
165-
166- // Need to unlock now, as forceRelease might deallocate memory backing
167- // the Mutex.
168- lock.unlock ();
169-
170- return this ->forceRelease ();
175+ return UR_RESULT_SUCCESS;
171176}
172177
173178bool ur_event_handle_t_::isTimestamped () const {
@@ -189,6 +194,8 @@ ur_context_handle_t ur_event_handle_t_::getContext() const { return hContext; }
189194
190195ur_command_t ur_event_handle_t_::getCommandType () const { return commandType; }
191196
197+ ur_device_handle_t ur_event_handle_t_::getDevice () const { return hDevice; }
198+
192199ur_event_handle_t_::ur_event_handle_t_ (
193200 ur_context_handle_t hContext,
194201 v2::raii::cache_borrowed_event eventAllocation, v2::event_pool *pool)
@@ -209,16 +216,6 @@ ur_event_handle_t_::ur_event_handle_t_(
209216 ,
210217 nullptr ) {}
211218
212- ur_result_t ur_event_handle_t_::forceRelease () {
213- if (event_pool) {
214- event_pool->free (this );
215- } else {
216- std::get<v2::raii::ze_event_handle_t >(hZeEvent).release ();
217- delete this ;
218- }
219- return UR_RESULT_SUCCESS;
220- }
221-
222219namespace ur ::level_zero {
223220ur_result_t urEventRetain (ur_event_handle_t hEvent) try {
224221 return hEvent->retain ();
@@ -323,19 +320,14 @@ ur_result_t urEventGetProfilingInfo(
323320 }
324321 }
325322
326- auto hQueue = hEvent->getQueue ();
327- if (!hQueue ) {
323+ auto hDevice = hEvent->getDevice ();
324+ if (!hDevice ) {
328325 // no command has been enqueued with this event yet
329326 return UR_RESULT_ERROR_PROFILING_INFO_NOT_AVAILABLE;
330327 }
331328
332329 ze_kernel_timestamp_result_t tsResult;
333330
334- ur_device_handle_t hDevice;
335- UR_CALL_THROWS (hQueue->queueGetInfo (UR_QUEUE_INFO_DEVICE, sizeof (hDevice),
336- reinterpret_cast <void *>(&hDevice),
337- nullptr ));
338-
339331 auto zeTimerResolution = hDevice->ZeDeviceProperties ->timerResolution ;
340332 auto timestampMaxValue = hDevice->getTimestampMask ();
341333
0 commit comments