From 26d61dfdbaebdcae4ab8ecb05bdda33f45c7b825 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 25 Jul 2026 22:29:35 +0200 Subject: [PATCH] Make find zone sample percentages relative to the matched zones. The find zone samples list shares the drawing code with the sampling statistics window, which computed the percentage denominator from its own state: the whole-trace sample count, or the statistics range filter, if one was active. The find zone counts are scoped to the matched zones, so the percentages mixed two meanings in one table: the time column was relative to the zone selection while the count column was relative to the whole trace, and changing the range filter in the statistics window silently rescaled it. Pass the denominator from the caller. Find zone sums its zone-scoped counts, so both columns are now relative to the selection, and the statistics window computes the same denominator as before. --- profiler/src/profiler/TracyView.hpp | 2 +- profiler/src/profiler/TracyView_FindZone.cpp | 9 ++++-- profiler/src/profiler/TracyView_Samples.cpp | 28 +------------------ .../src/profiler/TracyView_Statistics.cpp | 28 ++++++++++++++++++- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/profiler/src/profiler/TracyView.hpp b/profiler/src/profiler/TracyView.hpp index 7391d246..4af48732 100644 --- a/profiler/src/profiler/TracyView.hpp +++ b/profiler/src/profiler/TracyView.hpp @@ -332,7 +332,7 @@ private: void DrawFindZone(); void AccumulationModeComboBox(); void DrawStatistics(); - void DrawSamplesStatistics(Vector& data, int64_t timeRange, AccumulationMode accumulationMode); + void DrawSamplesStatistics(Vector& data, int64_t timeRange, uint64_t totalSamples, AccumulationMode accumulationMode); void DrawMemory(); void DrawAllocList(); void DrawCompare(); diff --git a/profiler/src/profiler/TracyView_FindZone.cpp b/profiler/src/profiler/TracyView_FindZone.cpp index 181522d0..d6aefdad 100644 --- a/profiler/src/profiler/TracyView_FindZone.cpp +++ b/profiler/src/profiler/TracyView_FindZone.cpp @@ -2136,9 +2136,14 @@ void View::DrawFindZone() Vector data; data.reserve( m_findZone.samples.counts.size() ); - for( auto it: m_findZone.samples.counts ) data.push_back_no_space_check( it ); + uint64_t totalSamples = 0; + for( auto it: m_findZone.samples.counts ) + { + data.push_back_no_space_check( it ); + totalSamples += it.excl; + } int64_t timeRange = ( m_findZone.selGroup != m_findZone.Unselected ) ? m_findZone.selTotal : m_findZone.total; - DrawSamplesStatistics( data, timeRange, AccumulationMode::SelfOnly ); + DrawSamplesStatistics( data, timeRange, totalSamples, AccumulationMode::SelfOnly ); ImGui::TreePop(); } diff --git a/profiler/src/profiler/TracyView_Samples.cpp b/profiler/src/profiler/TracyView_Samples.cpp index eb18c2ed..a61bd9cd 100644 --- a/profiler/src/profiler/TracyView_Samples.cpp +++ b/profiler/src/profiler/TracyView_Samples.cpp @@ -85,7 +85,7 @@ void View::DrawSampleList( const TimelineContext& ctx, const std::vector& data, int64_t timeRange, AccumulationMode accumulationMode ) +void View::DrawSamplesStatistics( Vector& data, int64_t timeRange, uint64_t totalSamples, AccumulationMode accumulationMode ) { static unordered_flat_map inlineMap; assert( inlineMap.empty() ); @@ -156,32 +156,6 @@ void View::DrawSamplesStatistics( Vector& data, int64_t timeRange, Accu ImGui::TableSetupColumn( "Code size", ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoResize ); ImGui::TableHeadersRow(); - // The denominator is the number of collected samples, excluding context - // switch samples, which do not participate in the sampling statistics. This - // way the percentages have the same meaning with and without an active - // range filter. - uint64_t totalSamples; - if( m_statRange.active ) - { - static const auto CountInRange = []( const auto& vec, int64_t min, int64_t max ) -> uint64_t { - auto it = std::lower_bound( vec.begin(), vec.end(), min, []( const auto& lhs, const auto& rhs ) { return lhs.time.Val() < rhs; } ); - auto end = std::lower_bound( it, vec.end(), max, []( const auto& lhs, const auto& rhs ) { return lhs.time.Val() < rhs; } ); - return end - it; - }; - totalSamples = 0; - for( auto& td : m_worker.GetThreadData() ) - { - const auto cnt = CountInRange( td->samples, m_statRange.min, m_statRange.max ); - const auto ctx = CountInRange( td->ctxSwitchSamples, m_statRange.min, m_statRange.max ); - if( cnt > ctx ) totalSamples += cnt - ctx; - } - } - else - { - const auto cnt = m_worker.GetCallstackSampleCount(); - const auto ctx = m_worker.GetContextSwitchSampleCount(); - totalSamples = cnt > ctx ? cnt - ctx : 0; - } const double revSampleCount100 = totalSamples == 0 ? 0 : 100. / totalSamples; const bool showAll = m_showAllSymbols; diff --git a/profiler/src/profiler/TracyView_Statistics.cpp b/profiler/src/profiler/TracyView_Statistics.cpp index 8540b791..93195452 100644 --- a/profiler/src/profiler/TracyView_Statistics.cpp +++ b/profiler/src/profiler/TracyView_Statistics.cpp @@ -1000,7 +1000,33 @@ void View::DrawStatistics() } } - DrawSamplesStatistics( data, timeRange, m_statAccumulationMode ); + // The denominator is the number of collected samples, excluding context + // switch samples, which do not participate in the sampling statistics. This + // way the percentages have the same meaning with and without an active + // range filter. + uint64_t totalSamples; + if( m_statRange.active ) + { + static const auto CountInRange = []( const auto& vec, int64_t min, int64_t max ) -> uint64_t { + auto it = std::lower_bound( vec.begin(), vec.end(), min, []( const auto& lhs, const auto& rhs ) { return lhs.time.Val() < rhs; } ); + auto end = std::lower_bound( it, vec.end(), max, []( const auto& lhs, const auto& rhs ) { return lhs.time.Val() < rhs; } ); + return end - it; + }; + totalSamples = 0; + for( auto& td : m_worker.GetThreadData() ) + { + const auto cnt = CountInRange( td->samples, m_statRange.min, m_statRange.max ); + const auto ctx = CountInRange( td->ctxSwitchSamples, m_statRange.min, m_statRange.max ); + if( cnt > ctx ) totalSamples += cnt - ctx; + } + } + else + { + const auto cnt = m_worker.GetCallstackSampleCount(); + const auto ctx = m_worker.GetContextSwitchSampleCount(); + totalSamples = cnt > ctx ? cnt - ctx : 0; + } + DrawSamplesStatistics( data, timeRange, totalSamples, m_statAccumulationMode ); } ImGui::End(); }