Draw thread context switches using precalculated data.

This commit is contained in:
Bartosz Taudul 2023-03-22 19:38:58 +01:00
parent 88f0b9c754
commit 50ccdc1ef4
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
4 changed files with 97 additions and 130 deletions

View File

@ -259,7 +259,7 @@ void TimelineItemThread::HeaderExtraContents( const TimelineContext& ctx, int of
bool TimelineItemThread::DrawContents( const TimelineContext& ctx, int& offset ) bool TimelineItemThread::DrawContents( const TimelineContext& ctx, int& offset )
{ {
const auto res = m_view.DrawThread( ctx, *m_thread, m_draw, offset, m_depth ); const auto res = m_view.DrawThread( ctx, *m_thread, m_draw, m_ctxDraw, offset, m_depth );
if( !res ) if( !res )
{ {
auto& crash = m_worker.GetCrashEvent(); auto& crash = m_worker.GetCrashEvent();

View File

@ -42,6 +42,7 @@ class FileRead;
class SourceView; class SourceView;
struct TimelineContext; struct TimelineContext;
struct TimelineDraw; struct TimelineDraw;
struct ContextSwitchDraw;
class View class View
{ {
@ -122,7 +123,7 @@ public:
void HighlightThread( uint64_t thread ); void HighlightThread( uint64_t thread );
void ZoomToRange( int64_t start, int64_t end, bool pause = true ); void ZoomToRange( int64_t start, int64_t end, bool pause = true );
bool DrawPlot( const TimelineContext& ctx, PlotData& plot, int& offset ); bool DrawPlot( const TimelineContext& ctx, PlotData& plot, int& offset );
bool DrawThread( const TimelineContext& ctx, const ThreadData& thread, const std::vector<TimelineDraw>& draw, int& offset, int depth ); bool DrawThread( const TimelineContext& ctx, const ThreadData& thread, const std::vector<TimelineDraw>& draw, const std::vector<ContextSwitchDraw>& ctxDraw, int& offset, int depth );
void DrawThreadMessages( const TimelineContext& ctx, const ThreadData& thread, int offset ); void DrawThreadMessages( const TimelineContext& ctx, const ThreadData& thread, int offset );
void DrawThreadOverlays( const ThreadData& thread, const ImVec2& ul, const ImVec2& dr ); void DrawThreadOverlays( const ThreadData& thread, const ImVec2& ul, const ImVec2& dr );
bool DrawGpu( const TimelineContext& ctx, const GpuCtxData& gpu, int& offset ); bool DrawGpu( const TimelineContext& ctx, const GpuCtxData& gpu, int& offset );
@ -205,9 +206,9 @@ private:
void DrawTimelineFramesHeader(); void DrawTimelineFramesHeader();
void DrawTimelineFrames( const FrameData& frames ); void DrawTimelineFrames( const FrameData& frames );
void DrawTimeline(); void DrawTimeline();
void DrawContextSwitches( const ContextSwitch* ctx, const Vector<SampleData>& sampleData, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int offset, int endOffset, bool isFiber );
void DrawSamples( const Vector<SampleData>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int offset ); void DrawSamples( const Vector<SampleData>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int offset );
void DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineDraw>& drawList, int offset, uint64_t tid ); void DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineDraw>& drawList, int offset, uint64_t tid );
void DrawContextSwitchList( const TimelineContext& ctx, const std::vector<ContextSwitchDraw>& drawList, int offset, int endOffset, bool isFiber );
int DispatchGpuZoneLevel( const Vector<short_ptr<GpuEvent>>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int offset, int depth, uint64_t thread, float yMin, float yMax, int64_t begin, int drift ); int DispatchGpuZoneLevel( const Vector<short_ptr<GpuEvent>>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int offset, int depth, uint64_t thread, float yMin, float yMax, int64_t begin, int drift );
template<typename Adapter, typename V> template<typename Adapter, typename V>
int DrawGpuZoneLevel( const V& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int offset, int depth, uint64_t thread, float yMin, float yMax, int64_t begin, int drift ); int DrawGpuZoneLevel( const V& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int offset, int depth, uint64_t thread, float yMin, float yMax, int64_t begin, int drift );

View File

@ -3,6 +3,8 @@
#include "TracyImGui.hpp" #include "TracyImGui.hpp"
#include "TracyMouse.hpp" #include "TracyMouse.hpp"
#include "TracyPrint.hpp" #include "TracyPrint.hpp"
#include "TracyTimelineContext.hpp"
#include "TracyTimelineDraw.hpp"
#include "TracyView.hpp" #include "TracyView.hpp"
namespace tracy namespace tracy
@ -134,37 +136,33 @@ const char* View::DecodeContextSwitchState( uint8_t state )
} }
} }
void View::DrawContextSwitches( const ContextSwitch* ctx, const Vector<SampleData>& sampleData, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int offset, int endOffset, bool isFiber ) void View::DrawContextSwitchList( const TimelineContext& ctx, const std::vector<ContextSwitchDraw>& drawList, int offset, int endOffset, bool isFiber )
{ {
const auto vStart = ctx.vStart;
const auto vEnd = ctx.vEnd;
const auto& wpos = ctx.wpos;
const auto pxns = ctx.pxns;
const auto hover = ctx.hover;
const auto w = ctx.w;
const auto ty = ctx.ty;
const auto lineSize = 2 * GetScale(); const auto lineSize = 2 * GetScale();
auto& vec = ctx->v;
auto it = std::lower_bound( vec.begin(), vec.end(), std::max<int64_t>( 0, m_vd.zvStart ), [] ( const auto& l, const auto& r ) { return (uint64_t)l.End() < (uint64_t)r; } );
if( it == vec.end() ) return;
if( it != vec.begin() ) --it;
auto citend = std::lower_bound( it, vec.end(), m_vd.zvEnd, [] ( const auto& l, const auto& r ) { return l.Start() < r; } );
if( it == citend ) return;
if( citend != vec.end() ) ++citend;
const auto w = ImGui::GetContentRegionAvail().x - 1;
const auto ty = round( ImGui::GetTextLineHeight() * 0.75f );
const auto ty05 = round( ty * 0.5f );
auto draw = ImGui::GetWindowDrawList(); auto draw = ImGui::GetWindowDrawList();
const auto dpos = wpos + ImVec2( 0.5f, 0.5f ); const auto dpos = wpos + ImVec2( 0.5f, 0.5f );
const auto ty05 = round( ty * 0.5f );
auto pit = citend; for( auto& v : drawList )
double minpx = -10.0;
while( it < citend )
{ {
auto& ev = *it; const auto& ev = *v.ev;
if( pit != citend ) switch( v.type )
{ {
const bool migration = pit->Cpu() != ev.Cpu(); case ContextSwitchDrawType::Waiting:
const auto px0 = std::max( { ( pit->End() - m_vd.zvStart ) * pxns, -10.0, minpx } ); {
const auto pxw = ( ev.WakeupVal() - m_vd.zvStart ) * pxns; const auto& prev = *v.waiting.prev;
const auto px1 = std::min( ( ev.Start() - m_vd.zvStart ) * pxns, w + 10.0 ); const bool migration = prev.Cpu() != ev.Cpu();
const auto px0 = std::max( { ( prev.End() - vStart ) * pxns, -10.0, double( v.minpx ) } );
const auto pxw = ( ev.WakeupVal() - vStart ) * pxns;
const auto px1 = std::min( ( ev.Start() - vStart ) * pxns, w + 10.0 );
const auto color = migration ? 0xFFEE7711 : 0xFF2222AA; const auto color = migration ? 0xFFEE7711 : 0xFF2222AA;
if( m_vd.darkenContextSwitches ) if( m_vd.darkenContextSwitches )
{ {
@ -185,15 +183,15 @@ void View::DrawContextSwitches( const ContextSwitch* ctx, const Vector<SampleDat
if( isFiber ) if( isFiber )
{ {
TextFocused( "Fiber is", "yielding" ); TextFocused( "Fiber is", "yielding" );
TextFocused( "Yield time:", TimeToString( ev.Start() - pit->End() ) ); TextFocused( "Yield time:", TimeToString( ev.Start() - prev.End() ) );
} }
else else
{ {
TextFocused( "Thread is", migration ? "migrating CPUs" : "waiting" ); TextFocused( "Thread is", migration ? "migrating CPUs" : "waiting" );
TextFocused( "Waiting time:", TimeToString( ev.WakeupVal() - pit->End() ) ); TextFocused( "Waiting time:", TimeToString( ev.WakeupVal() - prev.End() ) );
if( migration ) if( migration )
{ {
TextFocused( "CPU:", RealToString( pit->Cpu() ) ); TextFocused( "CPU:", RealToString( prev.Cpu() ) );
ImGui::SameLine(); ImGui::SameLine();
TextFocused( ICON_FA_RIGHT_LONG, RealToString( ev.Cpu() ) ); TextFocused( ICON_FA_RIGHT_LONG, RealToString( ev.Cpu() ) );
} }
@ -201,27 +199,27 @@ void View::DrawContextSwitches( const ContextSwitch* ctx, const Vector<SampleDat
{ {
TextFocused( "CPU:", RealToString( ev.Cpu() ) ); TextFocused( "CPU:", RealToString( ev.Cpu() ) );
} }
if( pit->Reason() != 100 ) if( prev.Reason() != 100 )
{ {
TextFocused( "Wait reason:", DecodeContextSwitchReasonCode( pit->Reason() ) ); TextFocused( "Wait reason:", DecodeContextSwitchReasonCode( prev.Reason() ) );
ImGui::SameLine(); ImGui::SameLine();
ImGui::PushFont( m_smallFont ); ImGui::PushFont( m_smallFont );
ImGui::AlignTextToFramePadding(); ImGui::AlignTextToFramePadding();
TextDisabledUnformatted( DecodeContextSwitchReason( pit->Reason() ) ); TextDisabledUnformatted( DecodeContextSwitchReason( prev.Reason() ) );
ImGui::PopFont(); ImGui::PopFont();
} }
TextFocused( "Wait state:", DecodeContextSwitchStateCode( pit->State() ) ); TextFocused( "Wait state:", DecodeContextSwitchStateCode( prev.State() ) );
ImGui::SameLine(); ImGui::SameLine();
ImGui::PushFont( m_smallFont ); ImGui::PushFont( m_smallFont );
ImGui::AlignTextToFramePadding(); ImGui::AlignTextToFramePadding();
TextDisabledUnformatted( DecodeContextSwitchState( pit->State() ) ); TextDisabledUnformatted( DecodeContextSwitchState( prev.State() ) );
ImGui::PopFont(); ImGui::PopFont();
} }
tooltip = true; tooltip = true;
if( IsMouseClicked( 2 ) ) if( IsMouseClicked( 2 ) )
{ {
ZoomToRange( pit->End(), ev.WakeupVal() ); ZoomToRange( prev.End(), ev.WakeupVal() );
} }
} }
else if( ev.WakeupVal() != ev.Start() && ImGui::IsMouseHoveringRect( wpos + ImVec2( pxw, offset ), wpos + ImVec2( px1, offset + ty ) ) ) else if( ev.WakeupVal() != ev.Start() && ImGui::IsMouseHoveringRect( wpos + ImVec2( pxw, offset ), wpos + ImVec2( px1, offset + ty ) ) )
@ -233,69 +231,35 @@ void View::DrawContextSwitches( const ContextSwitch* ctx, const Vector<SampleDat
TextFocused( "CPU:", RealToString( ev.Cpu() ) ); TextFocused( "CPU:", RealToString( ev.Cpu() ) );
if( IsMouseClicked( 2 ) ) if( IsMouseClicked( 2 ) )
{ {
ZoomToRange( pit->End(), ev.WakeupVal() ); ZoomToRange( prev.End(), ev.WakeupVal() );
} }
tooltip = true; tooltip = true;
} }
if( tooltip ) if( tooltip )
{ {
if( !sampleData.empty() ) const auto waitStack = v.waiting.waitStack.Val();
{ if( waitStack )
auto sdit = std::lower_bound( sampleData.begin(), sampleData.end(), ev.Start(), [] ( const auto& l, const auto& r ) { return l.time.Val() < r; } );
bool found = sdit != sampleData.end() && sdit->time.Val() == ev.Start();
if( !found && it != vec.begin() )
{
auto eit = it;
--eit;
sdit = std::lower_bound( sampleData.begin(), sampleData.end(), eit->End(), [] ( const auto& l, const auto& r ) { return l.time.Val() < r; } );
found = sdit != sampleData.end() && sdit->time.Val() == eit->End();
}
if( found )
{ {
ImGui::Separator(); ImGui::Separator();
TextDisabledUnformatted( ICON_FA_HOURGLASS_HALF " Wait stack:" ); TextDisabledUnformatted( ICON_FA_HOURGLASS_HALF " Wait stack:" );
CallstackTooltipContents( sdit->callstack.Val() ); CallstackTooltipContents( waitStack );
if( ImGui::IsMouseClicked( 0 ) ) if( ImGui::IsMouseClicked( 0 ) )
{ {
m_callstackInfoWindow = sdit->callstack.Val(); m_callstackInfoWindow = waitStack;
}
} }
} }
ImGui::EndTooltip(); ImGui::EndTooltip();
} }
} }
break;
} }
case ContextSwitchDrawType::FoldedOne:
const auto end = ev.IsEndValid() ? ev.End() : m_worker.GetLastTime();
const auto zsz = std::max( ( end - ev.Start() ) * pxns, pxns * 0.5 );
if( zsz < MinCtxSize )
{ {
const auto MinCtxNs = MinCtxSize * nspx; const auto px0 = std::max( ( ev.Start() - vStart ) * pxns, -10.0 );
int num = 0; DrawLine( draw, dpos + ImVec2( px0, offset + ty05 - 0.5f ), dpos + ImVec2( v.minpx, offset + ty05 - 0.5f ), 0xFF22DD22, lineSize );
const auto px0 = std::max( ( ev.Start() - m_vd.zvStart ) * pxns, -10.0 ); if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( v.minpx, offset + ty + 1 ) ) )
auto px1ns = end - m_vd.zvStart;
auto rend = end;
auto nextTime = end + MinCtxNs;
for(;;)
{
const auto prevIt = it;
it = std::lower_bound( it, citend, nextTime, [] ( const auto& l, const auto& r ) { return (uint64_t)l.End() < (uint64_t)r; } );
if( it == prevIt ) ++it;
num += std::distance( prevIt, it );
if( it == citend ) break;
const auto nend = it->IsEndValid() ? it->End() : m_worker.GetLastTime();
const auto nsnext = nend - m_vd.zvStart;
if( nsnext - px1ns >= MinCtxNs * 2 ) break;
px1ns = nsnext;
rend = nend;
nextTime = nend + nspx;
}
minpx = std::min( std::max( px1ns * pxns, px0+MinCtxSize ), double( w + 10 ) );
if( num == 1 )
{
DrawLine( draw, dpos + ImVec2( px0, offset + ty05 - 0.5f ), dpos + ImVec2( minpx, offset + ty05 - 0.5f ), 0xFF22DD22, lineSize );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( minpx, offset + ty + 1 ) ) )
{ {
const auto end = v.folded.rend.Val();
ImGui::BeginTooltip(); ImGui::BeginTooltip();
if( isFiber ) if( isFiber )
{ {
@ -316,32 +280,35 @@ void View::DrawContextSwitches( const ContextSwitch* ctx, const Vector<SampleDat
if( IsMouseClicked( 2 ) ) if( IsMouseClicked( 2 ) )
{ {
ZoomToRange( ev.Start(), rend ); ZoomToRange( ev.Start(), end );
} }
} }
break;
} }
else case ContextSwitchDrawType::FoldedMulti:
{ {
DrawZigZag( draw, wpos + ImVec2( 0, offset + ty05 ), px0, minpx, ty/4, 0xFF888888, 1.5 ); const auto px0 = std::max( ( ev.Start() - vStart ) * pxns, -10.0 );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( minpx, offset + ty + 1 ) ) ) DrawZigZag( draw, wpos + ImVec2( 0, offset + ty05 ), px0, v.minpx, ty/4, 0xFF888888, 1.5 );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( v.minpx, offset + ty + 1 ) ) )
{ {
const auto end = v.folded.rend.Val();
ImGui::BeginTooltip(); ImGui::BeginTooltip();
TextFocused( isFiber ? "Fiber is" : "Thread is", "changing activity multiple times" ); TextFocused( isFiber ? "Fiber is" : "Thread is", "changing activity multiple times" );
TextFocused( "Number of running regions:", RealToString( num ) ); TextFocused( "Number of running regions:", RealToString( v.folded.num ) );
TextFocused( "Time:", TimeToString( rend - ev.Start() ) ); TextFocused( "Time:", TimeToString( end - ev.Start() ) );
ImGui::EndTooltip(); ImGui::EndTooltip();
if( IsMouseClicked( 2 ) ) if( IsMouseClicked( 2 ) )
{ {
ZoomToRange( ev.Start(), rend ); ZoomToRange( ev.Start(), end );
} }
} }
break;
} }
pit = it-1; case ContextSwitchDrawType::Running:
}
else
{ {
const auto px0 = std::max( { ( ev.Start() - m_vd.zvStart ) * pxns, -10.0, minpx } ); const auto end = ev.IsEndValid() ? ev.End() : m_worker.GetLastTime();
const auto px0 = std::max( { ( ev.Start() - m_vd.zvStart ) * pxns, -10.0, double( v.minpx ) } );
const auto px1 = std::min( ( end - m_vd.zvStart ) * pxns, w + 10.0 ); const auto px1 = std::min( ( end - m_vd.zvStart ) * pxns, w + 10.0 );
DrawLine( draw, dpos + ImVec2( px0, offset + ty05 - 0.5f ), dpos + ImVec2( px1, offset + ty05 - 0.5f ), 0xFF22DD22, lineSize ); DrawLine( draw, dpos + ImVec2( px0, offset + ty05 - 0.5f ), dpos + ImVec2( px1, offset + ty05 - 0.5f ), 0xFF22DD22, lineSize );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + ty + 1 ) ) ) if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + ty + 1 ) ) )
@ -369,9 +336,12 @@ void View::DrawContextSwitches( const ContextSwitch* ctx, const Vector<SampleDat
ZoomToRange( ev.Start(), end ); ZoomToRange( ev.Start(), end );
} }
} }
pit = it; break;
++it;
} }
default:
assert( false );
break;
};
} }
} }

View File

@ -23,7 +23,7 @@ static tracy_force_inline uint32_t MixGhostColor( uint32_t c0, uint32_t c1 )
( ( ( ( ( c0 & 0x000000FF ) ) + 3 * ( ( c1 & 0x000000FF ) ) ) >> 2 ) ); ( ( ( ( ( c0 & 0x000000FF ) ) + 3 * ( ( c1 & 0x000000FF ) ) ) >> 2 ) );
} }
bool View::DrawThread( const TimelineContext& ctx, const ThreadData& thread, const std::vector<TimelineDraw>& draw, int& offset, int depth ) bool View::DrawThread( const TimelineContext& ctx, const ThreadData& thread, const std::vector<TimelineDraw>& draw, const std::vector<ContextSwitchDraw>& ctxDraw, int& offset, int depth )
{ {
const auto& wpos = ctx.wpos; const auto& wpos = ctx.wpos;
const auto ty = ctx.ty; const auto ty = ctx.ty;
@ -41,7 +41,7 @@ bool View::DrawThread( const TimelineContext& ctx, const ThreadData& thread, con
const auto sampleOffset = offset; const auto sampleOffset = offset;
const auto hasSamples = m_vd.drawSamples && !thread.samples.empty(); const auto hasSamples = m_vd.drawSamples && !thread.samples.empty();
const auto hasCtxSwitch = m_vd.drawContextSwitches && m_worker.GetContextSwitchData( thread.id ); const auto hasCtxSwitch = m_vd.drawContextSwitches && !ctxDraw.empty();
if( hasSamples ) if( hasSamples )
{ {
@ -70,11 +70,7 @@ bool View::DrawThread( const TimelineContext& ctx, const ThreadData& thread, con
if( hasCtxSwitch ) if( hasCtxSwitch )
{ {
auto ctxSwitch = m_worker.GetContextSwitchData( thread.id ); DrawContextSwitchList( ctx, ctxDraw, ctxOffset, offset, thread.isFiber );
if( ctxSwitch )
{
DrawContextSwitches( ctxSwitch, thread.samples, hover, pxns, int64_t( nspx ), wpos, ctxOffset, offset, thread.isFiber );
}
} }
if( hasSamples ) if( hasSamples )
{ {