DPC++ Runtime
Runtime libraries for oneAPI DPC++
tracing.cpp
Go to the documentation of this file.
1 //===-------------- tracing.cpp - Level-Zero Host API Tracing --------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifdef XPTI_ENABLE_INSTRUMENTATION
10 #include <xpti/xpti_data_types.h>
11 #include <xpti/xpti_trace_framework.h>
12 #endif
13 
14 #include <exception>
15 #include <layers/zel_tracing_api.h>
16 #include <ze_api.h>
17 
19 
20 constexpr auto ZE_CALL_STREAM_NAME = "sycl.experimental.level_zero.call";
21 constexpr auto ZE_DEBUG_STREAM_NAME = "sycl.experimental.level_zero.debug";
22 
23 thread_local uint64_t CallCorrelationID = 0;
24 thread_local uint64_t DebugCorrelationID = 0;
25 
26 constexpr auto GVerStr = "0.1";
27 constexpr int GMajVer = 0;
28 constexpr int GMinVer = 1;
29 
30 #ifdef XPTI_ENABLE_INSTRUMENTATION
31 static xpti_td *GCallEvent = nullptr;
32 static xpti_td *GDebugEvent = nullptr;
33 static uint8_t GCallStreamID = 0;
34 static uint8_t GDebugStreamID = 0;
35 #endif // XPTI_ENABLE_INSTRUMENTATION
36 
37 enum class ZEApiKind {
38 #define _ZE_API(call, domain, cb, params_type) call,
39 #include "ze_api.def"
40 #undef _ZE_API
41 };
42 
44 #ifdef XPTI_ENABLE_INSTRUMENTATION
45  if (!xptiTraceEnabled())
46  return;
47 
48  // Initialize the required streams and stream ID for use
49  GCallStreamID = xptiRegisterStream(ZE_CALL_STREAM_NAME);
50  xptiInitialize(ZE_CALL_STREAM_NAME, GMajVer, GMinVer, GVerStr);
51  GDebugStreamID = xptiRegisterStream(ZE_DEBUG_STREAM_NAME);
52  xptiInitialize(ZE_DEBUG_STREAM_NAME, GMajVer, GMinVer, GVerStr);
53 
54  uint64_t Dummy;
55  xpti::payload_t ZePayload("Level Zero Plugin Layer");
56  GCallEvent =
57  xptiMakeEvent("Level Zero Plugin Layer", &ZePayload,
58  xpti::trace_algorithm_event, xpti_at::active, &Dummy);
59 
60  xpti::payload_t ZeDebugPayload("Level Zero Plugin Debug Layer");
61  GDebugEvent =
62  xptiMakeEvent("Level Zero Plugin Debug Layer", &ZeDebugPayload,
63  xpti::trace_algorithm_event, xpti_at::active, &Dummy);
64 
65  ze_result_t Status = zeInit(0);
66  if (Status != ZE_RESULT_SUCCESS) {
67  // Most likey there are no Level Zero devices.
68  return;
69  }
70 
71  int Foo = 0;
72  zel_tracer_desc_t TracerDesc = {ZEL_STRUCTURE_TYPE_TRACER_EXP_DESC, nullptr,
73  &Foo};
74  zel_tracer_handle_t Tracer = nullptr;
75 
76  Status = zelTracerCreate(&TracerDesc, &Tracer);
77 
78  if (Status != ZE_RESULT_SUCCESS || Tracer == nullptr) {
79  std::cerr << "[WARNING] Failed to create Level Zero tracer: " << Status
80  << "\n";
81  return;
82  }
83 
84  zel_core_callbacks_t Prologue = {};
85  zel_core_callbacks_t Epilogue = {};
86 
87 #define _ZE_API(call, domain, cb, params_type) \
88  Prologue.domain.cb = [](params_type *Params, ze_result_t, void *, void **) { \
89  if (xptiTraceEnabled()) { \
90  const char *FuncName = #call; \
91  if (xptiCheckTraceEnabled( \
92  GCallStreamID, \
93  (uint16_t)xpti::trace_point_type_t::function_begin)) { \
94  CallCorrelationID = xptiGetUniqueId(); \
95  xptiNotifySubscribers( \
96  GCallStreamID, (uint16_t)xpti::trace_point_type_t::function_begin, \
97  GCallEvent, nullptr, CallCorrelationID, FuncName); \
98  } \
99  if (xptiCheckTraceEnabled( \
100  GDebugStreamID, \
101  (uint16_t)xpti::trace_point_type_t::function_with_args_begin)) { \
102  DebugCorrelationID = xptiGetUniqueId(); \
103  uint32_t FuncID = static_cast<uint32_t>(ZEApiKind::call); \
104  xpti::function_with_args_t Payload{FuncID, FuncName, Params, nullptr, \
105  nullptr}; \
106  xptiNotifySubscribers( \
107  GDebugStreamID, \
108  (uint16_t)xpti::trace_point_type_t::function_with_args_begin, \
109  GDebugEvent, nullptr, DebugCorrelationID, &Payload); \
110  } \
111  } \
112  }; \
113  Epilogue.domain.cb = [](params_type *Params, ze_result_t Result, void *, \
114  void **) { \
115  if (xptiTraceEnabled()) { \
116  const char *FuncName = #call; \
117  if (xptiCheckTraceEnabled( \
118  GCallStreamID, \
119  (uint16_t)xpti::trace_point_type_t::function_end)) { \
120  xptiNotifySubscribers( \
121  GCallStreamID, (uint16_t)xpti::trace_point_type_t::function_end, \
122  GCallEvent, nullptr, CallCorrelationID, FuncName); \
123  } \
124  if (xptiCheckTraceEnabled( \
125  GDebugStreamID, \
126  (uint16_t)xpti::trace_point_type_t::function_with_args_end)) { \
127  uint32_t FuncID = static_cast<uint32_t>(ZEApiKind::call); \
128  xpti::function_with_args_t Payload{FuncID, FuncName, Params, &Result, \
129  nullptr}; \
130  xptiNotifySubscribers( \
131  GDebugStreamID, \
132  (uint16_t)xpti::trace_point_type_t::function_with_args_end, \
133  GDebugEvent, nullptr, DebugCorrelationID, &Payload); \
134  } \
135  } \
136  };
137 
138 #include "ze_api.def"
139 
140 #undef _ZE_API
141 
142  Status = zelTracerSetPrologues(Tracer, &Prologue);
143  if (Status != ZE_RESULT_SUCCESS) {
144  std::cerr << "Failed to enable Level Zero tracing\n";
145  std::terminate();
146  }
147  Status = zelTracerSetEpilogues(Tracer, &Epilogue);
148  if (Status != ZE_RESULT_SUCCESS) {
149  std::cerr << "Failed to enable Level Zero tracing\n";
150  std::terminate();
151  }
152 
153  Status = zelTracerSetEnabled(Tracer, true);
154  if (Status != ZE_RESULT_SUCCESS) {
155  std::cerr << "Failed to enable Level Zero tracing\n";
156  std::terminate();
157  }
158 #endif // XPTI_ENABLE_INSTRUMENTATION
159 }
160 
162 #ifdef XPTI_ENABLE_INSTRUMENTATION
163  if (!xptiTraceEnabled())
164  return;
165 
166  xptiFinalize(ZE_CALL_STREAM_NAME);
167  xptiFinalize(ZE_DEBUG_STREAM_NAME);
168 #endif // XPTI_ENABLE_INSTRUMENTATION
169 }
__SYCL_EXTERN_STREAM_ATTRS ostream cerr
Linked to standard error (unbuffered)
constexpr auto ZE_DEBUG_STREAM_NAME
Definition: tracing.cpp:21
void disableZeTracing()
Definition: tracing.cpp:161
thread_local uint64_t CallCorrelationID
Definition: tracing.cpp:23
ZEApiKind
Definition: tracing.cpp:37
constexpr int GMajVer
Definition: tracing.cpp:27
constexpr auto ZE_CALL_STREAM_NAME
Definition: tracing.cpp:20
constexpr int GMinVer
Definition: tracing.cpp:28
constexpr auto GVerStr
Definition: tracing.cpp:26
void enableZeTracing()
Definition: tracing.cpp:43
thread_local uint64_t DebugCorrelationID
Definition: tracing.cpp:24