Standard way of string reference storage in SourceLocation.

StringRef::isptr was changed to isidx, as initialization of empty
SourceLocation zeroes the struct.
This commit is contained in:
Bartosz Taudul 2017-11-11 02:02:47 +01:00
parent 947cd04e5e
commit 24084cbcd2
3 changed files with 48 additions and 90 deletions

View File

@ -12,12 +12,28 @@ namespace tracy
struct StringRef struct StringRef
{ {
enum Type { Ptr, Idx };
StringRef() {}
StringRef( Type t, uint64_t data )
: isidx( t == Idx )
{
if( isidx )
{
stridx = (uint32_t)data;
}
else
{
strptr = data;
}
}
union union
{ {
uint64_t strptr; uint64_t strptr;
uint32_t stridx; uint32_t stridx;
}; };
bool isptr; bool isidx;
}; };
struct TextData struct TextData
@ -28,11 +44,10 @@ struct TextData
struct SourceLocation struct SourceLocation
{ {
uint64_t function; // ptr StringRef function;
uint64_t file; // ptr StringRef file;
uint32_t line; uint32_t line;
uint32_t color : 31; uint32_t color;
uint32_t stringsAllocated : 1;
}; };
enum { SourceLocationSize = sizeof( SourceLocation ) }; enum { SourceLocationSize = sizeof( SourceLocation ) };

View File

@ -265,24 +265,8 @@ View::View( FileRead& f )
m_sourceLocationPayload.reserve( sz ); m_sourceLocationPayload.reserve( sz );
for( uint64_t i=0; i<sz; i++ ) for( uint64_t i=0; i<sz; i++ )
{ {
uint32_t color, line;
f.Read( &color, sizeof( color ) );
f.Read( &line, sizeof( line ) );
uint64_t fsz;
f.Read( &fsz, sizeof( fsz ) );
auto func = m_slab.Alloc<char>( fsz+1 );
f.Read( func, fsz );
func[fsz] = '\0';
f.Read( &fsz, sizeof( fsz ) );
auto file = m_slab.Alloc<char>( fsz+1 );
f.Read( file, fsz );
file[fsz] = '\0';
auto srcloc = m_slab.Alloc<SourceLocation>(); auto srcloc = m_slab.Alloc<SourceLocation>();
srcloc->function = (uint64_t)func; f.Read( srcloc, sizeof( *srcloc ) );
srcloc->file = (uint64_t)file;
srcloc->line = line;
srcloc->color = color;
srcloc->stringsAllocated = 1;
m_sourceLocationPayload.push_back( srcloc ); m_sourceLocationPayload.push_back( srcloc );
} }
@ -827,7 +811,7 @@ void View::ProcessMessageLiteral( const QueueMessage& ev )
CheckString( ev.text ); CheckString( ev.text );
auto msg = m_slab.Alloc<MessageData>(); auto msg = m_slab.Alloc<MessageData>();
msg->time = int64_t( ev.time * m_timerMul ); msg->time = int64_t( ev.time * m_timerMul );
msg->ref.isptr = true; msg->ref.isidx = false;
msg->ref.strptr = ev.text; msg->ref.strptr = ev.text;
InsertMessageData( msg, ev.thread ); InsertMessageData( msg, ev.thread );
} }
@ -946,10 +930,10 @@ void View::AddSourceLocation( const QueueSourceLocation& srcloc )
CheckString( srcloc.function ); CheckString( srcloc.function );
uint32_t color = ( srcloc.r << 16 ) | ( srcloc.g << 8 ) | srcloc.b; uint32_t color = ( srcloc.r << 16 ) | ( srcloc.g << 8 ) | srcloc.b;
std::lock_guard<std::mutex> lock( m_lock ); std::lock_guard<std::mutex> lock( m_lock );
m_sourceLocation.emplace( srcloc.ptr, SourceLocation { srcloc.function, srcloc.file, srcloc.line, color, 0 } ); m_sourceLocation.emplace( srcloc.ptr, SourceLocation { StringRef( StringRef::Ptr, srcloc.function ), StringRef( StringRef::Ptr, srcloc.file ), srcloc.line, color } );
} }
void View::AddSourceLocationPayload( uint64_t ptr, const char* data, size_t sz ) void View::AddSourceLocationPayload( uint64_t ptr, char* data, size_t sz )
{ {
const auto start = data; const auto start = data;
@ -962,20 +946,17 @@ void View::AddSourceLocationPayload( uint64_t ptr, const char* data, size_t sz )
data += 8; data += 8;
auto end = data; auto end = data;
while( *end ) end++; while( *end ) end++;
const auto func = StoreString( data, end - data );
end++; end++;
char* func = m_slab.Alloc<char>( end - data );
memcpy( func, data, end - data );
const auto ssz = sz - ( end - start ); const auto ssz = sz - ( end - start );
char* source = m_slab.Alloc<char>( ssz+1 ); const auto source = StoreString( end, ssz );
memcpy( source, end, ssz );
source[ssz] = '\0';
auto srcloc = m_slab.Alloc<SourceLocation>(); auto srcloc = m_slab.Alloc<SourceLocation>();
srcloc->function = (uint64_t)func; srcloc->function = StringRef( StringRef::Idx, func.idx );
srcloc->file = (uint64_t)source; srcloc->file = StringRef( StringRef::Idx, source.idx );
srcloc->line = line; srcloc->line = line;
srcloc->color = color; srcloc->color = color;
srcloc->stringsAllocated = 1;
std::unique_lock<std::mutex> lock( m_lock ); std::unique_lock<std::mutex> lock( m_lock );
pit->second->srcloc = -int32_t( m_sourceLocationPayload.size() + 1 ); pit->second->srcloc = -int32_t( m_sourceLocationPayload.size() + 1 );
@ -992,7 +973,7 @@ void View::AddMessageData( uint64_t ptr, char* str, size_t sz )
assert( it != m_pendingMessages.end() ); assert( it != m_pendingMessages.end() );
auto msg = m_slab.Alloc<MessageData>(); auto msg = m_slab.Alloc<MessageData>();
msg->time = it->second.time; msg->time = it->second.time;
msg->ref.isptr = false; msg->ref.isidx = true;
msg->ref.stridx = sl.idx; msg->ref.stridx = sl.idx;
InsertMessageData( msg, it->second.thread ); InsertMessageData( msg, it->second.thread );
m_pendingMessages.erase( it ); m_pendingMessages.erase( it );
@ -1327,13 +1308,13 @@ const char* View::GetString( uint64_t ptr ) const
const char* View::GetString( const StringRef& ref ) const const char* View::GetString( const StringRef& ref ) const
{ {
if( ref.isptr ) if( ref.isidx )
{ {
return GetString( ref.strptr ); return m_stringData[ref.stridx];
} }
else else
{ {
return m_stringData[ref.stridx]; return GetString( ref.strptr );
} }
} }
@ -2136,7 +2117,7 @@ int View::DrawZoneLevel( const Vector<ZoneEvent*>& vec, bool hover, double pxns,
} }
else else
{ {
zoneName = GetSrcLocFunction( srcloc ); zoneName = GetString( srcloc.function );
} }
int dmul = 1; int dmul = 1;
@ -2517,8 +2498,8 @@ int View::DrawLocks( uint64_t tid, bool hover, double pxns, const ImVec2& wpos,
ImGui::BeginTooltip(); ImGui::BeginTooltip();
ImGui::Text( "Lock #%" PRIu32, v.first ); ImGui::Text( "Lock #%" PRIu32, v.first );
ImGui::Text( "%s", GetSrcLocFunction( srcloc ) ); ImGui::Text( "%s", GetString( srcloc.function ) );
ImGui::Text( "%s:%i", GetSrcLocFile( srcloc ), srcloc.line ); ImGui::Text( "%s:%i", GetString( srcloc.file ), srcloc.line );
ImGui::Text( "Time: %s", TimeToString( t1 - t0 ) ); ImGui::Text( "Time: %s", TimeToString( t1 - t0 ) );
ImGui::Separator(); ImGui::Separator();
@ -2541,8 +2522,8 @@ int View::DrawLocks( uint64_t tid, bool hover, double pxns, const ImVec2& wpos,
{ {
auto& marklocdata = GetSourceLocation( markloc ); auto& marklocdata = GetSourceLocation( markloc );
ImGui::Text( "Lock event location:" ); ImGui::Text( "Lock event location:" );
ImGui::Text( "%s", GetSrcLocFunction( marklocdata ) ); ImGui::Text( "%s", GetString( marklocdata.function ) );
ImGui::Text( "%s:%i", GetSrcLocFile( marklocdata ), marklocdata.line ); ImGui::Text( "%s:%i", GetString( marklocdata.file ), marklocdata.line );
ImGui::Separator(); ImGui::Separator();
} }
@ -2636,7 +2617,7 @@ int View::DrawLocks( uint64_t tid, bool hover, double pxns, const ImVec2& wpos,
if( drawn ) if( drawn )
{ {
char buf[1024]; char buf[1024];
sprintf( buf, "%" PRIu32 ": %s", v.first, GetSrcLocFunction( srcloc ) ); sprintf( buf, "%" PRIu32 ": %s", v.first, GetString( srcloc.function ) );
draw->AddText( wpos + ImVec2( 0, offset ), 0xFF8888FF, buf ); draw->AddText( wpos + ImVec2( 0, offset ), 0xFF8888FF, buf );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( 0, offset ), wpos + ImVec2( ty + ImGui::CalcTextSize( buf ).x, offset + ty ) ) ) if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( 0, offset ), wpos + ImVec2( ty + ImGui::CalcTextSize( buf ).x, offset + ty ) ) )
{ {
@ -2902,8 +2883,8 @@ void View::DrawZoneInfoWindow()
dmul++; dmul++;
} }
auto& srcloc = GetSourceLocation( ev.srcloc ); auto& srcloc = GetSourceLocation( ev.srcloc );
ImGui::Text( "Function: %s", GetSrcLocFunction( srcloc ) ); ImGui::Text( "Function: %s", GetString( srcloc.function ) );
ImGui::Text( "Location: %s:%i", GetSrcLocFile( srcloc ), srcloc.line ); ImGui::Text( "Location: %s:%i", GetString( srcloc.file ), srcloc.line );
if( ev.text != -1 && GetTextData( ev )->userText ) if( ev.text != -1 && GetTextData( ev )->userText )
{ {
ImGui::Text( "User text: %s", GetTextData( ev )->userText ); ImGui::Text( "User text: %s", GetTextData( ev )->userText );
@ -2952,7 +2933,7 @@ void View::DrawZoneInfoWindow()
else else
{ {
auto& srcloc = GetSourceLocation( cev.srcloc ); auto& srcloc = GetSourceLocation( cev.srcloc );
ImGui::Text( "%s", GetSrcLocFunction( srcloc ) ); ImGui::Text( "%s", GetString( srcloc.function ) );
} }
if( ImGui::IsItemHovered() ) if( ImGui::IsItemHovered() )
{ {
@ -2998,7 +2979,7 @@ void View::DrawOptions()
for( auto& l : m_lockMap ) for( auto& l : m_lockMap )
{ {
char buf[1024]; char buf[1024];
sprintf( buf, "%" PRIu32 ": %s", l.first, GetSrcLocFunction( GetSourceLocation( l.second.srcloc ) ) ); sprintf( buf, "%" PRIu32 ": %s", l.first, GetString( GetSourceLocation( l.second.srcloc ).function ) );
ImGui::Checkbox( buf , &l.second.visible ); ImGui::Checkbox( buf , &l.second.visible );
} }
ImGui::Unindent( tw ); ImGui::Unindent( tw );
@ -3047,30 +3028,6 @@ void View::DrawMessages()
ImGui::End(); ImGui::End();
} }
const char* View::GetSrcLocFunction( const SourceLocation& srcloc )
{
if( srcloc.stringsAllocated )
{
return (const char*)srcloc.function;
}
else
{
return GetString( srcloc.function );
}
}
const char* View::GetSrcLocFile( const SourceLocation& srcloc )
{
if( srcloc.stringsAllocated )
{
return (const char*)srcloc.file;
}
else
{
return GetString( srcloc.file );
}
}
uint32_t View::GetZoneColor( const ZoneEvent& ev ) uint32_t View::GetZoneColor( const ZoneEvent& ev )
{ {
return GetZoneColor( GetSourceLocation( ev.srcloc ) ); return GetZoneColor( GetSourceLocation( ev.srcloc ) );
@ -3138,7 +3095,7 @@ void View::ZoneTooltip( const ZoneEvent& ev )
auto& srcloc = GetSourceLocation( ev.srcloc ); auto& srcloc = GetSourceLocation( ev.srcloc );
const auto filename = GetSrcLocFile( srcloc ); const auto filename = GetString( srcloc.file );
const auto line = srcloc.line; const auto line = srcloc.line;
const char* func; const char* func;
@ -3146,11 +3103,11 @@ void View::ZoneTooltip( const ZoneEvent& ev )
if( ev.text != -1 && GetTextData( ev )->zoneName ) if( ev.text != -1 && GetTextData( ev )->zoneName )
{ {
zoneName = GetString( GetTextData( ev )->zoneName ); zoneName = GetString( GetTextData( ev )->zoneName );
func = GetSrcLocFunction( srcloc ); func = GetString( srcloc.function );
} }
else else
{ {
func = zoneName = GetSrcLocFunction( srcloc ); func = zoneName = GetString( srcloc.function );
} }
const auto end = GetZoneEnd( ev ); const auto end = GetZoneEnd( ev );
@ -3281,18 +3238,7 @@ void View::Write( FileWrite& f )
f.Write( &sz, sizeof( sz ) ); f.Write( &sz, sizeof( sz ) );
for( auto& v : m_sourceLocationPayload ) for( auto& v : m_sourceLocationPayload )
{ {
assert( v->stringsAllocated == 1 ); f.Write( v, sizeof( *v ) );
uint32_t color = v->color;
f.Write( &color, sizeof( color ) );
f.Write( &v->line, sizeof( v->line ) );
auto func = (const char*)v->function;
sz = strlen( func );
f.Write( &sz, sizeof( sz ) );
f.Write( func, sz );
auto file = (const char*)v->file;
sz = strlen( file );
f.Write( &sz, sizeof( sz ) );
f.Write( file, sz );
} }
sz = m_lockMap.size(); sz = m_lockMap.size();

View File

@ -140,7 +140,7 @@ private:
void AddThreadString( uint64_t id, char* str, size_t sz ); void AddThreadString( uint64_t id, char* str, size_t sz );
void AddCustomString( uint64_t ptr, char* str, size_t sz ); void AddCustomString( uint64_t ptr, char* str, size_t sz );
void AddSourceLocation( const QueueSourceLocation& srcloc ); void AddSourceLocation( const QueueSourceLocation& srcloc );
void AddSourceLocationPayload( uint64_t ptr, const char* data, size_t sz ); void AddSourceLocationPayload( uint64_t ptr, char* data, size_t sz );
void AddMessageData( uint64_t ptr, char* str, size_t sz ); void AddMessageData( uint64_t ptr, char* str, size_t sz );
StringLocation StoreString( char* str, size_t sz ); StringLocation StoreString( char* str, size_t sz );
@ -191,9 +191,6 @@ private:
void HandleZoneViewMouse( int64_t timespan, const ImVec2& wpos, float w, double& pxns ); void HandleZoneViewMouse( int64_t timespan, const ImVec2& wpos, float w, double& pxns );
const char* GetSrcLocFunction( const SourceLocation& srcloc );
const char* GetSrcLocFile( const SourceLocation& srcloc );
uint32_t GetZoneColor( const ZoneEvent& ev ); uint32_t GetZoneColor( const ZoneEvent& ev );
uint32_t GetZoneColor( const SourceLocation& srcloc ); uint32_t GetZoneColor( const SourceLocation& srcloc );
uint32_t GetZoneHighlight( const ZoneEvent& ev, bool migration ); uint32_t GetZoneHighlight( const ZoneEvent& ev, bool migration );