Make it possible to store multiple frames at single frame address.

This commit is contained in:
Bartosz Taudul 2019-01-20 19:11:48 +01:00
parent b9dc9f043c
commit ddad475c19
10 changed files with 216 additions and 100 deletions

View File

@ -37,9 +37,9 @@ void InitCallstack()
SymSetOptions( SYMOPT_LOAD_LINES );
}
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
{
CallstackEntry ret;
static CallstackEntry ret;
const auto proc = GetCurrentProcess();
@ -82,7 +82,7 @@ CallstackEntry DecodeCallstackPtr( uint64_t ptr )
ret.file = file;
return ret;
return { &ret, 1 };
}
#elif TRACY_HAS_CALLSTACK >= 2
@ -219,12 +219,12 @@ static void CallstackErrorCb( void* data, const char* msg, int errnum )
cb_num = 1;
}
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
{
cb_num = 0;
backtrace_pcinfo( cb_bts, ptr, CallstackDataCb, CallstackErrorCb, nullptr );
assert( cb_num == 1 );
return cb_data[0];
return { cb_data, cb_num };
}
#endif

View File

@ -41,7 +41,13 @@ struct CallstackEntry
uint32_t line;
};
CallstackEntry DecodeCallstackPtr( uint64_t ptr );
struct CallstackEntryData
{
const CallstackEntry* data;
uint8_t size;
};
CallstackEntryData DecodeCallstackPtr( uint64_t ptr );
void InitCallstack();
#if TRACY_HAS_CALLSTACK == 1

View File

@ -1487,14 +1487,26 @@ void Profiler::SendCallstackPayload( uint64_t _ptr )
void Profiler::SendCallstackFrame( uint64_t ptr )
{
#ifdef TRACY_HAS_CALLSTACK
auto frame = DecodeCallstackPtr( ptr );
const auto frameData = DecodeCallstackPtr( ptr );
{
QueueItem item;
MemWrite( &item.hdr.type, QueueType::CallstackFrameSize );
MemWrite( &item.callstackFrameSize.ptr, ptr );
MemWrite( &item.callstackFrameSize.size, frameData.size );
AppendData( &item, QueueDataSize[(int)QueueType::CallstackFrameSize] );
}
for( uint8_t i=0; i<frameData.size; i++ )
{
const auto& frame = frameData.data[i];
SendString( uint64_t( frame.name ), frame.name, QueueType::CustomStringData );
SendString( uint64_t( frame.file ), frame.file, QueueType::CustomStringData );
QueueItem item;
MemWrite( &item.hdr.type, QueueType::CallstackFrame );
MemWrite( &item.callstackFrame.ptr, ptr );
MemWrite( &item.callstackFrame.name, (uint64_t)frame.name );
MemWrite( &item.callstackFrame.file, (uint64_t)frame.file );
MemWrite( &item.callstackFrame.line, frame.line );
@ -1503,6 +1515,7 @@ void Profiler::SendCallstackFrame( uint64_t ptr )
tracy_free( (void*)frame.name );
tracy_free( (void*)frame.file );
}
#endif
}
@ -1720,10 +1733,13 @@ void Profiler::SendCallstack( int depth, uint64_t thread, const char* skipBefore
uintptr_t i;
for( i=0; i<sz; i++ )
{
auto frame = DecodeCallstackPtr( uint64_t( data[i] ) );
const bool found = strcmp( frame.name, skipBefore ) == 0;
tracy_free( (void*)frame.name );
tracy_free( (void*)frame.file );
auto frameData = DecodeCallstackPtr( uint64_t( data[i] ) );
const bool found = strcmp( frameData.data[0].name, skipBefore ) == 0;
for( uint8_t j=0; j<frameData.size; j++ )
{
tracy_free( (void*)frameData.data[j].name );
tracy_free( (void*)frameData.data[j].file );
}
if( found )
{
i++;

View File

@ -9,7 +9,7 @@
namespace tracy
{
enum : uint32_t { ProtocolVersion = 2 };
enum : uint32_t { ProtocolVersion = 3 };
using lz4sz_t = uint32_t;

View File

@ -46,6 +46,7 @@ enum class QueueType : uint8_t
MemFree,
MemAllocCallstack,
MemFreeCallstack,
CallstackFrameSize,
CallstackFrame,
StringData,
ThreadName,
@ -244,9 +245,14 @@ struct QueueCallstack
uint64_t thread;
};
struct QueueCallstackFrame
struct QueueCallstackFrameSize
{
uint64_t ptr;
uint8_t size;
};
struct QueueCallstackFrame
{
uint64_t name;
uint64_t file;
uint32_t line;
@ -296,6 +302,7 @@ struct QueueItem
QueueMemFree memFree;
QueueCallstackMemory callstackMemory;
QueueCallstack callstack;
QueueCallstackFrameSize callstackFrameSize;
QueueCallstackFrame callstackFrame;
QueueCrashReport crashReport;
};
@ -345,6 +352,7 @@ static const size_t QueueDataSize[] = {
sizeof( QueueHeader ) + sizeof( QueueMemFree ),
sizeof( QueueHeader ) + sizeof( QueueMemAlloc ), // callstack
sizeof( QueueHeader ) + sizeof( QueueMemFree ), // callstack
sizeof( QueueHeader ) + sizeof( QueueCallstackFrameSize ),
sizeof( QueueHeader ) + sizeof( QueueCallstackFrame ),
// keep all QueueStringTransfer below
sizeof( QueueHeader ) + sizeof( QueueStringTransfer ), // string data

View File

@ -171,6 +171,15 @@ struct CallstackFrame
enum { CallstackFrameSize = sizeof( CallstackFrame ) };
struct CallstackFrameData
{
CallstackFrame* data;
uint8_t size;
};
enum { CallstackFrameDataSize = sizeof( CallstackFrameData ) };
struct CallstackFrameTree
{
uint64_t frame;

View File

@ -7,7 +7,7 @@ namespace Version
{
enum { Major = 0 };
enum { Minor = 4 };
enum { Patch = 2 };
enum { Patch = 3 };
}
}

View File

@ -3616,7 +3616,8 @@ void DrawZoneTrace( T zone, const std::vector<T>& trace, const Worker& worker, B
}
for( int8_t j=1; j<idx; j++ )
{
auto frame = worker.GetCallstackFrame( prevCs[j] );
auto frameData = worker.GetCallstackFrame( prevCs[j] );
auto frame = frameData->data;
ImGui::TextDisabled( "%s", worker.GetString( frame->name ) );
ImGui::SameLine();
ImGui::Spacing();
@ -3672,7 +3673,8 @@ void DrawZoneTrace( T zone, const std::vector<T>& trace, const Worker& worker, B
const auto csz = cs.size();
for( uint8_t i=1; i<csz; i++ )
{
auto frame = worker.GetCallstackFrame( cs[i] );
auto frameData = worker.GetCallstackFrame( cs[i] );
auto frame = frameData->data;
ImGui::TextDisabled( "%s", worker.GetString( frame->name ) );
ImGui::SameLine();
ImGui::Spacing();
@ -5559,7 +5561,7 @@ void View::DrawFindZone()
else
{
auto& callstack = m_worker.GetCallstack( v->first );
hdrString = m_worker.GetString( m_worker.GetCallstackFrame( *callstack.begin() )->name );
hdrString = m_worker.GetString( m_worker.GetCallstackFrame( *callstack.begin() )->data[0].name );
}
break;
default:
@ -6463,14 +6465,11 @@ void View::DrawCallstackWindow()
ImGui::NextColumn();
int fidx = 0;
int bidx = 0;
for( auto& entry : cs )
{
ImGui::Separator();
ImGui::Text( "%i", fidx++ );
ImGui::NextColumn();
auto frame = m_worker.GetCallstackFrame( entry );
if( !frame )
auto frameData = m_worker.GetCallstackFrame( entry );
if( !frameData )
{
char buf[32];
sprintf( buf, "%p", (void*)entry );
@ -6484,7 +6483,23 @@ void View::DrawCallstackWindow()
}
else
{
auto txt = m_worker.GetString( frame->name );
for( uint8_t f=0; f<frameData->size; f++ )
{
const auto& frame = frameData->data[f];
bidx++;
ImGui::Separator();
if( f == 0 )
{
ImGui::Text( "%i", fidx++ );
}
else
{
ImGui::TextDisabled( "inline" );
}
ImGui::NextColumn();
auto txt = m_worker.GetString( frame.name );
ImGui::TextWrapped( "%s", txt );
if( ImGui::IsItemClicked() )
{
@ -6493,13 +6508,13 @@ void View::DrawCallstackWindow()
ImGui::NextColumn();
ImGui::PushTextWrapPos( 0.0f );
float indentVal = 0.f;
if( m_callstackBuzzAnim.Match( fidx ) )
if( m_callstackBuzzAnim.Match( bidx ) )
{
const auto time = m_callstackBuzzAnim.Time();
indentVal = sin( time * 60.f ) * 10.f * time;
ImGui::Indent( indentVal );
}
txt = m_worker.GetString( frame->file );
txt = m_worker.GetString( frame.file );
if( m_showCallstackFrameAddress )
{
ImGui::TextDisabled( "0x%" PRIx64, entry );
@ -6512,13 +6527,13 @@ void View::DrawCallstackWindow()
}
else
{
if( frame->line == 0 )
if( frame.line == 0 )
{
ImGui::TextDisabled( "%s", txt );
}
else
{
ImGui::TextDisabled( "%s:%i", txt, frame->line );
ImGui::TextDisabled( "%s:%i", txt, frame.line );
}
if( ImGui::IsItemClicked() )
{
@ -6529,11 +6544,11 @@ void View::DrawCallstackWindow()
{
if( FileExists( txt ) )
{
SetTextEditorFile( txt, frame->line );
SetTextEditorFile( txt, frame.line );
}
else
{
m_callstackBuzzAnim.Enable( fidx, 0.5f );
m_callstackBuzzAnim.Enable( bidx, 0.5f );
}
}
if( indentVal != 0.f )
@ -6544,6 +6559,7 @@ void View::DrawCallstackWindow()
ImGui::NextColumn();
}
}
}
ImGui::EndColumns();
ImGui::End();
@ -7910,7 +7926,7 @@ void View::DrawFrameTreeLevel( std::vector<CallstackFrameTree>& tree, int& idx )
for( auto& v : tree )
{
idx++;
auto frame = m_worker.GetCallstackFrame( v.frame );
auto frame = m_worker.GetCallstackFrame( v.frame )->data;
bool expand = false;
if( v.children.empty() )
{
@ -8454,7 +8470,7 @@ void View::CallstackTooltip( uint32_t idx )
}
else
{
ImGui::Text( "%s", m_worker.GetString( frame->name ) );
ImGui::Text( "%s", m_worker.GetString( frame->data->name ) );
}
}
ImGui::EndTooltip();

View File

@ -14,6 +14,7 @@
#include <chrono>
#include <mutex>
#include <string.h>
#include <inttypes.h>
#if ( defined _MSC_VER && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L
# if __has_include(<execution>)
@ -217,6 +218,8 @@ Worker::Worker( const char* addr )
, m_pendingThreads( 0 )
, m_pendingSourceLocation( 0 )
, m_pendingCallstackFrames( 0 )
, m_pendingCallstackSubframes( 0 )
, m_callstackFrameStaging( nullptr )
, m_traceVersion( CurrentVersion )
, m_loadTime( 0 )
{
@ -1020,6 +1023,8 @@ Worker::Worker( FileRead& f, EventType::Type eventMask )
m_data.callstackPayload.push_back_no_space_check( arr );
}
if( fileVer >= FileVersion( 0, 4, 3 ) )
{
f.Read( sz );
m_data.callstackFrameMap.reserve( sz );
for( uint64_t i=0; i<sz; i++ )
@ -1027,10 +1032,32 @@ Worker::Worker( FileRead& f, EventType::Type eventMask )
uint64_t ptr;
f.Read( ptr );
auto frame = m_slab.Alloc<CallstackFrame>();
f.Read( frame, sizeof( CallstackFrame ) );
auto frameData = m_slab.Alloc<CallstackFrameData>();
f.Read( frameData->size );
m_data.callstackFrameMap.emplace( ptr, frame );
frameData->data = m_slab.Alloc<CallstackFrame>( frameData->size );
f.Read( frameData->data, sizeof( CallstackFrame ) * frameData->size );
m_data.callstackFrameMap.emplace( ptr, frameData );
}
}
else
{
f.Read( sz );
m_data.callstackFrameMap.reserve( sz );
for( uint64_t i=0; i<sz; i++ )
{
uint64_t ptr;
f.Read( ptr );
auto frameData = m_slab.Alloc<CallstackFrameData>();
frameData->size = 1;
frameData->data = m_slab.Alloc<CallstackFrame>();
f.Read( frameData->data, sizeof( CallstackFrame ) );
m_data.callstackFrameMap.emplace( ptr, frameData );
}
}
finishLoading:
@ -1201,7 +1228,7 @@ std::pair <int, int> Worker::GetFrameRange( const FrameData& fd, int64_t from, i
return std::make_pair( zbegin, zend );
}
const CallstackFrame* Worker::GetCallstackFrame( uint64_t ptr ) const
const CallstackFrameData* Worker::GetCallstackFrame( uint64_t ptr ) const
{
auto it = m_data.callstackFrameMap.find( ptr );
if( it == m_data.callstackFrameMap.end() )
@ -1582,7 +1609,7 @@ void Worker::Exec()
if( m_terminate )
{
if( m_pendingStrings != 0 || m_pendingThreads != 0 || m_pendingSourceLocation != 0 || m_pendingCallstackFrames != 0 ||
!m_pendingCustomStrings.empty() || m_data.plots.IsPending() || !m_pendingCallstacks.empty() )
!m_pendingCustomStrings.empty() || m_data.plots.IsPending() || !m_pendingCallstacks.empty() || m_pendingCallstackSubframes != 0 )
{
continue;
}
@ -2197,6 +2224,9 @@ bool Worker::Process( const QueueItem& ev )
case QueueType::Callstack:
ProcessCallstack( ev.callstack );
break;
case QueueType::CallstackFrameSize:
ProcessCallstackFrameSize( ev.callstackFrameSize );
break;
case QueueType::CallstackFrame:
ProcessCallstackFrame( ev.callstackFrame );
break;
@ -2961,28 +2991,53 @@ void Worker::ProcessCallstack( const QueueCallstack& ev )
m_pendingCallstacks.erase( it );
}
void Worker::ProcessCallstackFrame( const QueueCallstackFrame& ev )
void Worker::ProcessCallstackFrameSize( const QueueCallstackFrameSize& ev )
{
assert( !m_callstackFrameStaging );
assert( m_pendingCallstackSubframes == 0 );
assert( m_pendingCallstackFrames > 0 );
m_pendingCallstackFrames--;
m_pendingCallstackSubframes = ev.size;
// Frames may be duplicated due to recursion
auto fmit = m_data.callstackFrameMap.find( ev.ptr );
if( fmit == m_data.callstackFrameMap.end() )
{
m_callstackFrameStaging = m_slab.Alloc<CallstackFrameData>();
m_callstackFrameStaging->size = ev.size;
m_callstackFrameStaging->data = m_slab.Alloc<CallstackFrame>( ev.size );
m_callstackFrameStagingPtr = ev.ptr;
}
}
void Worker::ProcessCallstackFrame( const QueueCallstackFrame& ev )
{
assert( m_pendingCallstackSubframes > 0 );
auto nit = m_pendingCustomStrings.find( ev.name );
assert( nit != m_pendingCustomStrings.end() );
auto fit = m_pendingCustomStrings.find( ev.file );
assert( fit != m_pendingCustomStrings.end() );
// Frames may be duplicated due to recursion
if( fmit == m_data.callstackFrameMap.end() )
if( m_callstackFrameStaging )
{
CheckString( ev.file );
const auto idx = m_callstackFrameStaging->size - m_pendingCallstackSubframes;
auto frame = m_slab.Alloc<CallstackFrame>();
frame->name = StringIdx( nit->second.idx );
frame->file = StringIdx( fit->second.idx );
frame->line = ev.line;
m_callstackFrameStaging->data[idx].name = StringIdx( nit->second.idx );
m_callstackFrameStaging->data[idx].file = StringIdx( fit->second.idx );
m_callstackFrameStaging->data[idx].line = ev.line;
m_data.callstackFrameMap.emplace( ev.ptr, frame );
if( --m_pendingCallstackSubframes == 0 )
{
assert( m_data.callstackFrameMap.find( m_callstackFrameStagingPtr ) == m_data.callstackFrameMap.end() );
m_data.callstackFrameMap.emplace( m_callstackFrameStagingPtr, m_callstackFrameStaging );
m_callstackFrameStaging = nullptr;
}
}
else
{
m_pendingCallstackSubframes--;
}
m_pendingCustomStrings.erase( nit );
@ -3622,7 +3677,8 @@ void Worker::Write( FileWrite& f )
for( auto& frame : m_data.callstackFrameMap )
{
f.Write( &frame.first, sizeof( uint64_t ) );
f.Write( frame.second, sizeof( CallstackFrame ) );
f.Write( &frame.second->size, sizeof( frame.second->size ) );
f.Write( frame.second->data, sizeof( CallstackFrame ) * frame.second->size );
}
}

View File

@ -130,7 +130,7 @@ private:
flat_hash_map<VarArray<uint64_t>*, uint32_t, VarArrayHasherPOT<uint64_t>, VarArrayComparator<uint64_t>> callstackMap;
Vector<VarArray<uint64_t>*> callstackPayload;
flat_hash_map<uint64_t, CallstackFrame*> callstackFrameMap;
flat_hash_map<uint64_t, CallstackFrameData*> callstackFrameMap;
std::map<uint32_t, LockMap> lockMap;
@ -229,7 +229,7 @@ public:
const MemData& GetMemData() const { return m_data.memory; }
const VarArray<uint64_t>& GetCallstack( uint32_t idx ) const { return *m_data.callstackPayload[idx]; }
const CallstackFrame* GetCallstackFrame( uint64_t ptr ) const;
const CallstackFrameData* GetCallstackFrame( uint64_t ptr ) const;
const CrashEvent& GetCrashEvent() const { return m_data.m_crashEvent; }
@ -331,6 +331,7 @@ private:
tracy_force_inline void ProcessMemFreeCallstack( const QueueMemFree& ev );
tracy_force_inline void ProcessCallstackMemory( const QueueCallstackMemory& ev );
tracy_force_inline void ProcessCallstack( const QueueCallstack& ev );
tracy_force_inline void ProcessCallstackFrameSize( const QueueCallstackFrameSize& ev );
tracy_force_inline void ProcessCallstackFrame( const QueueCallstackFrame& ev );
tracy_force_inline void ProcessCrashReport( const QueueCrashReport& ev );
@ -438,6 +439,10 @@ private:
uint32_t m_pendingThreads;
uint32_t m_pendingSourceLocation;
uint32_t m_pendingCallstackFrames;
uint8_t m_pendingCallstackSubframes;
CallstackFrameData* m_callstackFrameStaging;
uint64_t m_callstackFrameStagingPtr;
uint64_t m_lastMemActionCallstack;
bool m_lastMemActionWasAlloc;