@@ -150,12 +150,21 @@ inline bool InMemoryCoreApis(const char* function_name) {
150150
151151class SyclCollector {
152152 public:
153+ // Variables to indicate the presence of a foreign subscriber to XPTI.
154+ // To be set via call to ptiSetXPTIEnvironmentDetails before SyclCollector created
155+ static inline bool foreign_subscriber_ = false ;
156+ static inline bool likely_unitrace_subscriber_ = false ;
157+
153158 inline static auto & Instance () {
154159 static SyclCollector sycl_collector{nullptr };
155160 return sycl_collector;
156161 }
157162
158163 inline void EnableTracing () {
164+ // Do not change the behaviour here depending on foreign_subscriber_!
165+ // The current behavior ensures that in the absense of XPTI subscription (and Sycl records)
166+ // PTI generates to-called Special Records
167+ // About Special Records: see comments in unitrace.h
159168 enabled_ = true ;
160169 xptiForceSetTraceEnabled (enabled_);
161170 }
@@ -361,8 +370,24 @@ class SyclCollector {
361370 }
362371
363372 private:
373+ #define WARNING_FOREIGN_SUBSCRIBER \
374+ " Another subscriber already subscribed to Sycl runtime events, " \
375+ " so PTI will not subscribe to them. It will affect correctness of PTI profile: " \
376+ " e.g. report zero XPU time for CPU callers of GPU kernels."
377+
378+ #define WARNING_LIKELY_UNITRACE_SUBSCRIBER \
379+ " Likely the application running under Unitrace. " \
380+ " To get correct PTI profile - run without Unitrace."
381+
364382 explicit SyclCollector (OnSyclRuntimeViewCallback buffer_callback)
365- : acallback_(buffer_callback), xptiGetStashedKV(GetStashedFuncPtrFromSharedObject()) {}
383+ : acallback_(buffer_callback), xptiGetStashedKV(GetStashedFuncPtrFromSharedObject()) {
384+ static constexpr char warn_foreign_subscriber[] = WARNING_FOREIGN_SUBSCRIBER;
385+ static constexpr char warn_likely_unitrace_subscriber[] = WARNING_LIKELY_UNITRACE_SUBSCRIBER;
386+ if (foreign_subscriber_) {
387+ const char * warn_add = likely_unitrace_subscriber_ ? warn_likely_unitrace_subscriber : " " ;
388+ SPDLOG_WARN (" {}{}" , warn_foreign_subscriber, warn_add);
389+ }
390+ }
366391
367392 int32_t trace_all_env_value = utils::IsSetEnv(" PTI_VIEW_RUNTIME_API" );
368393 inline static thread_local ZeKernelCommandExecutionRecord sycl_runtime_rec_;
@@ -571,18 +596,30 @@ XPTI_CALLBACK_API void xptiTraceInit(unsigned int major_version, unsigned int mi
571596}
572597
573598XPTI_CALLBACK_API void xptiTraceFinish (const char * /* stream_name*/ ) {}
599+
600+ // clang-format off
601+ extern " C" {
602+ void
603+ #if (defined(_WIN32) || defined(_WIN64))
604+ __declspec (dllexport)
605+ #else
606+ __attribute__ ((visibility(" default" )))
607+ #endif
608+ PtiSetXPTIEnvironmentDetails (bool is_foreign_subscriber,
609+ bool is_likely_unitrace_subscriber) {
610+ SyclCollector::foreign_subscriber_ = is_foreign_subscriber;
611+ SyclCollector::likely_unitrace_subscriber_ = is_likely_unitrace_subscriber;
612+ }
613+ }
614+ // clang-format on
615+
574616#if (defined(_WIN32) || defined(_WIN64))
575617
576618#include < windows.h>
577619
578- BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fwdReason, LPVOID /* lpvReserved*/ ) {
620+ BOOL WINAPI DllMain (HINSTANCE /* hinstDLL*/ , DWORD fwdReason, LPVOID /* lpvReserved*/ ) {
579621 switch (fwdReason) {
580622 case DLL_PROCESS_ATTACH: {
581- utils::SetEnv (" XPTI_SUBSCRIBERS" , utils::GetPathToSharedObject (hinstDLL).c_str ());
582- utils::SetEnv (" XPTI_FRAMEWORK_DISPATCHER" ,
583- utils::GetPathToSharedObject (pti::strings::kXptiLibName ).c_str ());
584- utils::SetEnv (" XPTI_TRACE_ENABLE" , " 1" );
585- utils::SetEnv (" UR_ENABLE_LAYERS" , " UR_LAYER_TRACING" );
586623 break ;
587624 }
588625 case DLL_THREAD_ATTACH:
@@ -599,16 +636,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID /*lpvReserved*/)
599636
600637#else // Linux (possibly macOS?)
601638
602- // Work-around for ensuring XPTI_SUBSCRIBERS and XPTI_FRAMEWORK_DISPATCHER are
603- // set before xptiTraceInit() is called.
604- __attribute__ ((constructor)) static void framework_init() {
605- utils::SetEnv (" XPTI_SUBSCRIBERS" , utils::GetPathToSharedObject (Truncate).c_str ());
606- utils::SetEnv (" XPTI_FRAMEWORK_DISPATCHER" , utils::GetPathToSharedObject (xptiReset).c_str ());
607- utils::SetEnv (" XPTI_TRACE_ENABLE" , " 1" );
608-
609- utils::SetEnv (" UR_ENABLE_LAYERS" , " UR_LAYER_TRACING" );
610- }
611-
612639__attribute__ ((destructor)) static void framework_fini() { framework_finalized = true ; }
613640
614641#endif
0 commit comments