99# include < tlhelp32.h>
1010# include < inttypes.h>
1111# include < intrin.h>
12- # include " ../common/TracyWinFamily .hpp"
12+ # include " ../common/TracyUwp .hpp"
1313# ifndef _MSC_VER
1414# include < excpt.h>
1515# endif
@@ -327,13 +327,7 @@ static inline void CpuId( uint32_t* regs, uint32_t leaf )
327327
328328static void InitFailure ( const char * msg )
329329{
330- #if defined TRACY_GDK
331- const char * format = " Tracy Profiler initialization failure: %s\n " ;
332- const int length = snprintf ( nullptr , 0 , format, msg );
333- char * buffer = (char *)alloca ( length + 1 );
334- snprintf ( buffer, length + 1 , format, msg );
335- OutputDebugStringA ( buffer );
336- #elif defined _WIN32
330+ #if defined _WIN32
337331 bool hasConsole = false ;
338332 bool reopen = false ;
339333 const auto attached = AttachConsole ( ATTACH_PARENT_PROCESS );
@@ -516,7 +510,7 @@ static const char* GetHostInfo()
516510 static char buf[1024 ];
517511 auto ptr = buf;
518512#if defined _WIN32
519- # if defined TRACY_WIN32_NO_DESKTOP
513+ # ifdef TRACY_UWP
520514 auto GetVersion = &::GetVersionEx;
521515# else
522516 auto GetVersion = (t_RtlGetVersion)GetProcAddress ( GetModuleHandleA ( " ntdll.dll" ), " RtlGetVersion" );
@@ -599,7 +593,7 @@ static const char* GetHostInfo()
599593 char hostname[512 ];
600594 gethostname ( hostname, 512 );
601595
602- # if defined TRACY_WIN32_NO_DESKTOP
596+ # ifdef TRACY_UWP
603597 const char * user = " " ;
604598# else
605599 DWORD userSz = UNLEN+1 ;
@@ -810,7 +804,7 @@ static BroadcastMessage& GetBroadcastMessage( const char* procname, size_t pnsz,
810804 return msg;
811805}
812806
813- #if defined _WIN32 && !defined TRACY_WIN32_NO_DESKTOP && !defined TRACY_NO_CRASH_HANDLER
807+ #if defined _WIN32 && !defined TRACY_UWP && !defined TRACY_NO_CRASH_HANDLER
814808static DWORD s_profilerThreadId = 0 ;
815809static DWORD s_symbolThreadId = 0 ;
816810static char s_crashText[1024 ];
@@ -1171,38 +1165,6 @@ static void CrashHandler( int signal, siginfo_t* info, void* /*ucontext*/ )
11711165}
11721166#endif
11731167
1174- #ifdef TRACY_HAS_SYSTEM_TRACING
1175- static void StartSystemTracing ( int64_t & samplingPeriod )
1176- {
1177- assert ( s_sysTraceThread == nullptr );
1178-
1179- // use TRACY_NO_SYS_TRACE=1 to force disabling sys tracing (even if available in the underlying system)
1180- // as it can have significant impact on the size of the traces
1181- const char * noSysTrace = GetEnvVar ( " TRACY_NO_SYS_TRACE" );
1182- const bool disableSystrace = (noSysTrace && noSysTrace[0 ] == ' 1' );
1183- if ( disableSystrace )
1184- {
1185- TracyDebug (" TRACY: Sys Trace was disabled by 'TRACY_NO_SYS_TRACE=1'\n " );
1186- }
1187- else if ( SysTraceStart ( samplingPeriod ) )
1188- {
1189- s_sysTraceThread = (Thread*)tracy_malloc ( sizeof ( Thread ) );
1190- new (s_sysTraceThread) Thread ( SysTraceWorker, nullptr );
1191- std::this_thread::sleep_for ( std::chrono::milliseconds ( 1 ) );
1192- }
1193- }
1194-
1195- static void StopSystemTracing ()
1196- {
1197- if ( s_sysTraceThread )
1198- {
1199- SysTraceStop ();
1200- s_sysTraceThread->~Thread ();
1201- tracy_free ( s_sysTraceThread );
1202- s_sysTraceThread = nullptr ;
1203- }
1204- }
1205- #endif
12061168
12071169enum { QueuePrealloc = 256 * 1024 };
12081170
@@ -1556,7 +1518,7 @@ void Profiler::InstallCrashHandler()
15561518 sigaction ( SIGABRT, &crashHandler, &m_prevSignal.abrt );
15571519#endif
15581520
1559- #if defined _WIN32 && !defined TRACY_WIN32_NO_DESKTOP && !defined TRACY_NO_CRASH_HANDLER
1521+ #if defined _WIN32 && !defined TRACY_UWP && !defined TRACY_NO_CRASH_HANDLER
15601522 // We cannot use Vectored Exception handling because it catches application-wide frame-based SEH blocks. We only
15611523 // want to catch unhandled exceptions.
15621524 m_prevHandler = reinterpret_cast <void *>( SetUnhandledExceptionFilter ( CrashFilter ) );
@@ -1570,7 +1532,7 @@ void Profiler::InstallCrashHandler()
15701532
15711533void Profiler::RemoveCrashHandler ()
15721534{
1573- #if defined _WIN32 && !defined TRACY_WIN32_NO_DESKTOP && !defined TRACY_NO_CRASH_HANDLER
1535+ #if defined _WIN32 && !defined TRACY_UWP && !defined TRACY_NO_CRASH_HANDLER
15741536 if ( m_crashHandlerInstalled )
15751537 {
15761538 auto prev = SetUnhandledExceptionFilter ( (LPTOP_LEVEL_EXCEPTION_FILTER)m_prevHandler );
@@ -1601,7 +1563,20 @@ void Profiler::RemoveCrashHandler()
16011563void Profiler::SpawnWorkerThreads ()
16021564{
16031565#ifdef TRACY_HAS_SYSTEM_TRACING
1604- StartSystemTracing ( m_samplingPeriod );
1566+ // use TRACY_NO_SYS_TRACE=1 to force disabling sys tracing (even if available in the underlying system)
1567+ // as it can have significant impact on the size of the traces
1568+ const char * noSysTrace = GetEnvVar ( " TRACY_NO_SYS_TRACE" );
1569+ const bool disableSystrace = (noSysTrace && noSysTrace[0 ] == ' 1' );
1570+ if ( disableSystrace )
1571+ {
1572+ TracyDebug (" TRACY: Sys Trace was disabled by 'TRACY_NO_SYS_TRACE=1'\n " );
1573+ }
1574+ else if ( SysTraceStart ( m_samplingPeriod ) )
1575+ {
1576+ s_sysTraceThread = (Thread*)tracy_malloc ( sizeof ( Thread ) );
1577+ new (s_sysTraceThread) Thread ( SysTraceWorker, nullptr );
1578+ std::this_thread::sleep_for ( std::chrono::milliseconds ( 1 ) );
1579+ }
16051580#endif
16061581
16071582 s_thread = (Thread*)tracy_malloc ( sizeof ( Thread ) );
@@ -1617,7 +1592,7 @@ void Profiler::SpawnWorkerThreads()
16171592 new (s_symbolThread) Thread ( LaunchSymbolWorker, this );
16181593#endif
16191594
1620- #if defined _WIN32 && !defined TRACY_WIN32_NO_DESKTOP && !defined TRACY_NO_CRASH_HANDLER
1595+ #if defined _WIN32 && !defined TRACY_UWP && !defined TRACY_NO_CRASH_HANDLER
16211596 s_profilerThreadId = GetThreadId ( s_thread->Handle () );
16221597# ifdef TRACY_HAS_CALLSTACK
16231598 s_symbolThreadId = GetThreadId ( s_symbolThread->Handle () );
@@ -1638,7 +1613,12 @@ Profiler::~Profiler()
16381613 RemoveCrashHandler ();
16391614
16401615#ifdef TRACY_HAS_SYSTEM_TRACING
1641- StopSystemTracing ();
1616+ if ( s_sysTraceThread )
1617+ {
1618+ SysTraceStop ();
1619+ s_sysTraceThread->~Thread ();
1620+ tracy_free ( s_sysTraceThread );
1621+ }
16421622#endif
16431623
16441624#ifdef TRACY_HAS_CALLSTACK
@@ -1791,6 +1771,7 @@ void Profiler::Worker()
17911771 MemWrite ( &welcome.timerMul , m_timerMul );
17921772 MemWrite ( &welcome.initBegin , GetInitTime () );
17931773 MemWrite ( &welcome.initEnd , m_timeBegin.load ( std::memory_order_relaxed ) );
1774+ MemWrite ( &welcome.delay , m_delay );
17941775 MemWrite ( &welcome.resolution , m_resolution );
17951776 MemWrite ( &welcome.epoch , m_epoch );
17961777 MemWrite ( &welcome.exectime , m_exectime );
@@ -2030,6 +2011,7 @@ void Profiler::Worker()
20302011 }
20312012 else if ( status == DequeueStatus::QueueEmpty && serialStatus == DequeueStatus::QueueEmpty )
20322013 {
2014+ if ( ShouldExit () ) break ;
20332015 if ( m_bufferOffset != m_bufferStart )
20342016 {
20352017 if ( !CommitData () ) break ;
@@ -2060,7 +2042,7 @@ void Profiler::Worker()
20602042 connActive = HandleServerQuery ();
20612043 if ( !connActive ) break ;
20622044 }
2063- if ( !connActive || ShouldExit () ) break ;
2045+ if ( !connActive ) break ;
20642046 }
20652047 if ( ShouldExit () ) break ;
20662048
@@ -2126,13 +2108,7 @@ void Profiler::Worker()
21262108 while ( s_symbolThreadGone.load () == false ) { YieldThread (); }
21272109#endif
21282110
2129- // Client is exiting.
2130- #ifdef TRACY_HAS_SYSTEM_TRACING
2131- // Stop filling queues with new data.
2132- StopSystemTracing ();
2133- #endif
2134-
2135- // Send items remaining in queues.
2111+ // Client is exiting. Send items remaining in queues.
21362112 for (;;)
21372113 {
21382114 const auto status = Dequeue ( token );
@@ -2383,10 +2359,6 @@ static void FreeAssociatedMemory( const QueueItem& item )
23832359 tracy_free ( (void *)ptr );
23842360 break ;
23852361#endif
2386- case QueueType::GpuAnnotationName:
2387- ptr = MemRead<uint64_t >( &item.gpuAnnotationNameFat .ptr );
2388- tracy_free ( (void *)ptr );
2389- break ;
23902362#ifdef TRACY_ON_DEMAND
23912363 case QueueType::MessageAppInfo:
23922364 case QueueType::GpuContextName:
@@ -2602,12 +2574,6 @@ Profiler::DequeueStatus Profiler::Dequeue( moodycamel::ConsumerToken& token )
26022574 tracy_free_fast ( (void *)ptr );
26032575#endif
26042576 break ;
2605- case QueueType::GpuAnnotationName:
2606- ptr = MemRead<uint64_t >( &item->gpuAnnotationNameFat .ptr );
2607- size = MemRead<uint16_t >( &item->gpuAnnotationNameFat .size );
2608- SendSingleString ( (const char *)ptr, size );
2609- tracy_free_fast ( (void *)ptr );
2610- break ;
26112577 case QueueType::PlotDataInt:
26122578 case QueueType::PlotDataFloat:
26132579 case QueueType::PlotDataDouble:
@@ -2966,14 +2932,6 @@ Profiler::DequeueStatus Profiler::DequeueSerial()
29662932#endif
29672933 break ;
29682934 }
2969- case QueueType::GpuAnnotationName:
2970- {
2971- ptr = MemRead<uint64_t >( &item->gpuAnnotationNameFat .ptr );
2972- uint16_t size = MemRead<uint16_t >( &item->gpuAnnotationNameFat .size );
2973- SendSingleString ( (const char *)ptr, size );
2974- tracy_free_fast ( (void *)ptr );
2975- break ;
2976- }
29772935#ifdef TRACY_FIBERS
29782936 case QueueType::ZoneBegin:
29792937 case QueueType::ZoneBeginCallstack:
@@ -3853,6 +3811,43 @@ void Profiler::CalibrateDelay()
38533811 if ( dti > 0 && dti < mindiff ) mindiff = dti;
38543812 }
38553813 m_resolution = mindiff;
3814+
3815+ #ifdef TRACY_DELAYED_INIT
3816+ m_delay = m_resolution;
3817+ #else
3818+ constexpr int Events = Iterations * 2 ; // start + end
3819+ static_assert ( Events < QueuePrealloc, " Delay calibration loop will allocate memory in queue" );
3820+
3821+ static const tracy::SourceLocationData __tracy_source_location { nullptr , TracyFunction, TracyFile, (uint32_t )TracyLine, 0 };
3822+ const auto t0 = GetTime ();
3823+ for ( int i=0 ; i<Iterations; i++ )
3824+ {
3825+ {
3826+ TracyLfqPrepare ( QueueType::ZoneBegin );
3827+ MemWrite ( &item->zoneBegin .time , Profiler::GetTime () );
3828+ MemWrite ( &item->zoneBegin .srcloc , (uint64_t )&__tracy_source_location );
3829+ TracyLfqCommit;
3830+ }
3831+ {
3832+ TracyLfqPrepare ( QueueType::ZoneEnd );
3833+ MemWrite ( &item->zoneEnd .time , GetTime () );
3834+ TracyLfqCommit;
3835+ }
3836+ }
3837+ const auto t1 = GetTime ();
3838+ const auto dt = t1 - t0;
3839+ m_delay = dt / Events;
3840+
3841+ moodycamel::ConsumerToken token ( GetQueue () );
3842+ int left = Events;
3843+ while ( left != 0 )
3844+ {
3845+ const auto sz = GetQueue ().try_dequeue_bulk_single ( token, [](const uint64_t &){}, [](QueueItem* item, size_t sz){} );
3846+ assert ( sz > 0 );
3847+ left -= (int )sz;
3848+ }
3849+ assert ( GetQueue ().size_approx () == 0 );
3850+ #endif
38563851}
38573852
38583853void Profiler::ReportTopology ()
@@ -3867,7 +3862,7 @@ void Profiler::ReportTopology()
38673862 };
38683863
38693864#if defined _WIN32
3870- # if defined TRACY_WIN32_NO_DESKTOP
3865+ # ifdef TRACY_UWP
38713866 t_GetLogicalProcessorInformationEx _GetLogicalProcessorInformationEx = &::GetLogicalProcessorInformationEx;
38723867# else
38733868 t_GetLogicalProcessorInformationEx _GetLogicalProcessorInformationEx = (t_GetLogicalProcessorInformationEx)GetProcAddress ( GetModuleHandleA ( " kernel32.dll" ), " GetLogicalProcessorInformationEx" );
0 commit comments