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