7
7
//
8
8
#include " xpti_trace_framework.hpp"
9
9
10
+ #include < atomic>
10
11
#include < iostream>
11
12
#include < memory>
12
13
#include < unordered_map>
13
14
#include < vector>
14
15
15
16
enum functions_t {
17
+ XPTI_FRAMEWORK_INITIALIZE,
18
+ XPTI_FRAMEWORK_FINALIZE,
16
19
XPTI_INITIALIZE,
17
20
XPTI_FINALIZE,
18
21
XPTI_GET_UNIQUE_ID,
@@ -42,6 +45,8 @@ enum functions_t {
42
45
namespace xpti {
43
46
class ProxyLoader {
44
47
std::unordered_map<int , const char *> m_function_names = {
48
+ {XPTI_FRAMEWORK_INITIALIZE, " xptiFrameworkInitialize" },
49
+ {XPTI_FRAMEWORK_FINALIZE, " xptiFrameworkFinalize" },
45
50
{XPTI_INITIALIZE, " xptiInitialize" },
46
51
{XPTI_FINALIZE, " xptiFinalize" },
47
52
{XPTI_GET_UNIQUE_ID, " xptiGetUniqueId" },
@@ -116,26 +121,58 @@ class ProxyLoader {
116
121
inline bool noErrors () { return m_loaded; }
117
122
118
123
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) {
120
125
return reinterpret_cast <void *>(m_dispatch_table[index]);
121
126
}
122
127
return nullptr ;
123
128
}
124
129
130
+ static ProxyLoader &instance () {
131
+ static ProxyLoader *loader = new ProxyLoader ();
132
+ return *loader;
133
+ }
134
+
125
135
private:
126
136
bool m_loaded;
127
137
xpti_plugin_handle_t m_fw_plugin_handle;
128
138
dispatch_table_t m_dispatch_table;
129
139
xpti::utils::PlatformHelper m_loader;
130
140
};
131
-
132
- static ProxyLoader g_loader;
133
141
} // namespace xpti
134
142
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
+
135
171
XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedTracePoint (
136
172
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);
139
176
if (f) {
140
177
return (*(xpti_register_user_defined_tp_t )f)(tool_name, user_defined_tp);
141
178
}
@@ -145,8 +182,9 @@ XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedTracePoint(
145
182
146
183
XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedEventType (
147
184
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);
150
188
if (f) {
151
189
return (*(xpti_register_user_defined_et_t )f)(tool_name,
152
190
user_defined_event);
@@ -158,8 +196,8 @@ XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedEventType(
158
196
XPTI_EXPORT_API xpti::result_t xptiInitialize (const char *stream, uint32_t maj,
159
197
uint32_t min,
160
198
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);
163
201
if (f) {
164
202
return (*(xpti_initialize_t )f)(stream, maj, min, version);
165
203
}
@@ -168,17 +206,17 @@ XPTI_EXPORT_API xpti::result_t xptiInitialize(const char *stream, uint32_t maj,
168
206
}
169
207
170
208
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);
173
211
if (f) {
174
212
(*(xpti_finalize_t )f)(stream);
175
213
}
176
214
}
177
215
}
178
216
179
217
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);
182
220
if (f) {
183
221
return (*(xpti_get_unique_id_t )f)();
184
222
}
@@ -188,8 +226,9 @@ XPTI_EXPORT_API uint64_t xptiGetUniqueId() {
188
226
189
227
XPTI_EXPORT_API xpti::string_id_t xptiRegisterString (const char *string,
190
228
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);
193
232
if (f) {
194
233
return (*(xpti_register_string_t )f)(string, table_string);
195
234
}
@@ -198,8 +237,8 @@ XPTI_EXPORT_API xpti::string_id_t xptiRegisterString(const char *string,
198
237
}
199
238
200
239
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);
203
242
if (f) {
204
243
return (*(xpti_lookup_string_t )f)(id);
205
244
}
@@ -208,8 +247,9 @@ XPTI_EXPORT_API const char *xptiLookupString(xpti::string_id_t id) {
208
247
}
209
248
210
249
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);
213
253
if (f) {
214
254
return (*(xpti_register_payload_t )f)(payload);
215
255
}
@@ -218,8 +258,9 @@ XPTI_EXPORT_API uint64_t xptiRegisterPayload(xpti::payload_t *payload) {
218
258
}
219
259
220
260
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);
223
264
if (f) {
224
265
return (*(xpti_register_stream_t )f)(stream_name);
225
266
}
@@ -228,8 +269,9 @@ XPTI_EXPORT_API uint8_t xptiRegisterStream(const char *stream_name) {
228
269
}
229
270
230
271
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);
233
275
if (f) {
234
276
return (*(xpti_unregister_stream_t )f)(stream_name);
235
277
}
@@ -239,8 +281,8 @@ XPTI_EXPORT_API xpti::result_t xptiUnregisterStream(const char *stream_name) {
239
281
XPTI_EXPORT_API xpti::trace_event_data_t *
240
282
xptiMakeEvent (const char *name, xpti::payload_t *payload, uint16_t event,
241
283
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);
244
286
if (f) {
245
287
return (*(xpti_make_event_t )f)(name, payload, event, activity,
246
288
instance_no);
@@ -250,8 +292,8 @@ xptiMakeEvent(const char *name, xpti::payload_t *payload, uint16_t event,
250
292
}
251
293
252
294
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);
255
297
if (f) {
256
298
return (*(xpti_find_event_t )f)(uid);
257
299
}
@@ -261,8 +303,8 @@ XPTI_EXPORT_API const xpti::trace_event_data_t *xptiFindEvent(uint64_t uid) {
261
303
262
304
XPTI_EXPORT_API const xpti::payload_t *
263
305
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);
266
308
if (f) {
267
309
return (*(xpti_query_payload_t )f)(lookup_object);
268
310
}
@@ -271,8 +313,9 @@ xptiQueryPayload(xpti::trace_event_data_t *lookup_object) {
271
313
}
272
314
273
315
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);
276
319
if (f) {
277
320
return (*(xpti_query_payload_by_uid_t )f)(uid);
278
321
}
@@ -283,8 +326,9 @@ XPTI_EXPORT_API const xpti::payload_t *xptiQueryPayloadByUID(uint64_t uid) {
283
326
XPTI_EXPORT_API xpti::result_t
284
327
xptiRegisterCallback (uint8_t stream_id, uint16_t trace_type,
285
328
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);
288
332
if (f) {
289
333
return (*(xpti_register_cb_t )f)(stream_id, trace_type, cb);
290
334
}
@@ -295,8 +339,9 @@ xptiRegisterCallback(uint8_t stream_id, uint16_t trace_type,
295
339
XPTI_EXPORT_API xpti::result_t
296
340
xptiUnregisterCallback (uint8_t stream_id, uint16_t trace_type,
297
341
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);
300
345
if (f) {
301
346
return (*(xpti_unregister_cb_t )f)(stream_id, trace_type, cb);
302
347
}
@@ -309,8 +354,9 @@ xptiNotifySubscribers(uint8_t stream_id, uint16_t trace_type,
309
354
xpti::trace_event_data_t *parent,
310
355
xpti::trace_event_data_t *object, uint64_t instance,
311
356
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);
314
360
if (f) {
315
361
return (*(xpti_notify_subscribers_t )f)(stream_id, trace_type, parent,
316
362
object, instance, user_data);
@@ -320,8 +366,8 @@ xptiNotifySubscribers(uint8_t stream_id, uint16_t trace_type,
320
366
}
321
367
322
368
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);
325
371
if (f) {
326
372
return (*(xpti_trace_enabled_t )f)();
327
373
}
@@ -332,8 +378,8 @@ XPTI_EXPORT_API bool xptiTraceEnabled() {
332
378
XPTI_EXPORT_API xpti::result_t xptiAddMetadata (xpti::trace_event_data_t *e,
333
379
const char *key,
334
380
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);
337
383
if (f) {
338
384
return (*(xpti_add_metadata_t )f)(e, key, value);
339
385
}
@@ -343,8 +389,8 @@ XPTI_EXPORT_API xpti::result_t xptiAddMetadata(xpti::trace_event_data_t *e,
343
389
344
390
XPTI_EXPORT_API xpti::metadata_t *
345
391
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);
348
394
if (f) {
349
395
return (*(xpti_query_metadata_t )f)(lookup_object);
350
396
}
0 commit comments