Skip to content

Commit 51f5258

Browse files
committed
[SYCL][XPTI] Revisit resource management strategy
1 parent e61dcc6 commit 51f5258

File tree

6 files changed

+182
-65
lines changed

6 files changed

+182
-65
lines changed

sycl/source/detail/pi.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ static void initializePlugins(std::vector<plugin> *Plugins) {
447447
}
448448

449449
#ifdef XPTI_ENABLE_INSTRUMENTATION
450+
GlobalHandler::instance().getXPTIRegistry().initializeFrameworkOnce();
451+
450452
if (!(xptiTraceEnabled() && !XPTIInitDone))
451453
return;
452454
// Not sure this is the best place to initialize the framework; SYCL runtime

sycl/source/detail/xpti_registry.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ inline constexpr const char *SYCL_PIDEBUGCALL_STREAM_NAME = "sycl.pi.debug";
3131

3232
class XPTIRegistry {
3333
public:
34+
void initializeFrameworkOnce() {
35+
#ifdef XPTI_ENABLE_INSTRUMENTATION
36+
if (!MInitialized) {
37+
xpti::result_t result = xptiFrameworkInitialize();
38+
if (result != xpti::result_t::XPTI_RESULT_SUCCESS) {
39+
throw std::runtime_error("Failed to initialize XPTI");
40+
}
41+
MInitialized = true;
42+
}
43+
#endif
44+
}
45+
3446
/// Notifies XPTI subscribers about new stream.
3547
///
3648
/// \param StreamName is a name of newly initialized stream.
@@ -50,11 +62,13 @@ class XPTIRegistry {
5062
for (const auto &StreamName : MActiveStreams) {
5163
xptiFinalize(StreamName.c_str());
5264
}
65+
xptiFrameworkFinalize();
5366
#endif // XPTI_ENABLE_INSTRUMENTATION
5467
}
5568

5669
private:
5770
std::unordered_set<std::string> MActiveStreams;
71+
bool MInitialized = false;
5872
};
5973
} // namespace detail
6074
} // namespace sycl

xpti/include/xpti_trace_framework.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
extern "C" {
3636

37+
XPTI_EXPORT_API xpti::result_t xptiFrameworkInitialize();
38+
XPTI_EXPORT_API xpti::result_t xptiFrameworkFinalize();
39+
3740
/// @brief Initialization function that is called when a new stream is generated
3841
/// @details When a runtime or application that uses XPTI instrumentation API
3942
/// starts to generate a new stream, a call to xptiInitialize() must be made to
@@ -413,6 +416,8 @@ XPTI_EXPORT_API void xptiReset();
413416
/// The proxy/stub library does not implement this function.
414417
XPTI_EXPORT_API void xptiForceSetTraceEnabled(bool yesOrNo);
415418

419+
typedef xpti::result_t (*xpti_framework_initialize_t)();
420+
typedef xpti::result_t (*xpti_framework_finalize_t)();
416421
typedef xpti::result_t (*xpti_initialize_t)(const char *, uint32_t, uint32_t,
417422
const char *);
418423
typedef void (*xpti_finalize_t)(const char *);

xpti/include/xpti_trace_framework.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//
88
#pragma once
9+
10+
#include <atomic>
911
#include <cstdint>
1012
#include <memory>
1113
#include <sstream>
@@ -268,6 +270,20 @@ class PlatformHelper {
268270
return false;
269271
}
270272
};
273+
274+
/// This is an implementation of a SpinLock synchronization primitive, that has
275+
/// trivial constructor and destructor.
276+
class SpinLock {
277+
public:
278+
void lock() {
279+
while (MLock.test_and_set(std::memory_order_acquire))
280+
std::this_thread::yield();
281+
}
282+
void unlock() { MLock.clear(std::memory_order_release); }
283+
284+
private:
285+
std::atomic_flag MLock = ATOMIC_FLAG_INIT;
286+
};
271287
} // namespace utils
272288

273289
namespace framework {

xpti/src/xpti_proxy.cpp

Lines changed: 89 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
//
88
#include "xpti_trace_framework.hpp"
99

10+
#include <atomic>
1011
#include <iostream>
1112
#include <memory>
1213
#include <unordered_map>
1314
#include <vector>
1415

1516
enum functions_t {
17+
XPTI_FRAMEWORK_INITIALIZE,
18+
XPTI_FRAMEWORK_FINALIZE,
1619
XPTI_INITIALIZE,
1720
XPTI_FINALIZE,
1821
XPTI_GET_UNIQUE_ID,
@@ -42,6 +45,8 @@ enum functions_t {
4245
namespace xpti {
4346
class ProxyLoader {
4447
std::unordered_map<int, const char *> m_function_names = {
48+
{XPTI_FRAMEWORK_INITIALIZE, "xptiFrameworkInitialize"},
49+
{XPTI_FRAMEWORK_FINALIZE, "xptiFrameworkFinalize"},
4550
{XPTI_INITIALIZE, "xptiInitialize"},
4651
{XPTI_FINALIZE, "xptiFinalize"},
4752
{XPTI_GET_UNIQUE_ID, "xptiGetUniqueId"},
@@ -116,26 +121,58 @@ class ProxyLoader {
116121
inline bool noErrors() { return m_loaded; }
117122

118123
void *functionByIndex(int index) {
119-
if (index >= XPTI_INITIALIZE && index < XPTI_FW_API_COUNT) {
124+
if (index >= XPTI_FRAMEWORK_INITIALIZE && index < XPTI_FW_API_COUNT) {
120125
return reinterpret_cast<void *>(m_dispatch_table[index]);
121126
}
122127
return nullptr;
123128
}
124129

130+
static ProxyLoader &instance() {
131+
static ProxyLoader *loader = new ProxyLoader();
132+
return *loader;
133+
}
134+
125135
private:
126136
bool m_loaded;
127137
xpti_plugin_handle_t m_fw_plugin_handle;
128138
dispatch_table_t m_dispatch_table;
129139
xpti::utils::PlatformHelper m_loader;
130140
};
131-
132-
static ProxyLoader g_loader;
133141
} // namespace xpti
134142

143+
XPTI_EXPORT_API xpti::result_t xptiFrameworkInitialize() {
144+
if (xpti::ProxyLoader::instance().noErrors()) {
145+
void *f = xpti::ProxyLoader::instance().functionByIndex(
146+
XPTI_FRAMEWORK_INITIALIZE);
147+
if (f) {
148+
return (*reinterpret_cast<xpti_framework_initialize_t>(f))();
149+
}
150+
}
151+
152+
return xpti::result_t::XPTI_RESULT_FAIL;
153+
}
154+
155+
XPTI_EXPORT_API xpti::result_t xptiFrameworkFinalize() {
156+
xpti::result_t result = xpti::result_t::XPTI_RESULT_FAIL;
157+
158+
if (xpti::ProxyLoader::instance().noErrors()) {
159+
void *f = xpti::ProxyLoader::instance().functionByIndex(
160+
XPTI_FRAMEWORK_INITIALIZE);
161+
if (f) {
162+
result = (*reinterpret_cast<xpti_framework_initialize_t>(f))();
163+
}
164+
}
165+
166+
delete &xpti::ProxyLoader::instance();
167+
168+
return result;
169+
}
170+
135171
XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedTracePoint(
136172
const char *tool_name, uint8_t user_defined_tp) {
137-
if (xpti::g_loader.noErrors()) {
138-
auto f = xpti::g_loader.functionByIndex(XPTI_REGISTER_USER_DEFINED_TP);
173+
if (xpti::ProxyLoader::instance().noErrors()) {
174+
auto f = xpti::ProxyLoader::instance().functionByIndex(
175+
XPTI_REGISTER_USER_DEFINED_TP);
139176
if (f) {
140177
return (*(xpti_register_user_defined_tp_t)f)(tool_name, user_defined_tp);
141178
}
@@ -145,8 +182,9 @@ XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedTracePoint(
145182

146183
XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedEventType(
147184
const char *tool_name, uint8_t user_defined_event) {
148-
if (xpti::g_loader.noErrors()) {
149-
auto f = xpti::g_loader.functionByIndex(XPTI_REGISTER_USER_DEFINED_ET);
185+
if (xpti::ProxyLoader::instance().noErrors()) {
186+
auto f = xpti::ProxyLoader::instance().functionByIndex(
187+
XPTI_REGISTER_USER_DEFINED_ET);
150188
if (f) {
151189
return (*(xpti_register_user_defined_et_t)f)(tool_name,
152190
user_defined_event);
@@ -158,8 +196,8 @@ XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedEventType(
158196
XPTI_EXPORT_API xpti::result_t xptiInitialize(const char *stream, uint32_t maj,
159197
uint32_t min,
160198
const char *version) {
161-
if (xpti::g_loader.noErrors()) {
162-
auto f = xpti::g_loader.functionByIndex(XPTI_INITIALIZE);
199+
if (xpti::ProxyLoader::instance().noErrors()) {
200+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_INITIALIZE);
163201
if (f) {
164202
return (*(xpti_initialize_t)f)(stream, maj, min, version);
165203
}
@@ -168,17 +206,17 @@ XPTI_EXPORT_API xpti::result_t xptiInitialize(const char *stream, uint32_t maj,
168206
}
169207

170208
XPTI_EXPORT_API void xptiFinalize(const char *stream) {
171-
if (xpti::g_loader.noErrors()) {
172-
auto f = xpti::g_loader.functionByIndex(XPTI_FINALIZE);
209+
if (xpti::ProxyLoader::instance().noErrors()) {
210+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_FINALIZE);
173211
if (f) {
174212
(*(xpti_finalize_t)f)(stream);
175213
}
176214
}
177215
}
178216

179217
XPTI_EXPORT_API uint64_t xptiGetUniqueId() {
180-
if (xpti::g_loader.noErrors()) {
181-
auto f = xpti::g_loader.functionByIndex(XPTI_GET_UNIQUE_ID);
218+
if (xpti::ProxyLoader::instance().noErrors()) {
219+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_GET_UNIQUE_ID);
182220
if (f) {
183221
return (*(xpti_get_unique_id_t)f)();
184222
}
@@ -188,8 +226,9 @@ XPTI_EXPORT_API uint64_t xptiGetUniqueId() {
188226

189227
XPTI_EXPORT_API xpti::string_id_t xptiRegisterString(const char *string,
190228
char **table_string) {
191-
if (xpti::g_loader.noErrors()) {
192-
auto f = xpti::g_loader.functionByIndex(XPTI_REGISTER_STRING);
229+
if (xpti::ProxyLoader::instance().noErrors()) {
230+
auto f =
231+
xpti::ProxyLoader::instance().functionByIndex(XPTI_REGISTER_STRING);
193232
if (f) {
194233
return (*(xpti_register_string_t)f)(string, table_string);
195234
}
@@ -198,8 +237,8 @@ XPTI_EXPORT_API xpti::string_id_t xptiRegisterString(const char *string,
198237
}
199238

200239
XPTI_EXPORT_API const char *xptiLookupString(xpti::string_id_t id) {
201-
if (xpti::g_loader.noErrors()) {
202-
auto f = xpti::g_loader.functionByIndex(XPTI_LOOKUP_STRING);
240+
if (xpti::ProxyLoader::instance().noErrors()) {
241+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_LOOKUP_STRING);
203242
if (f) {
204243
return (*(xpti_lookup_string_t)f)(id);
205244
}
@@ -208,8 +247,9 @@ XPTI_EXPORT_API const char *xptiLookupString(xpti::string_id_t id) {
208247
}
209248

210249
XPTI_EXPORT_API uint64_t xptiRegisterPayload(xpti::payload_t *payload) {
211-
if (xpti::g_loader.noErrors()) {
212-
auto f = xpti::g_loader.functionByIndex(XPTI_REGISTER_PAYLOAD);
250+
if (xpti::ProxyLoader::instance().noErrors()) {
251+
auto f =
252+
xpti::ProxyLoader::instance().functionByIndex(XPTI_REGISTER_PAYLOAD);
213253
if (f) {
214254
return (*(xpti_register_payload_t)f)(payload);
215255
}
@@ -218,8 +258,9 @@ XPTI_EXPORT_API uint64_t xptiRegisterPayload(xpti::payload_t *payload) {
218258
}
219259

220260
XPTI_EXPORT_API uint8_t xptiRegisterStream(const char *stream_name) {
221-
if (xpti::g_loader.noErrors()) {
222-
auto f = xpti::g_loader.functionByIndex(XPTI_REGISTER_STREAM);
261+
if (xpti::ProxyLoader::instance().noErrors()) {
262+
auto f =
263+
xpti::ProxyLoader::instance().functionByIndex(XPTI_REGISTER_STREAM);
223264
if (f) {
224265
return (*(xpti_register_stream_t)f)(stream_name);
225266
}
@@ -228,8 +269,9 @@ XPTI_EXPORT_API uint8_t xptiRegisterStream(const char *stream_name) {
228269
}
229270

230271
XPTI_EXPORT_API xpti::result_t xptiUnregisterStream(const char *stream_name) {
231-
if (xpti::g_loader.noErrors()) {
232-
auto f = xpti::g_loader.functionByIndex(XPTI_UNREGISTER_STREAM);
272+
if (xpti::ProxyLoader::instance().noErrors()) {
273+
auto f =
274+
xpti::ProxyLoader::instance().functionByIndex(XPTI_UNREGISTER_STREAM);
233275
if (f) {
234276
return (*(xpti_unregister_stream_t)f)(stream_name);
235277
}
@@ -239,8 +281,8 @@ XPTI_EXPORT_API xpti::result_t xptiUnregisterStream(const char *stream_name) {
239281
XPTI_EXPORT_API xpti::trace_event_data_t *
240282
xptiMakeEvent(const char *name, xpti::payload_t *payload, uint16_t event,
241283
xpti::trace_activity_type_t activity, uint64_t *instance_no) {
242-
if (xpti::g_loader.noErrors()) {
243-
auto f = xpti::g_loader.functionByIndex(XPTI_MAKE_EVENT);
284+
if (xpti::ProxyLoader::instance().noErrors()) {
285+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_MAKE_EVENT);
244286
if (f) {
245287
return (*(xpti_make_event_t)f)(name, payload, event, activity,
246288
instance_no);
@@ -250,8 +292,8 @@ xptiMakeEvent(const char *name, xpti::payload_t *payload, uint16_t event,
250292
}
251293

252294
XPTI_EXPORT_API const xpti::trace_event_data_t *xptiFindEvent(uint64_t uid) {
253-
if (xpti::g_loader.noErrors()) {
254-
auto f = xpti::g_loader.functionByIndex(XPTI_FIND_EVENT);
295+
if (xpti::ProxyLoader::instance().noErrors()) {
296+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_FIND_EVENT);
255297
if (f) {
256298
return (*(xpti_find_event_t)f)(uid);
257299
}
@@ -261,8 +303,8 @@ XPTI_EXPORT_API const xpti::trace_event_data_t *xptiFindEvent(uint64_t uid) {
261303

262304
XPTI_EXPORT_API const xpti::payload_t *
263305
xptiQueryPayload(xpti::trace_event_data_t *lookup_object) {
264-
if (xpti::g_loader.noErrors()) {
265-
auto f = xpti::g_loader.functionByIndex(XPTI_QUERY_PAYLOAD);
306+
if (xpti::ProxyLoader::instance().noErrors()) {
307+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_QUERY_PAYLOAD);
266308
if (f) {
267309
return (*(xpti_query_payload_t)f)(lookup_object);
268310
}
@@ -271,8 +313,9 @@ xptiQueryPayload(xpti::trace_event_data_t *lookup_object) {
271313
}
272314

273315
XPTI_EXPORT_API const xpti::payload_t *xptiQueryPayloadByUID(uint64_t uid) {
274-
if (xpti::g_loader.noErrors()) {
275-
auto f = xpti::g_loader.functionByIndex(XPTI_QUERY_PAYLOAD_BY_UID);
316+
if (xpti::ProxyLoader::instance().noErrors()) {
317+
auto f = xpti::ProxyLoader::instance().functionByIndex(
318+
XPTI_QUERY_PAYLOAD_BY_UID);
276319
if (f) {
277320
return (*(xpti_query_payload_by_uid_t)f)(uid);
278321
}
@@ -283,8 +326,9 @@ XPTI_EXPORT_API const xpti::payload_t *xptiQueryPayloadByUID(uint64_t uid) {
283326
XPTI_EXPORT_API xpti::result_t
284327
xptiRegisterCallback(uint8_t stream_id, uint16_t trace_type,
285328
xpti::tracepoint_callback_api_t cb) {
286-
if (xpti::g_loader.noErrors()) {
287-
auto f = xpti::g_loader.functionByIndex(XPTI_REGISTER_CALLBACK);
329+
if (xpti::ProxyLoader::instance().noErrors()) {
330+
auto f =
331+
xpti::ProxyLoader::instance().functionByIndex(XPTI_REGISTER_CALLBACK);
288332
if (f) {
289333
return (*(xpti_register_cb_t)f)(stream_id, trace_type, cb);
290334
}
@@ -295,8 +339,9 @@ xptiRegisterCallback(uint8_t stream_id, uint16_t trace_type,
295339
XPTI_EXPORT_API xpti::result_t
296340
xptiUnregisterCallback(uint8_t stream_id, uint16_t trace_type,
297341
xpti::tracepoint_callback_api_t cb) {
298-
if (xpti::g_loader.noErrors()) {
299-
auto f = xpti::g_loader.functionByIndex(XPTI_UNREGISTER_CALLBACK);
342+
if (xpti::ProxyLoader::instance().noErrors()) {
343+
auto f =
344+
xpti::ProxyLoader::instance().functionByIndex(XPTI_UNREGISTER_CALLBACK);
300345
if (f) {
301346
return (*(xpti_unregister_cb_t)f)(stream_id, trace_type, cb);
302347
}
@@ -309,8 +354,9 @@ xptiNotifySubscribers(uint8_t stream_id, uint16_t trace_type,
309354
xpti::trace_event_data_t *parent,
310355
xpti::trace_event_data_t *object, uint64_t instance,
311356
const void *user_data) {
312-
if (xpti::g_loader.noErrors()) {
313-
auto f = xpti::g_loader.functionByIndex(XPTI_NOTIFY_SUBSCRIBERS);
357+
if (xpti::ProxyLoader::instance().noErrors()) {
358+
auto f =
359+
xpti::ProxyLoader::instance().functionByIndex(XPTI_NOTIFY_SUBSCRIBERS);
314360
if (f) {
315361
return (*(xpti_notify_subscribers_t)f)(stream_id, trace_type, parent,
316362
object, instance, user_data);
@@ -320,8 +366,8 @@ xptiNotifySubscribers(uint8_t stream_id, uint16_t trace_type,
320366
}
321367

322368
XPTI_EXPORT_API bool xptiTraceEnabled() {
323-
if (xpti::g_loader.noErrors()) {
324-
auto f = xpti::g_loader.functionByIndex(XPTI_TRACE_ENABLED);
369+
if (xpti::ProxyLoader::instance().noErrors()) {
370+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_TRACE_ENABLED);
325371
if (f) {
326372
return (*(xpti_trace_enabled_t)f)();
327373
}
@@ -332,8 +378,8 @@ XPTI_EXPORT_API bool xptiTraceEnabled() {
332378
XPTI_EXPORT_API xpti::result_t xptiAddMetadata(xpti::trace_event_data_t *e,
333379
const char *key,
334380
const char *value) {
335-
if (xpti::g_loader.noErrors()) {
336-
auto f = xpti::g_loader.functionByIndex(XPTI_ADD_METADATA);
381+
if (xpti::ProxyLoader::instance().noErrors()) {
382+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_ADD_METADATA);
337383
if (f) {
338384
return (*(xpti_add_metadata_t)f)(e, key, value);
339385
}
@@ -343,8 +389,8 @@ XPTI_EXPORT_API xpti::result_t xptiAddMetadata(xpti::trace_event_data_t *e,
343389

344390
XPTI_EXPORT_API xpti::metadata_t *
345391
xptiQueryMetadata(xpti::trace_event_data_t *lookup_object) {
346-
if (xpti::g_loader.noErrors()) {
347-
auto f = xpti::g_loader.functionByIndex(XPTI_QUERY_METADATA);
392+
if (xpti::ProxyLoader::instance().noErrors()) {
393+
auto f = xpti::ProxyLoader::instance().functionByIndex(XPTI_QUERY_METADATA);
348394
if (f) {
349395
return (*(xpti_query_metadata_t)f)(lookup_object);
350396
}

0 commit comments

Comments
 (0)