From 69e5cf796d216548ce2d10d368fd3c078aa117be Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 6 Nov 2021 20:22:38 +0100 Subject: [PATCH] Construct faux context switch data from fiber events. --- server/TracyEvent.hpp | 4 ++ server/TracyVersion.hpp | 2 +- server/TracyWorker.cpp | 92 +++++++++++++++++++++++++++++++++-------- 3 files changed, 79 insertions(+), 19 deletions(-) diff --git a/server/TracyEvent.hpp b/server/TracyEvent.hpp index 5e462cd3..8cde3678 100644 --- a/server/TracyEvent.hpp +++ b/server/TracyEvent.hpp @@ -484,6 +484,7 @@ enum { CrashEventSize = sizeof( CrashEvent ) }; struct ContextSwitchData { + enum : int8_t { Fiber = 99 }; enum : int8_t { NoState = 100 }; enum : int8_t { Wakeup = -2 }; @@ -500,6 +501,8 @@ struct ContextSwitchData tracy_force_inline void SetState( int8_t state ) { memcpy( &_end_reason_state, &state, 1 ); } tracy_force_inline int64_t WakeupVal() const { return _wakeup.Val(); } tracy_force_inline void SetWakeup( int64_t wakeup ) { assert( wakeup < (int64_t)( 1ull << 47 ) ); _wakeup.SetVal( wakeup ); } + tracy_force_inline uint16_t Thread() const { return _thread; } + tracy_force_inline void SetThread( uint16_t thread ) { _thread = thread; } tracy_force_inline void SetStartCpu( int64_t start, uint8_t cpu ) { assert( start < (int64_t)( 1ull << 47 ) ); _start_cpu = ( uint64_t( start ) << 16 ) | cpu; } tracy_force_inline void SetEndReasonState( int64_t end, int8_t reason, int8_t state ) { assert( end < (int64_t)( 1ull << 47 ) ); _end_reason_state = ( uint64_t( end ) << 16 ) | ( uint64_t( reason ) << 8 ) | uint8_t( state ); } @@ -507,6 +510,7 @@ struct ContextSwitchData uint64_t _start_cpu; uint64_t _end_reason_state; Int48 _wakeup; + uint16_t _thread; }; enum { ContextSwitchDataSize = sizeof( ContextSwitchData ) }; diff --git a/server/TracyVersion.hpp b/server/TracyVersion.hpp index 49d7bea8..3a0aff12 100644 --- a/server/TracyVersion.hpp +++ b/server/TracyVersion.hpp @@ -7,7 +7,7 @@ namespace Version { enum { Major = 0 }; enum { Minor = 7 }; -enum { Patch = 11 }; +enum { Patch = 12 }; } } diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index e1b49b54..a0ae44d0 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -1550,20 +1550,43 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks ) int64_t runningTime = 0; int64_t refTime = 0; auto ptr = data->v.data(); - for( uint64_t j=0; j= FileVersion( 0, 7, 12 ) ) { - int64_t deltaWakeup, deltaStart, diff; - uint8_t cpu; - int8_t reason, state; - f.Read6( deltaWakeup, deltaStart, diff, cpu, reason, state ); - refTime += deltaWakeup; - ptr->SetWakeup( refTime ); - refTime += deltaStart; - ptr->SetStartCpu( refTime, cpu ); - if( diff > 0 ) runningTime += diff; - refTime += diff; - ptr->SetEndReasonState( refTime, reason, state ); - ptr++; + for( uint64_t j=0; jSetWakeup( refTime ); + refTime += deltaStart; + ptr->SetStartCpu( refTime, cpu ); + if( diff > 0 ) runningTime += diff; + refTime += diff; + ptr->SetEndReasonState( refTime, reason, state ); + ptr->SetThread( CompressThread( thread ) ); + ptr++; + } + } + else + { + for( uint64_t j=0; jSetWakeup( refTime ); + refTime += deltaStart; + ptr->SetStartCpu( refTime, cpu ); + if( diff > 0 ) runningTime += diff; + refTime += diff; + ptr->SetEndReasonState( refTime, reason, state ); + ptr->SetThread( 0 ); + ptr++; + } } data->runningTime = runningTime; m_data.ctxSwitch.emplace( thread, data ); @@ -1579,7 +1602,14 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks ) f.Skip( sizeof( uint64_t ) ); uint64_t csz; f.Read( csz ); - f.Skip( csz * ( sizeof( int64_t ) * 3 + sizeof( int8_t ) * 3 ) ); + if( fileVer >= FileVersion( 0, 7, 12 ) ) + { + f.Skip( csz * ( sizeof( int64_t ) * 4 + sizeof( int8_t ) * 3 ) ); + } + else + { + f.Skip( csz * ( sizeof( int64_t ) * 3 + sizeof( int8_t ) * 3 ) ); + } } } @@ -6640,6 +6670,7 @@ void Worker::ProcessContextSwitch( const QueueContextSwitch& ev ) item->SetCpu( ev.cpu ); item->SetReason( -1 ); item->SetState( -1 ); + item->SetThread( 0 ); auto& cx = cs.push_next(); cx.SetStart( time ); @@ -6682,6 +6713,7 @@ void Worker::ProcessThreadWakeup( const QueueThreadWakeup& ev ) item.SetCpu( 0 ); item.SetReason( ContextSwitchData::Wakeup ); item.SetState( -1 ); + item.SetThread( 0 ); } void Worker::ProcessTidToPid( const QueueTidToPid& ev ) @@ -6785,6 +6817,19 @@ void Worker::ProcessFiberEnter( const QueueFiberEnter& ev ) auto td = NoticeThread( ev.thread ); td->fiber = RetrieveThread( tid ); assert( td->fiber ); + + auto cit = m_data.ctxSwitch.find( tid ); + if( cit == m_data.ctxSwitch.end() ) + { + auto ctx = m_slab.AllocInit(); + cit = m_data.ctxSwitch.emplace( tid, ctx ).first; + } + auto& data = cit->second->v; + auto& item = data.push_next(); + item.SetStartCpu( t, 0 ); + item.SetWakeup( t ); + item.SetEndReasonState( -1, ContextSwitchData::Fiber, -1 ); + item.SetThread( CompressThread( ev.thread ) ); } void Worker::ProcessFiberLeave( const QueueFiberLeave& ev ) @@ -6798,11 +6843,20 @@ void Worker::ProcessFiberLeave( const QueueFiberLeave& ev ) if( !td->fiber ) { FiberLeaveFailure(); + return; } - else - { - td->fiber = nullptr; - } + + auto cit = m_data.ctxSwitch.find( td->fiber->id ); + assert( cit != m_data.ctxSwitch.end() ); + auto& data = cit->second->v; + assert( !data.empty() ); + auto& item = data.back(); + item.SetEnd( t ); + + const auto dt = t - item.Start(); + cit->second->runningTime += dt; + + td->fiber = nullptr; } void Worker::MemAllocChanged( uint64_t memname, MemData& memdata, int64_t time ) @@ -7935,9 +7989,11 @@ void Worker::Write( FileWrite& f, bool fiDict ) uint8_t cpu = cs.Cpu(); int8_t reason = cs.Reason(); int8_t state = cs.State(); + uint64_t thread = DecompressThread( cs.Thread() ); f.Write( &cpu, sizeof( cpu ) ); f.Write( &reason, sizeof( reason ) ); f.Write( &state, sizeof( state ) ); + f.Write( &thread, sizeof( thread ) ); } }