From 229d547e91bceb0a912960af09e40fab1b987a5c Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 1 Nov 2021 23:56:19 +0100 Subject: [PATCH] Add fiber support on client side. --- Tracy.hpp | 8 ++++++++ client/TracyProfiler.cpp | 18 ++++++++++++++++++ client/TracyProfiler.hpp | 17 +++++++++++++++++ common/TracyProtocol.hpp | 2 +- common/TracyQueue.hpp | 21 ++++++++++++++++++++- 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/Tracy.hpp b/Tracy.hpp index 8d9ada59..6d22c7d8 100644 --- a/Tracy.hpp +++ b/Tracy.hpp @@ -97,6 +97,9 @@ #define TracyParameterSetup(x,y,z,w) #define TracyIsConnected false +#define TracyFiberEnter(x) +#define TracyFiberLeave + #else #include @@ -254,6 +257,11 @@ #define TracyParameterSetup( idx, name, isBool, val ) tracy::Profiler::ParameterSetup( idx, name, isBool, val ); #define TracyIsConnected tracy::GetProfiler().IsConnected() +#ifdef TRACY_FIBERS +# define TracyFiberEnter( fiber ) tracy::Profiler::EnterFiber( fiber ); +# define TracyFiberLeave tracy::Profiler::LeaveFiber(); +#endif + #endif #endif diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 0daf6ab4..55a13c9e 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -2752,6 +2752,24 @@ Profiler::DequeueStatus Profiler::DequeueSerial() tracy_free_fast( (void*)ptr ); break; } + case QueueType::FiberEnter: + { + ThreadCtxCheckSerial( fiberEnter ); + int64_t t = MemRead( &item->fiberEnter.time ); + int64_t dt = t - refThread; + refThread = t; + MemWrite( &item->fiberEnter.time, dt ); + break; + } + case QueueType::FiberLeave: + { + ThreadCtxCheckSerial( fiberLeave ); + int64_t t = MemRead( &item->fiberLeave.time ); + int64_t dt = t - refThread; + refThread = t; + MemWrite( &item->fiberLeave.time, dt ); + break; + } #endif default: assert( false ); diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 179c5186..0dbeaa70 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -584,6 +584,23 @@ public: TracyLfqCommit; } +#ifdef TRACY_FIBERS + static tracy_force_inline void EnterFiber( const char* fiber ) + { + TracyQueuePrepare( QueueType::FiberEnter ); + MemWrite( &item->fiberEnter.time, GetTime() ); + MemWrite( &item->fiberEnter.fiber, (uint64_t)fiber ); + TracyQueueCommit( fiberEnter ); + } + + static tracy_force_inline void LeaveFiber() + { + TracyQueuePrepare( QueueType::FiberLeave ); + MemWrite( &item->fiberLeave.time, GetTime() ); + TracyQueueCommit( fiberLeave ); + } +#endif + void SendCallstack( int depth, const char* skipBefore ); static void CutCallstack( void* callstack, const char* skipBefore ); diff --git a/common/TracyProtocol.hpp b/common/TracyProtocol.hpp index 5497c228..e773c7f7 100644 --- a/common/TracyProtocol.hpp +++ b/common/TracyProtocol.hpp @@ -9,7 +9,7 @@ namespace tracy constexpr unsigned Lz4CompressBound( unsigned isize ) { return isize + ( isize / 255 ) + 16; } -enum : uint32_t { ProtocolVersion = 51 }; +enum : uint32_t { ProtocolVersion = 52 }; enum : uint16_t { BroadcastVersion = 2 }; using lz4sz_t = uint32_t; diff --git a/common/TracyQueue.hpp b/common/TracyQueue.hpp index f1db518b..3e45efff 100644 --- a/common/TracyQueue.hpp +++ b/common/TracyQueue.hpp @@ -59,6 +59,8 @@ enum class QueueType : uint8_t SymbolInformation, CodeInformation, ExternalNameMetadata, + FiberEnter, + FiberLeave, Terminate, KeepAlive, ThreadContext, @@ -236,6 +238,19 @@ struct QueueLockAnnounce LockType type; }; +struct QueueFiberEnter +{ + int64_t time; + uint64_t fiber; // ptr + uint32_t thread; +}; + +struct QueueFiberLeave +{ + int64_t time; + uint32_t thread; +}; + struct QueueLockTerminate { uint32_t id; @@ -692,6 +707,8 @@ struct QueueItem QueueParamSetup paramSetup; QueueCpuTopology cpuTopology; QueueExternalNameMetadata externalNameMetadata; + QueueFiberEnter fiberEnter; + QueueFiberLeave fiberLeave; }; }; #pragma pack() @@ -750,7 +767,9 @@ static constexpr size_t QueueDataSize[] = { sizeof( QueueHeader ) + sizeof( QueueCallstackFrameSize ), sizeof( QueueHeader ) + sizeof( QueueSymbolInformation ), sizeof( QueueHeader ) + sizeof( QueueCodeInformation ), - sizeof( QueueHeader), // ExternalNameMetadata - not for wire transfer + sizeof( QueueHeader ), // ExternalNameMetadata - not for wire transfer + sizeof( QueueHeader ) + sizeof( QueueFiberEnter ), + sizeof( QueueHeader ) + sizeof( QueueFiberLeave ), // above items must be first sizeof( QueueHeader ), // terminate sizeof( QueueHeader ), // keep alive