-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathze_lib.cpp
229 lines (204 loc) · 7.67 KB
/
ze_lib.cpp
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
/*
*
* Copyright (C) 2019-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
* @file ze_lib.cpp
*
*/
#include "ze_lib.h"
#ifndef DYNAMIC_LOAD_LOADER
#include "../loader/ze_loader_api.h"
#endif
namespace ze_lib
{
///////////////////////////////////////////////////////////////////////////////
context_t *context;
bool destruction = false;
///////////////////////////////////////////////////////////////////////////////
context_t::context_t()
{
};
///////////////////////////////////////////////////////////////////////////////
context_t::~context_t()
{
#ifdef DYNAMIC_LOAD_LOADER
if (loader) {
FREE_DRIVER_LIBRARY( loader );
}
#endif
ze_lib::destruction = true;
};
//////////////////////////////////////////////////////////////////////////
__zedlllocal ze_result_t context_t::Init(ze_init_flags_t flags, bool sysmanOnly)
{
ze_result_t result;
#ifdef DYNAMIC_LOAD_LOADER
std::string loaderLibraryPath;
#ifdef _WIN32
loaderLibraryPath = readLevelZeroLoaderLibraryPath();
#endif
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 )
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 ) {
typedef HMODULE (ZE_APICALL *getTracing_t)();
auto getTracing = reinterpret_cast<getTracing_t>(
GET_FUNCTION_PTR(loader, "zeLoaderGetTracingHandle") );
tracing_lib = getTracing();
}
#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);
}
// 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();
}
// Init the ZET DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zetDdiTableInit();
}
// Init the ZES DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zesDdiTableInit();
}
// Init the Tracing API DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zelTracingDdiTableInit();
}
// Init the stored ddi tables for the tracing layer
if( ZE_RESULT_SUCCESS == result )
{
result = zelLoaderTracingLayerInit(this->pTracingZeDdiTable);
}
// 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;
auto sysmanEnv = getenv_tobool( "ZES_ENABLE_SYSMAN" );
result = zelLoaderDriverCheck(flags, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly);
// 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 ZET_ENABLE_PROGRAM_INSTRUMENTATION is enabled, then reInit is not possible due to the functions being intercepted with the previous ddi tables.
auto programInstrumentationEnabled = getenv_tobool( "ZET_ENABLE_PROGRAM_INSTRUMENTATION" );
if (requireDdiReinit && !programInstrumentationEnabled) {
if (!sysmanOnly) {
// reInit the ZE DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zeDdiTableInit();
}
// reInit the ZET DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zetDdiTableInit();
}
// 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.
loader::context->intercept_enabled = false;
}
if (sysmanOnly || sysmanEnv) {
// reInit the ZES DDI Tables
if( ZE_RESULT_SUCCESS == result )
{
result = zesDdiTableInit();
}
}
}
}
if( ZE_RESULT_SUCCESS == result )
{
isInitialized = true;
}
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;
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
zelReloadDrivers(
ze_init_flags_t flags)
{
#ifdef DYNAMIC_LOAD_LOADER
if(nullptr == ze_lib::context->loader)
return ZE_RESULT_ERROR;
typedef ze_result_t (ZE_APICALL *zelReloadDriver_t)(ze_driver_handle_t hDriver);
auto reloadDrivers = reinterpret_cast<zelReloadDriver_t>(
GET_FUNCTION_PTR(ze_lib::context->loader, "zelReloadDriversInternal") );
return reloadDrivers(flags);
#else
return zelReloadDriversInternal(flags);
#endif
}
ze_result_t ZE_APICALL
zelLoaderTranslateHandle(
zel_handle_type_t handleType,
void *handleIn,
void **handleOut)
{
return zelLoaderTranslateHandleInternal(handleType, handleIn, handleOut);
}
ze_result_t ZE_APICALL
zelSetDriverTeardown()
{
ze_result_t result = ZE_RESULT_SUCCESS;
if (!ze_lib::destruction) {
ze_lib::context->inTeardown = true;
}
return result;
}
ze_result_t ZE_APICALL
zelEnableTracingLayer()
{
if (ze_lib::context->tracingLayerEnableCounter.fetch_add(1) == 0) {
ze_lib::context->zeDdiTable.exchange(ze_lib::context->pTracingZeDdiTable);
}
return ZE_RESULT_SUCCESS;
}
ze_result_t ZE_APICALL
zelDisableTracingLayer()
{
if (ze_lib::context->tracingLayerEnableCounter.fetch_sub(1) <= 1) {
ze_lib::context->zeDdiTable.exchange(&ze_lib::context->initialzeDdiTable);
}
return ZE_RESULT_SUCCESS;
}
} //extern "c"