diff --git a/server/TracyEvent.hpp b/server/TracyEvent.hpp index f45fa40b..bc33045c 100644 --- a/server/TracyEvent.hpp +++ b/server/TracyEvent.hpp @@ -219,6 +219,18 @@ struct CrashEvent enum { CrashEventSize = sizeof( CrashEvent ) }; + +struct ContextSwitchData +{ + int64_t start; + int64_t end; + uint8_t cpu; + int8_t reason; + int8_t state; +}; + +enum { ContextSwitchDataSize = sizeof( ContextSwitchData ) }; + #pragma pack() @@ -364,6 +376,11 @@ struct FrameImage uint8_t flip; }; +struct ContextSwitch +{ + Vector v; +}; + } #endif diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index c9e4a79a..2339003b 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -2611,6 +2611,9 @@ bool Worker::Process( const QueueItem& ev ) case QueueType::SysTimeReport: ProcessSysTime( ev.sysTime ); break; + case QueueType::ContextSwitch: + ProcessContextSwitch( ev.contextSwitch ); + break; default: assert( false ); break; @@ -3629,6 +3632,44 @@ void Worker::ProcessSysTime( const QueueSysTime& ev ) } } +void Worker::ProcessContextSwitch( const QueueContextSwitch& ev ) +{ + const auto time = TscTime( ev.time ); + m_data.lastTime = std::max( m_data.lastTime, time ); + + if( ev.oldThread != 0 ) + { + auto it = m_data.ctxSwitch.find( ev.oldThread ); + if( it != m_data.ctxSwitch.end() ) + { + auto& data = it->second->v; + assert( !data.empty() ); + auto& item = data.back(); + assert( item.start <= time ); + item.end = time; + item.reason = ev.reason; + item.state = ev.state; + } + } + if( ev.newThread != 0 ) + { + auto it = m_data.ctxSwitch.find( ev.newThread ); + if( it == m_data.ctxSwitch.end() ) + { + auto ctx = m_slab.AllocInit(); + it = m_data.ctxSwitch.emplace( ev.newThread, ctx ).first; + } + auto& data = it->second->v; + assert( data.empty() || (uint64_t)data.back().end <= time ); + auto& item = data.push_next(); + item.start = time; + item.end = -1; + item.cpu = ev.cpu; + item.reason = -1; + item.state = -1; + } +} + void Worker::MemAllocChanged( int64_t time ) { const auto val = (double)m_data.memory.usage; diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index 550d9a8f..c4ef69ce 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -194,6 +194,8 @@ private: Vector appInfo; CrashEvent crashEvent; + + flat_hash_map> ctxSwitch; }; struct MbpsBlock @@ -412,6 +414,7 @@ private: tracy_force_inline void ProcessCallstackFrame( const QueueCallstackFrame& ev ); tracy_force_inline void ProcessCrashReport( const QueueCrashReport& ev ); tracy_force_inline void ProcessSysTime( const QueueSysTime& ev ); + tracy_force_inline void ProcessContextSwitch( const QueueContextSwitch& ev ); tracy_force_inline void ProcessZoneBeginImpl( ZoneEvent* zone, const QueueZoneBegin& ev ); tracy_force_inline void ProcessZoneBeginAllocSrcLocImpl( ZoneEvent* zone, const QueueZoneBegin& ev );