From 69527d2f71d0b4f991287a91e0ee3aaa87c70b55 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 16 Aug 2019 16:28:58 +0200 Subject: [PATCH] Collect per-cpu context switch data. --- server/TracyEvent.hpp | 29 +++++++++++++++++++++++++++++ server/TracyWorker.cpp | 11 +++++++++++ server/TracyWorker.hpp | 2 ++ 3 files changed, 42 insertions(+) diff --git a/server/TracyEvent.hpp b/server/TracyEvent.hpp index d366892c..9dafde0e 100644 --- a/server/TracyEvent.hpp +++ b/server/TracyEvent.hpp @@ -252,6 +252,30 @@ struct ContextSwitchData enum { ContextSwitchDataSize = sizeof( ContextSwitchData ) }; +// Thread can't be compressed here, because we want to ignore external threads and we can't +// determine whether thread is local or external when context information arrives (thread data +// might come after context switch data). +struct ContextSwitchCpu +{ + int64_t Start() const { return int64_t( _start_thread1 ) >> 16; } + void SetStart( int64_t start ) { assert( start < ( 1ll << 47 ) ); _start_thread1 = ( _start_thread1 & 0xFFFF ) | uint64_t( start << 16 ); } + int64_t End() const { return int64_t( _end_thread2 ) >> 16; } + void SetEnd( int64_t end ) { assert( end < ( 1ll << 47 ) ); _end_thread2 = ( _end_thread2 & 0xFFFF ) | uint64_t( end << 16 ); } + uint64_t Thread() const { return uint64_t( uint16_t( _start_thread1 ) ) | ( uint64_t( uint16_t( _end_thread2 ) ) << 16 ) | ( uint64_t( _thread3 ) << 32 ); } + void SetThread( uint64_t thread ) { + _start_thread1 = ( _start_thread1 & 0xFFFFFFFFFFFF0000 ) | uint16_t( thread ); + _end_thread2 = ( _end_thread2 & 0xFFFFFFFFFFFF0000 ) | uint16_t( thread >> 16 ); + _thread3 = uint32_t( thread >> 32 ); + } + + uint64_t _start_thread1; + uint64_t _end_thread2; + uint32_t _thread3; +}; + +enum { ContextSwitchCpuSize = sizeof( ContextSwitchCpu ) }; + + struct MessageData { int64_t time; @@ -409,6 +433,11 @@ struct ContextSwitch int64_t runningTime = 0; }; +struct CpuData +{ + Vector cs; +}; + } #endif diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index 8766002d..2bae4c07 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -3963,6 +3963,7 @@ void Worker::ProcessContextSwitch( const QueueContextSwitch& ev ) const auto time = TscTime( ev.time - m_data.baseTime ); m_data.lastTime = std::max( m_data.lastTime, time ); + auto& cs = m_data.cpuData[ev.cpu].cs; if( ev.oldThread != 0 ) { auto it = m_data.ctxSwitch.find( ev.oldThread ); @@ -3978,6 +3979,12 @@ void Worker::ProcessContextSwitch( const QueueContextSwitch& ev ) it->second->runningTime += time - item.Start(); } + if( !cs.empty() ) + { + auto& cx = cs.back(); + assert( cx.Thread() == ev.oldThread ); + cx.SetEnd( time ); + } } if( ev.newThread != 0 ) { @@ -3995,6 +4002,10 @@ void Worker::ProcessContextSwitch( const QueueContextSwitch& ev ) item.SetCpu( ev.cpu ); item.SetReason( -1 ); item.SetState( -1 ); + + auto& cx = cs.push_next(); + cx.SetStart( time ); + cx.SetThread( ev.newThread ); } } diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index eba74201..a0d320a6 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -208,6 +208,8 @@ private: flat_hash_map> ctxSwitch; std::pair ctxSwitchLast; + + CpuData cpuData[256]; }; struct MbpsBlock