@@ -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+ }();
5540static thread_local xpti_td *activeEvent;
5641
5742// /////////////////////////////////////////////////////////////////////////////
5843context_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
7053void 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
8160uint64_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
10277void 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
0 commit comments