From ee22cf3b0c7ea7706db09c242b5ffd577cab7a36 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 23 May 2020 15:14:57 +0200 Subject: [PATCH] Use cached source files. --- server/TracyFilesystem.cpp | 3 +- server/TracyFilesystem.hpp | 3 +- server/TracySourceView.cpp | 61 +++++++++++++++++++++++--------------- server/TracySourceView.hpp | 7 +++-- server/TracyView.cpp | 28 ++++++++--------- 5 files changed, 59 insertions(+), 43 deletions(-) diff --git a/server/TracyFilesystem.cpp b/server/TracyFilesystem.cpp index 6b4bdd8a..2570cd30 100644 --- a/server/TracyFilesystem.cpp +++ b/server/TracyFilesystem.cpp @@ -4,8 +4,9 @@ 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; if( stat( view.SourceSubstitution( fn ), &buf ) == 0 && ( buf.st_mode & S_IFREG ) != 0 ) { diff --git a/server/TracyFilesystem.hpp b/server/TracyFilesystem.hpp index 8b246dee..a03a78bd 100644 --- a/server/TracyFilesystem.hpp +++ b/server/TracyFilesystem.hpp @@ -8,6 +8,7 @@ namespace tracy { class View; +class Worker; 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; } -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 ); } diff --git a/server/TracySourceView.cpp b/server/TracySourceView.cpp index 421a7b25..dfb7b596 100644 --- a/server/TracySourceView.cpp +++ b/server/TracySourceView.cpp @@ -75,6 +75,7 @@ SourceView::SourceView( ImFont* font ) , m_symAddr( 0 ) , m_targetAddr( 0 ) , m_data( nullptr ) + , m_dataBuf( nullptr ) , m_dataSize( 0 ) , m_targetLine( 0 ) , m_selectedLine( 0 ) @@ -294,7 +295,7 @@ SourceView::SourceView( ImFont* font ) SourceView::~SourceView() { - delete[] m_data; + delete[] m_dataBuf; } static constexpr uint32_t PackCpuInfo( uint32_t cpuid ) @@ -385,7 +386,7 @@ void SourceView::SetCpuId( uint32_t cpuId ) 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_selectedLine = line; @@ -394,7 +395,7 @@ void SourceView::OpenSource( const char* fileName, int line, const View& view ) m_symAddr = 0; m_sourceFiles.clear(); - ParseSource( fileName, nullptr, view ); + ParseSource( fileName, worker, view ); assert( !m_lines.empty() ); } @@ -408,7 +409,7 @@ void SourceView::OpenSymbol( const char* fileName, int line, uint64_t baseAddr, m_selectedAddresses.clear(); m_selectedAddresses.emplace( symAddr ); - ParseSource( fileName, &worker, view ); + ParseSource( fileName, worker, view ); Disassemble( baseAddr, worker ); 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 ) { m_file = fileName; - m_fileStringIdx = worker ? worker->FindStringIdx( fileName ) : 0; + m_fileStringIdx = worker.FindStringIdx( fileName ); m_lines.clear(); if( fileName ) { - FILE* f = fopen( view.SourceSubstitution( fileName ), "rb" ); - fseek( f, 0, SEEK_END ); - const auto sz = ftell( f ); - fseek( f, 0, SEEK_SET ); - if( sz > m_dataSize ) + uint32_t sz; + const auto srcCache = worker.GetSourceFileFromCache( fileName ); + if( srcCache.data != nullptr ) { - delete[] m_data; - m_data = new char[sz+1]; - m_dataSize = sz; + m_data = srcCache.data; + sz = srcCache.len; + } + 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(); auto txt = m_data; @@ -1132,7 +1145,7 @@ void SourceView::RenderSymbolView( const Worker& worker, View& view ) { auto line = sym->line; 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; line = 0; @@ -1211,12 +1224,12 @@ void SourceView::RenderSymbolSourceView( uint32_t iptotal, unordered_flat_mapOpenSource( 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 ) @@ -213,7 +213,7 @@ bool View::ViewDispatch( const char* fileName, int line, uint64_t symAddr ) } else { - if( !SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) + if( !SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) ) { fileName = nullptr; line = 0; @@ -6403,7 +6403,7 @@ void View::DrawZoneInfoWindow() } } 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(); bool hilite = m_sourceViewFile == fileName; @@ -6941,7 +6941,7 @@ void View::DrawZoneInfoWindow() ImGui::PopID(); if( ImGui::IsItemClicked( 1 ) ) { - if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) + if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) ) { ViewSource( fileName, srcloc.line ); } @@ -7363,7 +7363,7 @@ void View::DrawGpuInfoWindow() } } 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(); bool hilite = m_sourceViewFile == fileName; @@ -7478,7 +7478,7 @@ void View::DrawGpuInfoWindow() ImGui::PopID(); if( ImGui::IsItemClicked( 1 ) ) { - if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) + if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) ) { ViewSource( fileName, srcloc.line ); } @@ -8054,7 +8054,7 @@ void View::DrawOptions() ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line ); if( ImGui::IsItemClicked( 1 ) ) { - if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) + if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) ) { ViewSource( fileName, sl.line ); } @@ -8128,7 +8128,7 @@ void View::DrawOptions() ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line ); if( ImGui::IsItemClicked( 1 ) ) { - if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) + if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) ) { ViewSource( fileName, sl.line ); } @@ -8202,7 +8202,7 @@ void View::DrawOptions() ImGui::TextDisabled( "(%s) %s:%i", RealToString( l.second->timeline.size() ), fileName, sl.line ); if( ImGui::IsItemClicked( 1 ) ) { - if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) + if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) ) { 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 ); if( ImGui::IsItemClicked( 1 ) ) { - if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) + if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) ) { ViewSource( fileName, srcloc.line ); } @@ -11407,7 +11407,7 @@ void View::DrawStatistics() ImGui::TextDisabled( "%s:%i", file, srcloc.line ); if( ImGui::IsItemClicked( 1 ) ) { - if( SourceFileValid( file, m_worker.GetCaptureTime(), *this ) ) + if( SourceFileValid( file, m_worker.GetCaptureTime(), *this, m_worker ) ) { ViewSource( file, srcloc.line ); } @@ -11781,7 +11781,7 @@ void View::DrawStatistics() } 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 ); if( !m_statSeparateInlines ) m_sourceView->CalcInlineStats( false ); @@ -11942,7 +11942,7 @@ void View::DrawStatistics() } 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 ); if( !m_statSeparateInlines ) m_sourceView->CalcInlineStats( true ); @@ -13366,7 +13366,7 @@ void View::DrawLockInfoWindow() ImGui::Text( "%s:%i", fileName, srcloc.line ); if( ImGui::IsItemClicked( 1 ) ) { - if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this ) ) + if( SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) ) { ViewSource( fileName, srcloc.line ); }