From b8935a8a565dab07fdc6f940a741488175e532b1 Mon Sep 17 00:00:00 2001 From: Lectem Date: Tue, 20 Oct 2026 19:54:22 +0200 Subject: [PATCH 1/3] Group sampling globals into SysTraceState Move the sampling client's file-level globals into a single SysTraceState struct, so that the state of a trace session is in one place. --- public/client/TracySysTrace.cpp | 108 +++++++++++++++++--------------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git a/public/client/TracySysTrace.cpp b/public/client/TracySysTrace.cpp index 48910ef3..58ec5e80 100644 --- a/public/client/TracySysTrace.cpp +++ b/public/client/TracySysTrace.cpp @@ -412,13 +412,17 @@ void SysTraceGetExternalName( uint64_t thread, const char*& threadName, const ch namespace tracy { -static std::atomic traceActive { false }; -static int s_numCpus = 0; -static int s_numBuffers = 0; -static int s_ctxBufferIdx = 0; -static bool s_ctxSwitchCallchain = false; +struct SysTraceState +{ + std::atomic traceActive { false }; + int numCpus = 0; + int numBuffers = 0; + int ctxBufferIdx = 0; + bool ctxSwitchCallchain = false; + RingBuffer* ring = nullptr; +}; -static RingBuffer* s_ring = nullptr; +static SysTraceState s_state; struct PerfIterTarget @@ -545,9 +549,9 @@ static bool OpenSampleEvent( const PerfIterTarget& tgt, const perf_event_attr& i TracyDebug( " Failed to setup!" ); return false; } - new( s_ring + s_numBuffers ) RingBuffer( 64 * 1024, fd, eventId ); - if( !s_ring[s_numBuffers].IsValid() ) return false; - s_numBuffers++; + new( s_state.ring + s_state.numBuffers ) RingBuffer( 64 * 1024, fd, eventId ); + if( !s_state.ring[s_state.numBuffers].IsValid() ) return false; + s_state.numBuffers++; return true; } @@ -1009,7 +1013,7 @@ bool SysTraceStart( int64_t& samplingPeriod ) #endif uint32_t currentPid = externalPid != 0 ? externalPid : (uint32_t)getpid(); - s_numCpus = (int)std::thread::hardware_concurrency(); + s_state.numCpus = (int)std::thread::hardware_concurrency(); PerfIterTarget* iter; int numIter; @@ -1025,20 +1029,20 @@ bool SysTraceStart( int64_t& samplingPeriod ) } if( numTids == 1 ) { - iter = (PerfIterTarget*)tracy_malloc( sizeof( PerfIterTarget ) * s_numCpus ); - for( int i=0; i= KERNEL_VERSION( 4, 8, 0 ) @@ -1226,15 +1230,15 @@ bool SysTraceStart( int64_t& samplingPeriod ) } TracyDebug( "Setup context switch capture" ); - for( int i=0; i Date: Tue, 20 Oct 2026 19:54:22 +0200 Subject: [PATCH 2/3] Share one ring per event kind and CPU in the sampling client Sampling events of one kind on one CPU now share a ring: the first thread's event owns it, and the others are redirected into it with PERF_EVENT_IOC_SET_OUTPUT. Redirected fds are kept open and enabled by the worker. --- public/client/TracySysTrace.cpp | 46 ++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/public/client/TracySysTrace.cpp b/public/client/TracySysTrace.cpp index 58ec5e80..4672c485 100644 --- a/public/client/TracySysTrace.cpp +++ b/public/client/TracySysTrace.cpp @@ -420,10 +420,22 @@ struct SysTraceState int ctxBufferIdx = 0; bool ctxSwitchCallchain = false; RingBuffer* ring = nullptr; + int* redirectFd = nullptr; + int numRedirectFd = 0; }; static SysTraceState s_state; +// Sampling events of one kind on one CPU share a ring: the first thread's event owns it +// (the sharedRingFd array, per event kind and CPU), the others are redirected into it with +// PERF_EVENT_IOC_SET_OUTPUT. Redirected fds are kept open, and enabled by the worker. +static void CloseRedirectFds() +{ + for( int i=0; i Date: Thu, 29 Oct 2026 13:15:46 +0100 Subject: [PATCH 3/3] Address review comments on the shared sampling rings Move the shared ring comment onto `OpenSampleEvent`, where the sharing happens. Name the number of sampling event kinds with `NumSampleEventKinds` instead of using `EventVsync` directly, and assert in `OpenSampleEvent` that the event id is below it. --- public/client/TracySysTrace.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/public/client/TracySysTrace.cpp b/public/client/TracySysTrace.cpp index 4672c485..1407a3df 100644 --- a/public/client/TracySysTrace.cpp +++ b/public/client/TracySysTrace.cpp @@ -426,9 +426,6 @@ struct SysTraceState static SysTraceState s_state; -// Sampling events of one kind on one CPU share a ring: the first thread's event owns it -// (the sharedRingFd array, per event kind and CPU), the others are redirected into it with -// PERF_EVENT_IOC_SET_OUTPUT. Redirected fds are kept open, and enabled by the worker. static void CloseRedirectFds() { for( int i=0; i