13 #include <unordered_set>
16 #include <sycl/version.hpp>
18 #ifdef XPTI_ENABLE_INSTRUMENTATION
21 #include "xpti/xpti_trace_framework.hpp"
36 "sycl.experimental.mem_alloc";
38 #ifdef XPTI_ENABLE_INSTRUMENTATION
39 extern uint8_t GBufferStreamID;
40 extern uint8_t GMemAllocStreamID;
41 extern xpti::trace_event_data_t *GMemAllocEvent;
42 extern xpti::trace_event_data_t *GSYCLGraphEvent;
45 #define SYCL_VERSION_STR \
46 "sycl " STR(__LIBSYCL_MAJOR_VERSION) "." STR(__LIBSYCL_MINOR_VERSION)
50 constexpr uint32_t
GMajVer = __LIBSYCL_MAJOR_VERSION;
51 constexpr uint32_t
GMinVer = __LIBSYCL_MINOR_VERSION;
52 constexpr
const char *
GVerStr = SYCL_VERSION_STR;
57 "sycl.experimental.buffer";
62 #ifdef XPTI_ENABLE_INSTRUMENTATION
63 std::call_once(MInitialized, [
this] {
64 xptiFrameworkInitialize();
72 xpti::payload_t MAPayload(
"SYCL Memory Allocations Layer");
73 uint64_t MAInstanceNo = 0;
74 GMemAllocEvent = xptiMakeEvent(
"SYCL Memory Allocations", &MAPayload,
75 xpti::trace_algorithm_event,
76 xpti_at::active, &MAInstanceNo);
88 uint32_t MinVer,
const std::string &VerStr) {
89 #ifdef XPTI_ENABLE_INSTRUMENTATION
90 MActiveStreams.insert(StreamName);
91 xptiInitialize(StreamName.c_str(), MajVer, MinVer, VerStr.c_str());
92 #endif // XPTI_ENABLE_INSTRUMENTATION
96 #ifdef XPTI_ENABLE_INSTRUMENTATION
97 for (
const auto &StreamName : MActiveStreams) {
98 xptiFinalize(StreamName.c_str());
100 xptiFrameworkFinalize();
101 #endif // XPTI_ENABLE_INSTRUMENTATION
104 static void bufferConstructorNotification(
const void *,
106 const void *,
const void *,
107 uint32_t, uint32_t,
size_t[3]);
108 static void bufferAssociateNotification(
const void *,
const void *);
109 static void bufferReleaseNotification(
const void *,
const void *);
110 static void bufferDestructorNotification(
const void *);
111 static void bufferAccessorNotification(
const void *,
const void *, uint32_t,
116 std::unordered_set<std::string> MActiveStreams;
117 std::once_flag MInitialized;
119 #ifdef XPTI_ENABLE_INSTRUMENTATION
120 static xpti::trace_event_data_t *
121 createTraceEvent(
const void *Obj,
const void *ObjName, uint64_t &IId,
123 uint16_t TraceEventType);
124 #endif // XPTI_ENABLE_INSTRUMENTATION
130 #if XPTI_ENABLE_INSTRUMENTATION
133 using TracePoint = xpti::framework::tracepoint_t;
141 XPTIScope(
void *CodePtr, uint16_t TraceType,
const char *StreamName,
142 const char *UserData)
143 : MUserData(UserData), MStreamID(0), MInstanceID(0), MScopedNotify(false),
146 auto TData = Tls.
query();
149 const char *FuncName = UserData;
150 if (TData.functionName())
153 MTP =
new TracePoint(TData.fileName(), FuncName, TData.lineNumber(),
154 TData.columnNumber(), CodePtr);
155 if (MTraceType == (uint16_t)xpti::trace_point_type_t::graph_create ||
156 MTraceType == (uint16_t)xpti::trace_point_type_t::node_create ||
157 MTraceType == (uint16_t)xpti::trace_point_type_t::edge_create)
158 MTP->parent_event(GSYCLGraphEvent);
160 if (xptiTraceEnabled() && MTP) {
161 MTP->stream(StreamName).trace_type((xpti::trace_point_type_t)TraceType);
162 MTraceEvent =
const_cast<xpti::trace_event_data_t *
>(MTP->trace_event());
163 MStreamID = MTP->stream_id();
164 MInstanceID = MTP->instance_id();
168 xpti::trace_event_data_t *traceEvent() {
return MTraceEvent; }
170 uint8_t streamID() {
return MStreamID; }
172 uint64_t instanceID() {
return MTP ? MTP->instance_id() : 0; }
175 addMetadata(
const std::function<
void(xpti::trace_event_data_t *)> &Callback) {
176 if (xptiTraceEnabled() && MTP) {
177 auto TEvent =
const_cast<xpti::trace_event_data_t *
>(MTP->trace_event());
183 XPTIScope ¬ify() {
184 MTP->notify(
static_cast<const void *
>(MUserData));
190 XPTIScope &scopedNotify(uint16_t TraceType) {
191 if (xptiTraceEnabled() && MTP) {
192 MTraceType = TraceType & 0xfffe;
193 MScopedNotify =
true;
194 xptiNotifySubscribers(MStreamID, MTraceType,
nullptr, MTraceEvent,
195 MInstanceID,
static_cast<const void *
>(MUserData));
200 if (xptiTraceEnabled() && MTP && MScopedNotify) {
201 if (MTraceType == (uint16_t)xpti::trace_point_type_t::signal ||
202 MTraceType == (uint16_t)xpti::trace_point_type_t::graph_create ||
203 MTraceType == (uint16_t)xpti::trace_point_type_t::node_create ||
204 MTraceType == (uint16_t)xpti::trace_point_type_t::edge_create ||
205 MTraceType == (uint16_t)xpti::trace_point_type_t::diagnostics)
213 if (MTraceType == (uint16_t)xpti::trace_point_type_t::mem_alloc_begin) {
214 MTraceType = (uint16_t)xpti::trace_point_type_t::mem_alloc_end;
215 }
else if (MTraceType ==
216 (uint16_t)xpti::trace_point_type_t::mem_release_begin) {
217 MTraceType = (uint16_t)xpti::trace_point_type_t::mem_release_end;
218 }
else if (MTraceType ==
219 (uint16_t)xpti::trace_point_type_t::offload_alloc_construct) {
220 MTraceType = (uint16_t)xpti::trace_point_type_t::offload_alloc_destruct;
222 MTraceType = MTraceType | 1;
225 xptiNotifySubscribers(MStreamID, MTraceType,
nullptr, MTraceEvent,
226 MInstanceID,
static_cast<const void *
>(MUserData));
235 TracePoint *MTP =
nullptr;
237 xpti::trace_event_data_t *MTraceEvent =
nullptr;
239 const char *MUserData =
nullptr;
244 uint64_t MInstanceID;