Skip to content

Commit da4b83c

Browse files
[SYCL][XPTI] Add unique queue ID to trace (#11548)
Signed-off-by: Tikhomirova, Kseniya <[email protected]>
1 parent c4e8e2a commit da4b83c

21 files changed

+676
-114
lines changed

sycl/doc/design/SYCLInstrumentationUsingXPTI.md

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

sycl/source/detail/queue_impl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
namespace sycl {
2727
inline namespace _V1 {
2828
namespace detail {
29+
30+
std::atomic<unsigned long long> queue_impl::MNextAvailableQueueID = 0;
31+
2932
template <>
3033
uint32_t queue_impl::get_info<info::queue::reference_count>() const {
3134
sycl::detail::pi::PiResult result = PI_SUCCESS;
@@ -75,6 +78,7 @@ event queue_impl::memset(const std::shared_ptr<detail::queue_impl> &Self,
7578
xpti::addMetadata(TEvent, "memory_ptr", reinterpret_cast<size_t>(Ptr));
7679
xpti::addMetadata(TEvent, "value_set", Value);
7780
xpti::addMetadata(TEvent, "memory_size", Count);
81+
xpti::addMetadata(TEvent, "queue_id", MQueueID);
7882
});
7983
// Notify XPTI about the memset submission
8084
PrepareNotify.notify();
@@ -152,6 +156,7 @@ event queue_impl::memcpy(const std::shared_ptr<detail::queue_impl> &Self,
152156
xpti::addMetadata(TEvent, "dest_memory_ptr",
153157
reinterpret_cast<size_t>(Dest));
154158
xpti::addMetadata(TEvent, "memory_size", Count);
159+
xpti::addMetadata(TEvent, "queue_id", MQueueID);
155160
});
156161
// Notify XPTI about the memset submission
157162
PrepareNotify.notify();
@@ -448,7 +453,7 @@ void *queue_impl::instrumentationProlog(const detail::code_location &CodeLoc,
448453
DevStr = "ACCELERATOR";
449454
else
450455
DevStr = "UNKNOWN";
451-
xpti::addMetadata(WaitEvent, "sycl_device", DevStr);
456+
xpti::addMetadata(WaitEvent, "sycl_device_type", DevStr);
452457
if (HasSourceInfo) {
453458
xpti::addMetadata(WaitEvent, "sym_function_name", CodeLoc.functionName());
454459
xpti::addMetadata(WaitEvent, "sym_source_file_name", CodeLoc.fileName());

sycl/source/detail/queue_impl.hpp

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -113,39 +113,9 @@ class queue_impl {
113113
has_property<ext::oneapi::property::queue::discard_events>()),
114114
MIsProfilingEnabled(has_property<property::queue::enable_profiling>()),
115115
MHasDiscardEventsSupport(MDiscardEvents &&
116-
(MHostQueue ? true : MIsInorder)) {
117-
// We enable XPTI tracing events using the TLS mechanism; if the code
118-
// location data is available, then the tracing data will be rich.
119-
#if XPTI_ENABLE_INSTRUMENTATION
120-
/// This section of code is relying on scoped objects, so they cannot be
121-
/// encapsulated in a function
122-
constexpr uint16_t NotificationTraceType =
123-
static_cast<uint16_t>(xpti::trace_point_type_t::queue_create);
124-
XPTIScope PrepareNotify((void *)this, NotificationTraceType,
125-
SYCL_STREAM_NAME, "queue_create");
126-
// Cache the trace event, stream id and instance IDs for the destructor
127-
if (xptiCheckTraceEnabled(PrepareNotify.streamID(),
128-
NotificationTraceType)) {
129-
MTraceEvent = (void *)PrepareNotify.traceEvent();
130-
MStreamID = PrepareNotify.streamID();
131-
MInstanceID = PrepareNotify.instanceID();
132-
// Add the function to capture meta data for the XPTI trace event
133-
PrepareNotify.addMetadata([&](auto TEvent) {
134-
xpti::addMetadata(TEvent, "sycl_context",
135-
reinterpret_cast<size_t>(MContext->getHandleRef()));
136-
if (MDevice) {
137-
xpti::addMetadata(TEvent, "sycl_device_name",
138-
MDevice->getDeviceName());
139-
xpti::addMetadata(
140-
TEvent, "sycl_device",
141-
reinterpret_cast<size_t>(
142-
MDevice->is_host() ? 0 : MDevice->getHandleRef()));
143-
}
144-
xpti::addMetadata(TEvent, "is_inorder", MIsInorder);
145-
});
146-
PrepareNotify.notify();
147-
}
148-
#endif
116+
(MHostQueue ? true : MIsInorder)),
117+
MQueueID{
118+
MNextAvailableQueueID.fetch_add(1, std::memory_order_relaxed)} {
149119
if (has_property<property::queue::enable_profiling>()) {
150120
if (has_property<ext::oneapi::property::queue::discard_events>())
151121
throw sycl::exception(make_error_code(errc::invalid),
@@ -196,28 +166,19 @@ class queue_impl {
196166
// This section is the second part of the instrumentation that uses the
197167
// tracepoint information and notifies
198168
}
199-
}
200-
201-
private:
202-
void queue_impl_interop(sycl::detail::pi::PiQueue PiQueue) {
203-
// The following commented section provides a guideline on how to use the
204-
// TLS enabled mechanism to create a tracepoint and notify using XPTI. This
205-
// is the prolog section and the epilog section will initiate the
206-
// notification.
169+
// We enable XPTI tracing events using the TLS mechanism; if the code
170+
// location data is available, then the tracing data will be rich.
207171
#if XPTI_ENABLE_INSTRUMENTATION
208-
/// This section of code is relying on scoped objects, so they cannot be
209-
/// encapsulated in a function
210172
constexpr uint16_t NotificationTraceType =
211173
static_cast<uint16_t>(xpti::trace_point_type_t::queue_create);
212174
XPTIScope PrepareNotify((void *)this, NotificationTraceType,
213175
SYCL_STREAM_NAME, "queue_create");
176+
// Cache the trace event, stream id and instance IDs for the destructor
214177
if (xptiCheckTraceEnabled(PrepareNotify.streamID(),
215178
NotificationTraceType)) {
216-
// Cache the trace event, stream id and instance IDs for the destructor
217179
MTraceEvent = (void *)PrepareNotify.traceEvent();
218180
MStreamID = PrepareNotify.streamID();
219181
MInstanceID = PrepareNotify.instanceID();
220-
221182
// Add the function to capture meta data for the XPTI trace event
222183
PrepareNotify.addMetadata([&](auto TEvent) {
223184
xpti::addMetadata(TEvent, "sycl_context",
@@ -231,10 +192,18 @@ class queue_impl {
231192
MDevice->is_host() ? 0 : MDevice->getHandleRef()));
232193
}
233194
xpti::addMetadata(TEvent, "is_inorder", MIsInorder);
195+
xpti::addMetadata(TEvent, "queue_id", MQueueID);
196+
if (!MHostQueue)
197+
xpti::addMetadata(TEvent, "queue_handle",
198+
reinterpret_cast<size_t>(getHandleRef()));
234199
});
235200
PrepareNotify.notify();
236201
}
237202
#endif
203+
}
204+
205+
private:
206+
void queue_impl_interop(sycl::detail::pi::PiQueue PiQueue) {
238207
if (has_property<ext::oneapi::property::queue::discard_events>() &&
239208
has_property<property::queue::enable_profiling>()) {
240209
throw sycl::exception(make_error_code(errc::invalid),
@@ -255,6 +224,42 @@ class queue_impl {
255224
make_error_code(errc::invalid),
256225
"Device provided by native Queue not found in Context.");
257226
}
227+
// The following commented section provides a guideline on how to use the
228+
// TLS enabled mechanism to create a tracepoint and notify using XPTI. This
229+
// is the prolog section and the epilog section will initiate the
230+
// notification.
231+
#if XPTI_ENABLE_INSTRUMENTATION
232+
constexpr uint16_t NotificationTraceType =
233+
static_cast<uint16_t>(xpti::trace_point_type_t::queue_create);
234+
XPTIScope PrepareNotify((void *)this, NotificationTraceType,
235+
SYCL_STREAM_NAME, "queue_create");
236+
if (xptiCheckTraceEnabled(PrepareNotify.streamID(),
237+
NotificationTraceType)) {
238+
// Cache the trace event, stream id and instance IDs for the destructor
239+
MTraceEvent = (void *)PrepareNotify.traceEvent();
240+
MStreamID = PrepareNotify.streamID();
241+
MInstanceID = PrepareNotify.instanceID();
242+
243+
// Add the function to capture meta data for the XPTI trace event
244+
PrepareNotify.addMetadata([&](auto TEvent) {
245+
xpti::addMetadata(TEvent, "sycl_context",
246+
reinterpret_cast<size_t>(MContext->getHandleRef()));
247+
if (MDevice) {
248+
xpti::addMetadata(TEvent, "sycl_device_name",
249+
MDevice->getDeviceName());
250+
xpti::addMetadata(
251+
TEvent, "sycl_device",
252+
reinterpret_cast<size_t>(
253+
MDevice->is_host() ? 0 : MDevice->getHandleRef()));
254+
}
255+
xpti::addMetadata(TEvent, "is_inorder", MIsInorder);
256+
xpti::addMetadata(TEvent, "queue_id", MQueueID);
257+
if (!MHostQueue)
258+
xpti::addMetadata(TEvent, "queue_handle", getHandleRef());
259+
});
260+
PrepareNotify.notify();
261+
}
262+
#endif
258263
}
259264

260265
public:
@@ -273,7 +278,9 @@ class queue_impl {
273278
has_property<ext::oneapi::property::queue::discard_events>()),
274279
MIsProfilingEnabled(has_property<property::queue::enable_profiling>()),
275280
MHasDiscardEventsSupport(MDiscardEvents &&
276-
(MHostQueue ? true : MIsInorder)) {
281+
(MHostQueue ? true : MIsInorder)),
282+
MQueueID{
283+
MNextAvailableQueueID.fetch_add(1, std::memory_order_relaxed)} {
277284
queue_impl_interop(PiQueue);
278285
}
279286

@@ -310,6 +317,7 @@ class queue_impl {
310317
(xpti::trace_event_data_t *)MTraceEvent,
311318
MInstanceID,
312319
static_cast<const void *>("queue_destroy"));
320+
xptiReleaseEvent((xpti::trace_event_data_t *)MTraceEvent);
313321
}
314322
#endif
315323
throw_asynchronous();
@@ -695,6 +703,8 @@ class queue_impl {
695703
return MGraph.lock();
696704
}
697705

706+
unsigned long long getQueueID() { return MQueueID; }
707+
698708
protected:
699709
// Hook to the scheduler to clean up any fusion command held on destruction.
700710
void cleanup_fusion_cmd();
@@ -890,6 +900,9 @@ class queue_impl {
890900
// recording commands to it.
891901
std::weak_ptr<ext::oneapi::experimental::detail::graph_impl> MGraph{};
892902

903+
unsigned long long MQueueID;
904+
static std::atomic<unsigned long long> MNextAvailableQueueID;
905+
893906
friend class sycl::ext::oneapi::experimental::detail::node_impl;
894907
};
895908

0 commit comments

Comments
 (0)