20 #ifdef XPTI_ENABLE_INSTRUMENTATION
22 #include "xpti/xpti_trace_framework.h"
26 inline namespace _V1 {
28 #ifdef XPTI_ENABLE_INSTRUMENTATION
29 extern xpti::trace_event_data_t *GPICallEvent;
30 extern xpti::trace_event_data_t *GPIArgCallEvent;
31 extern uint8_t PiCallStreamID;
32 extern uint8_t PiDebugCallStreamID;
35 template <
PiApiKind Kind,
size_t Idx,
typename... Args>
40 #define _PI_API(api) \
41 template <> struct PiApiArgTuple<PiApiKind::api> { \
42 using type = typename function_traits<decltype(api)>::args_type; \
45 #include <sycl/detail/pi.def>
48 template <PiApiKind Kind,
size_t Idx,
typename T>
50 static void fill(
unsigned char *Dst, T &&Arg) {
53 auto RealArg = (std::tuple_element_t<Idx, ArgsTuple>)(Arg);
54 *(std::remove_cv_t<std::tuple_element_t<Idx, ArgsTuple>> *)Dst = RealArg;
58 template <
PiApiKind Kind,
size_t Idx,
typename T,
typename... Args>
60 static void fill(
unsigned char *Dst,
const T &&Arg, Args &&...Rest) {
63 auto RealArg = (std::tuple_element_t<Idx, ArgsTuple>)(Arg);
64 *(std::remove_cv_t<std::tuple_element_t<Idx, ArgsTuple>> *)Dst = RealArg;
66 Dst +
sizeof(decltype(RealArg)), std::forward<Args>(Rest)...);
70 template <
typename... Ts>
71 constexpr
size_t totalSize(
const std::tuple<Ts...> &) {
72 return (
sizeof(Ts) + ...);
75 template <
PiApiKind Kind,
typename... ArgsT>
79 constexpr
size_t TotalSize =
totalSize(ArgsTuple{});
81 std::array<unsigned char, TotalSize> ArgsData;
83 std::forward<ArgsT>(Args)...);
95 plugin(
const std::shared_ptr<sycl::detail::pi::PiPlugin> &Plugin,
96 backend UseBackend,
void *LibraryHandle)
97 : MPlugin(Plugin), MBackend(UseBackend), MLibraryHandle(LibraryHandle),
98 TracingMutex(
std::make_shared<
std::mutex>()),
99 MPluginMutex(
std::make_shared<
std::mutex>()) {}
118 template <
typename Exception = sycl::runtime_error>
120 char *message =
nullptr;
121 if (
pi_result == PI_ERROR_PLUGIN_SPECIFIC_ERROR) {
122 pi_result = call_nocheck<PiApiKind::piPluginGetLastError>(&message);
136 template <sycl::errc errc>
138 if (
pi_result == PI_ERROR_PLUGIN_SPECIFIC_ERROR) {
139 char *message =
nullptr;
140 pi_result = call_nocheck<PiApiKind::piPluginGetLastError>(&message);
156 throw sycl::runtime_error(std::string(
context) +
157 " API failed with error: " +
174 template <
PiApiKind PiApiOffset,
typename... ArgsT>
177 #ifdef XPTI_ENABLE_INSTRUMENTATION
178 bool CorrelationIDAvailable =
false, CorrelationIDWithArgsAvailable =
false;
182 const char *PIFnName = PiCallInfo.getFuncName();
183 uint64_t CorrelationIDWithArgs = 0, CorrelationID = 0;
185 if (xptiCheckTraceEnabled(
187 (uint16_t)xpti::trace_point_type_t::function_begin)) {
189 CorrelationIDAvailable =
true;
191 unsigned char *ArgsDataPtr =
nullptr;
197 if (xptiCheckTraceEnabled(
199 (uint16_t)xpti::trace_point_type_t::function_with_args_begin)) {
200 using PackCallArgumentsTy = decltype(packCallArguments<PiApiOffset>(
201 std::forward<ArgsT>(Args)...));
204 ? packCallArguments<PiApiOffset>(std::forward<ArgsT>(Args)...)
205 : PackCallArgumentsTy{};
207 ArgsDataPtr = ArgsData.data();
209 static_cast<uint32_t
>(PiApiOffset), PIFnName, ArgsDataPtr, *MPlugin);
210 CorrelationIDWithArgsAvailable =
true;
215 std::lock_guard<std::mutex> Guard(*TracingMutex);
216 const char *FnName = PiCallInfo.getFuncName();
217 std::cout <<
"---> " << FnName <<
"(" << std::endl;
220 R = PiCallInfo.getFuncPtr(*MPlugin)(Args...);
227 std::cout <<
"API Called After Plugin Teardown, Functon Call ignored.";
232 R = PiCallInfo.getFuncPtr(*MPlugin)(Args...);
235 #ifdef XPTI_ENABLE_INSTRUMENTATION
239 if (CorrelationIDAvailable) {
243 if (CorrelationIDWithArgsAvailable) {
245 static_cast<uint32_t
>(PiApiOffset),
246 PIFnName, ArgsDataPtr, R, *MPlugin);
255 template <
PiApiKind PiApiOffset,
typename... ArgsT>
256 void call(ArgsT... Args)
const {
263 void call(ArgsT... Args)
const {
265 checkPiResult<errc>(Err);
284 auto It = std::find(PiPlatforms.begin(), PiPlatforms.end(), Platform);
285 if (It != PiPlatforms.end())
286 return It - PiPlatforms.begin();
288 PiPlatforms.push_back(Platform);
289 LastDeviceIds.push_back(0);
290 return PiPlatforms.size() - 1;
302 return LastDeviceIds[PlatformId - 1];
309 LastDeviceIds[PlatformId] = Id;
317 if (PlatformId > 0 &&
318 LastDeviceIds[PlatformId] < LastDeviceIds[PlatformId - 1])
319 LastDeviceIds[PlatformId] = LastDeviceIds[PlatformId - 1];
323 auto It = std::find(PiPlatforms.begin(), PiPlatforms.end(), Platform);
324 return It != PiPlatforms.end();
331 std::shared_ptr<sycl::detail::pi::PiPlugin> MPlugin;
333 void *MLibraryHandle;
334 std::shared_ptr<std::mutex> TracingMutex;
338 std::shared_ptr<std::mutex> MPluginMutex;
340 std::vector<sycl::detail::pi::PiPlatform> PiPlatforms;
343 std::vector<int> LastDeviceIds;
346 using PluginPtr = std::shared_ptr<plugin>;