Skip to content

Commit 2f67b38

Browse files
Merge pull request #2000 from igchor/tracing_fix
[Loader] Fix xpti finalization order
2 parents 843034d + 7075a0a commit 2f67b38

File tree

2 files changed

+12
-43
lines changed

2 files changed

+12
-43
lines changed

source/loader/layers/tracing/ur_tracing_layer.cpp

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,19 @@ constexpr auto STREAM_VER_MINOR = UR_MINOR_VERSION(UR_API_VERSION_CURRENT);
2929
// Unfortunately this doesn't match the semantics of XPTI, which can be initialized
3030
// and finalized exactly once. To workaround this, XPTI is globally initialized on
3131
// first use and finalized in the destructor.
32-
class XptiContext {
33-
XptiContext() {
34-
xptiFrameworkInitialize();
35-
inited = true;
36-
}
37-
38-
~XptiContext() {
39-
xptiFrameworkFinalize();
40-
inited = false;
41-
}
42-
43-
// Accessing this after destruction is technically UB, but if we get there,
44-
// it means something is calling UR after it has been destroyed at program
45-
// exit.
46-
std::atomic_bool inited;
47-
48-
public:
49-
static bool running() {
50-
static XptiContext context;
51-
return context.inited;
52-
}
32+
struct XptiContextManager {
33+
XptiContextManager() { xptiFrameworkInitialize(); }
34+
~XptiContextManager() { xptiFrameworkFinalize(); }
5335
};
5436

37+
static std::shared_ptr<XptiContextManager> xptiContextManagerGlobal = [] {
38+
return std::make_shared<XptiContextManager>();
39+
}();
5540
static thread_local xpti_td *activeEvent;
5641

5742
///////////////////////////////////////////////////////////////////////////////
5843
context_t::context_t() : logger(logger::create_logger("tracing", true, true)) {
59-
if (!XptiContext::running()) {
60-
return;
61-
}
44+
this->xptiContextManager = xptiContextManagerGlobal;
6245

6346
call_stream_id = xptiRegisterStream(CALL_STREAM_NAME);
6447
std::ostringstream streamv;
@@ -69,20 +52,12 @@ context_t::context_t() : logger(logger::create_logger("tracing", true, true)) {
6952

7053
void context_t::notify(uint16_t trace_type, uint32_t id, const char *name,
7154
void *args, ur_result_t *resultp, uint64_t instance) {
72-
if (!XptiContext::running()) {
73-
return;
74-
}
75-
7655
xpti::function_with_args_t payload{id, name, args, resultp, nullptr};
7756
xptiNotifySubscribers(call_stream_id, trace_type, nullptr, activeEvent,
7857
instance, &payload);
7958
}
8059

8160
uint64_t context_t::notify_begin(uint32_t id, const char *name, void *args) {
82-
if (!XptiContext::running()) {
83-
return 0;
84-
}
85-
8661
if (auto loc = codelocData.get_codeloc()) {
8762
xpti::payload_t payload =
8863
xpti::payload_t(loc->functionName, loc->sourceFile, loc->lineNumber,
@@ -101,20 +76,10 @@ uint64_t context_t::notify_begin(uint32_t id, const char *name, void *args) {
10176

10277
void context_t::notify_end(uint32_t id, const char *name, void *args,
10378
ur_result_t *resultp, uint64_t instance) {
104-
if (!XptiContext::running()) {
105-
return;
106-
}
107-
10879
notify((uint16_t)xpti::trace_point_type_t::function_with_args_end, id, name,
10980
args, resultp, instance);
11081
}
11182

11283
///////////////////////////////////////////////////////////////////////////////
113-
context_t::~context_t() {
114-
if (!XptiContext::running()) {
115-
return;
116-
}
117-
118-
xptiFinalize(CALL_STREAM_NAME);
119-
}
84+
context_t::~context_t() { xptiFinalize(CALL_STREAM_NAME); }
12085
} // namespace ur_tracing_layer

source/loader/layers/tracing/ur_tracing_layer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#define TRACING_COMP_NAME "tracing layer"
2222

2323
namespace ur_tracing_layer {
24+
struct XptiContextManager;
25+
2426
///////////////////////////////////////////////////////////////////////////////
2527
class __urdlllocal context_t : public proxy_layer_context_t,
2628
public AtomicSingleton<context_t> {
@@ -47,6 +49,8 @@ class __urdlllocal context_t : public proxy_layer_context_t,
4749
uint8_t call_stream_id;
4850

4951
inline static const std::string name = "UR_LAYER_TRACING";
52+
53+
std::shared_ptr<XptiContextManager> xptiContextManager;
5054
};
5155

5256
context_t *getContext();

0 commit comments

Comments
 (0)