|
| 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 | +#include "xpti/xpti_data_types.h" |
| 10 | +#include <exception> |
| 11 | +#include <level_zero/layers/zel_tracing_api.h> |
| 12 | +#include <level_zero/ze_api.h> |
| 13 | +#include <xpti/xpti_trace_framework.h> |
| 14 | + |
| 15 | +#include <iostream> |
| 16 | + |
| 17 | +constexpr auto ZE_CALL_STREAM_NAME = "sycl.experimental.level_zero.call"; |
| 18 | +constexpr auto ZE_DEBUG_STREAM_NAME = "sycl.experimental.level_zero.debug"; |
| 19 | + |
| 20 | +thread_local uint64_t CallCorrelationID = 0; |
| 21 | +thread_local uint64_t DebugCorrelationID = 0; |
| 22 | + |
| 23 | +constexpr auto GVerStr = "0.1"; |
| 24 | +constexpr int GMajVer = 0; |
| 25 | +constexpr int GMinVer = 1; |
| 26 | + |
| 27 | +#ifdef XPTI_ENABLE_INSTRUMENTATION |
| 28 | +static xpti_td *GCallEvent = nullptr; |
| 29 | +static xpti_td *GDebugEvent = nullptr; |
| 30 | +#endif // XPTI_ENABLE_INSTRUMENTATION |
| 31 | + |
| 32 | +enum class ZEApiKind { |
| 33 | +#define _ZE_API(call, domain, cb, params_type) call, |
| 34 | +#include "ze_api.def" |
| 35 | +#undef _ZE_API |
| 36 | +}; |
| 37 | + |
| 38 | +void enableZeTracing() { |
| 39 | +#ifdef XPTI_ENABLE_INSTRUMENTATION |
| 40 | + if (!xptiTraceEnabled()) |
| 41 | + return; |
| 42 | + |
| 43 | + xptiRegisterStream(ZE_CALL_STREAM_NAME); |
| 44 | + xptiInitialize(ZE_CALL_STREAM_NAME, GMajVer, GMinVer, GVerStr); |
| 45 | + xptiRegisterStream(ZE_DEBUG_STREAM_NAME); |
| 46 | + xptiInitialize(ZE_DEBUG_STREAM_NAME, GMajVer, GMinVer, GVerStr); |
| 47 | + |
| 48 | + uint64_t Dummy; |
| 49 | + xpti::payload_t ZePayload("Level Zero Plugin Layer"); |
| 50 | + GCallEvent = |
| 51 | + xptiMakeEvent("Level Zero Plugin Layer", &ZePayload, |
| 52 | + xpti::trace_algorithm_event, xpti_at::active, &Dummy); |
| 53 | + |
| 54 | + xpti::payload_t ZeDebugPayload("Level Zero Plugin Debug Layer"); |
| 55 | + GDebugEvent = |
| 56 | + xptiMakeEvent("Level Zero Plugin Debug Layer", &ZeDebugPayload, |
| 57 | + xpti::trace_algorithm_event, xpti_at::active, &Dummy); |
| 58 | + |
| 59 | + ze_result_t Status = zeInit(0); |
| 60 | + if (Status != ZE_RESULT_SUCCESS) { |
| 61 | + // Most likey there are no Level Zero devices. |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + int Foo = 0; |
| 66 | + zel_tracer_desc_t TracerDesc = {ZEL_STRUCTURE_TYPE_TRACER_EXP_DESC, nullptr, |
| 67 | + &Foo}; |
| 68 | + zel_tracer_handle_t Tracer = nullptr; |
| 69 | + |
| 70 | + Status = zelTracerCreate(&TracerDesc, &Tracer); |
| 71 | + |
| 72 | + if (Status != ZE_RESULT_SUCCESS || Tracer == nullptr) { |
| 73 | + std::cerr << "[WARNING] Failed to create Level Zero tracer: " << Status |
| 74 | + << "\n"; |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + zel_core_callbacks_t Prologue = {}; |
| 79 | + zel_core_callbacks_t Epilogue = {}; |
| 80 | + |
| 81 | +#define _ZE_API(call, domain, cb, params_type) \ |
| 82 | + Prologue.domain.cb = [](params_type *Params, ze_result_t, void *, void **) { \ |
| 83 | + if (xptiTraceEnabled()) { \ |
| 84 | + uint8_t CallStreamID = xptiRegisterStream(ZE_CALL_STREAM_NAME); \ |
| 85 | + uint8_t DebugStreamID = xptiRegisterStream(ZE_DEBUG_STREAM_NAME); \ |
| 86 | + CallCorrelationID = xptiGetUniqueId(); \ |
| 87 | + DebugCorrelationID = xptiGetUniqueId(); \ |
| 88 | + const char *FuncName = #call; \ |
| 89 | + xptiNotifySubscribers( \ |
| 90 | + CallStreamID, (uint16_t)xpti::trace_point_type_t::function_begin, \ |
| 91 | + GCallEvent, nullptr, CallCorrelationID, FuncName); \ |
| 92 | + uint32_t FuncID = static_cast<uint32_t>(ZEApiKind::call); \ |
| 93 | + xpti::function_with_args_t Payload{FuncID, FuncName, Params, nullptr, \ |
| 94 | + nullptr}; \ |
| 95 | + xptiNotifySubscribers( \ |
| 96 | + DebugStreamID, \ |
| 97 | + (uint16_t)xpti::trace_point_type_t::function_with_args_begin, \ |
| 98 | + GDebugEvent, nullptr, DebugCorrelationID, &Payload); \ |
| 99 | + } \ |
| 100 | + }; \ |
| 101 | + Epilogue.domain.cb = [](params_type *Params, ze_result_t Result, void *, \ |
| 102 | + void **) { \ |
| 103 | + if (xptiTraceEnabled()) { \ |
| 104 | + uint8_t CallStreamID = xptiRegisterStream(ZE_CALL_STREAM_NAME); \ |
| 105 | + uint8_t DebugStreamID = xptiRegisterStream(ZE_DEBUG_STREAM_NAME); \ |
| 106 | + const char *FuncName = #call; \ |
| 107 | + xptiNotifySubscribers(CallStreamID, \ |
| 108 | + (uint16_t)xpti::trace_point_type_t::function_end, \ |
| 109 | + GCallEvent, nullptr, CallCorrelationID, FuncName); \ |
| 110 | + uint32_t FuncID = static_cast<uint32_t>(ZEApiKind::call); \ |
| 111 | + xpti::function_with_args_t Payload{FuncID, FuncName, Params, &Result, \ |
| 112 | + nullptr}; \ |
| 113 | + xptiNotifySubscribers( \ |
| 114 | + DebugStreamID, \ |
| 115 | + (uint16_t)xpti::trace_point_type_t::function_with_args_end, \ |
| 116 | + GDebugEvent, nullptr, DebugCorrelationID, &Payload); \ |
| 117 | + } \ |
| 118 | + }; |
| 119 | + |
| 120 | +#include "ze_api.def" |
| 121 | + |
| 122 | +#undef _ZE_API |
| 123 | + |
| 124 | + Status = zelTracerSetPrologues(Tracer, &Prologue); |
| 125 | + if (Status != ZE_RESULT_SUCCESS) { |
| 126 | + std::cerr << "Failed to enable Level Zero tracing\n"; |
| 127 | + std::terminate(); |
| 128 | + } |
| 129 | + Status = zelTracerSetEpilogues(Tracer, &Epilogue); |
| 130 | + if (Status != ZE_RESULT_SUCCESS) { |
| 131 | + std::cerr << "Failed to enable Level Zero tracing\n"; |
| 132 | + std::terminate(); |
| 133 | + } |
| 134 | + |
| 135 | + Status = zelTracerSetEnabled(Tracer, true); |
| 136 | + if (Status != ZE_RESULT_SUCCESS) { |
| 137 | + std::cerr << "Failed to enable Level Zero tracing\n"; |
| 138 | + std::terminate(); |
| 139 | + } |
| 140 | +#endif // XPTI_ENABLE_INSTRUMENTATION |
| 141 | +} |
| 142 | + |
| 143 | +void disableZeTracing() { |
| 144 | +#ifdef XPTI_ENABLE_INSTRUMENTATION |
| 145 | + if (!xptiTraceEnabled()) |
| 146 | + return; |
| 147 | + |
| 148 | + xptiFinalize(ZE_CALL_STREAM_NAME); |
| 149 | + xptiFinalize(ZE_DEBUG_STREAM_NAME); |
| 150 | +#endif // XPTI_ENABLE_INSTRUMENTATION |
| 151 | +} |
0 commit comments