@@ -29,36 +29,19 @@ constexpr auto STREAM_VER_MINOR = UR_MINOR_VERSION(UR_API_VERSION_CURRENT);
29
29
// Unfortunately this doesn't match the semantics of XPTI, which can be initialized
30
30
// and finalized exactly once. To workaround this, XPTI is globally initialized on
31
31
// 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 (); }
53
35
};
54
36
37
+ static std::shared_ptr<XptiContextManager> xptiContextManagerGlobal = [] {
38
+ return std::make_shared<XptiContextManager>();
39
+ }();
55
40
static thread_local xpti_td *activeEvent;
56
41
57
42
// /////////////////////////////////////////////////////////////////////////////
58
43
context_t::context_t () : logger(logger::create_logger(" tracing" , true , true )) {
59
- if (!XptiContext::running ()) {
60
- return ;
61
- }
44
+ this ->xptiContextManager = xptiContextManagerGlobal;
62
45
63
46
call_stream_id = xptiRegisterStream (CALL_STREAM_NAME);
64
47
std::ostringstream streamv;
@@ -69,20 +52,12 @@ context_t::context_t() : logger(logger::create_logger("tracing", true, true)) {
69
52
70
53
void context_t::notify (uint16_t trace_type, uint32_t id, const char *name,
71
54
void *args, ur_result_t *resultp, uint64_t instance) {
72
- if (!XptiContext::running ()) {
73
- return ;
74
- }
75
-
76
55
xpti::function_with_args_t payload{id, name, args, resultp, nullptr };
77
56
xptiNotifySubscribers (call_stream_id, trace_type, nullptr , activeEvent,
78
57
instance, &payload);
79
58
}
80
59
81
60
uint64_t context_t::notify_begin (uint32_t id, const char *name, void *args) {
82
- if (!XptiContext::running ()) {
83
- return 0 ;
84
- }
85
-
86
61
if (auto loc = codelocData.get_codeloc ()) {
87
62
xpti::payload_t payload =
88
63
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) {
101
76
102
77
void context_t::notify_end (uint32_t id, const char *name, void *args,
103
78
ur_result_t *resultp, uint64_t instance) {
104
- if (!XptiContext::running ()) {
105
- return ;
106
- }
107
-
108
79
notify ((uint16_t )xpti::trace_point_type_t ::function_with_args_end, id, name,
109
80
args, resultp, instance);
110
81
}
111
82
112
83
// /////////////////////////////////////////////////////////////////////////////
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); }
120
85
} // namespace ur_tracing_layer
0 commit comments