1
0
mirror of https://github.com/wolfpld/tracy synced 2025-04-29 12:23:53 +00:00

Extract thread compression into a separate class.

This commit is contained in:
Bartosz Taudul 2019-08-19 22:56:02 +02:00
parent 94382f54ca
commit 21e7a4bb16
10 changed files with 173 additions and 63 deletions

View File

@ -135,6 +135,7 @@
<ClCompile Include="..\..\..\common\tracy_lz4hc.cpp" /> <ClCompile Include="..\..\..\common\tracy_lz4hc.cpp" />
<ClCompile Include="..\..\..\server\TracyMemory.cpp" /> <ClCompile Include="..\..\..\server\TracyMemory.cpp" />
<ClCompile Include="..\..\..\server\TracyPrint.cpp" /> <ClCompile Include="..\..\..\server\TracyPrint.cpp" />
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp" />
<ClCompile Include="..\..\..\server\TracyWorker.cpp" /> <ClCompile Include="..\..\..\server\TracyWorker.cpp" />
<ClCompile Include="..\..\src\capture.cpp" /> <ClCompile Include="..\..\src\capture.cpp" />
<ClCompile Include="..\..\src\getopt.c" /> <ClCompile Include="..\..\src\getopt.c" />
@ -159,6 +160,7 @@
<ClInclude Include="..\..\..\server\TracyPopcnt.hpp" /> <ClInclude Include="..\..\..\server\TracyPopcnt.hpp" />
<ClInclude Include="..\..\..\server\TracyPrint.hpp" /> <ClInclude Include="..\..\..\server\TracyPrint.hpp" />
<ClInclude Include="..\..\..\server\TracySlab.hpp" /> <ClInclude Include="..\..\..\server\TracySlab.hpp" />
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp" />
<ClInclude Include="..\..\..\server\TracyVector.hpp" /> <ClInclude Include="..\..\..\server\TracyVector.hpp" />
<ClInclude Include="..\..\..\server\TracyWorker.hpp" /> <ClInclude Include="..\..\..\server\TracyWorker.hpp" />
<ClInclude Include="..\..\..\server\tracy_flat_hash_map.hpp" /> <ClInclude Include="..\..\..\server\tracy_flat_hash_map.hpp" />

View File

@ -39,6 +39,9 @@
<ClCompile Include="..\..\..\server\TracyPrint.cpp"> <ClCompile Include="..\..\..\server\TracyPrint.cpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp">
<Filter>server</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\common\tracy_lz4.hpp"> <ClInclude Include="..\..\..\common\tracy_lz4.hpp">
@ -110,5 +113,8 @@
<ClInclude Include="..\..\..\server\TracyPrint.hpp"> <ClInclude Include="..\..\..\server\TracyPrint.hpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp">
<Filter>server</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -117,6 +117,7 @@
<ClCompile Include="..\..\..\server\TracyPrint.cpp" /> <ClCompile Include="..\..\..\server\TracyPrint.cpp" />
<ClCompile Include="..\..\..\server\TracyStorage.cpp" /> <ClCompile Include="..\..\..\server\TracyStorage.cpp" />
<ClCompile Include="..\..\..\server\TracyTexture.cpp" /> <ClCompile Include="..\..\..\server\TracyTexture.cpp" />
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp" />
<ClCompile Include="..\..\..\server\TracyUserData.cpp" /> <ClCompile Include="..\..\..\server\TracyUserData.cpp" />
<ClCompile Include="..\..\..\server\TracyView.cpp" /> <ClCompile Include="..\..\..\server\TracyView.cpp" />
<ClCompile Include="..\..\..\server\TracyWorker.cpp" /> <ClCompile Include="..\..\..\server\TracyWorker.cpp" />
@ -172,6 +173,7 @@
<ClInclude Include="..\..\..\server\TracyStorage.hpp" /> <ClInclude Include="..\..\..\server\TracyStorage.hpp" />
<ClInclude Include="..\..\..\server\TracyStringDiscovery.hpp" /> <ClInclude Include="..\..\..\server\TracyStringDiscovery.hpp" />
<ClInclude Include="..\..\..\server\TracyTexture.hpp" /> <ClInclude Include="..\..\..\server\TracyTexture.hpp" />
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp" />
<ClInclude Include="..\..\..\server\TracyUserData.hpp" /> <ClInclude Include="..\..\..\server\TracyUserData.hpp" />
<ClInclude Include="..\..\..\server\TracyVarArray.hpp" /> <ClInclude Include="..\..\..\server\TracyVarArray.hpp" />
<ClInclude Include="..\..\..\server\TracyVector.hpp" /> <ClInclude Include="..\..\..\server\TracyVector.hpp" />

View File

@ -105,6 +105,9 @@
<ClCompile Include="..\..\..\server\TracyUserData.cpp"> <ClCompile Include="..\..\..\server\TracyUserData.cpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp">
<Filter>server</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\common\tracy_lz4.hpp"> <ClInclude Include="..\..\..\common\tracy_lz4.hpp">
@ -281,6 +284,9 @@
<ClInclude Include="..\..\..\server\TracyUserData.hpp"> <ClInclude Include="..\..\..\server\TracyUserData.hpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp">
<Filter>server</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="DebugVis.natvis" /> <Natvis Include="DebugVis.natvis" />

View File

@ -0,0 +1,78 @@
#include <limits>
#include "TracyFileRead.hpp"
#include "TracyFileWrite.hpp"
#include "TracyThreadCompress.hpp"
namespace tracy
{
ThreadCompress::ThreadCompress()
: m_threadLast( std::numeric_limits<uint64_t>::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; i<sz; i++ )
{
m_threadMap.emplace( m_threadExpand[i], i );
}
}
else
{
f.Read( sz );
m_threadExpand.reserve( sz );
m_threadExpand.push_back( 0 );
}
}
void ThreadCompress::Save( FileWrite& f ) const
{
uint64_t sz = m_threadExpand.size();
f.Write( &sz, sizeof( sz ) );
f.Write( m_threadExpand.data(), sz * sizeof( uint64_t ) );
}
uint16_t ThreadCompress::CompressThreadReal( uint64_t thread )
{
auto it = m_threadMap.find( thread );
if( it != m_threadMap.end() )
{
m_threadLast.first = thread;
m_threadLast.second = it->second;
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;
}
}

View File

@ -0,0 +1,61 @@
#ifndef __TRACY__THREADCOMPRESS_HPP__
#define __TRACY__THREADCOMPRESS_HPP__
#include <assert.h>
#include <stdint.h>
#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<uint64_t, uint16_t, nohash<uint64_t>> m_threadMap;
Vector<uint64_t> m_threadExpand;
std::pair<uint64_t, uint16_t> m_threadLast;
};
}
#endif

View File

@ -250,7 +250,7 @@ Worker::Worker( const char* addr )
, m_loadTime( 0 ) , m_loadTime( 0 )
{ {
m_data.sourceLocationExpand.push_back( 0 ); m_data.sourceLocationExpand.push_back( 0 );
m_data.threadExpand.push_back( 0 ); m_data.localThreadCompress.InitZero();
m_data.callstackPayload.push_back( nullptr ); m_data.callstackPayload.push_back( nullptr );
memset( m_gpuCtxMap, 0, sizeof( m_gpuCtxMap ) ); memset( m_gpuCtxMap, 0, sizeof( m_gpuCtxMap ) );
@ -515,23 +515,7 @@ Worker::Worker( FileRead& f, EventType::Type eventMask )
} }
} }
if( fileVer >= FileVersion( 0, 4, 4 ) ) m_data.localThreadCompress.Load( f, fileVer );
{
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; i<sz; i++ )
{
m_data.threadMap.emplace( m_data.threadExpand[i], i );
}
}
else
{
f.Read( sz );
m_data.threadExpand.reserve( sz );
m_data.threadExpand.push_back( 0 );
}
f.Read( sz ); f.Read( sz );
for( uint64_t i=0; i<sz; i++ ) for( uint64_t i=0; i<sz; i++ )
@ -1548,9 +1532,7 @@ Worker::Worker( FileRead& f, EventType::Type eventMask )
if( !t->timeline.empty() ) if( !t->timeline.empty() )
{ {
// Don't touch thread compression cache in a thread. // Don't touch thread compression cache in a thread.
auto it = m_data.threadMap.find( t->id ); ProcessTimeline( t->timeline, m_data.localThreadCompress.DecompressMustRaw( t->id ) );
assert( it != m_data.threadMap.end() );
ProcessTimeline( t->timeline, it->second );
} }
} }
for( auto& v : m_data.sourceLocationZones ) 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 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 const SourceLocation& Worker::GetSourceLocation( int16_t srcloc ) const
@ -2040,31 +2022,6 @@ const Worker::SourceLocationZones& Worker::GetZonesForSourceLocation( int16_t sr
} }
#endif #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() void Worker::Exec()
{ {
auto ShouldExit = [this] auto ShouldExit = [this]
@ -4828,9 +4785,7 @@ void Worker::Write( FileWrite& f )
f.Write( &ptr, sizeof( ptr ) ); f.Write( &ptr, sizeof( ptr ) );
} }
sz = m_data.threadExpand.size(); m_data.localThreadCompress.Save( f );
f.Write( &sz, sizeof( sz ) );
f.Write( m_data.threadExpand.data(), sz * sizeof( uint64_t ) );
sz = m_data.sourceLocation.size(); sz = m_data.sourceLocation.size();
f.Write( &sz, sizeof( sz ) ); f.Write( &sz, sizeof( sz ) );
@ -5044,7 +4999,7 @@ void Worker::Write( FileWrite& f )
ctxValid.reserve( m_data.ctxSwitch.size() ); ctxValid.reserve( m_data.ctxSwitch.size() );
for( auto it = m_data.ctxSwitch.begin(); it != m_data.ctxSwitch.end(); ++it ) 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 ); ctxValid.emplace_back( it );
} }

View File

@ -18,6 +18,7 @@
#include "TracyEvent.hpp" #include "TracyEvent.hpp"
#include "TracySlab.hpp" #include "TracySlab.hpp"
#include "TracyStringDiscovery.hpp" #include "TracyStringDiscovery.hpp"
#include "TracyThreadCompress.hpp"
#include "TracyVarArray.hpp" #include "TracyVarArray.hpp"
namespace tracy namespace tracy
@ -151,7 +152,6 @@ private:
, baseTime( 0 ) , baseTime( 0 )
, lastTime( 0 ) , lastTime( 0 )
, frameOffset( 0 ) , frameOffset( 0 )
, threadLast( std::numeric_limits<uint64_t>::max(), 0 )
, threadDataLast( std::numeric_limits<uint64_t>::max(), nullptr ) , threadDataLast( std::numeric_limits<uint64_t>::max(), nullptr )
, ctxSwitchLast( std::numeric_limits<uint64_t>::max(), nullptr ) , ctxSwitchLast( std::numeric_limits<uint64_t>::max(), nullptr )
{} {}
@ -193,9 +193,7 @@ private:
flat_hash_map<uint32_t, LockMap*, nohash<uint32_t>> lockMap; flat_hash_map<uint32_t, LockMap*, nohash<uint32_t>> lockMap;
flat_hash_map<uint64_t, uint16_t, nohash<uint64_t>> threadMap; ThreadCompress localThreadCompress;
Vector<uint64_t> threadExpand;
std::pair<uint64_t, uint16_t> threadLast;
std::pair<uint64_t, ThreadData*> threadDataLast; std::pair<uint64_t, ThreadData*> threadDataLast;
Vector<Vector<ZoneEvent*>> zoneChildren; Vector<Vector<ZoneEvent*>> zoneChildren;
@ -359,12 +357,8 @@ public:
bool AreSourceLocationZonesReady() const { return m_data.sourceLocationZonesReady; } bool AreSourceLocationZonesReady() const { return m_data.sourceLocationZonesReady; }
#endif #endif
tracy_force_inline uint16_t CompressThread( uint64_t 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 ); }
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]; }
std::shared_mutex& GetMbpsDataLock() { return m_mbpsData.lock; } std::shared_mutex& GetMbpsDataLock() { return m_mbpsData.lock; }
const std::vector<float>& GetMbpsData() const { return m_mbpsData.mbps; } const std::vector<float>& GetMbpsData() const { return m_mbpsData.mbps; }
@ -508,8 +502,6 @@ private:
void HandlePostponedPlots(); void HandlePostponedPlots();
StringLocation StoreString( char* str, size_t sz ); 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 ); const ContextSwitch* const GetContextSwitchDataImpl( uint64_t thread );
tracy_force_inline void ReadTimeline( FileRead& f, ZoneEvent* zone, uint16_t thread, int64_t& refTime ); tracy_force_inline void ReadTimeline( FileRead& f, ZoneEvent* zone, uint16_t thread, int64_t& refTime );

View File

@ -134,6 +134,7 @@
<ClCompile Include="..\..\..\common\tracy_lz4.cpp" /> <ClCompile Include="..\..\..\common\tracy_lz4.cpp" />
<ClCompile Include="..\..\..\common\tracy_lz4hc.cpp" /> <ClCompile Include="..\..\..\common\tracy_lz4hc.cpp" />
<ClCompile Include="..\..\..\server\TracyMemory.cpp" /> <ClCompile Include="..\..\..\server\TracyMemory.cpp" />
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp" />
<ClCompile Include="..\..\..\server\TracyWorker.cpp" /> <ClCompile Include="..\..\..\server\TracyWorker.cpp" />
<ClCompile Include="..\..\src\update.cpp" /> <ClCompile Include="..\..\src\update.cpp" />
</ItemGroup> </ItemGroup>
@ -156,6 +157,7 @@
<ClInclude Include="..\..\..\server\TracyMemory.hpp" /> <ClInclude Include="..\..\..\server\TracyMemory.hpp" />
<ClInclude Include="..\..\..\server\TracyPopcnt.hpp" /> <ClInclude Include="..\..\..\server\TracyPopcnt.hpp" />
<ClInclude Include="..\..\..\server\TracySlab.hpp" /> <ClInclude Include="..\..\..\server\TracySlab.hpp" />
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp" />
<ClInclude Include="..\..\..\server\TracyVector.hpp" /> <ClInclude Include="..\..\..\server\TracyVector.hpp" />
<ClInclude Include="..\..\..\server\TracyWorker.hpp" /> <ClInclude Include="..\..\..\server\TracyWorker.hpp" />
<ClInclude Include="..\..\..\server\tracy_flat_hash_map.hpp" /> <ClInclude Include="..\..\..\server\tracy_flat_hash_map.hpp" />

View File

@ -33,6 +33,9 @@
<ClCompile Include="..\..\..\common\tracy_lz4hc.cpp"> <ClCompile Include="..\..\..\common\tracy_lz4hc.cpp">
<Filter>common</Filter> <Filter>common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp">
<Filter>server</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\common\tracy_lz4.hpp"> <ClInclude Include="..\..\..\common\tracy_lz4.hpp">
@ -98,5 +101,8 @@
<ClInclude Include="..\..\..\common\tracy_lz4hc.hpp"> <ClInclude Include="..\..\..\common\tracy_lz4hc.hpp">
<Filter>common</Filter> <Filter>common</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp">
<Filter>server</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>