From a35634078300e4a0cbbd8d7f4c2bbc0845573eff Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 25 Jul 2022 22:57:20 +0200 Subject: [PATCH] Extract connection history functionality. --- profiler/build/win32/Tracy.vcxproj | 2 + profiler/build/win32/Tracy.vcxproj.filters | 6 ++ profiler/src/ConnectionHistory.cpp | 83 ++++++++++++++++++++++ profiler/src/ConnectionHistory.hpp | 32 +++++++++ profiler/src/main.cpp | 79 +++----------------- 5 files changed, 134 insertions(+), 68 deletions(-) create mode 100644 profiler/src/ConnectionHistory.cpp create mode 100644 profiler/src/ConnectionHistory.hpp diff --git a/profiler/build/win32/Tracy.vcxproj b/profiler/build/win32/Tracy.vcxproj index 8ab1ddde..7dea1ca6 100644 --- a/profiler/build/win32/Tracy.vcxproj +++ b/profiler/build/win32/Tracy.vcxproj @@ -195,6 +195,7 @@ + @@ -314,6 +315,7 @@ + diff --git a/profiler/build/win32/Tracy.vcxproj.filters b/profiler/build/win32/Tracy.vcxproj.filters index 12672e3f..89433012 100644 --- a/profiler/build/win32/Tracy.vcxproj.filters +++ b/profiler/build/win32/Tracy.vcxproj.filters @@ -342,6 +342,9 @@ src + + src + @@ -695,6 +698,9 @@ src + + src + diff --git a/profiler/src/ConnectionHistory.cpp b/profiler/src/ConnectionHistory.cpp new file mode 100644 index 00000000..074426b6 --- /dev/null +++ b/profiler/src/ConnectionHistory.cpp @@ -0,0 +1,83 @@ +#include +#include +#include + +#include "ConnectionHistory.hpp" + +#include "../../server/tracy_pdqsort.h" +#include "../../server/TracyStorage.hpp" + + +ConnectionHistory::ConnectionHistory() + : m_fn( tracy::GetSavePath( "connection.history" ) ) +{ + FILE* f = fopen( m_fn.c_str(), "rb" ); + if( !f ) return; + + uint64_t sz; + fread( &sz, 1, sizeof( sz ), f ); + for( uint64_t i=0; i::const_iterator> vec; + vec.reserve( m_connHistMap.size() ); + for( auto it = m_connHistMap.begin(); it != m_connHistMap.end(); ++it ) + { + vec.emplace_back( it ); + } + tracy::pdqsort_branchless( vec.begin(), vec.end(), []( const auto& lhs, const auto& rhs ) { return lhs->second > rhs->second; } ); + std::swap( m_connHistVec, vec ); +} + +void ConnectionHistory::Count( const char* name ) +{ + std::string addr( name ); + auto it = m_connHistMap.find( addr ); + if( it != m_connHistMap.end() ) + { + it->second++; + } + else + { + m_connHistMap.emplace( std::move( addr ), 1 ); + } + Rebuild(); +} + +void ConnectionHistory::Erase( size_t idx ) +{ + assert( idx < m_connHistVec.size() ); + m_connHistMap.erase( m_connHistVec[idx] ); + Rebuild(); +} diff --git a/profiler/src/ConnectionHistory.hpp b/profiler/src/ConnectionHistory.hpp new file mode 100644 index 00000000..1a5c823c --- /dev/null +++ b/profiler/src/ConnectionHistory.hpp @@ -0,0 +1,32 @@ +#ifndef __CONNECTIONHISTORY_HPP__ +#define __CONNECTIONHISTORY_HPP__ + +#include +#include +#include +#include + +class ConnectionHistory +{ +public: + ConnectionHistory(); + ~ConnectionHistory(); + + const std::string& Name( size_t idx ) const { return m_connHistVec[idx]->first; } + + void Count( const char* name ); + void Erase( size_t idx ); + + bool empty() const { return m_connHistVec.empty(); } + size_t size() const { return m_connHistVec.size(); } + +private: + void Rebuild(); + + std::string m_fn; + + std::unordered_map m_connHistMap; + std::vector::const_iterator> m_connHistVec; +}; + +#endif diff --git a/profiler/src/main.cpp b/profiler/src/main.cpp index aa33b927..b4fdf7b5 100644 --- a/profiler/src/main.cpp +++ b/profiler/src/main.cpp @@ -50,6 +50,7 @@ #include "icon.hpp" #include "Fonts.hpp" +#include "ConnectionHistory.hpp" #include "HttpRequest.hpp" #include "NativeWindow.hpp" #include "ResolvService.hpp" @@ -79,18 +80,6 @@ static void WindowRefreshCallback( GLFWwindow* window ) DrawContents(); } -std::vector::const_iterator> RebuildConnectionHistory( const std::unordered_map& connHistMap ) -{ - std::vector::const_iterator> ret; - ret.reserve( connHistMap.size() ); - for( auto it = connHistMap.begin(); it != connHistMap.end(); ++it ) - { - ret.emplace_back( it ); - } - tracy::pdqsort_branchless( ret.begin(), ret.end(), []( const auto& lhs, const auto& rhs ) { return lhs->second > rhs->second; } ); - return ret; -} - struct ClientData { int64_t time; @@ -115,8 +104,7 @@ static std::mutex resolvLock; static tracy::unordered_flat_map resolvMap; static ResolvService resolv( port ); static char addr[1024] = { "127.0.0.1" }; -static std::unordered_map connHistMap; -static std::vector::const_iterator> connHistVec; +static ConnectionHistory* connHist; static std::atomic viewShutdown { ViewShutdown::False }; static double animTime = 0; static float dpiScale = 1.f; @@ -200,28 +188,10 @@ int main( int argc, char** argv ) } WindowPosition winPos; + ConnectionHistory connHistory; + + connHist = &connHistory; - std::string connHistFile = tracy::GetSavePath( "connection.history" ); - { - FILE* f = fopen( connHistFile.c_str(), "rb" ); - if( f ) - { - uint64_t sz; - fread( &sz, 1, sizeof( sz ), f ); - for( uint64_t i=0; iempty() ) { ImGui::SameLine(); if( ImGui::BeginCombo( "##frameCombo", nullptr, ImGuiComboFlags_NoPreview ) ) { int idxRemove = -1; - const auto sz = std::min( 5, connHistVec.size() ); + const auto sz = std::min( 5, connHist->size() ); for( size_t i=0; ifirst; + const auto& str = connHist->Name( i ); if( ImGui::Selectable( str.c_str() ) ) { memcpy( addr, str.c_str(), str.size() + 1 ); @@ -671,8 +625,7 @@ static void DrawContents() } if( idxRemove >= 0 ) { - connHistMap.erase( connHistVec[idxRemove] ); - connHistVec = RebuildConnectionHistory( connHistMap ); + connHist->Erase( idxRemove ); } ImGui::EndCombo(); } @@ -680,19 +633,9 @@ static void DrawContents() connectClicked |= ImGui::Button( ICON_FA_WIFI " Connect" ); if( connectClicked && *addr && !loadThread.joinable() ) { - const auto addrLen = strlen( addr ); - std::string addrStr( addr, addr+addrLen ); - auto it = connHistMap.find( addrStr ); - if( it != connHistMap.end() ) - { - it->second++; - } - else - { - connHistMap.emplace( std::move( addrStr ), 1 ); - } - connHistVec = RebuildConnectionHistory( connHistMap ); + connHist->Count( addr ); + const auto addrLen = strlen( addr ); auto ptr = addr + addrLen - 1; while( ptr > addr && *ptr != ':' ) ptr--; if( *ptr == ':' )