From 21e7a4bb160c1f2285e854dbc76e4b7b0b58ef31 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 19 Aug 2019 22:56:02 +0200 Subject: [PATCH] Extract thread compression into a separate class. --- capture/build/win32/capture.vcxproj | 2 + capture/build/win32/capture.vcxproj.filters | 6 ++ profiler/build/win32/Tracy.vcxproj | 2 + profiler/build/win32/Tracy.vcxproj.filters | 6 ++ server/TracyThreadCompress.cpp | 78 +++++++++++++++++++++ server/TracyThreadCompress.hpp | 61 ++++++++++++++++ server/TracyWorker.cpp | 57 ++------------- server/TracyWorker.hpp | 16 ++--- update/build/win32/update.vcxproj | 2 + update/build/win32/update.vcxproj.filters | 6 ++ 10 files changed, 173 insertions(+), 63 deletions(-) create mode 100644 server/TracyThreadCompress.cpp create mode 100644 server/TracyThreadCompress.hpp diff --git a/capture/build/win32/capture.vcxproj b/capture/build/win32/capture.vcxproj index 2430db08..94a84fbf 100644 --- a/capture/build/win32/capture.vcxproj +++ b/capture/build/win32/capture.vcxproj @@ -135,6 +135,7 @@ + @@ -159,6 +160,7 @@ + diff --git a/capture/build/win32/capture.vcxproj.filters b/capture/build/win32/capture.vcxproj.filters index 4eeef4e9..d8dd1204 100644 --- a/capture/build/win32/capture.vcxproj.filters +++ b/capture/build/win32/capture.vcxproj.filters @@ -39,6 +39,9 @@ server + + server + @@ -110,5 +113,8 @@ server + + server + \ No newline at end of file diff --git a/profiler/build/win32/Tracy.vcxproj b/profiler/build/win32/Tracy.vcxproj index 62608eb4..8652921c 100644 --- a/profiler/build/win32/Tracy.vcxproj +++ b/profiler/build/win32/Tracy.vcxproj @@ -117,6 +117,7 @@ + @@ -172,6 +173,7 @@ + diff --git a/profiler/build/win32/Tracy.vcxproj.filters b/profiler/build/win32/Tracy.vcxproj.filters index 09a419d6..c8cb7bd0 100644 --- a/profiler/build/win32/Tracy.vcxproj.filters +++ b/profiler/build/win32/Tracy.vcxproj.filters @@ -105,6 +105,9 @@ server + + server + @@ -281,6 +284,9 @@ server + + server + diff --git a/server/TracyThreadCompress.cpp b/server/TracyThreadCompress.cpp new file mode 100644 index 00000000..8da6b118 --- /dev/null +++ b/server/TracyThreadCompress.cpp @@ -0,0 +1,78 @@ +#include + +#include "TracyFileRead.hpp" +#include "TracyFileWrite.hpp" +#include "TracyThreadCompress.hpp" + +namespace tracy +{ + +ThreadCompress::ThreadCompress() + : m_threadLast( std::numeric_limits::max(), 0 ) +{ +} + +void ThreadCompress::InitZero() +{ + assert( m_threadExpand.empty() ); + m_threadExpand.push_back( 0 ); +} + +void ThreadCompress::Load( FileRead& f, int fileVer ) +{ + assert( m_threadExpand.empty() ); + assert( m_threadMap.empty() ); + + uint64_t sz; + if( fileVer >= FileVersion( 0, 4, 4 ) ) + { + f.Read( sz ); + m_threadExpand.reserve_and_use( sz ); + f.Read( m_threadExpand.data(), sizeof( uint64_t ) * sz ); + m_threadMap.reserve( sz ); + for( size_t i=0; isecond; + return it->second; + } + else + { + return CompressThreadNew( thread ); + } +} + +uint16_t ThreadCompress::CompressThreadNew( uint64_t thread ) +{ + auto sz = m_threadExpand.size(); + m_threadExpand.push_back( thread ); + m_threadMap.emplace( thread, sz ); + m_threadLast.first = thread; + m_threadLast.second = sz; + return sz; +} + +} diff --git a/server/TracyThreadCompress.hpp b/server/TracyThreadCompress.hpp new file mode 100644 index 00000000..af348b51 --- /dev/null +++ b/server/TracyThreadCompress.hpp @@ -0,0 +1,61 @@ +#ifndef __TRACY__THREADCOMPRESS_HPP__ +#define __TRACY__THREADCOMPRESS_HPP__ + +#include +#include + +#include "../common/TracyForceInline.hpp" +#include "tracy_flat_hash_map.hpp" +#include "TracyVector.hpp" + +namespace tracy +{ + +class FileRead; +class FileWrite; + +class ThreadCompress +{ +public: + ThreadCompress(); + + void InitZero(); + void Load( FileRead& f, int fileVer ); + void Save( FileWrite& f ) const; + + tracy_force_inline uint16_t CompressThread( uint64_t thread ) + { + if( m_threadLast.first == thread ) return m_threadLast.second; + return CompressThreadReal( thread ); + } + + tracy_force_inline uint64_t DecompressThread( uint16_t thread ) const + { + assert( thread < m_threadExpand.size() ); + return m_threadExpand[thread]; + } + + tracy_force_inline uint16_t DecompressMustRaw( uint64_t thread ) const + { + auto it = m_threadMap.find( thread ); + assert( it != m_threadMap.end() ); + return it->second; + } + + tracy_force_inline bool Exists( uint64_t thread ) const + { + return m_threadMap.find( thread ) != m_threadMap.end(); + } + +private: + uint16_t CompressThreadReal( uint64_t thread ); + uint16_t CompressThreadNew( uint64_t thread ); + + flat_hash_map> m_threadMap; + Vector m_threadExpand; + std::pair m_threadLast; +}; + +} + +#endif diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index fe44d620..89166fe2 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -250,7 +250,7 @@ Worker::Worker( const char* addr ) , m_loadTime( 0 ) { m_data.sourceLocationExpand.push_back( 0 ); - m_data.threadExpand.push_back( 0 ); + m_data.localThreadCompress.InitZero(); m_data.callstackPayload.push_back( nullptr ); memset( m_gpuCtxMap, 0, sizeof( m_gpuCtxMap ) ); @@ -515,23 +515,7 @@ Worker::Worker( FileRead& f, EventType::Type eventMask ) } } - if( fileVer >= FileVersion( 0, 4, 4 ) ) - { - f.Read( sz ); - m_data.threadExpand.reserve_and_use( sz ); - f.Read( m_data.threadExpand.data(), sizeof( uint64_t ) * sz ); - m_data.threadMap.reserve( sz ); - for( size_t i=0; itimeline.empty() ) { // Don't touch thread compression cache in a thread. - auto it = m_data.threadMap.find( t->id ); - assert( it != m_data.threadMap.end() ); - ProcessTimeline( t->timeline, it->second ); + ProcessTimeline( t->timeline, m_data.localThreadCompress.DecompressMustRaw( t->id ) ); } } for( auto& v : m_data.sourceLocationZones ) @@ -1881,7 +1863,7 @@ const char* Worker::GetThreadString( uint64_t id ) const bool Worker::IsThreadLocal( uint64_t id ) const { - return m_data.threadMap.find( id ) != m_data.threadMap.end(); + return m_data.localThreadCompress.Exists( id ); } const SourceLocation& Worker::GetSourceLocation( int16_t srcloc ) const @@ -2040,31 +2022,6 @@ const Worker::SourceLocationZones& Worker::GetZonesForSourceLocation( int16_t sr } #endif -uint16_t Worker::CompressThreadReal( uint64_t thread ) -{ - auto it = m_data.threadMap.find( thread ); - if( it != m_data.threadMap.end() ) - { - m_data.threadLast.first = thread; - m_data.threadLast.second = it->second; - return it->second; - } - else - { - return CompressThreadNew( thread ); - } -} - -uint16_t Worker::CompressThreadNew( uint64_t thread ) -{ - auto sz = m_data.threadExpand.size(); - m_data.threadExpand.push_back( thread ); - m_data.threadMap.emplace( thread, sz ); - m_data.threadLast.first = thread; - m_data.threadLast.second = sz; - return sz; -} - void Worker::Exec() { auto ShouldExit = [this] @@ -4828,9 +4785,7 @@ void Worker::Write( FileWrite& f ) f.Write( &ptr, sizeof( ptr ) ); } - sz = m_data.threadExpand.size(); - f.Write( &sz, sizeof( sz ) ); - f.Write( m_data.threadExpand.data(), sz * sizeof( uint64_t ) ); + m_data.localThreadCompress.Save( f ); sz = m_data.sourceLocation.size(); f.Write( &sz, sizeof( sz ) ); @@ -5044,7 +4999,7 @@ void Worker::Write( FileWrite& f ) ctxValid.reserve( m_data.ctxSwitch.size() ); for( auto it = m_data.ctxSwitch.begin(); it != m_data.ctxSwitch.end(); ++it ) { - if( m_data.threadMap.find( it->first ) != m_data.threadMap.end() ) + if( m_data.localThreadCompress.Exists( it->first ) ) { ctxValid.emplace_back( it ); } diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index 62afaa40..b483dc27 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -18,6 +18,7 @@ #include "TracyEvent.hpp" #include "TracySlab.hpp" #include "TracyStringDiscovery.hpp" +#include "TracyThreadCompress.hpp" #include "TracyVarArray.hpp" namespace tracy @@ -151,7 +152,6 @@ private: , baseTime( 0 ) , lastTime( 0 ) , frameOffset( 0 ) - , threadLast( std::numeric_limits::max(), 0 ) , threadDataLast( std::numeric_limits::max(), nullptr ) , ctxSwitchLast( std::numeric_limits::max(), nullptr ) {} @@ -193,9 +193,7 @@ private: flat_hash_map> lockMap; - flat_hash_map> threadMap; - Vector threadExpand; - std::pair threadLast; + ThreadCompress localThreadCompress; std::pair threadDataLast; Vector> zoneChildren; @@ -359,12 +357,8 @@ public: bool AreSourceLocationZonesReady() const { return m_data.sourceLocationZonesReady; } #endif - tracy_force_inline uint16_t CompressThread( uint64_t thread ) - { - if( m_data.threadLast.first == thread ) return m_data.threadLast.second; - return CompressThreadReal( thread ); - } - tracy_force_inline uint64_t DecompressThread( uint16_t thread ) const { assert( thread < m_data.threadExpand.size() ); return m_data.threadExpand[thread]; } + tracy_force_inline uint16_t CompressThread( uint64_t thread ) { return m_data.localThreadCompress.CompressThread( thread ); } + tracy_force_inline uint64_t DecompressThread( uint16_t thread ) const { return m_data.localThreadCompress.DecompressThread( thread ); } std::shared_mutex& GetMbpsDataLock() { return m_mbpsData.lock; } const std::vector& GetMbpsData() const { return m_mbpsData.mbps; } @@ -508,8 +502,6 @@ private: void HandlePostponedPlots(); StringLocation StoreString( char* str, size_t sz ); - uint16_t CompressThreadReal( uint64_t thread ); - uint16_t CompressThreadNew( uint64_t thread ); const ContextSwitch* const GetContextSwitchDataImpl( uint64_t thread ); tracy_force_inline void ReadTimeline( FileRead& f, ZoneEvent* zone, uint16_t thread, int64_t& refTime ); diff --git a/update/build/win32/update.vcxproj b/update/build/win32/update.vcxproj index da7abe49..d632fc36 100644 --- a/update/build/win32/update.vcxproj +++ b/update/build/win32/update.vcxproj @@ -134,6 +134,7 @@ + @@ -156,6 +157,7 @@ + diff --git a/update/build/win32/update.vcxproj.filters b/update/build/win32/update.vcxproj.filters index fa1b604e..4d373293 100644 --- a/update/build/win32/update.vcxproj.filters +++ b/update/build/win32/update.vcxproj.filters @@ -33,6 +33,9 @@ common + + server + @@ -98,5 +101,8 @@ common + + server + \ No newline at end of file