Skip to content

Commit d568ca4

Browse files
committed
Add support for enabling/disabling tracing layer during runtime
* Add support for enabling/disabling tracing layer during runtime Related-To: VLCLJ-2093 - Added new apis zelEnableTracingLayer & zelDisableTracingLayer to enable the tracing layer during runtime without needing to set ZE_ENABLE_TRACING_LAYER=1. - Tracing Layer Library is now always loaded by the Loader enabling for tracing to be enabled dynamically. Signed-off-by: Spruit, Neil R <[email protected]>
1 parent b3b048c commit d568ca4

19 files changed

+791
-515
lines changed

doc/loader_api.md

+9
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,18 @@ To solve this issue, `zelLoaderTranslateHandle` is used to retrieve the raw driv
3535
- __**handleOut__ Output location to store the translated handle
3636

3737

38+
### zelEnableTracingLayer
3839

40+
When a user wants to enable the Tracing Layer for API Tracing, one usually set `ZE_ENABLE_TRACING_LAYER=1` before the call to zeInit(), however if one wanted to enable and disable tracing during runtime, the only way previously would be to enable tracing with the tracers disabled. This causes a performance hit due to the tracing layer intercepts.
3941

42+
To resolve this, the tracing layer additionally can be enabled thru this new api `zelEnableTracingLayer`. This will enable the api tracing layer until the program exits or the user calls `zelDisableTracingLayer`.
4043

44+
This call enables the tracing layer for all calls to the Loader after this call completes for all initialized drivers.
4145

46+
### zelDisableTracingLayer
4247

48+
Disables the tracing layer intercepts at runtime by restoring the previous call path thru the loader before tracing was enabled.
4349

50+
This does not unload the tracing layer library such that one can call `zelEnableTracingLayer` and `zelDisableTracingLayer` as many times one needs to during the application.
51+
52+
NOTE: The each call to `zelEnableTracingLayer` tracks a reference count of how many calls to enable have been seen. The Tracing Layer intercepts will not be removed until the reference count has reached 0 indicating that all users of the tracing layer have called `zelDisableTracingLayer`.

include/loader/ze_loader.h

+12
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ zelLoaderTranslateHandle(
7070
ZE_DLLEXPORT ze_result_t ZE_APICALL
7171
zelSetDriverTeardown();
7272

73+
///////////////////////////////////////////////////////////////////////////////
74+
/// @brief Exported function for Enabling the Tracing Layer During Runtime.
75+
///
76+
ZE_DLLEXPORT ze_result_t ZE_APICALL
77+
zelEnableTracingLayer();
78+
79+
///////////////////////////////////////////////////////////////////////////////
80+
/// @brief Exported function for Disabling the Tracing Layer During Runtime.
81+
///
82+
ZE_DLLEXPORT ze_result_t ZE_APICALL
83+
zelDisableTracingLayer();
84+
7385
#if defined(__cplusplus)
7486
} // extern "C"
7587
#endif

samples/zello_world/zello_world.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void print_loader_versions(){
3939
//////////////////////////////////////////////////////////////////////////
4040
int main( int argc, char *argv[] )
4141
{
42-
42+
bool tracing_runtime_enabled = false;
4343
if( argparse( argc, argv, "-null", "--enable_null_driver" ) )
4444
{
4545
putenv_safe( const_cast<char *>( "ZE_ENABLE_NULL_DRIVER=1" ) );
@@ -57,6 +57,10 @@ int main( int argc, char *argv[] )
5757
{
5858
putenv_safe( const_cast<char *>( "ZE_ENABLE_TRACING_LAYER=1" ) );
5959
}
60+
if( argparse( argc, argv, "-tracerun", "--enable_tracing_layer_runtime" ) )
61+
{
62+
tracing_runtime_enabled = true;
63+
}
6064

6165
ze_result_t status;
6266
const ze_device_type_t type = ZE_DEVICE_TYPE_GPU;
@@ -68,6 +72,15 @@ int main( int argc, char *argv[] )
6872

6973
print_loader_versions();
7074

75+
if (tracing_runtime_enabled) {
76+
std::cout << "Enabling Tracing Layer after init" << std::endl;
77+
status = zelEnableTracingLayer();
78+
if(status != ZE_RESULT_SUCCESS) {
79+
std::cout << "zelEnableTracingLayer Failed with return code: " << to_string(status) << std::endl;
80+
exit(1);
81+
}
82+
}
83+
7184
uint32_t driverCount = 0;
7285
status = zeDriverGet(&driverCount, nullptr);
7386
if(status != ZE_RESULT_SUCCESS) {
@@ -154,5 +167,14 @@ int main( int argc, char *argv[] )
154167
zeEventDestroy(event);
155168
zeEventPoolDestroy(event_pool);
156169

170+
if (tracing_runtime_enabled) {
171+
std::cout << "Disable Tracing Layer after init" << std::endl;
172+
status = zelDisableTracingLayer();
173+
if(status != ZE_RESULT_SUCCESS) {
174+
std::cout << "zelDisableTracingLayer Failed with return code: " << to_string(status) << std::endl;
175+
exit(1);
176+
}
177+
}
178+
157179
return 0;
158180
}

scripts/templates/ldrddi.cpp.mako

+7-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,13 @@ ${tbl['export']['name']}(
361361
GET_FUNCTION_PTR(loader::context->tracingLayer, "${tbl['export']['name']}") );
362362
if(!getTable)
363363
return ${X}_RESULT_ERROR_UNINITIALIZED;
364-
result = getTable( version, pDdiTable );
364+
${tbl['type']} dditable;
365+
memcpy(&dditable, pDdiTable, sizeof(${tbl['type']}));
366+
result = getTable( version, &dditable );
367+
loader::context->tracing_dditable.${n}.${tbl['name']} = dditable;
368+
if ( loader::context->tracingLayerEnabled ) {
369+
result = getTable( version, pDdiTable );
370+
}
365371
}
366372

367373
%endif

scripts/templates/libapi.cpp.mako

+9-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ ${th.make_func_name(n, tags, obj)}(
6969
return ${X}_RESULT_ERROR_UNINITIALIZED;
7070
}
7171

72-
auto ${th.make_pfn_name(n, tags, obj)} = ${x}_lib::context->${n}DdiTable.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)};
72+
auto ${th.make_pfn_name(n, tags, obj)} = ${x}_lib::context->${n}DdiTable.load()->${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)};
7373
if( nullptr == ${th.make_pfn_name(n, tags, obj)} ) {
7474
if(!ze_lib::context->isInitialized)
7575
return ${X}_RESULT_ERROR_UNINITIALIZED;
@@ -89,7 +89,7 @@ ${th.make_func_name(n, tags, obj)}(
8989
return ${X}_RESULT_ERROR_UNINITIALIZED;
9090
}
9191

92-
auto ${th.make_pfn_name(n, tags, obj)} = ${x}_lib::context->${n}DdiTable.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)};
92+
auto ${th.make_pfn_name(n, tags, obj)} = ${x}_lib::context->${n}DdiTable.load()->${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)};
9393
if( nullptr == ${th.make_pfn_name(n, tags, obj)} ) {
9494
if(!ze_lib::context->isInitialized)
9595
return ${X}_RESULT_ERROR_UNINITIALIZED;
@@ -113,7 +113,13 @@ ${th.make_func_name(n, tags, obj)}(
113113
return ${X}_RESULT_ERROR_UNINITIALIZED;
114114
}
115115

116-
auto ${th.make_pfn_name(n, tags, obj)} = ${x}_lib::context->${n}DdiTable.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)};
116+
%if re.match("DriverGet", obj['name']):
117+
if (${x}_lib::context->${n}DdiTable == nullptr) {
118+
return ${X}_RESULT_ERROR_UNINITIALIZED;
119+
}
120+
121+
%endif
122+
auto ${th.make_pfn_name(n, tags, obj)} = ${x}_lib::context->${n}DdiTable.load()->${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)};
117123
if( nullptr == ${th.make_pfn_name(n, tags, obj)} ) {
118124
if(!ze_lib::context->isInitialized)
119125
return ${X}_RESULT_ERROR_UNINITIALIZED;

scripts/templates/libddi.cpp.mako

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace ${x}_lib
3535
{
3636
auto getTable = reinterpret_cast<${tbl['pfn']}>(
3737
GET_FUNCTION_PTR(loader, "${tbl['export']['name']}") );
38-
result = getTable( ${X}_API_VERSION_CURRENT, &${n}DdiTable.${tbl['name']} );
38+
result = getTable( ${X}_API_VERSION_CURRENT, &initial${n}DdiTable.${tbl['name']} );
3939
}
4040

4141
%endfor
@@ -49,7 +49,7 @@ namespace ${x}_lib
4949
%for tbl in th.get_pfntables(specs, meta, n, tags):
5050
if( ${X}_RESULT_SUCCESS == result )
5151
{
52-
result = ${tbl['export']['name']}( ${X}_API_VERSION_CURRENT, &${n}DdiTable.${tbl['name']} );
52+
result = ${tbl['export']['name']}( ${X}_API_VERSION_CURRENT, &initial${n}DdiTable.${tbl['name']} );
5353
}
5454

5555
%endfor

source/lib/ze_lib.cpp

+32-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ namespace ze_lib
6767

6868
#endif
6969

70+
if ( ZE_RESULT_SUCCESS == result )
71+
{
72+
ze_lib::context->zeDdiTable.exchange(&ze_lib::context->initialzeDdiTable);
73+
ze_lib::context->zetDdiTable.exchange(&ze_lib::context->initialzetDdiTable);
74+
ze_lib::context->zesDdiTable.exchange(&ze_lib::context->initialzesDdiTable);
75+
}
76+
7077
if( ZE_RESULT_SUCCESS == result )
7178
{
7279
result = zeInit();
@@ -87,6 +94,11 @@ namespace ze_lib
8794
result = zelTracingInit();
8895
}
8996

97+
if( ZE_RESULT_SUCCESS == result )
98+
{
99+
result = zelLoaderTracingLayerInit(this->pTracingZeDdiTable, this->pTracingZetDdiTable, this->pTracingZesDdiTable);
100+
}
101+
90102
if( ZE_RESULT_SUCCESS == result && !sysmanOnly)
91103
{
92104
//Check which drivers support the ze_driver_flag_t specified
@@ -137,7 +149,6 @@ zelLoaderTranslateHandle(
137149

138150
ze_result_t ZE_APICALL
139151
zelSetDriverTeardown()
140-
141152
{
142153
ze_result_t result = ZE_RESULT_SUCCESS;
143154
if (!ze_lib::destruction) {
@@ -146,7 +157,26 @@ zelSetDriverTeardown()
146157
return result;
147158
}
148159

160+
ze_result_t ZE_APICALL
161+
zelEnableTracingLayer()
162+
{
163+
if (ze_lib::context->tracingLayerEnableCounter.fetch_add(1) == 0) {
164+
ze_lib::context->zeDdiTable.exchange(ze_lib::context->pTracingZeDdiTable);
165+
ze_lib::context->zetDdiTable.exchange(ze_lib::context->pTracingZetDdiTable);
166+
ze_lib::context->zesDdiTable.exchange(ze_lib::context->pTracingZesDdiTable);
167+
}
168+
return ZE_RESULT_SUCCESS;
169+
}
149170

150-
171+
ze_result_t ZE_APICALL
172+
zelDisableTracingLayer()
173+
{
174+
if (ze_lib::context->tracingLayerEnableCounter.fetch_sub(1) <= 1) {
175+
ze_lib::context->zeDdiTable.exchange(&ze_lib::context->initialzeDdiTable);
176+
ze_lib::context->zetDdiTable.exchange(&ze_lib::context->initialzetDdiTable);
177+
ze_lib::context->zesDdiTable.exchange(&ze_lib::context->initialzesDdiTable);
178+
}
179+
return ZE_RESULT_SUCCESS;
180+
}
151181

152182
} //extern "c"

source/lib/ze_lib.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ze_util.h"
2020
#include <vector>
2121
#include <mutex>
22+
#include <atomic>
2223

2324
namespace ze_lib
2425
{
@@ -38,16 +39,23 @@ namespace ze_lib
3839
ze_result_t Init(ze_init_flags_t flags, bool sysmanOnly);
3940

4041
ze_result_t zeInit();
41-
ze_dditable_t zeDdiTable = {};
42+
std::atomic<ze_dditable_t *> zeDdiTable = {nullptr};
4243

4344
ze_result_t zetInit();
44-
zet_dditable_t zetDdiTable = {};
45+
std::atomic<zet_dditable_t *> zetDdiTable = {nullptr};
4546

4647
ze_result_t zesInit();
47-
zes_dditable_t zesDdiTable = {};
48+
std::atomic<zes_dditable_t *> zesDdiTable = {nullptr};
4849

4950
ze_result_t zelTracingInit();
5051
zel_tracing_dditable_t zelTracingDdiTable = {};
52+
std::atomic<ze_dditable_t *> pTracingZeDdiTable = {nullptr};
53+
std::atomic<zet_dditable_t *> pTracingZetDdiTable = {nullptr};
54+
std::atomic<zes_dditable_t *> pTracingZesDdiTable = {nullptr};
55+
ze_dditable_t initialzeDdiTable;
56+
zet_dditable_t initialzetDdiTable;
57+
zes_dditable_t initialzesDdiTable;
58+
std::atomic_uint32_t tracingLayerEnableCounter;
5159

5260
HMODULE tracing_lib = nullptr;
5361
bool isInitialized = false;

0 commit comments

Comments
 (0)