From 72ad40698be9d7dbaa2d5b32f1fb6540d6f9df72 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 16 Aug 2022 13:55:46 +0200 Subject: [PATCH] Move initialization of callstack structs to a thread. Initializing structures for callstack processing (building memory map of the process, gathering kernel symbols, etc) takes some time, which in some cases may be significant. Callstack queries are now handled on a separate thread. In such setup it no longer makes sense to block main thread execution with this lengthy init process. All the heavy initialization phase has been now moved to this separate processing thread. Some initial callstack queries may now not produce responses as promptly as before, but this is only because the main thread is able to start working earlier. Some parts of the initialization process may be critical to do in the main thread, for example because the function responsible for gathering callstacks must be loaded first. This is done still on the main thread, in a new function InitCallstackCritical(). --- public/client/TracyCallstack.cpp | 14 +++++++++++++- public/client/TracyCallstack.hpp | 1 + public/client/TracyProfiler.cpp | 6 +++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/public/client/TracyCallstack.cpp b/public/client/TracyCallstack.cpp index 0108deac..6560de33 100644 --- a/public/client/TracyCallstack.cpp +++ b/public/client/TracyCallstack.cpp @@ -138,9 +138,13 @@ KernelDriver* s_krnlCache = nullptr; size_t s_krnlCacheCnt; -void InitCallstack() +void InitCallstackCritical() { ___tracy_RtlWalkFrameChain = (___tracy_t_RtlWalkFrameChain)GetProcAddress( GetModuleHandleA( "ntdll.dll" ), "RtlWalkFrameChain" ); +} + +void InitCallstack() +{ _SymAddrIncludeInlineTrace = (t_SymAddrIncludeInlineTrace)GetProcAddress( GetModuleHandleA( "dbghelp.dll" ), "SymAddrIncludeInlineTrace" ); _SymQueryInlineTrace = (t_SymQueryInlineTrace)GetProcAddress( GetModuleHandleA( "dbghelp.dll" ), "SymQueryInlineTrace" ); _SymFromInlineContext = (t_SymFromInlineContext)GetProcAddress( GetModuleHandleA( "dbghelp.dll" ), "SymFromInlineContext" ); @@ -695,6 +699,10 @@ static void InitKernelSymbols() } #endif +void InitCallstackCritical() +{ +} + void InitCallstack() { cb_bts = backtrace_create_state( nullptr, 0, nullptr, nullptr ); @@ -1028,6 +1036,10 @@ CallstackEntryData DecodeCallstackPtr( uint64_t ptr ) #elif TRACY_HAS_CALLSTACK == 5 +void InitCallstackCritical() +{ +} + void InitCallstack() { ___tracy_init_demangle_buffer(); diff --git a/public/client/TracyCallstack.hpp b/public/client/TracyCallstack.hpp index 117d8044..bc50561c 100644 --- a/public/client/TracyCallstack.hpp +++ b/public/client/TracyCallstack.hpp @@ -55,6 +55,7 @@ CallstackSymbolData DecodeCodeAddress( uint64_t ptr ); const char* DecodeCallstackPtrFast( uint64_t ptr ); CallstackEntryData DecodeCallstackPtr( uint64_t ptr ); void InitCallstack(); +void InitCallstackCritical(); void EndCallstack(); const char* GetKernelModulePath( uint64_t addr ); diff --git a/public/client/TracyProfiler.cpp b/public/client/TracyProfiler.cpp index 658f0b39..f48f96e2 100644 --- a/public/client/TracyProfiler.cpp +++ b/public/client/TracyProfiler.cpp @@ -1444,7 +1444,7 @@ void Profiler::SpawnWorkerThreads() #endif #ifdef TRACY_HAS_CALLSTACK - InitCallstack(); + InitCallstackCritical(); #endif m_timeBegin.store( GetTime(), std::memory_order_relaxed ); @@ -3296,11 +3296,11 @@ void Profiler::SymbolWorker() { ThreadExitHandler threadExitHandler; SetThreadName( "Tracy Symbol Worker" ); - while( m_timeBegin.load( std::memory_order_relaxed ) == 0 ) std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); - #ifdef TRACY_USE_RPMALLOC rpmalloc_thread_initialize(); #endif + InitCallstack(); + while( m_timeBegin.load( std::memory_order_relaxed ) == 0 ) std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); for(;;) {