Skip to content

Commit 182ff91

Browse files
authored
Update Tracing Layer Documentation for the expanded support (#414)
- Fix the Tracing Layer Readme to outline the full feature set as of additions including dynamic tracing enable/disable and query of tracing state and additional clarifications. Signed-off-by: Neil R. Spruit <[email protected]>
1 parent f096108 commit 182ff91

File tree

1 file changed

+89
-3
lines changed

1 file changed

+89
-3
lines changed

source/layers/tracing/README.md

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,50 @@ Tracing provides for a tool to create one or more __tracers__. A __tracer__ is
88
In summary, this tracing implementation provides functions to create one or more __tracers__, acquiring a __tracer handle__ for each, then registering a set of __prologue__ and __epilogue__ functions for each __tracer handle__, enabling and disabling a __tracer__, and for destroying a __tracer__.
99

1010
## Enabling Tracing in the Loader
11-
Tracing is implemented as a layer in the loader. This tracing layer must be enabled by setting the environment variable **ZE_ENABLE_TRACING_LAYER** to 1. This environment variable must be defined in the application process's context before that process calls _zeInit()_.
11+
Tracing is implemented as a layer in the loader. There are two ways to enable the tracing layer:
12+
13+
### Static Tracing Layer Enablement (Environment Variable)
14+
The tracing layer can be enabled for the entire process runtime by setting the environment variable **ZE_ENABLE_TRACING_LAYER** to 1. This environment variable must be defined in the application process's context before that process calls _zeInitDrivers()_. When enabled this way, the tracing layer remains active for the entire duration of the application.
15+
16+
### Dynamic Tracing Layer Enablement (Runtime Control)
17+
The tracing layer can also be enabled and disabled dynamically at runtime using the following APIs:
18+
19+
- **zelEnableTracingLayer()** - Enables the tracing layer at runtime. Can be called at any point during application execution, but will only affect subsequent API calls made after the call to this function.
20+
21+
- **zelDisableTracingLayer()** - Disables the tracing layer at runtime. After calling this function, subsequent API calls will not be intercepted by the tracing layer.
22+
23+
- **zelGetTracingLayerState(bool* enabled)** - Queries the current state of the tracing layer. Sets the boolean pointed to by `enabled` to `true` if the tracing layer is currently active, or `false` if it is disabled.
24+
25+
These functions are defined in `include/loader/ze_loader.h`.
26+
27+
Dynamic tracing control provides the flexibility to:
28+
- Enable tracing only for specific regions of code
29+
- Reduce performance overhead by disabling tracing when not needed
30+
- Programmatically control tracing based on runtime conditions
31+
32+
### Tracing Layer vs Tracer Enable/Disable
33+
34+
It is important to understand the distinction between **enabling/disabling the tracing layer** and **enabling/disabling individual tracers**:
35+
36+
#### Tracing Layer Enable/Disable
37+
- Controls whether the tracing layer infrastructure is active in the loader
38+
- Affects **all** Level Zero API calls globally
39+
- When the tracing layer is disabled, **no** API calls are intercepted regardless of individual tracer states
40+
- Controlled via `zelEnableTracingLayer()`, `zelDisableTracingLayer()`, and the `ZE_ENABLE_TRACING_LAYER` environment variable
41+
- Represents a global on/off switch for the entire tracing infrastructure
42+
43+
#### Individual Tracer Enable/Disable
44+
- Controls whether a specific tracer's callbacks are invoked
45+
- Requires the tracing layer to be enabled first
46+
- Controlled via `zelTracerSetEnabled()` for each tracer handle
47+
- Multiple tracers can coexist, each with their own enabled/disabled state
48+
- A tracer being enabled has no effect if the tracing layer itself is disabled
49+
50+
**In summary:**
51+
- The **tracing layer** must be enabled for any tracing to occur
52+
- Individual **tracers** must also be enabled for their callbacks to be invoked
53+
- Disabling the tracing layer stops all tracing regardless of individual tracer states
54+
- When the tracing layer is enabled, only those tracers that are also enabled will have their callbacks invoked
1255

1356
## Tracing API
1457
The API for using this tracing implementation is this header file below. Please examine that header file for tracing API details.
@@ -89,6 +132,7 @@ The following pseudo-code demonstrates a basic usage of API tracing:
89132
```
90133
#include "level_zero/ze_api.h"
91134
#include "level_zero/layers/zel_tracing_api.h"
135+
#include "level_zero/loader/ze_loader.h"
92136
93137
typedef struct _my_tracer_data_t
94138
{
@@ -137,7 +181,7 @@ void TracingExample1( ... )
137181
tracer_desc.stype = ZEL_STRUCTURE_TYPE_TRACER_DESC;
138182
tracer_desc.pUserData = &tracer_data;
139183
zel_tracer_handle_t hTracer;
140-
zelTracerCreate(hDevice, &tracer_desc, &hTracer);
184+
zelTracerCreate(&tracer_desc, &hTracer);
141185
142186
// Set all callbacks
143187
zel_core_callbacks_t prologCbs = {};
@@ -164,7 +208,7 @@ void TracingExample2( ... )
164208
tracer_desc.stype = ZEL_STRUCTURE_TYPE_TRACER_DESC;
165209
tracer_desc.pUserData = &tracer_data;
166210
zel_tracer_handle_t hTracer;
167-
zelTracerCreate(hDevice, &tracer_desc, &hTracer);
211+
zelTracerCreate(&tracer_desc, &hTracer);
168212
169213
zelTracerCommandListAppendLaunchKernelRegisterCallback(hTracer, ZEL_REGISTER_PROLOGUE, OnEnterCommandListAppendLaunchKernel);
170214
zelTracerCommandListAppendLaunchKernelRegisterCallback(hTracer, ZEL_REGISTER_EPILOGUE, OnExitCommandListAppendLaunchKernel);
@@ -176,4 +220,46 @@ void TracingExample2( ... )
176220
zelTracerSetEnabled(hTracer, false);
177221
zelTracerDestroy(hTracer);
178222
}
223+
224+
// An example demonstrating dynamic tracing layer control
225+
void DynamicTracingExample( ... )
226+
{
227+
// Query current tracing layer state
228+
bool tracingEnabled = false;
229+
zelGetTracingLayerState(&tracingEnabled);
230+
printf("Tracing layer initially %s\n", tracingEnabled ? "enabled" : "disabled");
231+
232+
// Enable the tracing layer dynamically
233+
ze_result_t result = zelEnableTracingLayer();
234+
if (result == ZE_RESULT_SUCCESS) {
235+
printf("Tracing layer enabled successfully\n");
236+
}
237+
238+
// Create and configure tracer
239+
my_tracer_data_t tracer_data = {};
240+
zel_tracer_desc_t tracer_desc;
241+
tracer_desc.stype = ZEL_STRUCTURE_TYPE_TRACER_DESC;
242+
tracer_desc.pUserData = &tracer_data;
243+
zel_tracer_handle_t hTracer;
244+
zelTracerCreate(&tracer_desc, &hTracer);
245+
246+
zelTracerCommandListAppendLaunchKernelRegisterCallback(hTracer, ZEL_REGISTER_PROLOGUE, OnEnterCommandListAppendLaunchKernel);
247+
zelTracerCommandListAppendLaunchKernelRegisterCallback(hTracer, ZEL_REGISTER_EPILOGUE, OnExitCommandListAppendLaunchKernel);
248+
249+
// Enable the tracer (note: tracing layer must also be enabled)
250+
zelTracerSetEnabled(hTracer, true);
251+
252+
// Code section where tracing is active
253+
zeCommandListAppendLaunchKernel(hCommandList, hFunction, &launchArgs, nullptr, 0, nullptr);
254+
255+
// Disable the tracer
256+
zelTracerSetEnabled(hTracer, false);
257+
zelTracerDestroy(hTracer);
258+
259+
// Disable the tracing layer to reduce overhead for subsequent code
260+
zelDisableTracingLayer();
261+
262+
// Subsequent API calls will not be traced
263+
zeCommandListAppendLaunchKernel(hCommandList, hFunction, &launchArgs, nullptr, 0, nullptr);
264+
}
179265
```

0 commit comments

Comments
 (0)