From 92ae0033084cb38cd2c7f4b1abb7b901b4de48b5 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 15 May 2021 14:25:45 +0200 Subject: [PATCH] Improve trace saving experience. This adds additional dialog, which allows selection of compression mode. Also, when a trace cannot be saved, a failure popup will be displayed. --- server/TracyView.cpp | 120 +++++++++++++++++++++++++++++++++++-------- server/TracyView.hpp | 4 +- 2 files changed, 103 insertions(+), 21 deletions(-) diff --git a/server/TracyView.cpp b/server/TracyView.cpp index dea19863..520cae1b 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -36,7 +36,6 @@ #include "tracy_pdqsort.h" #include "TracyColor.hpp" #include "TracyFileRead.hpp" -#include "TracyFileWrite.hpp" #include "TracyFilesystem.hpp" #include "TracyMouse.hpp" #include "TracyPopcnt.hpp" @@ -345,6 +344,24 @@ void View::DrawHelpMarker( const char* desc ) const } } +static const char* CompressionName[] = { + "LZ4", + "LZ4 HC", + "LZ4 HC extreme", + "Zstd", + nullptr +}; + +static const char* CompressionDesc[] = { + "Fastest save, fast load time, big file size", + "Slow save, fastest load time, reasonable file size", + "Very slow save, fastest load time, file smaller than LZ4 HC", + "Configurable save time (fast-slowest), reasonable load time, smallest file size", + nullptr +}; + +static_assert( sizeof( CompressionName ) == sizeof( CompressionDesc ), "Unmatched compression names and descriptions" ); + bool View::Draw() { HandshakeStatus status = (HandshakeStatus)s_instance->m_worker.GetHandshakeStatus(); @@ -606,6 +623,66 @@ bool View::Draw() ImGui::EndPopup(); } + bool saveFailed = false; + if( !s_instance->m_filenameStaging.empty() ) + { + ImGui::OpenPopup( "Save trace" ); + } + if( ImGui::BeginPopupModal( "Save trace", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) ) + { + assert( !s_instance->m_filenameStaging.empty() ); + auto fn = s_instance->m_filenameStaging.c_str(); + TextFocused( "Path:", fn ); + ImGui::Separator(); + + static FileWrite::Compression comp = FileWrite::Compression::Fast; + static int zlvl = 6; + ImGui::TextUnformatted( ICON_FA_FILE_ARCHIVE " Trace compression" ); + ImGui::SameLine(); + TextDisabledUnformatted( "Can be changed later with the upgrade utility" ); + ImGui::Indent(); + int idx = 0; + while( CompressionName[idx] ) + { + if( ImGui::RadioButton( CompressionName[idx], (int)comp == idx ) ) comp = (FileWrite::Compression)idx; + ImGui::SameLine(); + ImGui::TextDisabled( CompressionDesc[idx] ); + idx++; + } + ImGui::Unindent(); + ImGui::TextUnformatted( "Zstd level" ); + ImGui::SameLine(); + TextDisabledUnformatted( "Increasing level decreases file size, but increases save and load times" ); + ImGui::Indent(); + ImGui::SliderInt( "##zstd", &zlvl, 1, 22, "%d", ImGuiSliderFlags_AlwaysClamp ); + ImGui::Unindent(); + + ImGui::Separator(); + if( ImGui::Button( ICON_FA_SAVE " Save trace" ) ) + { + saveFailed = !s_instance->Save( fn, comp, zlvl ); + s_instance->m_filenameStaging.clear(); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if( ImGui::Button( "Cancel" ) ) + { + s_instance->m_filenameStaging.clear(); + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } + + if( saveFailed ) ImGui::OpenPopup( "Save failed" ); + if( ImGui::BeginPopupModal( "Save failed", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) ) + { + TextCentered( ICON_FA_EXCLAMATION_TRIANGLE ); + ImGui::TextUnformatted( "Could not save trace at the specified location. Try again somewhere else." ); + ImGui::Separator(); + if( ImGui::Button( "Oh well" ) ) ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + s_time += ImGui::GetIO().DeltaTime; return s_instance->DrawImpl(); } @@ -1466,33 +1543,16 @@ bool View::DrawConnection() const char* fn = "trace.tracy"; #endif { - std::unique_ptr f; const auto sz = strlen( fn ); if( sz < 7 || memcmp( fn + sz - 6, ".tracy", 6 ) != 0 ) { char tmp[1024]; sprintf( tmp, "%s.tracy", fn ); - f.reset( FileWrite::Open( tmp ) ); - if( f ) m_filename = tmp; + m_filenameStaging = tmp; } else { - f.reset( FileWrite::Open( fn ) ); - if( f ) m_filename = fn; - } - if( f ) - { - m_userData.StateShouldBePreserved(); - m_saveThreadState.store( SaveThreadState::Saving, std::memory_order_relaxed ); - m_saveThread = std::thread( [this, f{std::move( f )}] { - std::lock_guard lock( m_worker.GetDataLock() ); - m_worker.Write( *f ); - f->Finish(); - const auto stats = f->GetCompressionStatistics(); - m_srcFileBytes.store( stats.first, std::memory_order_relaxed ); - m_dstFileBytes.store( stats.second, std::memory_order_relaxed ); - m_saveThreadState.store( SaveThreadState::NeedsJoin, std::memory_order_release ); - } ); + m_filenameStaging = fn; } } } @@ -17976,4 +18036,24 @@ void View::DrawSourceTooltip( const char* filename, uint32_t srcline, int before if( separateTooltip ) ImGui::EndTooltip(); } +bool View::Save( const char* fn, FileWrite::Compression comp, int zlevel ) +{ + std::unique_ptr f( FileWrite::Open( fn, comp, zlevel ) ); + if( !f ) return false; + + m_userData.StateShouldBePreserved(); + m_saveThreadState.store( SaveThreadState::Saving, std::memory_order_relaxed ); + m_saveThread = std::thread( [this, f{std::move( f )}] { + std::lock_guard lock( m_worker.GetDataLock() ); + m_worker.Write( *f ); + f->Finish(); + const auto stats = f->GetCompressionStatistics(); + m_srcFileBytes.store( stats.first, std::memory_order_relaxed ); + m_dstFileBytes.store( stats.second, std::memory_order_relaxed ); + m_saveThreadState.store( SaveThreadState::NeedsJoin, std::memory_order_release ); + } ); + + return true; +} + } diff --git a/server/TracyView.hpp b/server/TracyView.hpp index c1b76884..99e3d427 100644 --- a/server/TracyView.hpp +++ b/server/TracyView.hpp @@ -12,6 +12,7 @@ #include "TracyBadVersion.hpp" #include "TracyBuzzAnim.hpp" #include "TracyDecayValue.hpp" +#include "TracyFileWrite.hpp" #include "TracyImGui.hpp" #include "TracyShortPtr.hpp" #include "TracySourceContents.hpp" @@ -282,6 +283,7 @@ private: void CalcZoneTimeDataImpl( const V& children, const ContextSwitch* ctx, unordered_flat_map& data, int64_t& ztime, const ZoneEvent& zone ); void SetPlaybackFrame( uint32_t idx ); + bool Save( const char* fn, FileWrite::Compression comp, int zlevel ); unordered_flat_map m_visData; unordered_flat_map m_visibleMsgThread; @@ -323,7 +325,7 @@ private: void AdjustThreadHeight( View::VisData& vis, int oldOffset, int& offset ); Worker m_worker; - std::string m_filename; + std::string m_filename, m_filenameStaging; bool m_staticView; ViewMode m_viewMode; bool m_viewModeHeuristicTry = false;