forked from oneapi-src/level-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathze_lib.cpp
More file actions
658 lines (623 loc) · 28.1 KB
/
ze_lib.cpp
File metadata and controls
658 lines (623 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
*
* Copyright (C) 2019-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
* @file ze_lib.cpp
*
*/
#include "ze_lib.h"
#include "../loader/ze_loader_api.h"
#include "../loader/ze_loader_internal.h"
#include <thread>
#include <future>
#include <stdexcept>
namespace ze_lib
{
///////////////////////////////////////////////////////////////////////////////
context_t *context = nullptr;
#ifdef DYNAMIC_LOAD_LOADER
void context_at_exit_destructor()
{
if (ze_lib::context) {
delete ze_lib::context;
ze_lib::context = nullptr;
}
}
#define ZEL_STABILITY_THREAD_STATE_RUNNING 1
#define ZEL_STABILITY_THREAD_STATE_POLLING 0
#define ZEL_STABILITY_THREAD_STATE_SHUTDOWN -1
bool delayContextDestruction = false;
std::mutex *stabilityMutex = nullptr;
std::promise<int> *stabilityPromiseResult = nullptr;
std::future<int> *resultFutureResult = nullptr;
std::atomic<int> *stabilityCheckThreadStatus = nullptr;
std::thread *stabilityThread = nullptr;
#endif
bool destruction = false;
///////////////////////////////////////////////////////////////////////////////
__zedlllocal context_t::context_t()
{
debugTraceEnabled = getenv_tobool( "ZE_ENABLE_LOADER_DEBUG_TRACE" );
};
///////////////////////////////////////////////////////////////////////////////
__zedlllocal context_t::~context_t()
{
#ifdef DYNAMIC_LOAD_LOADER
if (loader) {
FREE_DRIVER_LIBRARY( loader );
}
if (ze_lib::stabilityCheckThreadStatus)
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_SHUTDOWN);
try {
if (stabilityThread && stabilityThread->joinable()) {
stabilityThread->join();
}
} catch (...) {
// Ignore any exceptions from thread join
}
if (stabilityThread) {
delete stabilityThread;
stabilityThread = nullptr;
}
if (stabilityMutex) {
delete stabilityMutex;
stabilityMutex = nullptr;
}
if (stabilityPromiseResult) {
delete stabilityPromiseResult;
stabilityPromiseResult = nullptr;
}
if (resultFutureResult) {
delete resultFutureResult;
resultFutureResult = nullptr;
}
if (stabilityCheckThreadStatus) {
delete stabilityCheckThreadStatus;
stabilityCheckThreadStatus = nullptr;
}
#endif
ze_lib::destruction = true;
};
//////////////////////////////////////////////////////////////////////////
__zedlllocal ze_result_t context_t::Init(ze_init_flags_t flags, bool sysmanOnly, ze_init_driver_type_desc_t* desc)
{
ze_result_t result;
ze_api_version_t version = ZE_API_VERSION_CURRENT;
#ifdef DYNAMIC_LOAD_LOADER
std::string loaderLibraryPath;
auto loaderLibraryPathEnv = getenv_string("ZEL_LIBRARY_PATH");
if (!loaderLibraryPathEnv.empty()) {
loaderLibraryPath = loaderLibraryPathEnv;
}
#ifdef _WIN32
else {
loaderLibraryPath = readLevelZeroLoaderLibraryPath();
}
#endif
if (debugTraceEnabled)
debug_trace_message("Static Loader Using Loader Library Path: ", loaderLibraryPath);
std::string loaderFullLibraryPath = create_library_path(MAKE_LIBRARY_NAME( "ze_loader", L0_LOADER_VERSION), loaderLibraryPath.c_str());
loader = LOAD_DRIVER_LIBRARY(loaderFullLibraryPath.c_str());
if( NULL == loader ) {
std::string message = "ze_lib Context Init() Loader Library Load Failed with ";
debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED));
return ZE_RESULT_ERROR_UNINITIALIZED;
}
typedef ze_result_t (ZE_APICALL *loaderInit_t)();
auto loaderInit = reinterpret_cast<loaderInit_t>(
GET_FUNCTION_PTR(loader, "zeLoaderInit") );
result = loaderInit();
if( ZE_RESULT_SUCCESS != result ) {
std::string message = "ze_lib Context Init() Loader Init Failed with ";
debug_trace_message(message, to_string(result));
return result;
}
size_t size = 0;
result = zelLoaderGetVersions(&size, nullptr);
if (ZE_RESULT_SUCCESS != result) {
std::string message = "ze_lib Context Init() zelLoaderGetVersions Failed with";
debug_trace_message(message, to_string(result));
return result;
}
std::vector<zel_component_version_t> versions(size);
result = zelLoaderGetVersions(&size, versions.data());
if (ZE_RESULT_SUCCESS != result) {
std::string message = "ze_lib Context Init() zelLoaderGetVersions Failed to read component versions with ";
debug_trace_message(message, to_string(result));
return result;
}
bool zeInitDriversSupport = true;
const std::string loader_name = "loader";
for (auto &component : versions) {
if (loader_name == component.component_name) {
version = component.spec_version;
std::string message = "ze_lib Context Init() Static Loader Found Loader Version v" + std::to_string(component.component_lib_version.major) + "." + std::to_string(component.component_lib_version.minor) + "." + std::to_string(component.component_lib_version.patch);
debug_trace_message(message, "");
if(component.component_lib_version.major == 1) {
if (component.component_lib_version.minor < 18) {
std::string message = "ze_lib Context Init() Version Does not support zeInitDrivers";
debug_trace_message(message, "");
zeInitDriversSupport = false;
}
} else {
std::string message = "ze_lib Context Init() Loader version is too new, returning ";
debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNSUPPORTED_VERSION));
return ZE_RESULT_ERROR_UNSUPPORTED_VERSION;
}
}
}
typedef HMODULE (ZE_APICALL *getTracing_t)();
auto getTracing = reinterpret_cast<getTracing_t>(
GET_FUNCTION_PTR(loader, "zeLoaderGetTracingHandle") );
if (getTracing == nullptr) {
std::string message = "ze_lib Context Init() zeLoaderGetTracingHandle missing, returning ";
debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED));
return ZE_RESULT_ERROR_UNINITIALIZED;
}
tracing_lib = getTracing();
typedef ze_result_t (ZE_APICALL *zelLoaderTracingLayerInit_t)(std::atomic<ze_dditable_t *> &zeDdiTable);
auto loaderTracingLayerInit = reinterpret_cast<zelLoaderTracingLayerInit_t>(
GET_FUNCTION_PTR(loader, "zelLoaderTracingLayerInit") );
if (loaderTracingLayerInit == nullptr) {
std::string message = "ze_lib Context Init() zelLoaderTracingLayerInit missing, disabling dynamic tracer support ";
debug_trace_message(message, "");
this->dynamicTracingSupported = false;
}
typedef loader::context_t * (ZE_APICALL *zelLoaderGetContext_t)();
auto loaderGetContext = reinterpret_cast<zelLoaderGetContext_t>(
GET_FUNCTION_PTR(loader, "zelLoaderGetContext") );
if (loaderGetContext == nullptr) {
std::string message = "ze_lib Context Init() zelLoaderGetContext missing";
debug_trace_message(message, "");
}
std::string version_message = "Loader API Version to be requested is v" + std::to_string(ZE_MAJOR_VERSION(version)) + "." + std::to_string(ZE_MINOR_VERSION(version));
debug_trace_message(version_message, "");
loaderDriverGet = reinterpret_cast<ze_pfnDriverGet_t>(GET_FUNCTION_PTR(loader, "zeDriverGet"));
stabilityMutex = new std::mutex();
stabilityPromiseResult = new std::promise<int>();
resultFutureResult = new std::future<int>(stabilityPromiseResult->get_future());
stabilityCheckThreadStatus = new std::atomic<int>(ZEL_STABILITY_THREAD_STATE_POLLING);
#else
result = zeLoaderInit();
if( ZE_RESULT_SUCCESS == result ) {
tracing_lib = zeLoaderGetTracingHandle();
}
#endif
if ( ZE_RESULT_SUCCESS == result )
{
ze_lib::context->zeDdiTable.exchange(&ze_lib::context->initialzeDdiTable);
ze_lib::context->zetDdiTable.exchange(&ze_lib::context->initialzetDdiTable);
ze_lib::context->zesDdiTable.exchange(&ze_lib::context->initialzesDdiTable);
}
// Given zesInit, then zesDrivers needs to be used as the sysmanInstanceDrivers;
bool loaderContextAccessAllowed = true;
#ifdef DYNAMIC_LOAD_LOADER
loader::context_t *loaderContext = nullptr;
if (loaderGetContext == nullptr) {
loaderContextAccessAllowed = false;
} else {
loaderContext = loaderGetContext();
}
#else
loader::context_t *loaderContext = loader::context;
#endif
if (sysmanOnly && loaderContextAccessAllowed) {
loaderContext->sysmanInstanceDrivers = &loaderContext->zesDrivers;
}
// Always call the inits for all the ddi tables before checking which drivers are usable to enable Instrumentation correctly.
// Init the ZE DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zeDdiTableInit(version);
if (result != ZE_RESULT_SUCCESS) {
std::string message = "ze_lib Context Init() zeDdiTableInit failed with ";
debug_trace_message(message, to_string(result));
}
}
// Init the ZET DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zetDdiTableInit(version);
if( ZE_RESULT_SUCCESS != result ) {
std::string message = "ze_lib Context Init() zetDdiTableInit failed with ";
debug_trace_message(message, to_string(result));
}
}
// Init the ZES DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zesDdiTableInit(version);
if (result != ZE_RESULT_SUCCESS) {
std::string message = "ze_lib Context Init() zesDdiTableInit failed with ";
debug_trace_message(message, to_string(result));
}
}
// Init the Tracing API DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zelTracingDdiTableInit(version);
if (result != ZE_RESULT_SUCCESS) {
std::string message = "ze_lib Context Init() zelTracingDdiTableInit failed with ";
debug_trace_message(message, to_string(result));
}
}
// Init the stored ddi tables for the tracing layer
if( ZE_RESULT_SUCCESS == result )
{
#ifdef DYNAMIC_LOAD_LOADER
if (loaderTracingLayerInit) {
result = loaderTracingLayerInit(this->pTracingZeDdiTable);
}
#else
result = zelLoaderTracingLayerInit(this->pTracingZeDdiTable);
#endif
}
// End DDI Table Inits
// Check which drivers and layers can be init on this system.
if( ZE_RESULT_SUCCESS == result)
{
// Check which drivers support the ze_driver_flag_t specified
// No need to check if only initializing sysman
bool requireDdiReinit = false;
#ifdef DYNAMIC_LOAD_LOADER
if (zeInitDriversSupport) {
typedef ze_result_t (ZE_APICALL *zelLoaderDriverCheck_t)(ze_init_flags_t flags, ze_init_driver_type_desc_t* desc, ze_global_dditable_t *globalInitStored, zes_global_dditable_t *sysmanGlobalInitStored, bool *requireDdiReinit, bool sysmanOnly);
auto loaderDriverCheck = reinterpret_cast<zelLoaderDriverCheck_t>(
GET_FUNCTION_PTR(loader, "zelLoaderDriverCheck") );
if (loaderDriverCheck == nullptr) {
std::string message = "ze_lib Context Init() zelLoaderDriverCheck missing, returning ";
debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED));
return ZE_RESULT_ERROR_UNINITIALIZED;
}
result = loaderDriverCheck(flags, desc, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly);
} else {
typedef ze_result_t (ZE_APICALL *zelLoaderDriverCheck_t)(ze_init_flags_t flags, ze_global_dditable_t *globalInitStored, zes_global_dditable_t *sysmanGlobalInitStored, bool *requireDdiReinit, bool sysmanOnly);
auto loaderDriverCheck = reinterpret_cast<zelLoaderDriverCheck_t>(
GET_FUNCTION_PTR(loader, "zelLoaderDriverCheck") );
if (loaderDriverCheck == nullptr) {
std::string message = "ze_lib Context Init() zelLoaderDriverCheck missing, returning ";
debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED));
return ZE_RESULT_ERROR_UNINITIALIZED;
}
result = loaderDriverCheck(flags, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly);
}
#else
result = zelLoaderDriverCheck(flags, desc, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly);
#endif
if (result != ZE_RESULT_SUCCESS) {
std::string message = "ze_lib Context Init() zelLoaderDriverCheck failed with ";
debug_trace_message(message, to_string(result));
}
// If a driver was removed from the driver list, then the ddi tables need to be reinit to allow for passthru directly to the driver.
if (requireDdiReinit && loaderContextAccessAllowed) {
// If a user has already called the core apis, then ddi table reinit is not possible due to handles already being read by the user.
if (!sysmanOnly && !ze_lib::context->zeInuse) {
// reInit the ZE DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zeDdiTableInit(version);
}
// reInit the ZET DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zetDdiTableInit(version);
}
// If ze/zet ddi tables have been reinit and no longer use the intercept layer, then handles passed to zelLoaderTranslateHandleInternal do not require translation.
// Setting intercept_enabled==false changes the behavior of zelLoaderTranslateHandleInternal to avoid translation.
// Translation is only required if the intercept layer is enabled for the ZE handle types.
loaderContext->intercept_enabled = false;
}
// If a user has already called the zes/ze apis, then ddi table reinit is not possible due to handles already being read by the user.
if (!(ze_lib::context->zesInuse || ze_lib::context->zeInuse)) {
// reInit the ZES DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zesDdiTableInit(version);
}
}
}
}
if( ZE_RESULT_SUCCESS == result )
{
#ifdef DYNAMIC_LOAD_LOADER
// Init Dynamic Loader's Lib Context:
auto initDriversLoader = reinterpret_cast<ze_pfnInitDrivers_t>(
GET_FUNCTION_PTR(loader, "zeInitDrivers") );
auto initLoader = reinterpret_cast<ze_pfnInit_t>(
GET_FUNCTION_PTR(loader, "zeInit") );
if (initDriversLoader == nullptr && initLoader == nullptr) {
std::string message = "ze_lib Context Init() zeInitDrivers and zeInit missing, returning ";
debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED));
return ZE_RESULT_ERROR_UNINITIALIZED;
}
if (!desc) {
result = initLoader(flags);
} else if (initDriversLoader != nullptr) {
uint32_t pInitDriversCount = 0;
result = initDriversLoader(&pInitDriversCount, nullptr, desc);
} else {
ze_init_flags_t init_flags = flags;
if (desc) {
if(desc->flags & ZE_INIT_DRIVER_TYPE_FLAG_GPU) {
init_flags = ZE_INIT_FLAG_GPU_ONLY;
} else if(desc->flags & ZE_INIT_DRIVER_TYPE_FLAG_NPU) {
init_flags = ZE_INIT_FLAG_VPU_ONLY;
} else {
init_flags = 0;
}
}
result = initLoader(init_flags);
}
if (result != ZE_RESULT_SUCCESS) {
std::string message = "ze_lib Context Init() zeInitDrivers or zeInit failed with ";
debug_trace_message(message, to_string(result));
return result;
}
#endif
isInitialized = true;
}
#ifdef DYNAMIC_LOAD_LOADER
if (!delayContextDestruction) {
std::atexit(context_at_exit_destructor);
}
#endif
return result;
}
} // namespace ze_lib
extern "C" {
ze_result_t ZE_APICALL
zelLoaderGetVersions(
size_t *num_elems, //Pointer to num versions to get.
zel_component_version_t *versions) //Pointer to array of versions. If set to NULL, num_elems is returned
{
#ifdef DYNAMIC_LOAD_LOADER
if(nullptr == ze_lib::context->loader)
return ZE_RESULT_ERROR_UNINITIALIZED;
typedef ze_result_t (ZE_APICALL *zelLoaderGetVersions_t)(size_t *num_elems, zel_component_version_t *versions);
auto getVersions = reinterpret_cast<zelLoaderGetVersions_t>(
GET_FUNCTION_PTR(ze_lib::context->loader, "zelLoaderGetVersionsInternal") );
return getVersions(num_elems, versions);
#else
return zelLoaderGetVersionsInternal(num_elems, versions);
#endif
}
ze_result_t ZE_APICALL
zelLoaderTranslateHandle(
zel_handle_type_t handleType,
void *handleIn,
void **handleOut)
{
#ifdef DYNAMIC_LOAD_LOADER
if(nullptr == ze_lib::context->loader)
return ZE_RESULT_ERROR_UNINITIALIZED;
typedef ze_result_t (ZE_APICALL *zelLoaderTranslateHandleInternal_t)(zel_handle_type_t handleType, void *handleIn, void **handleOut);
auto translateHandle = reinterpret_cast<zelLoaderTranslateHandleInternal_t>(
GET_FUNCTION_PTR(ze_lib::context->loader, "zelLoaderTranslateHandleInternal") );
return translateHandle(handleType, handleIn, handleOut);
#else
return zelLoaderTranslateHandleInternal(handleType, handleIn, handleOut);
#endif
}
ze_result_t ZE_APICALL
zelSetDriverTeardown()
{
ze_result_t result = ZE_RESULT_SUCCESS;
if (!ze_lib::destruction) {
ze_lib::destruction = true;
}
return result;
}
void ZE_APICALL
zelSetDelayLoaderContextTeardown()
{
#ifdef DYNAMIC_LOAD_LOADER
if (!ze_lib::delayContextDestruction) {
ze_lib::delayContextDestruction = true;
}
#endif
}
#ifdef DYNAMIC_LOAD_LOADER
#define ZEL_STABILITY_CHECK_RESULT_SUCCESS 0
#define ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_NULL 1
#define ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_FAILED 2
#define ZEL_STABILITY_CHECK_RESULT_EXCEPTION 3
// The stability check thread timeout in milliseconds
#define ZEL_STABILITY_CHECK_THREAD_TIMEOUT 100
// The stability check thread polling interval in nanoseconds
#define ZEL_STABILITY_CHECK_THREAD_POLLING_INTERVAL 100
/**
* @brief Performs a stability check for the Level Zero loader.
*
* This function verifies the stability of the Level Zero loader by checking:
* - The presence of the loader module.
* - The validity of the `zeDriverGet` function pointer.
* - The ability to retrieve driver information.
*
* The result of the stability check is returned as an integer, with the following possible values:
* - `ZEL_STABILITY_CHECK_RESULT_SUCCESS`: The stability check was successful.
* - `ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_NULL`: The `zeDriverGet` function pointer is invalid.
* - `ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_FAILED`: The loader failed to retrieve driver information.
* - `ZEL_STABILITY_CHECK_RESULT_EXCEPTION`: An exception occurred during the stability check.
*
* If debug tracing is enabled, debug messages are logged for each failure scenario.
*
* @return An integer indicating the result of the stability check.
*
* @note If the loader is completely torn down, this function may fail due to invalid memory access.
* @note This function catches all exceptions internally and does not throw.
*/
int stabilityCheck() {
try {
if (!ze_lib::context->loaderDriverGet) {
if (ze_lib::context->debugTraceEnabled) {
std::string message = "LoaderDriverGet is a bad pointer. Exiting stability checker.";
ze_lib::context->debug_trace_message(message, "");
}
return ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_NULL;
}
uint32_t driverCount = 0;
ze_result_t result = ZE_RESULT_ERROR_UNINITIALIZED;
result = ze_lib::context->loaderDriverGet(&driverCount, nullptr);
if (result != ZE_RESULT_SUCCESS || driverCount == 0) {
if (ze_lib::context->debugTraceEnabled) {
std::string message = "Loader stability check failed. Exiting stability checker.";
ze_lib::context->debug_trace_message(message, "");
}
return ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_FAILED;
}
return ZEL_STABILITY_CHECK_RESULT_SUCCESS;
} catch (...) {
return ZEL_STABILITY_CHECK_RESULT_EXCEPTION;
}
}
#endif
/**
* @brief Checks if the loader is in the process of tearing down.
*
* This function determines whether the loader is in a teardown state by
* checking the destruction flag or the context pointer. If the loader is
* dynamically loaded thru the static loader code path, then it performs
* an additional stability check using a separate thread that could be killed.
*
* @return true if the loader is in teardown based on the stack variablrs
* or the stability check fails; false otherwise.
*
* @note If the macro DYNAMIC_LOAD_LOADER is defined, a stability checker
* thread is launched to perform additional checks. Any exceptions
* or errors during this process are logged if debug tracing is enabled.
*/
bool ZE_APICALL
zelCheckIsLoaderInTearDown() {
if (ze_lib::destruction || ze_lib::context == nullptr) {
return true;
}
#ifdef DYNAMIC_LOAD_LOADER
static bool unstable = false;
int threadResult = -1;
if (unstable) {
return true;
}
try {
// Launch the stability checker thread on the first call
static std::once_flag stabilityThreadFlag;
std::lock_guard<std::mutex> lock(*ze_lib::stabilityMutex);
*ze_lib::stabilityPromiseResult = std::promise<int>();
*ze_lib::resultFutureResult = ze_lib::stabilityPromiseResult->get_future();
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_RUNNING);
std::call_once(stabilityThreadFlag, []() {
ze_lib::stabilityThread = new std::thread([]() {
while (true) {
while(ze_lib::stabilityCheckThreadStatus && ze_lib::stabilityCheckThreadStatus->load() == ZEL_STABILITY_THREAD_STATE_POLLING) {
std::this_thread::sleep_for(std::chrono::nanoseconds(ZEL_STABILITY_CHECK_THREAD_POLLING_INTERVAL));
}
if (ze_lib::destruction || ze_lib::context == nullptr) {
break;
}
if (!ze_lib::stabilityCheckThreadStatus) {
break;
}
if (ze_lib::stabilityCheckThreadStatus->load() == ZEL_STABILITY_THREAD_STATE_SHUTDOWN) {
break;
}
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_POLLING);
int result = stabilityCheck();
if (result != ZEL_STABILITY_CHECK_RESULT_SUCCESS) {
if (ze_lib::context->debugTraceEnabled) {
std::string message = "Loader stability check thread failed with result: " + std::to_string(result);
ze_lib::context->debug_trace_message(message, "");
}
if (ze_lib::stabilityPromiseResult) {
ze_lib::stabilityPromiseResult->set_value(result);
}
break; // Exit the thread if stability check fails
}
if (ze_lib::stabilityPromiseResult) {
ze_lib::stabilityPromiseResult->set_value(result);
}
}
});
});
if (ze_lib::resultFutureResult->wait_for(std::chrono::milliseconds(ZEL_STABILITY_CHECK_THREAD_TIMEOUT)) == std::future_status::timeout) {
if (ze_lib::context->debugTraceEnabled) {
std::string message = "Stability Thread timeout, assuming thread has crashed";
ze_lib::context->debug_trace_message(message, "");
}
threadResult = ZEL_STABILITY_CHECK_RESULT_EXCEPTION;
} else {
threadResult = ze_lib::resultFutureResult->get();
}
} catch (const std::exception& e) {
if (ze_lib::context->debugTraceEnabled) {
std::string message = "Exception caught in parent thread: " + std::string(e.what());
ze_lib::context->debug_trace_message(message, "");
}
} catch (...) {
if (ze_lib::context->debugTraceEnabled) {
std::string message = "Unknown exception caught in parent thread.";
ze_lib::context->debug_trace_message(message, "");
}
}
if (threadResult != ZEL_STABILITY_CHECK_RESULT_SUCCESS) {
if (ze_lib::context->debugTraceEnabled) {
std::string message = "Loader stability check failed with result: " + std::to_string(threadResult);
ze_lib::context->debug_trace_message(message, "");
}
unstable = true;
return true;
}
#endif
return false;
}
void ZE_APICALL
zelLoaderContextTeardown()
{
#ifdef DYNAMIC_LOAD_LOADER
if (ze_lib::delayContextDestruction && ze_lib::context) {
delete ze_lib::context;
ze_lib::context = nullptr;
}
#endif
}
ze_result_t ZE_APICALL
zelEnableTracingLayer()
{
#ifdef DYNAMIC_LOAD_LOADER
if(nullptr == ze_lib::context->loader)
return ZE_RESULT_ERROR_UNINITIALIZED;
typedef ze_result_t (ZE_APICALL *zelEnableTracingLayerInternal_t)();
auto enableDynamicTracing = reinterpret_cast<zelEnableTracingLayerInternal_t>(
GET_FUNCTION_PTR(ze_lib::context->loader, "zelEnableTracingLayer") );
return enableDynamicTracing();
#else
if (ze_lib::context->dynamicTracingSupported == false) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
if (ze_lib::context->tracingLayerEnableCounter.fetch_add(1) == 0) {
ze_lib::context->zeDdiTable.exchange(ze_lib::context->pTracingZeDdiTable);
}
#endif
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL
zelDisableTracingLayer()
{
#ifdef DYNAMIC_LOAD_LOADER
if(nullptr == ze_lib::context->loader)
return ZE_RESULT_ERROR_UNINITIALIZED;
typedef ze_result_t (ZE_APICALL *zelDisableTracingLayerInternal_t)();
auto disableDynamicTracing = reinterpret_cast<zelDisableTracingLayerInternal_t>(
GET_FUNCTION_PTR(ze_lib::context->loader, "zelDisableTracingLayer") );
return disableDynamicTracing();
#else
if (ze_lib::context->dynamicTracingSupported == false) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
if (ze_lib::context->tracingLayerEnableCounter.fetch_sub(1) <= 1) {
ze_lib::context->zeDdiTable.exchange(&ze_lib::context->initialzeDdiTable);
}
#endif
return ZE_RESULT_SUCCESS;
}
} //extern "c"