|
| 1 | +//===-------------- tracing.cpp - CUDA 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 <cuda.h> |
| 15 | +#include <cupti.h> |
| 16 | + |
| 17 | +#include <exception> |
| 18 | +#include <iostream> |
| 19 | + |
| 20 | +constexpr auto CUDA_CALL_STREAM_NAME = "sycl.experimental.cuda.call"; |
| 21 | +constexpr auto CUDA_DEBUG_STREAM_NAME = "sycl.experimental.cuda.debug"; |
| 22 | + |
| 23 | +thread_local uint64_t CallCorrelationID = 0; |
| 24 | +thread_local uint64_t DebugCorrelationID = 0; |
| 25 | + |
| 26 | +#ifdef XPTI_ENABLE_INSTRUMENTATION |
| 27 | +static xpti_td *GCallEvent = nullptr; |
| 28 | +static xpti_td *GDebugEvent = nullptr; |
| 29 | +#endif // XPTI_ENABLE_INSTRUMENTATION |
| 30 | + |
| 31 | +constexpr auto GVerStr = "0.1"; |
| 32 | +constexpr int GMajVer = 0; |
| 33 | +constexpr int GMinVer = 1; |
| 34 | + |
| 35 | +#ifdef XPTI_ENABLE_INSTRUMENTATION |
| 36 | +static void cuptiCallback(void *userdata, CUpti_CallbackDomain, |
| 37 | + CUpti_CallbackId CBID, const void *CBData) { |
| 38 | + if (xptiTraceEnabled()) { |
| 39 | + const auto *CBInfo = static_cast<const CUpti_CallbackData *>(CBData); |
| 40 | + |
| 41 | + if (CBInfo->callbackSite == CUPTI_API_ENTER) { |
| 42 | + CallCorrelationID = xptiGetUniqueId(); |
| 43 | + DebugCorrelationID = xptiGetUniqueId(); |
| 44 | + } |
| 45 | + |
| 46 | + const char *FuncName = CBInfo->functionName; |
| 47 | + uint32_t FuncID = static_cast<uint32_t>(CBID); |
| 48 | + uint16_t TraceTypeArgs = CBInfo->callbackSite == CUPTI_API_ENTER |
| 49 | + ? xpti::trace_function_with_args_begin |
| 50 | + : xpti::trace_function_with_args_end; |
| 51 | + uint16_t TraceType = CBInfo->callbackSite == CUPTI_API_ENTER |
| 52 | + ? xpti::trace_function_begin |
| 53 | + : xpti::trace_function_end; |
| 54 | + |
| 55 | + uint8_t CallStreamID = xptiRegisterStream(CUDA_CALL_STREAM_NAME); |
| 56 | + uint8_t DebugStreamID = xptiRegisterStream(CUDA_DEBUG_STREAM_NAME); |
| 57 | + |
| 58 | + xptiNotifySubscribers(CallStreamID, TraceType, GCallEvent, nullptr, |
| 59 | + CallCorrelationID, FuncName); |
| 60 | + |
| 61 | + xpti::function_with_args_t Payload{ |
| 62 | + FuncID, FuncName, const_cast<void *>(CBInfo->functionParams), |
| 63 | + CBInfo->functionReturnValue, CBInfo->context}; |
| 64 | + xptiNotifySubscribers(DebugStreamID, TraceTypeArgs, GDebugEvent, nullptr, |
| 65 | + DebugCorrelationID, &Payload); |
| 66 | + } |
| 67 | +} |
| 68 | +#endif |
| 69 | + |
| 70 | +void enableCUDATracing() { |
| 71 | +#ifdef XPTI_ENABLE_INSTRUMENTATION |
| 72 | + if (!xptiTraceEnabled()) |
| 73 | + return; |
| 74 | + |
| 75 | + xptiRegisterStream(CUDA_CALL_STREAM_NAME); |
| 76 | + xptiInitialize(CUDA_CALL_STREAM_NAME, GMajVer, GMinVer, GVerStr); |
| 77 | + xptiRegisterStream(CUDA_DEBUG_STREAM_NAME); |
| 78 | + xptiInitialize(CUDA_DEBUG_STREAM_NAME, GMajVer, GMinVer, GVerStr); |
| 79 | + |
| 80 | + uint64_t Dummy; |
| 81 | + xpti::payload_t CUDAPayload("CUDA Plugin Layer"); |
| 82 | + GCallEvent = |
| 83 | + xptiMakeEvent("CUDA Plugin Layer", &CUDAPayload, |
| 84 | + xpti::trace_algorithm_event, xpti_at::active, &Dummy); |
| 85 | + |
| 86 | + xpti::payload_t CUDADebugPayload("CUDA Plugin Debug Layer"); |
| 87 | + GDebugEvent = |
| 88 | + xptiMakeEvent("CUDA Plugin Debug Layer", &CUDADebugPayload, |
| 89 | + xpti::trace_algorithm_event, xpti_at::active, &Dummy); |
| 90 | + |
| 91 | + CUpti_SubscriberHandle Subscriber; |
| 92 | + cuptiSubscribe(&Subscriber, cuptiCallback, nullptr); |
| 93 | + cuptiEnableDomain(1, Subscriber, CUPTI_CB_DOMAIN_DRIVER_API); |
| 94 | + cuptiEnableCallback(0, Subscriber, CUPTI_CB_DOMAIN_DRIVER_API, |
| 95 | + CUPTI_DRIVER_TRACE_CBID_cuGetErrorString); |
| 96 | + cuptiEnableCallback(0, Subscriber, CUPTI_CB_DOMAIN_DRIVER_API, |
| 97 | + CUPTI_DRIVER_TRACE_CBID_cuGetErrorName); |
| 98 | +#endif |
| 99 | +} |
| 100 | + |
| 101 | +void disableCUDATracing() { |
| 102 | +#ifdef XPTI_ENABLE_INSTRUMENTATION |
| 103 | + if (!xptiTraceEnabled()) |
| 104 | + return; |
| 105 | + |
| 106 | + xptiFinalize(CUDA_CALL_STREAM_NAME); |
| 107 | + xptiFinalize(CUDA_DEBUG_STREAM_NAME); |
| 108 | +#endif // XPTI_ENABLE_INSTRUMENTATION |
| 109 | +} |
0 commit comments