Use cached source files.

This commit is contained in:
Bartosz Taudul 2020-05-23 15:14:57 +02:00
parent d470202bca
commit ee22cf3b0c
5 changed files with 59 additions and 43 deletions

View File

@ -4,8 +4,9 @@
namespace tracy namespace tracy
{ {
bool SourceFileValid( const char* fn, uint64_t olderThan, const View& view ) bool SourceFileValid( const char* fn, uint64_t olderThan, const View& view, const Worker& worker )
{ {
if( worker.GetSourceFileFromCache( fn ).data != nullptr ) return true;
struct stat buf; struct stat buf;
if( stat( view.SourceSubstitution( fn ), &buf ) == 0 && ( buf.st_mode & S_IFREG ) != 0 ) if( stat( view.SourceSubstitution( fn ), &buf ) == 0 && ( buf.st_mode & S_IFREG ) != 0 )
{ {

View File

@ -8,6 +8,7 @@ namespace tracy
{ {
class View; class View;
class Worker;
static inline bool FileExists( const char* fn ) static inline bool FileExists( const char* fn )
{ {
@ -15,7 +16,7 @@ static inline bool FileExists( const char* fn )
return stat( fn, &buf ) == 0 && ( buf.st_mode & S_IFREG ) != 0; return stat( fn, &buf ) == 0 && ( buf.st_mode & S_IFREG ) != 0;
} }
bool SourceFileValid( const char* fn, uint64_t olderThan, const View& view ); bool SourceFileValid( const char* fn, uint64_t olderThan, const View& view, const Worker& worker );
bool SourceFileValid( const char* fn, uint64_t olderThan ); bool SourceFileValid( const char* fn, uint64_t olderThan );
} }

View File

@ -75,6 +75,7 @@ SourceView::SourceView( ImFont* font )
, m_symAddr( 0 ) , m_symAddr( 0 )
, m_targetAddr( 0 ) , m_targetAddr( 0 )
, m_data( nullptr ) , m_data( nullptr )
, m_dataBuf( nullptr )
, m_dataSize( 0 ) , m_dataSize( 0 )
, m_targetLine( 0 ) , m_targetLine( 0 )
, m_selectedLine( 0 ) , m_selectedLine( 0 )
@ -294,7 +295,7 @@ SourceView::SourceView( ImFont* font )
SourceView::~SourceView() SourceView::~SourceView()
{ {
delete[] m_data; delete[] m_dataBuf;
} }
static constexpr uint32_t PackCpuInfo( uint32_t cpuid ) static constexpr uint32_t PackCpuInfo( uint32_t cpuid )
@ -385,7 +386,7 @@ void SourceView::SetCpuId( uint32_t cpuId )
SelectMicroArchitecture( "ZEN2" ); SelectMicroArchitecture( "ZEN2" );
} }
void SourceView::OpenSource( const char* fileName, int line, const View& view ) void SourceView::OpenSource( const char* fileName, int line, const View& view, const Worker& worker )
{ {
m_targetLine = line; m_targetLine = line;
m_selectedLine = line; m_selectedLine = line;
@ -394,7 +395,7 @@ void SourceView::OpenSource( const char* fileName, int line, const View& view )
m_symAddr = 0; m_symAddr = 0;
m_sourceFiles.clear(); m_sourceFiles.clear();
ParseSource( fileName, nullptr, view ); ParseSource( fileName, worker, view );
assert( !m_lines.empty() ); assert( !m_lines.empty() );
} }
@ -408,7 +409,7 @@ void SourceView::OpenSymbol( const char* fileName, int line, uint64_t baseAddr,
m_selectedAddresses.clear(); m_selectedAddresses.clear();
m_selectedAddresses.emplace( symAddr ); m_selectedAddresses.emplace( symAddr );
ParseSource( fileName, &worker, view ); ParseSource( fileName, worker, view );
Disassemble( baseAddr, worker ); Disassemble( baseAddr, worker );
SelectLine( line, &worker, true, symAddr ); SelectLine( line, &worker, true, symAddr );
@ -430,28 +431,40 @@ void SourceView::OpenSymbol( const char* fileName, int line, uint64_t baseAddr,
} }
} }
void SourceView::ParseSource( const char* fileName, const Worker* worker, const View& view ) void SourceView::ParseSource( const char* fileName, const Worker& worker, const View& view )
{ {
if( m_file != fileName ) if( m_file != fileName )
{ {
m_file = fileName; m_file = fileName;
m_fileStringIdx = worker ? worker->FindStringIdx( fileName ) : 0; m_fileStringIdx = worker.FindStringIdx( fileName );
m_lines.clear(); m_lines.clear();
if( fileName ) if( fileName )
{ {
FILE* f = fopen( view.SourceSubstitution( fileName ), "rb" ); uint32_t sz;
fseek( f, 0, SEEK_END ); const auto srcCache = worker.GetSourceFileFromCache( fileName );
const auto sz = ftell( f ); if( srcCache.data != nullptr )
fseek( f, 0, SEEK_SET );
if( sz > m_dataSize )
{ {
delete[] m_data; m_data = srcCache.data;
m_data = new char[sz+1]; sz = srcCache.len;
m_dataSize = sz; }
else
{
FILE* f = fopen( view.SourceSubstitution( fileName ), "rb" );
assert( f );
fseek( f, 0, SEEK_END );
sz = ftell( f );
fseek( f, 0, SEEK_SET );
if( sz > m_dataSize )
{
delete[] m_dataBuf;
m_dataBuf = new char[sz+1];
m_dataSize = sz;
}
fread( m_dataBuf, 1, sz, f );
m_dataBuf[sz] = '\0';
m_data = m_dataBuf;
fclose( f );
} }
fread( m_data, 1, sz, f );
m_data[sz] = '\0';
fclose( f );
m_tokenizer.Reset(); m_tokenizer.Reset();
auto txt = m_data; auto txt = m_data;
@ -1132,7 +1145,7 @@ void SourceView::RenderSymbolView( const Worker& worker, View& view )
{ {
auto line = sym->line; auto line = sym->line;
auto file = line == 0 ? nullptr : worker.GetString( sym->file ); auto file = line == 0 ? nullptr : worker.GetString( sym->file );
if( file && !SourceFileValid( file, worker.GetCaptureTime(), view ) ) if( file && !SourceFileValid( file, worker.GetCaptureTime(), view, worker ) )
{ {
file = nullptr; file = nullptr;
line = 0; line = 0;
@ -1211,12 +1224,12 @@ void SourceView::RenderSymbolSourceView( uint32_t iptotal, unordered_flat_map<ui
SmallColorBox( color ); SmallColorBox( color );
ImGui::SameLine(); ImGui::SameLine();
auto fstr = worker.GetString( StringIdx( v.first ) ); auto fstr = worker.GetString( StringIdx( v.first ) );
if( SourceFileValid( fstr, worker.GetCaptureTime(), view ) ) if( SourceFileValid( fstr, worker.GetCaptureTime(), view, worker ) )
{ {
ImGui::PushID( v.first ); ImGui::PushID( v.first );
if( ImGui::Selectable( fstr, fstr == m_file ) ) if( ImGui::Selectable( fstr, fstr == m_file ) )
{ {
ParseSource( fstr, &worker, view ); ParseSource( fstr, worker, view );
m_targetLine = v.second; m_targetLine = v.second;
SelectLine( v.second, &worker ); SelectLine( v.second, &worker );
} }
@ -1296,7 +1309,7 @@ void SourceView::RenderSymbolSourceView( uint32_t iptotal, unordered_flat_map<ui
SmallColorBox( color ); SmallColorBox( color );
ImGui::SameLine(); ImGui::SameLine();
auto fstr = worker.GetString( StringIdx( v.first ) ); auto fstr = worker.GetString( StringIdx( v.first ) );
if( SourceFileValid( fstr, worker.GetCaptureTime(), view ) ) if( SourceFileValid( fstr, worker.GetCaptureTime(), view, worker ) )
{ {
ImGui::PushID( v.first ); ImGui::PushID( v.first );
if( ImGui::Selectable( fstr, fstr == m_file, ImGuiSelectableFlags_SpanAllColumns ) ) if( ImGui::Selectable( fstr, fstr == m_file, ImGuiSelectableFlags_SpanAllColumns ) )
@ -1310,7 +1323,7 @@ void SourceView::RenderSymbolSourceView( uint32_t iptotal, unordered_flat_map<ui
break; break;
} }
} }
ParseSource( fstr, &worker, view ); ParseSource( fstr, worker, view );
m_targetLine = line; m_targetLine = line;
SelectLine( line, &worker ); SelectLine( line, &worker );
} }
@ -2413,9 +2426,9 @@ void SourceView::RenderAsmLine( AsmLine& line, uint32_t ipcnt, uint32_t iptotal,
SelectLine( srcline, &worker, false ); SelectLine( srcline, &worker, false );
m_displayMode = DisplayMixed; m_displayMode = DisplayMixed;
} }
else if( SourceFileValid( fileName, worker.GetCaptureTime(), view ) ) else if( SourceFileValid( fileName, worker.GetCaptureTime(), view, worker ) )
{ {
ParseSource( fileName, &worker, view ); ParseSource( fileName, worker, view );
m_targetLine = srcline; m_targetLine = srcline;
SelectLine( srcline, &worker, false ); SelectLine( srcline, &worker, false );
m_displayMode = DisplayMixed; m_displayMode = DisplayMixed;

View File

@ -132,14 +132,14 @@ public:
void SetCpuId( uint32_t cpuid ); void SetCpuId( uint32_t cpuid );
void OpenSource( const char* fileName, int line, const View& view ); void OpenSource( const char* fileName, int line, const View& view, const Worker& worker );
void OpenSymbol( const char* fileName, int line, uint64_t baseAddr, uint64_t symAddr, const Worker& worker, const View& view ); void OpenSymbol( const char* fileName, int line, uint64_t baseAddr, uint64_t symAddr, const Worker& worker, const View& view );
void Render( const Worker& worker, View& view ); void Render( const Worker& worker, View& view );
void CalcInlineStats( bool val ) { m_calcInlineStats = val; } void CalcInlineStats( bool val ) { m_calcInlineStats = val; }
private: private:
void ParseSource( const char* fileName, const Worker* worker, const View& view ); void ParseSource( const char* fileName, const Worker& worker, const View& view );
bool Disassemble( uint64_t symAddr, const Worker& worker ); bool Disassemble( uint64_t symAddr, const Worker& worker );
void RenderSimpleSourceView(); void RenderSimpleSourceView();
@ -190,7 +190,8 @@ private:
uint64_t m_symAddr; uint64_t m_symAddr;
uint64_t m_baseAddr; uint64_t m_baseAddr;
uint64_t m_targetAddr; uint64_t m_targetAddr;
char* m_data; const char* m_data;
char* m_dataBuf;
size_t m_dataSize; size_t m_dataSize;
int m_targetLine; int m_targetLine;
int m_selectedLine; int m_selectedLine;

View File

@ -195,7 +195,7 @@ void View::ViewSource( const char* fileName, int line )
{ {
assert( fileName ); assert( fileName );
m_sourceViewFile = fileName; m_sourceViewFile = fileName;
m_sourceView->OpenSource( fileName, line, *this ); m_sourceView->OpenSource( fileName, line, *this, m_worker );
} }
void View::ViewSymbol( const char* fileName, int line, uint64_t baseAddr, uint64_t symAddr ) void View::ViewSymbol( const char* fileName, int line, uint64_t baseAddr, uint64_t symAddr )
@ -213,7 +213,7 @@ bool View::ViewDispatch( const char* fileName, int line, uint64_t symAddr )
} }
else else
{ {
if( !SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( !SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
fileName = nullptr; fileName = nullptr;
line = 0; line = 0;
@ -6403,7 +6403,7 @@ void View::DrawZoneInfoWindow()
} }
} }
const auto fileName = m_worker.GetString( srcloc.file ); const auto fileName = m_worker.GetString( srcloc.file );
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ImGui::SameLine(); ImGui::SameLine();
bool hilite = m_sourceViewFile == fileName; bool hilite = m_sourceViewFile == fileName;
@ -6941,7 +6941,7 @@ void View::DrawZoneInfoWindow()
ImGui::PopID(); ImGui::PopID();
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSource( fileName, srcloc.line ); ViewSource( fileName, srcloc.line );
} }
@ -7363,7 +7363,7 @@ void View::DrawGpuInfoWindow()
} }
} }
const auto fileName = m_worker.GetString( srcloc.file ); const auto fileName = m_worker.GetString( srcloc.file );
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ImGui::SameLine(); ImGui::SameLine();
bool hilite = m_sourceViewFile == fileName; bool hilite = m_sourceViewFile == fileName;
@ -7478,7 +7478,7 @@ void View::DrawGpuInfoWindow()
ImGui::PopID(); ImGui::PopID();
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSource( fileName, srcloc.line ); ViewSource( fileName, srcloc.line );
} }
@ -8054,7 +8054,7 @@ void View::DrawOptions()
ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line ); ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line );
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSource( fileName, sl.line ); ViewSource( fileName, sl.line );
} }
@ -8128,7 +8128,7 @@ void View::DrawOptions()
ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line ); ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line );
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSource( fileName, sl.line ); ViewSource( fileName, sl.line );
} }
@ -8202,7 +8202,7 @@ void View::DrawOptions()
ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line ); ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line );
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSource( fileName, sl.line ); ViewSource( fileName, sl.line );
} }
@ -8773,7 +8773,7 @@ void View::DrawFindZone()
ImGui::TextColored( ImVec4( 0.5, 0.5, 0.5, 1 ), "(%s) %s:%i", RealToString( zones.size() ), fileName, srcloc.line ); ImGui::TextColored( ImVec4( 0.5, 0.5, 0.5, 1 ), "(%s) %s:%i", RealToString( zones.size() ), fileName, srcloc.line );
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSource( fileName, srcloc.line ); ViewSource( fileName, srcloc.line );
} }
@ -11407,7 +11407,7 @@ void View::DrawStatistics()
ImGui::TextDisabled( "%s:%i", file, srcloc.line ); ImGui::TextDisabled( "%s:%i", file, srcloc.line );
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( file, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( file, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSource( file, srcloc.line ); ViewSource( file, srcloc.line );
} }
@ -11781,7 +11781,7 @@ void View::DrawStatistics()
} }
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( file, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( file, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSymbol( file, line, codeAddr, v.symAddr ); ViewSymbol( file, line, codeAddr, v.symAddr );
if( !m_statSeparateInlines ) m_sourceView->CalcInlineStats( false ); if( !m_statSeparateInlines ) m_sourceView->CalcInlineStats( false );
@ -11942,7 +11942,7 @@ void View::DrawStatistics()
} }
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( file, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( file, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSymbol( file, line, codeAddr, iv.symAddr ); ViewSymbol( file, line, codeAddr, iv.symAddr );
if( !m_statSeparateInlines ) m_sourceView->CalcInlineStats( true ); if( !m_statSeparateInlines ) m_sourceView->CalcInlineStats( true );
@ -13366,7 +13366,7 @@ void View::DrawLockInfoWindow()
ImGui::Text( "%s:%i", fileName, srcloc.line ); ImGui::Text( "%s:%i", fileName, srcloc.line );
if( ImGui::IsItemClicked( 1 ) ) if( ImGui::IsItemClicked( 1 ) )
{ {
if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{ {
ViewSource( fileName, srcloc.line ); ViewSource( fileName, srcloc.line );
} }