From 0bb6162a0178e0ed7db7a0106873d35be3434fb4 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 5 Feb 2020 17:05:15 +0100 Subject: [PATCH] Allow sorting memory data listings. --- server/TracyView.cpp | 64 +++++++++++++++++++++++++++++++------------- server/TracyView.hpp | 3 +-- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 8291649f..1adec61d 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -6229,8 +6229,8 @@ void View::DrawZoneInfoWindow() } pdqsort_branchless( v.begin(), v.end(), [] ( const auto& l, const auto& r ) { return l->TimeAlloc() < r->TimeAlloc(); } ); - ListMemData( v.begin(), v.end(), []( auto v ) { - ImGui::Text( "0x%" PRIx64, (*v)->Ptr() ); + ListMemData( v, []( auto v ) { + ImGui::Text( "0x%" PRIx64, v->Ptr() ); }, nullptr, m_allocTimeRelativeToZone ? ev.Start() : -1 ); ImGui::TreePop(); } @@ -12607,28 +12607,37 @@ void View::DrawAnnotationList() ImGui::End(); } -template -void View::ListMemData( T ptr, T end, std::function DrawAddress, const char* id, int64_t startTime ) +void View::ListMemData( std::vector& vec, std::function DrawAddress, const char* id, int64_t startTime ) { if( startTime == -1 ) startTime = 0; const auto& style = ImGui::GetStyle(); - const auto dist = std::distance( ptr, end ) + 1; + const auto dist = vec.size() + 1; const auto ty = ImGui::GetTextLineHeight() + style.ItemSpacing.y; + enum class SortBy + { + Address, + Size, + AllocTime, + Duration + }; + + static SortBy sortBy = SortBy::AllocTime; + ImGui::BeginChild( id ? id : "##memScroll", ImVec2( 0, std::max( ty * std::min( dist, 5 ), std::min( ty * dist, ImGui::GetContentRegionAvail().y ) ) ) ); ImGui::Columns( 8 ); - ImGui::TextUnformatted( "Address" ); + if( ImGui::SmallButton( "Address" ) ) sortBy = SortBy::Address; ImGui::SameLine(); DrawHelpMarker( "Click on address to display memory allocation info window.\nMiddle click to zoom to allocation range." ); ImGui::NextColumn(); - ImGui::TextUnformatted( "Size" ); + if( ImGui::SmallButton( "Size" ) ) sortBy = SortBy::Size; ImGui::NextColumn(); - ImGui::TextUnformatted( "Appeared at" ); + if( ImGui::SmallButton( "Appeared at" ) ) sortBy = SortBy::AllocTime; ImGui::SameLine(); DrawHelpMarker( "Click on entry to center timeline at the memory allocation time." ); ImGui::NextColumn(); - ImGui::TextUnformatted( "Duration" ); + if( ImGui::SmallButton( "Duration" ) ) sortBy = SortBy::Duration; ImGui::SameLine(); DrawHelpMarker( "Active allocations are displayed using green color.\nClick on entry to center timeline at the memory release time." ); ImGui::NextColumn(); @@ -12648,24 +12657,42 @@ void View::ListMemData( T ptr, T end, std::function DrawAddress, const const auto& mem = m_worker.GetMemData(); + switch( sortBy ) + { + case SortBy::Address: + pdqsort_branchless( vec.begin(), vec.end(), []( const auto& l, const auto& r ) { return l->Ptr() < r->Ptr(); } ); + break; + case SortBy::AllocTime: + break; + case SortBy::Duration: + pdqsort_branchless( vec.begin(), vec.end(), []( const auto& l, const auto& r ) { return ( l->TimeFree() - l->TimeAlloc() ) < ( r->TimeFree() - r->TimeAlloc() ); } ); + break; + case SortBy::Size: + pdqsort_branchless( vec.begin(), vec.end(), []( const auto& l, const auto& r ) { return l->Size() < r->Size(); } ); + break; + default: + assert( false ); + break; + } + int idx = 0; - ImGuiListClipper clipper( end - ptr ); + ImGuiListClipper clipper( vec.end() - vec.begin() ); while( clipper.Step() ) { for( auto i=clipper.DisplayStart; i( match.begin(), match.end(), [this]( auto it ) { - auto& v = *it; + ListMemData( match, [this]( auto v ) { if( v->Ptr() == m_memInfo.ptrFind ) { ImGui::Text( "0x%" PRIx64, m_memInfo.ptrFind ); @@ -13328,8 +13354,8 @@ void View::DrawMemory() if( !items.empty() ) { - ListMemData( items.begin(), items.end(), []( auto v ) { - ImGui::Text( "0x%" PRIx64, (*v)->Ptr() ); + ListMemData( items, []( auto v ) { + ImGui::Text( "0x%" PRIx64, v->Ptr() ); }, "##activeMem" ); } else @@ -13598,8 +13624,8 @@ void View::DrawAllocList() ImGui::SetNextWindowSize( ImVec2( 1100, 500 ), ImGuiCond_FirstUseEver ); ImGui::Begin( "Allocations list", &m_memInfo.showAllocList ); TextFocused( "Number of allocations:", RealToString( m_memInfo.allocList.size() ) ); - ListMemData( data.begin(), data.end(), []( auto v ) { - ImGui::Text( "0x%" PRIx64, (*v)->Ptr() ); + ListMemData( data, []( auto v ) { + ImGui::Text( "0x%" PRIx64, v->Ptr() ); }, "##allocations" ); ImGui::End(); } diff --git a/server/TracyView.hpp b/server/TracyView.hpp index 4c26eda0..d812849f 100644 --- a/server/TracyView.hpp +++ b/server/TracyView.hpp @@ -153,8 +153,7 @@ private: void DrawSelectedAnnotation(); void DrawAnnotationList(); - template - void ListMemData( T ptr, T end, std::function DrawAddress, const char* id = nullptr, int64_t startTime = -1 ); + void ListMemData( std::vector& vec, std::function DrawAddress, const char* id = nullptr, int64_t startTime = -1 ); unordered_flat_map GetCallstackPaths( const MemData& mem, bool onlyActive ) const; unordered_flat_map GetCallstackFrameTreeBottomUp( const MemData& mem ) const;