tracy/server/TracyView.cpp

1934 lines
58 KiB
C++
Raw Normal View History

2017-09-12 23:54:22 +00:00
#ifdef _MSC_VER
# include <winsock2.h>
#else
# include <sys/time.h>
#endif
2017-09-14 19:28:40 +00:00
#include <algorithm>
2017-09-12 23:33:50 +00:00
#include <assert.h>
2017-09-22 20:11:30 +00:00
#include <inttypes.h>
2017-09-16 23:41:18 +00:00
#include <limits>
#include <stdlib.h>
#include <time.h>
2017-09-12 23:33:50 +00:00
2017-09-13 21:40:28 +00:00
#include "../common/TracyProtocol.hpp"
2017-09-12 23:33:50 +00:00
#include "../common/TracySystem.hpp"
2017-09-13 21:40:28 +00:00
#include "../common/TracyQueue.hpp"
2017-09-30 14:58:02 +00:00
#include "TracyFileRead.hpp"
2017-09-30 14:20:08 +00:00
#include "TracyFileWrite.hpp"
2017-09-17 22:18:36 +00:00
#include "TracyImGui.hpp"
2017-09-12 23:33:50 +00:00
#include "TracyView.hpp"
2017-09-30 12:56:09 +00:00
#ifdef TRACY_FILESELECTOR
# include "../nfd/nfd.h"
#endif
2017-09-12 23:33:50 +00:00
namespace tracy
{
static TextData* GetTextData( Event& zone )
{
if( !zone.text ) zone.text = new TextData {};
return zone.text;
}
2017-09-12 23:33:50 +00:00
static View* s_instance = nullptr;
View::View( const char* addr )
: m_addr( addr )
, m_shutdown( false )
2017-09-17 22:31:09 +00:00
, m_connected( false )
, m_hasData( false )
2017-09-30 14:58:02 +00:00
, m_staticView( false )
2017-09-25 21:38:49 +00:00
, m_zonesCnt( 0 )
2017-09-15 00:29:48 +00:00
, m_mbps( 64 )
, m_stream( LZ4_createStreamDecode() )
, m_buffer( new char[TargetFrameSize*3] )
, m_bufferOffset( 0 )
2017-09-18 00:37:25 +00:00
, m_frameScale( 0 )
2017-09-18 22:26:40 +00:00
, m_pause( false )
, m_frameStart( 0 )
2017-09-20 19:21:21 +00:00
, m_zvStart( 0 )
, m_zvEnd( 0 )
2017-09-29 19:49:14 +00:00
, m_zoneInfoWindow( nullptr )
2017-09-12 23:33:50 +00:00
{
assert( s_instance == nullptr );
s_instance = this;
2017-09-19 22:46:30 +00:00
ImGuiStyle& style = ImGui::GetStyle();
style.FrameRounding = 2.f;
2017-09-12 23:33:50 +00:00
m_thread = std::thread( [this] { Worker(); } );
SetThreadName( m_thread, "Tracy View" );
}
2017-09-30 14:58:02 +00:00
View::View( FileRead& f )
: m_shutdown( false )
, m_connected( false )
, m_hasData( true )
, m_staticView( true )
, m_zonesCnt( 0 )
2017-09-30 17:21:51 +00:00
, m_stream( nullptr )
, m_buffer( nullptr )
2017-09-30 14:58:02 +00:00
, m_frameScale( 0 )
, m_pause( false )
, m_frameStart( 0 )
, m_zvStart( 0 )
, m_zvEnd( 0 )
, m_zoneInfoWindow( nullptr )
{
assert( s_instance == nullptr );
s_instance = this;
f.Read( &m_delay, sizeof( m_delay ) );
f.Read( &m_resolution, sizeof( m_resolution ) );
f.Read( &m_timerMul, sizeof( m_timerMul ) );
uint64_t sz;
2017-10-03 21:29:48 +00:00
{
f.Read( &sz, sizeof( sz ) );
assert( sz < 1024 );
char tmp[1024];
f.Read( tmp, sz );
m_captureName = std::string( tmp, tmp+sz );
}
2017-09-30 14:58:02 +00:00
f.Read( &sz, sizeof( sz ) );
2017-09-30 23:54:40 +00:00
m_frames.reserve( sz );
2017-09-30 14:58:02 +00:00
for( uint64_t i=0; i<sz; i++ )
{
uint64_t v;
f.Read( &v, sizeof( v ) );
m_frames.push_back( v );
}
f.Read( &sz, sizeof( sz ) );
for( uint64_t i=0; i<sz; i++ )
{
uint64_t ptr;
f.Read( &ptr, sizeof( ptr ) );
uint64_t ssz;
f.Read( &ssz, sizeof( ssz ) );
char tmp[16*1024];
f.Read( tmp, ssz );
m_strings.emplace( ptr, std::string( tmp, tmp+ssz ) );
}
f.Read( &sz, sizeof( sz ) );
for( uint64_t i=0; i<sz; i++ )
{
uint64_t ptr;
f.Read( &ptr, sizeof( ptr ) );
uint64_t ssz;
f.Read( &ssz, sizeof( ssz ) );
char tmp[16*1024];
f.Read( tmp, ssz );
m_threadNames.emplace( ptr, std::string( tmp, tmp+ssz ) );
}
std::unordered_map<uint64_t, const char*> stringMap;
f.Read( &sz, sizeof( sz ) );
for( uint64_t i=0; i<sz; i++ )
{
uint64_t ptr;
f.Read( &ptr, sizeof( ptr ) );
uint64_t ssz;
f.Read( &ssz, sizeof( ssz ) );
auto dst = new char[ssz+1];
f.Read( dst, ssz );
dst[ssz] = '\0';
m_customStrings.emplace( dst );
stringMap.emplace( ptr, dst );
}
f.Read( &sz, sizeof( sz ) );
for( uint64_t i=0; i<sz; i++ )
{
uint64_t ptr;
f.Read( &ptr, sizeof( ptr ) );
QueueSourceLocation srcloc;
f.Read( &srcloc, sizeof( srcloc ) );
m_sourceLocation.emplace( ptr, srcloc );
}
2017-10-04 18:10:38 +00:00
f.Read( &sz, sizeof( sz ) );
for( uint64_t i=0; i<sz; i++ )
{
LockMap lockmap;
uint64_t id, tsz;
f.Read( &id, sizeof( id ) );
2017-10-04 19:36:47 +00:00
f.Read( &lockmap.srcloc, sizeof( lockmap.srcloc ) );
2017-10-04 18:10:38 +00:00
f.Read( &tsz, sizeof( tsz ) );
for( uint64_t i=0; i<tsz; i++ )
{
uint64_t t;
f.Read( &t, sizeof( t ) );
lockmap.threads.emplace( t );
}
f.Read( &tsz, sizeof( tsz ) );
for( uint64_t i=0; i<tsz; i++ )
{
auto lev = m_slab.Alloc<LockEvent>();
f.Read( lev, sizeof( LockEvent ) );
lockmap.timeline.push_back( lev );
}
m_lockMap.emplace( id, std::move( lockmap ) );
}
2017-09-30 14:58:02 +00:00
f.Read( &sz, sizeof( sz ) );
2017-09-30 23:54:40 +00:00
m_threads.reserve( sz );
2017-09-30 14:58:02 +00:00
for( uint64_t i=0; i<sz; i++ )
{
auto td = new ThreadData;
f.Read( &td->id, sizeof( td->id ) );
ReadTimeline( f, td->timeline, nullptr, stringMap );
m_threads.push_back( td );
}
}
2017-09-12 23:33:50 +00:00
View::~View()
{
m_shutdown.store( true, std::memory_order_relaxed );
2017-09-30 14:58:02 +00:00
if( !m_staticView )
{
m_thread.join();
}
delete[] m_buffer;
LZ4_freeStreamDecode( m_stream );
assert( s_instance != nullptr );
s_instance = nullptr;
2017-09-12 23:33:50 +00:00
}
bool View::ShouldExit()
{
return s_instance->m_shutdown.load( std::memory_order_relaxed );
}
2017-09-12 23:33:50 +00:00
void View::Worker()
{
2017-09-12 23:54:22 +00:00
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000;
2017-09-12 23:33:50 +00:00
for(;;)
{
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
if( !m_sock.Connect( m_addr.c_str(), "8086" ) ) continue;
2017-09-12 23:54:22 +00:00
2017-09-15 19:09:19 +00:00
std::chrono::time_point<std::chrono::high_resolution_clock> t0;
2017-09-12 23:54:22 +00:00
uint8_t lz4;
2017-09-15 20:45:03 +00:00
uint64_t bytes = 0;
{
WelcomeMessage welcome;
if( !m_sock.Read( &welcome, sizeof( welcome ), &tv, ShouldExit ) ) goto close;
lz4 = welcome.lz4;
m_timerMul = welcome.timerMul;
m_frames.push_back( welcome.timeBegin * m_timerMul );
m_delay = welcome.delay * m_timerMul;
2017-09-29 16:32:07 +00:00
m_resolution = welcome.resolution * m_timerMul;
char dtmp[64];
time_t date = welcome.epoch;
auto lt = localtime( &date );
strftime( dtmp, 64, "%F %T", lt );
char tmp[1024];
sprintf( tmp, "%s @ %s###Profiler", welcome.programName, dtmp );
m_captureName = tmp;
}
2017-09-12 23:54:22 +00:00
m_hasData.store( true, std::memory_order_release );
2017-09-15 22:40:51 +00:00
LZ4_setStreamDecode( m_stream, nullptr, 0 );
2017-09-17 22:31:09 +00:00
m_connected.store( true, std::memory_order_relaxed );
2017-09-15 19:09:19 +00:00
t0 = std::chrono::high_resolution_clock::now();
2017-09-15 00:29:48 +00:00
2017-09-12 23:54:22 +00:00
for(;;)
{
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
2017-09-13 21:40:28 +00:00
if( lz4 )
{
auto buf = m_buffer + m_bufferOffset;
2017-09-13 21:40:28 +00:00
char lz4buf[LZ4Size];
lz4sz_t lz4sz;
if( !m_sock.Read( &lz4sz, sizeof( lz4sz ), &tv, ShouldExit ) ) goto close;
if( !m_sock.Read( lz4buf, lz4sz, &tv, ShouldExit ) ) goto close;
2017-09-15 00:29:48 +00:00
bytes += sizeof( lz4sz ) + lz4sz;
2017-09-13 21:40:28 +00:00
auto sz = LZ4_decompress_safe_continue( m_stream, lz4buf, buf, lz4sz, TargetFrameSize );
2017-09-13 21:40:28 +00:00
assert( sz >= 0 );
const char* ptr = buf;
const char* end = buf + sz;
while( ptr < end )
{
auto ev = (QueueItem*)ptr;
2017-09-14 17:44:49 +00:00
DispatchProcess( *ev, ptr );
2017-09-13 21:40:28 +00:00
}
m_bufferOffset += sz;
if( m_bufferOffset > TargetFrameSize * 2 ) m_bufferOffset = 0;
2017-09-13 21:40:28 +00:00
}
else
{
2017-09-13 23:06:40 +00:00
QueueItem ev;
if( !m_sock.Read( &ev.hdr, sizeof( QueueHeader ), &tv, ShouldExit ) ) goto close;
2017-09-14 17:44:49 +00:00
const auto payload = QueueDataSize[ev.hdr.idx] - sizeof( QueueHeader );
if( payload > 0 )
{
if( !m_sock.Read( ((char*)&ev) + sizeof( QueueHeader ), payload, &tv, ShouldExit ) ) goto close;
}
2017-09-15 00:29:48 +00:00
bytes += sizeof( QueueHeader ) + payload; // ignores string transfer
2017-09-14 17:44:49 +00:00
DispatchProcess( ev );
2017-09-13 21:40:28 +00:00
}
2017-09-15 00:29:48 +00:00
auto t1 = std::chrono::high_resolution_clock::now();
auto td = std::chrono::duration_cast<std::chrono::milliseconds>( t1 - t0 ).count();
enum { MbpsUpdateTime = 200 };
if( td > MbpsUpdateTime )
{
std::lock_guard<std::mutex> lock( m_mbpslock );
2017-09-15 00:29:48 +00:00
m_mbps.erase( m_mbps.begin() );
2017-09-15 23:47:04 +00:00
m_mbps.emplace_back( bytes / ( td * 125.f ) );
2017-09-15 00:29:48 +00:00
t0 = t1;
bytes = 0;
}
2017-09-12 23:54:22 +00:00
}
2017-09-13 00:00:22 +00:00
close:
m_sock.Close();
2017-09-17 22:31:09 +00:00
m_connected.store( false, std::memory_order_relaxed );
2017-09-12 23:33:50 +00:00
}
}
2017-09-14 17:44:49 +00:00
void View::DispatchProcess( const QueueItem& ev )
{
2017-09-27 00:18:17 +00:00
if( ev.hdr.type == QueueType::CustomStringData || ev.hdr.type == QueueType::StringData || ev.hdr.type == QueueType::ThreadName )
2017-09-14 17:44:49 +00:00
{
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000;
char buf[TargetFrameSize];
uint16_t sz;
m_sock.Read( &sz, sizeof( sz ), &tv, ShouldExit );
m_sock.Read( buf, sz, &tv, ShouldExit );
2017-09-27 00:18:17 +00:00
if( ev.hdr.type == QueueType::CustomStringData )
{
2017-10-03 14:41:32 +00:00
AddCustomString( ev.stringTransfer.ptr, std::string( buf, buf+sz ) );
2017-09-27 00:18:17 +00:00
}
else if( ev.hdr.type == QueueType::StringData )
2017-09-21 23:55:02 +00:00
{
2017-10-03 14:41:32 +00:00
AddString( ev.stringTransfer.ptr, std::string( buf, buf+sz ) );
2017-09-21 23:55:02 +00:00
}
else
{
2017-10-03 14:41:32 +00:00
AddThreadString( ev.stringTransfer.ptr, std::string( buf, buf+sz ) );
2017-09-21 23:55:02 +00:00
}
2017-09-14 17:44:49 +00:00
}
else
{
Process( ev );
}
}
void View::DispatchProcess( const QueueItem& ev, const char*& ptr )
{
ptr += QueueDataSize[ev.hdr.idx];
2017-09-27 00:18:17 +00:00
if( ev.hdr.type == QueueType::CustomStringData || ev.hdr.type == QueueType::StringData || ev.hdr.type == QueueType::ThreadName )
2017-09-14 17:44:49 +00:00
{
uint16_t sz;
memcpy( &sz, ptr, sizeof( sz ) );
ptr += sizeof( sz );
2017-09-27 00:18:17 +00:00
if( ev.hdr.type == QueueType::CustomStringData )
{
2017-10-03 14:41:32 +00:00
AddCustomString( ev.stringTransfer.ptr, std::string( ptr, ptr+sz ) );
2017-09-27 00:18:17 +00:00
}
else if( ev.hdr.type == QueueType::StringData )
2017-09-21 23:55:02 +00:00
{
2017-10-03 14:41:32 +00:00
AddString( ev.stringTransfer.ptr, std::string( ptr, ptr+sz ) );
2017-09-21 23:55:02 +00:00
}
else
{
2017-10-03 14:41:32 +00:00
AddThreadString( ev.stringTransfer.ptr, std::string( ptr, ptr+sz ) );
2017-09-21 23:55:02 +00:00
}
2017-09-14 17:44:49 +00:00
ptr += sz;
}
else
{
Process( ev );
}
}
void View::ServerQuery( uint8_t type, uint64_t data )
{
enum { DataSize = sizeof( type ) + sizeof( data ) };
char tmp[DataSize];
memcpy( tmp, &type, sizeof( type ) );
memcpy( tmp + sizeof( type ), &data, sizeof( data ) );
m_sock.Send( tmp, DataSize );
}
2017-09-13 21:40:28 +00:00
void View::Process( const QueueItem& ev )
{
2017-09-14 00:00:13 +00:00
switch( ev.hdr.type )
{
case QueueType::ZoneBegin:
2017-10-03 14:41:32 +00:00
ProcessZoneBegin( ev.zoneBegin );
2017-09-14 00:00:13 +00:00
break;
case QueueType::ZoneEnd:
2017-10-03 14:41:32 +00:00
ProcessZoneEnd( ev.zoneEnd );
2017-09-14 00:00:13 +00:00
break;
case QueueType::FrameMarkMsg:
2017-10-03 14:41:32 +00:00
ProcessFrameMark( ev.frameMark );
2017-09-15 22:30:27 +00:00
break;
case QueueType::SourceLocation:
2017-10-03 14:41:32 +00:00
AddSourceLocation( ev.srcloc );
break;
2017-09-27 00:18:17 +00:00
case QueueType::ZoneText:
2017-10-03 14:41:32 +00:00
ProcessZoneText( ev.zoneText );
2017-09-27 00:18:17 +00:00
break;
2017-09-28 17:28:24 +00:00
case QueueType::ZoneName:
2017-10-03 14:41:32 +00:00
ProcessZoneName( ev.zoneName );
2017-09-28 17:28:24 +00:00
break;
2017-10-04 14:16:40 +00:00
case QueueType::LockAnnounce:
ProcessLockAnnounce( ev.lockAnnounce );
break;
2017-10-04 14:45:46 +00:00
case QueueType::LockWait:
ProcessLockWait( ev.lockWait );
2017-10-04 14:45:46 +00:00
break;
case QueueType::LockObtain:
ProcessLockObtain( ev.lockObtain );
2017-10-04 14:45:46 +00:00
break;
case QueueType::LockRelease:
ProcessLockRelease( ev.lockRelease );
2017-10-04 14:45:46 +00:00
break;
2017-09-14 00:00:13 +00:00
default:
assert( false );
break;
}
}
2017-10-03 14:41:32 +00:00
void View::ProcessZoneBegin( const QueueZoneBegin& ev )
2017-09-14 00:00:13 +00:00
{
auto zone = m_slab.Alloc<Event>();
2017-09-21 19:57:40 +00:00
CheckSourceLocation( ev.srcloc );
2017-09-21 23:30:57 +00:00
CheckThreadString( ev.thread );
zone->start = ev.time * m_timerMul;
zone->end = -1;
zone->srcloc = ev.srcloc;
2017-10-01 17:16:44 +00:00
zone->cpu_start = ev.cpu;
2017-09-27 00:18:17 +00:00
zone->text = nullptr;
2017-09-21 19:57:40 +00:00
2017-09-14 00:00:13 +00:00
std::unique_lock<std::mutex> lock( m_lock );
NewZone( zone, ev.thread );
lock.unlock();
2017-10-03 14:41:32 +00:00
m_zoneStack[ev.thread].push_back( zone );
2017-09-14 00:00:13 +00:00
}
2017-10-03 14:41:32 +00:00
void View::ProcessZoneEnd( const QueueZoneEnd& ev )
2017-09-14 00:00:13 +00:00
{
2017-10-03 14:41:32 +00:00
auto& stack = m_zoneStack[ev.thread];
assert( !stack.empty() );
auto zone = stack.back();
stack.pop_back();
assert( zone->end == -1 );
2017-10-03 14:41:32 +00:00
std::unique_lock<std::mutex> lock( m_lock );
zone->end = ev.time * m_timerMul;
zone->cpu_end = ev.cpu;
2017-10-03 14:41:32 +00:00
lock.unlock();
assert( zone->end >= zone->start );
UpdateZone( zone );
2017-09-13 21:40:28 +00:00
}
2017-10-03 14:41:32 +00:00
void View::ProcessFrameMark( const QueueFrameMark& ev )
2017-09-15 22:40:51 +00:00
{
assert( !m_frames.empty() );
const auto lastframe = m_frames.back();
2017-10-03 14:41:32 +00:00
const auto time = ev.time * m_timerMul;
assert( lastframe < time );
2017-10-04 14:07:20 +00:00
std::lock_guard<std::mutex> lock( m_lock );
m_frames.push_back( time );
2017-09-15 22:40:51 +00:00
}
2017-10-03 14:41:32 +00:00
void View::ProcessZoneText( const QueueZoneText& ev )
2017-09-27 00:18:17 +00:00
{
2017-10-03 14:41:32 +00:00
auto& stack = m_zoneStack[ev.thread];
assert( !stack.empty() );
auto zone = stack.back();
CheckCustomString( ev.text, zone );
2017-09-27 00:18:17 +00:00
}
2017-10-03 14:41:32 +00:00
void View::ProcessZoneName( const QueueZoneName& ev )
2017-09-28 17:28:24 +00:00
{
2017-10-03 14:41:32 +00:00
auto& stack = m_zoneStack[ev.thread];
assert( !stack.empty() );
auto zone = stack.back();
2017-09-28 17:28:24 +00:00
CheckString( ev.name );
2017-10-04 14:07:20 +00:00
std::lock_guard<std::mutex> lock( m_lock );
2017-10-03 14:41:32 +00:00
GetTextData( *zone )->zoneName = ev.name;
2017-09-28 17:28:24 +00:00
}
2017-10-04 14:16:40 +00:00
void View::ProcessLockAnnounce( const QueueLockAnnounce& ev )
{
CheckSourceLocation( ev.srcloc );
2017-10-04 16:17:31 +00:00
assert( m_lockMap.find( ev.id ) == m_lockMap.end() );
2017-10-04 16:51:50 +00:00
auto it = m_pendingLocks.find( ev.id );
std::unique_lock<std::mutex> lock( m_lock );
2017-10-04 18:10:14 +00:00
auto& lockmap = m_lockMap.emplace( ev.id, LockMap { ev.srcloc } ).first->second;
2017-10-04 16:51:50 +00:00
if( it != m_pendingLocks.end() )
{
for( auto& v : it->second )
{
InsertLockEvent( lockmap, v );
2017-10-04 16:51:50 +00:00
}
lock.unlock();
m_pendingLocks.erase( it );
}
2017-10-04 14:16:40 +00:00
}
void View::ProcessLockWait( const QueueLockWait& ev )
{
2017-10-04 16:51:50 +00:00
auto lev = m_slab.Alloc<LockEvent>();
2017-10-04 17:24:06 +00:00
lev->time = ev.time * m_timerMul;
2017-10-04 16:51:50 +00:00
lev->thread = ev.thread;
lev->type = LockEvent::Type::Wait;
auto it = m_lockMap.find( ev.id );
if( it == m_lockMap.end() )
{
auto& v = m_pendingLocks[ev.id];
2017-10-04 16:51:50 +00:00
v.push_back( lev );
}
else
{
std::lock_guard<std::mutex> lock( m_lock );
InsertLockEvent( it->second, lev );
}
}
void View::ProcessLockObtain( const QueueLockObtain& ev )
{
2017-10-04 16:51:50 +00:00
auto lev = m_slab.Alloc<LockEvent>();
2017-10-04 17:24:06 +00:00
lev->time = ev.time * m_timerMul;
2017-10-04 16:51:50 +00:00
lev->thread = ev.thread;
lev->type = LockEvent::Type::Obtain;
auto it = m_lockMap.find( ev.id );
if( it == m_lockMap.end() )
{
auto& v = m_pendingLocks[ev.id];
2017-10-04 16:51:50 +00:00
v.push_back( lev );
}
else
{
std::lock_guard<std::mutex> lock( m_lock );
InsertLockEvent( it->second, lev );
}
}
void View::ProcessLockRelease( const QueueLockRelease& ev )
{
2017-10-04 16:51:50 +00:00
auto lev = m_slab.Alloc<LockEvent>();
2017-10-04 17:24:06 +00:00
lev->time = ev.time * m_timerMul;
2017-10-04 16:51:50 +00:00
lev->thread = ev.thread;
lev->type = LockEvent::Type::Release;
auto it = m_lockMap.find( ev.id );
if( it == m_lockMap.end() )
{
auto& v = m_pendingLocks[ev.id];
2017-10-04 16:51:50 +00:00
v.push_back( lev );
}
else
{
std::lock_guard<std::mutex> lock( m_lock );
InsertLockEvent( it->second, lev );
}
}
2017-09-14 00:16:51 +00:00
void View::CheckString( uint64_t ptr )
{
if( m_strings.find( ptr ) != m_strings.end() ) return;
if( m_pendingStrings.find( ptr ) != m_pendingStrings.end() ) return;
m_pendingStrings.emplace( ptr );
ServerQuery( ServerQueryString, ptr );
2017-09-14 00:16:51 +00:00
}
2017-09-21 23:30:57 +00:00
void View::CheckThreadString( uint64_t id )
{
if( m_threadNames.find( id ) != m_threadNames.end() ) return;
if( m_pendingThreads.find( id ) != m_pendingThreads.end() ) return;
m_pendingThreads.emplace( id );
2017-09-21 23:55:02 +00:00
ServerQuery( ServerQueryThreadString, id );
2017-09-21 23:30:57 +00:00
}
2017-09-27 00:18:17 +00:00
void View::CheckCustomString( uint64_t ptr, Event* dst )
{
assert( m_pendingCustomStrings.find( ptr ) == m_pendingCustomStrings.end() );
m_pendingCustomStrings.emplace( ptr, dst );
ServerQuery( ServerQueryCustomString, ptr );
2017-09-27 00:18:17 +00:00
}
void View::CheckSourceLocation( uint64_t ptr )
{
if( m_sourceLocation.find( ptr ) != m_sourceLocation.end() ) return;
if( m_pendingSourceLocation.find( ptr ) != m_pendingSourceLocation.end() ) return;
m_pendingSourceLocation.emplace( ptr );
ServerQuery( ServerQuerySourceLocation, ptr );
}
2017-09-14 17:43:40 +00:00
void View::AddString( uint64_t ptr, std::string&& str )
{
2017-09-21 23:30:43 +00:00
assert( m_strings.find( ptr ) == m_strings.end() );
2017-09-14 17:43:40 +00:00
auto it = m_pendingStrings.find( ptr );
assert( it != m_pendingStrings.end() );
m_pendingStrings.erase( it );
std::lock_guard<std::mutex> lock( m_lock );
m_strings.emplace( ptr, std::move( str ) );
}
2017-09-21 23:30:57 +00:00
void View::AddThreadString( uint64_t id, std::string&& str )
{
assert( m_threadNames.find( id ) == m_threadNames.end() );
auto it = m_pendingThreads.find( id );
assert( it != m_pendingThreads.end() );
m_pendingThreads.erase( it );
std::lock_guard<std::mutex> lock( m_lock );
m_threadNames.emplace( id, std::move( str ) );
}
2017-09-27 00:18:17 +00:00
void View::AddCustomString( uint64_t ptr, std::string&& str )
{
auto pit = m_pendingCustomStrings.find( ptr );
assert( pit != m_pendingCustomStrings.end() );
2017-10-04 14:07:20 +00:00
std::unique_lock<std::mutex> lock( m_lock );
auto sit = m_customStrings.find( str.c_str() );
2017-09-27 00:18:17 +00:00
if( sit == m_customStrings.end() )
{
const auto sz = str.size();
auto ptr = new char[sz+1];
memcpy( ptr, str.c_str(), sz );
ptr[sz] = '\0';
GetTextData( *pit->second )->userText = ptr;
m_customStrings.emplace( ptr );
2017-09-27 00:18:17 +00:00
}
else
{
GetTextData( *pit->second )->userText = *sit;
2017-09-27 00:18:17 +00:00
}
2017-10-04 14:07:20 +00:00
lock.unlock();
2017-09-27 00:18:17 +00:00
m_pendingCustomStrings.erase( pit );
}
2017-10-03 14:41:32 +00:00
void View::AddSourceLocation( const QueueSourceLocation& srcloc )
{
2017-10-03 14:41:32 +00:00
assert( m_sourceLocation.find( srcloc.ptr ) == m_sourceLocation.end() );
auto it = m_pendingSourceLocation.find( srcloc.ptr );
assert( it != m_pendingSourceLocation.end() );
m_pendingSourceLocation.erase( it );
CheckString( srcloc.file );
CheckString( srcloc.function );
std::lock_guard<std::mutex> lock( m_lock );
2017-10-03 14:41:32 +00:00
m_sourceLocation.emplace( srcloc.ptr, srcloc );
}
2017-09-21 23:30:57 +00:00
2017-09-21 23:15:58 +00:00
void View::NewZone( Event* zone, uint64_t thread )
2017-09-14 19:05:01 +00:00
{
2017-09-25 21:38:49 +00:00
m_zonesCnt++;
2017-09-21 23:15:58 +00:00
Vector<Event*>* timeline;
auto it = m_threadMap.find( thread );
if( it == m_threadMap.end() )
2017-09-14 19:28:40 +00:00
{
2017-09-25 23:45:59 +00:00
m_threadMap.emplace( thread, (uint32_t)m_threads.size() );
m_threads.push_back( new ThreadData { thread } );
timeline = &m_threads.back()->timeline;
2017-09-21 23:15:58 +00:00
}
else
{
timeline = &m_threads[it->second]->timeline;
2017-09-21 23:15:58 +00:00
}
InsertZone( zone, nullptr, *timeline );
}
void View::UpdateZone( Event* zone )
{
assert( zone->end != -1 );
2017-10-03 14:26:58 +00:00
assert( std::upper_bound( zone->child.begin(), zone->child.end(), zone->end, [] ( const auto& l, const auto& r ) { return l < r->start; } ) == zone->child.end() );
}
void View::InsertZone( Event* zone, Event* parent, Vector<Event*>& vec )
{
if( !vec.empty() )
2017-09-21 23:15:58 +00:00
{
const auto lastend = vec.back()->end;
if( lastend != -1 && lastend <= zone->start )
2017-09-14 19:28:40 +00:00
{
zone->parent = parent;
vec.push_back( zone );
2017-09-14 19:28:40 +00:00
}
else
{
assert( std::upper_bound( vec.begin(), vec.end(), zone->start, [] ( const auto& l, const auto& r ) { return l < r->start; } ) == vec.end() );
assert( vec.back()->end == -1 || vec.back()->end >= zone->end );
InsertZone( zone, vec.back(), vec.back()->child );
2017-09-14 19:28:40 +00:00
}
}
else
{
zone->parent = parent;
vec.push_back( zone );
2017-09-14 19:28:40 +00:00
}
2017-09-14 19:05:01 +00:00
}
void View::InsertLockEvent( LockMap& lockmap, LockEvent* lev )
2017-10-04 16:51:50 +00:00
{
lockmap.threads.insert( lev->thread );
auto& timeline = lockmap.timeline;
2017-10-04 17:24:24 +00:00
if( timeline.empty() || timeline.back()->time < lev->time )
{
timeline.push_back( lev );
2017-10-04 17:42:44 +00:00
UpdateLockCount( timeline, timeline.size() - 1 );
2017-10-04 17:24:24 +00:00
}
else
{
auto it = std::lower_bound( timeline.begin(), timeline.end(), lev->time, [] ( const auto& lhs, const auto& rhs ) { return lhs->time < rhs; } );
2017-10-04 17:42:44 +00:00
it = timeline.insert( it, lev );
UpdateLockCount( timeline, std::distance( timeline.begin(), it ) );
}
}
void View::UpdateLockCount( Vector<LockEvent*>& timeline, size_t pos )
{
uint8_t count = pos == 0 ? 0 : timeline[pos-1]->lockCount;
const auto end = timeline.size();
while( pos != end )
{
switch( timeline[pos]->type )
{
case LockEvent::Type::Obtain:
assert( count < std::numeric_limits<uint8_t>::max() );
count++;
break;
case LockEvent::Type::Release:
assert( count > 0 );
count--;
break;
default:
break;
}
timeline[pos]->lockCount = count;
pos++;
2017-10-04 17:24:24 +00:00
}
2017-10-04 16:51:50 +00:00
}
2017-09-18 19:26:22 +00:00
uint64_t View::GetFrameTime( size_t idx ) const
{
if( idx < m_frames.size() - 1 )
{
return m_frames[idx+1] - m_frames[idx];
}
else
{
const auto last = GetLastTime();
return last == 0 ? 0 : last - m_frames.back();
}
}
2017-09-20 20:34:56 +00:00
uint64_t View::GetFrameBegin( size_t idx ) const
{
assert( idx < m_frames.size() );
return m_frames[idx];
}
uint64_t View::GetFrameEnd( size_t idx ) const
{
if( idx < m_frames.size() - 1 )
{
return m_frames[idx+1];
}
else
{
return GetLastTime();
}
}
2017-09-18 19:26:22 +00:00
uint64_t View::GetLastTime() const
{
uint64_t last = 0;
if( !m_frames.empty() ) last = m_frames.back();
2017-09-21 23:15:58 +00:00
for( auto& v : m_threads )
{
if( !v->timeline.empty() )
2017-09-21 23:15:58 +00:00
{
auto ev = v->timeline.back();
2017-09-23 12:43:05 +00:00
if( ev->end == -1 )
{
if( ev->start > last ) last = ev->start;
}
else if( ev->end > last )
{
last = ev->end;
}
2017-09-21 23:15:58 +00:00
}
}
return last;
}
2017-09-24 14:40:16 +00:00
int64_t View::GetZoneEnd( const Event& ev ) const
{
2017-09-23 13:41:10 +00:00
auto ptr = &ev;
for(;;)
{
if( ptr->end != -1 ) return ptr->end;
if( ptr->child.empty() ) return ptr->start;
ptr = ptr->child.back();
}
}
2017-09-24 01:23:01 +00:00
Vector<Event*>& View::GetParentVector( const Event& ev )
{
if( ev.parent )
{
return ev.parent->child;
}
else
{
for( auto& t : m_threads )
{
auto it = std::lower_bound( t->timeline.begin(), t->timeline.end(), ev.start, [] ( const auto& l, const auto& r ) { return l->start < r; } );
if( it != t->timeline.end() && *it == &ev ) return t->timeline;
2017-09-24 01:23:01 +00:00
}
assert( false );
static Vector<Event*> empty;
return empty;
}
}
2017-09-24 14:45:08 +00:00
const char* View::TimeToString( int64_t ns ) const
2017-09-18 20:11:38 +00:00
{
2017-09-29 16:44:42 +00:00
enum { Pool = 8 };
2017-09-20 19:25:00 +00:00
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
2017-09-24 14:45:08 +00:00
const char* sign = "";
if( ns < 0 )
{
sign = "-";
ns = -ns;
}
2017-09-18 20:11:38 +00:00
if( ns < 1000 )
{
2017-09-24 14:45:08 +00:00
sprintf( buf, "%s%" PRIu64 " ns", sign, ns );
2017-09-18 20:11:38 +00:00
}
else if( ns < 1000ull * 1000 )
2017-09-18 20:11:38 +00:00
{
2017-09-24 14:45:08 +00:00
sprintf( buf, "%s%.2f us", sign, ns / 1000. );
2017-09-18 20:11:38 +00:00
}
else if( ns < 1000ull * 1000 * 1000 )
2017-09-18 20:11:38 +00:00
{
2017-09-24 14:45:08 +00:00
sprintf( buf, "%s%.2f ms", sign, ns / ( 1000. * 1000. ) );
}
else if( ns < 1000ull * 1000 * 1000 * 60 )
{
2017-09-24 14:45:08 +00:00
sprintf( buf, "%s%.2f s", sign, ns / ( 1000. * 1000. * 1000. ) );
2017-09-18 20:11:38 +00:00
}
else
{
const auto m = ns / ( 1000ull * 1000 * 1000 * 60 );
const auto s = ns - m * ( 1000ull * 1000 * 1000 * 60 );
2017-09-24 14:45:08 +00:00
sprintf( buf, "%s%" PRIu64 ":%04.1f", sign, m, s / ( 1000. * 1000. * 1000. ) );
2017-09-18 20:11:38 +00:00
}
return buf;
}
2017-09-21 00:10:20 +00:00
const char* View::GetString( uint64_t ptr ) const
{
const auto it = m_strings.find( ptr );
if( it == m_strings.end() )
{
return "???";
}
else
{
return it->second.c_str();
}
}
2017-09-21 23:30:57 +00:00
const char* View::GetThreadString( uint64_t id ) const
{
const auto it = m_threadNames.find( id );
if( it == m_threadNames.end() )
{
return "???";
}
else
{
return it->second.c_str();
}
}
const QueueSourceLocation& View::GetSourceLocation( uint64_t srcloc ) const
{
static const QueueSourceLocation empty = {};
const auto it = m_sourceLocation.find( srcloc );
if( it == m_sourceLocation.end() ) return empty;
return it->second;
}
2017-09-15 00:30:22 +00:00
void View::Draw()
{
s_instance->DrawImpl();
}
void View::DrawImpl()
{
if( !m_hasData.load( std::memory_order_acquire ) )
{
ImGui::Begin( m_addr.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ShowBorders );
ImGui::Text( "Waiting for connection..." );
ImGui::End();
return;
}
2017-09-30 14:58:02 +00:00
if( !m_staticView )
{
DrawConnection();
}
2017-09-30 12:37:21 +00:00
std::lock_guard<std::mutex> lock( m_lock );
ImGui::Begin( m_captureName.c_str(), nullptr, ImGuiWindowFlags_ShowBorders );
2017-09-30 12:37:21 +00:00
if( ImGui::Button( m_pause ? "Resume" : "Pause", ImVec2( 80, 0 ) ) ) m_pause = !m_pause;
ImGui::SameLine();
ImGui::Text( "Frames: %-7" PRIu64 " Time span: %-10s View span: %-10s Zones: %-10" PRIu64" Queue delay: %s Timer resolution: %s", m_frames.size(), TimeToString( GetLastTime() - m_frames[0] ), TimeToString( m_zvEnd - m_zvStart ), m_zonesCnt, TimeToString( m_delay ), TimeToString( m_resolution ) );
DrawFrames();
DrawZones();
ImGui::End();
m_zoneHighlight = nullptr;
DrawZoneInfoWindow();
if( m_zvStartNext != 0 )
{
m_zvStart = m_zvStartNext;
m_zvEnd = m_zvEndNext;
m_pause = true;
}
}
void View::DrawConnection()
{
2017-09-16 23:41:18 +00:00
ImGui::Begin( m_addr.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ShowBorders );
2017-09-15 00:30:22 +00:00
{
std::lock_guard<std::mutex> lock( m_mbpslock );
2017-09-15 00:30:22 +00:00
const auto mbps = m_mbps.back();
char buf[64];
if( mbps < 0.1f )
{
2017-09-16 23:41:18 +00:00
sprintf( buf, "%6.2f Kbps", mbps * 1000.f );
2017-09-15 00:30:22 +00:00
}
else
{
2017-09-16 23:41:18 +00:00
sprintf( buf, "%6.2f Mbps", mbps );
2017-09-15 00:30:22 +00:00
}
2017-09-17 22:31:38 +00:00
ImGui::Dummy( ImVec2( 10, 0 ) );
ImGui::SameLine();
ImGui::PlotLines( buf, m_mbps.data(), m_mbps.size(), 0, nullptr, 0, std::numeric_limits<float>::max(), ImVec2( 150, 0 ) );
2017-09-15 00:30:22 +00:00
}
2017-09-16 23:22:46 +00:00
ImGui::Text( "Memory usage: %.2f MB", memUsage.load( std::memory_order_relaxed ) / ( 1024.f * 1024.f ) );
2017-09-17 22:31:38 +00:00
const auto wpos = ImGui::GetWindowPos() + ImGui::GetWindowContentRegionMin();
ImGui::GetWindowDrawList()->AddCircleFilled( wpos + ImVec2( 6, 9 ), 5.f, m_connected.load( std::memory_order_relaxed ) ? 0xFF2222CC : 0xFF444444, 10 );
std::lock_guard<std::mutex> lock( m_lock );
2017-09-15 22:57:50 +00:00
{
const auto sz = m_frames.size();
if( sz > 1 )
{
const auto dt = m_frames[sz-1] - m_frames[sz-2];
const auto dtm = dt / 1000000.f;
const auto fps = 1000.f / dtm;
2017-09-16 23:41:18 +00:00
ImGui::Text( "FPS: %6.1f Frame time: %.2f ms", fps, dtm );
2017-09-15 22:57:50 +00:00
}
}
2017-09-18 00:37:25 +00:00
2017-09-30 12:56:09 +00:00
if( ImGui::Button( "Save trace" ) )
{
#ifdef TRACY_FILESELECTOR
nfdchar_t* fn;
auto res = NFD_SaveDialog( "tracy", nullptr, &fn );
if( res == NFD_OKAY )
#else
const char* fn = "trace.tracy";
#endif
{
2017-09-30 14:20:08 +00:00
auto f = std::unique_ptr<FileWrite>( FileWrite::Open( fn ) );
if( f )
{
Write( *f );
}
2017-09-30 12:56:09 +00:00
}
}
2017-09-18 00:37:25 +00:00
ImGui::End();
}
static ImU32 GetFrameColor( uint64_t frameTime )
{
enum { BestTime = 1000 * 1000 * 1000 / 143 };
enum { GoodTime = 1000 * 1000 * 1000 / 59 };
enum { BadTime = 1000 * 1000 * 1000 / 29 };
2017-09-18 17:03:29 +00:00
return frameTime > BadTime ? 0xFF2222DD :
frameTime > GoodTime ? 0xFF22DDDD :
frameTime > BestTime ? 0xFF22DD22 : 0xFFDD9900;
2017-09-18 00:37:25 +00:00
}
static int GetFrameWidth( int frameScale )
{
return frameScale == 0 ? 4 : ( frameScale == -1 ? 6 : 1 );
}
static int GetFrameGroup( int frameScale )
{
return frameScale < 2 ? 1 : ( 1 << ( frameScale - 1 ) );
}
2017-09-18 00:37:25 +00:00
void View::DrawFrames()
{
2017-09-20 19:21:21 +00:00
assert( !m_frames.empty() );
2017-09-18 00:37:25 +00:00
enum { Height = 40 };
enum { MaxFrameTime = 50 * 1000 * 1000 }; // 50ms
ImGuiWindow* window = ImGui::GetCurrentWindow();
if( window->SkipItems ) return;
auto& io = ImGui::GetIO();
const auto wpos = ImGui::GetCursorScreenPos();
2017-09-18 00:37:25 +00:00
const auto wspace = ImGui::GetWindowContentRegionMax() - ImGui::GetWindowContentRegionMin();
const auto w = wspace.x;
auto draw = ImGui::GetWindowDrawList();
ImGui::InvisibleButton( "##frames", ImVec2( w, Height ) );
bool hover = ImGui::IsItemHovered();
2017-09-18 00:37:25 +00:00
draw->AddRectFilled( wpos, wpos + ImVec2( w, Height ), 0x33FFFFFF );
2017-09-19 22:08:15 +00:00
const auto wheel = io.MouseWheel;
const auto prevScale = m_frameScale;
2017-09-18 20:12:39 +00:00
if( hover )
2017-09-18 00:37:25 +00:00
{
if( wheel > 0 )
{
2017-09-19 19:10:11 +00:00
if( m_frameScale > -1 ) m_frameScale--;
2017-09-18 00:37:25 +00:00
}
else if( wheel < 0 )
{
if( m_frameScale < 10 ) m_frameScale++;
}
}
2017-09-18 19:26:22 +00:00
const int fwidth = GetFrameWidth( m_frameScale );
const int group = GetFrameGroup( m_frameScale );
2017-09-18 00:37:25 +00:00
const int total = m_frames.size();
2017-09-18 23:24:14 +00:00
const int onScreen = ( w - 2 ) / fwidth;
2017-09-20 19:21:21 +00:00
if( !m_pause )
{
m_frameStart = ( total < onScreen * group ) ? 0 : total - onScreen * group;
2017-09-20 20:41:05 +00:00
m_zvStart = m_frames[std::max( 0, (int)m_frames.size() - 4 )];
if( m_frames.size() == 1 )
{
m_zvEnd = GetLastTime();
}
else
{
m_zvEnd = m_frames.back();
}
2017-09-20 19:21:21 +00:00
}
2017-09-18 19:26:22 +00:00
if( hover )
{
if( ImGui::IsMouseDragging( 1, 0 ) )
{
m_pause = true;
const auto delta = ImGui::GetMouseDragDelta( 1, 0 ).x;
if( abs( delta ) >= fwidth )
{
const auto d = (int)delta / fwidth;
m_frameStart = std::max( 0, m_frameStart - d * group );
io.MouseClickedPos[1].x = io.MousePos.x + d * fwidth - delta;
}
}
const auto mx = io.MousePos.x;
if( mx > wpos.x && mx < wpos.x + w - 1 )
{
const auto mo = mx - ( wpos.x + 1 );
const auto off = mo * group / fwidth;
2017-09-19 22:08:15 +00:00
const int sel = m_frameStart + off;
if( sel < total )
{
ImGui::BeginTooltip();
if( group > 1 )
{
uint64_t f = GetFrameTime( sel );
auto g = std::min( group, total - sel );
for( int j=1; j<g; j++ )
{
f = std::max( f, GetFrameTime( sel + j ) );
}
ImGui::Text( "Frames: %i - %i (%i)", sel, sel + g - 1, g );
ImGui::Text( "Max frame time: %s", TimeToString( f ) );
}
else
{
ImGui::Text( "Frame: %i", sel );
ImGui::Text( "Frame time: %s", TimeToString( GetFrameTime( sel ) ) );
}
ImGui::Text( "Time from start of program: %s", TimeToString( m_frames[sel] - m_frames[0] ) );
ImGui::EndTooltip();
2017-09-22 22:46:15 +00:00
if( ImGui::IsMouseClicked( 0 ) )
{
m_pause = true;
m_zvStart = GetFrameBegin( sel );
m_zvEnd = GetFrameEnd( sel + group - 1 );
if( m_zvStart == m_zvEnd ) m_zvStart--;
}
else if( ImGui::IsMouseDragging( 0 ) )
{
m_zvStart = std::min( m_zvStart, (int64_t)GetFrameBegin( sel ) );
m_zvEnd = std::max( m_zvEnd, (int64_t)GetFrameEnd( sel + group - 1 ) );
}
}
2017-09-19 22:08:15 +00:00
if( m_pause && wheel != 0 )
{
const int pfwidth = GetFrameWidth( prevScale );
const int pgroup = GetFrameGroup( prevScale );
const auto oldoff = mo * pgroup / pfwidth;
m_frameStart = std::min( total, std::max( 0, m_frameStart - int( off - oldoff ) ) );
}
}
}
2017-09-18 19:32:26 +00:00
int i = 0, idx = 0;
2017-09-18 22:26:40 +00:00
while( i < onScreen && m_frameStart + idx < total )
2017-09-18 00:37:25 +00:00
{
2017-09-18 22:26:40 +00:00
uint64_t f = GetFrameTime( m_frameStart + idx );
2017-09-18 20:12:39 +00:00
int g;
2017-09-18 19:32:26 +00:00
if( group > 1 )
{
2017-09-18 22:26:40 +00:00
g = std::min( group, total - ( m_frameStart + idx ) );
2017-09-18 19:32:26 +00:00
for( int j=1; j<g; j++ )
{
2017-09-18 22:26:40 +00:00
f = std::max( f, GetFrameTime( m_frameStart + idx + j ) );
2017-09-18 19:32:26 +00:00
}
}
2017-09-18 00:37:25 +00:00
const auto h = float( std::min<uint64_t>( MaxFrameTime, f ) ) / MaxFrameTime * ( Height - 2 );
if( fwidth != 1 )
{
draw->AddRectFilled( wpos + ImVec2( 1 + i*fwidth, Height-1-h ), wpos + ImVec2( fwidth + i*fwidth, Height-1 ), GetFrameColor( f ) );
2017-09-18 00:37:25 +00:00
}
else
{
draw->AddLine( wpos + ImVec2( 1+i, Height-2-h ), wpos + ImVec2( 1+i, Height-2 ), GetFrameColor( f ) );
}
2017-09-18 19:26:22 +00:00
i++;
2017-09-18 19:32:26 +00:00
idx += group;
2017-09-18 00:37:25 +00:00
}
2017-09-20 20:35:10 +00:00
const auto zitbegin = std::lower_bound( m_frames.begin(), m_frames.end(), m_zvStart );
2017-09-20 23:41:52 +00:00
if( zitbegin == m_frames.end() ) return;
2017-09-20 20:41:05 +00:00
const auto zitend = std::lower_bound( m_frames.begin(), m_frames.end(), m_zvEnd );
2017-09-20 23:13:23 +00:00
auto zbegin = (int)std::distance( m_frames.begin(), zitbegin );
if( zbegin > 0 && *zitbegin != m_zvStart ) zbegin--;
2017-09-20 20:35:10 +00:00
const auto zend = (int)std::distance( m_frames.begin(), zitend );
if( zend > m_frameStart && zbegin < m_frameStart + onScreen * group )
{
auto x0 = std::max( 0, ( zbegin - m_frameStart ) * fwidth / group );
auto x1 = std::min( onScreen * fwidth, ( zend - m_frameStart ) * fwidth / group );
if( x0 == x1 ) x1 = x0 + 1;
draw->AddRectFilled( wpos + ImVec2( 1+x0, 0 ), wpos + ImVec2( 1+x1, Height ), 0x55DD22DD );
}
2017-09-15 00:30:22 +00:00
}
2017-09-20 22:57:26 +00:00
struct TimeScale
{
uint64_t div;
const char* fmt;
};
void View::DrawZones()
{
if( m_zvStart == m_zvEnd ) return;
assert( m_zvStart < m_zvEnd );
ImGuiWindow* window = ImGui::GetCurrentWindow();
if( window->SkipItems ) return;
2017-09-20 23:13:23 +00:00
auto& io = ImGui::GetIO();
2017-09-20 22:57:26 +00:00
const auto wpos = ImGui::GetCursorScreenPos();
const auto w = ImGui::GetWindowContentRegionWidth();
const auto h = ImGui::GetContentRegionAvail().y;
auto draw = ImGui::GetWindowDrawList();
ImGui::InvisibleButton( "##zones", ImVec2( w, h ) );
bool hover = ImGui::IsItemHovered();
2017-09-20 23:39:07 +00:00
auto timespan = m_zvEnd - m_zvStart;
auto pxns = w / double( timespan );
2017-09-20 22:57:26 +00:00
2017-09-20 23:13:23 +00:00
if( hover )
{
if( ImGui::IsMouseDragging( 1, 0 ) )
{
m_pause = true;
const auto delta = ImGui::GetMouseDragDelta( 1, 0 ).x;
const auto nspx = double( timespan ) / w;
const auto dpx = int64_t( delta * nspx );
if( dpx != 0 )
{
m_zvStart -= dpx;
m_zvEnd -= dpx;
io.MouseClickedPos[1].x = io.MousePos.x;
}
2017-09-20 23:13:23 +00:00
}
2017-09-20 23:39:07 +00:00
const auto wheel = io.MouseWheel;
if( wheel != 0 )
{
m_pause = true;
const double mouse = io.MousePos.x - wpos.x;
const auto p = mouse / w;
const auto p1 = timespan * p;
const auto p2 = timespan - p1;
if( wheel > 0 )
{
2017-09-25 21:15:00 +00:00
m_zvStart += int64_t( p1 * 0.2f );
m_zvEnd -= int64_t( p2 * 0.2f );
2017-09-20 23:39:07 +00:00
}
else if( timespan < 1000ull * 1000 * 1000 * 60 )
{
2017-09-25 21:15:00 +00:00
m_zvStart -= std::max( int64_t( 1 ), int64_t( p1 * 0.2f ) );
m_zvEnd += std::max( int64_t( 1 ), int64_t( p2 * 0.2f ) );
2017-09-20 23:39:07 +00:00
}
timespan = m_zvEnd - m_zvStart;
pxns = w / double( timespan );
}
2017-09-20 23:13:23 +00:00
}
2017-09-29 21:08:10 +00:00
m_zvStartNext = 0;
2017-09-20 23:49:01 +00:00
// frames
2017-09-21 00:16:01 +00:00
do
2017-09-20 22:57:26 +00:00
{
2017-09-20 23:49:01 +00:00
const auto zitbegin = std::lower_bound( m_frames.begin(), m_frames.end(), m_zvStart );
2017-09-21 00:16:01 +00:00
if( zitbegin == m_frames.end() ) break;
2017-09-20 23:49:01 +00:00
const auto zitend = std::lower_bound( m_frames.begin(), m_frames.end(), m_zvEnd );
2017-09-20 22:57:26 +00:00
2017-09-20 23:49:01 +00:00
auto zbegin = (int)std::distance( m_frames.begin(), zitbegin );
if( zbegin > 0 && *zitbegin != m_zvStart ) zbegin--;
const auto zend = (int)std::distance( m_frames.begin(), zitend );
2017-09-20 22:57:26 +00:00
2017-09-20 23:49:01 +00:00
for( int i=zbegin; i<zend; i++ )
2017-09-20 23:18:24 +00:00
{
2017-09-20 23:49:01 +00:00
const auto ftime = GetFrameTime( i );
const auto fbegin = (int64_t)GetFrameBegin( i );
const auto fend = (int64_t)GetFrameEnd( i );
2017-09-20 23:18:24 +00:00
2017-09-20 23:49:01 +00:00
char buf[128];
sprintf( buf, "Frame %i (%s)", i, TimeToString( ftime ) );
const auto tsz = ImGui::CalcTextSize( buf );
const auto fsz = pxns * ftime;
2017-09-20 22:57:26 +00:00
2017-09-20 23:49:01 +00:00
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns, tsz.y ) ) )
2017-09-20 22:57:26 +00:00
{
2017-09-20 23:49:01 +00:00
ImGui::BeginTooltip();
2017-09-22 20:23:06 +00:00
ImGui::Text( "%s", buf );
ImGui::Text( "Time from start of program: %s", TimeToString( m_frames[i] - m_frames[0] ) );
2017-09-20 23:49:01 +00:00
ImGui::EndTooltip();
2017-09-29 21:08:10 +00:00
if( ImGui::IsMouseClicked( 2 ) )
{
m_zvStartNext = fbegin;
m_zvEndNext = fend;
m_pause = true;
}
2017-09-20 22:57:26 +00:00
}
2017-09-20 23:49:01 +00:00
if( fbegin >= m_zvStart && fsz > 4 )
2017-09-20 22:57:26 +00:00
{
2017-09-20 23:49:01 +00:00
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, h ), 0x22FFFFFF );
2017-09-20 22:57:26 +00:00
}
2017-09-20 23:49:01 +00:00
if( fsz >= 5 )
2017-09-20 22:57:26 +00:00
{
2017-09-20 23:49:01 +00:00
if( fbegin >= m_zvStart )
{
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, 1 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, tsz.y - 1 ), 0xFFFFFFFF );
}
if( fend <= m_zvEnd )
{
draw->AddLine( wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, 1 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, tsz.y - 1 ), 0xFFFFFFFF );
}
if( fsz - 5 > tsz.x )
{
const auto part = ( fsz - 5 - tsz.x ) / 2;
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, tsz.y / 2 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + part, tsz.y / 2 ), 0xFFFFFFFF );
draw->AddText( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2 + part, 0 ), 0xFFFFFFFF, buf );
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2 + part + tsz.x, tsz.y / 2 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, tsz.y / 2 ), 0xFFFFFFFF );
}
else
{
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns + 2, tsz.y / 2 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns - 2, tsz.y / 2 ), 0xFFFFFFFF );
}
2017-09-20 22:57:26 +00:00
}
}
2017-09-20 23:49:01 +00:00
const auto fend = GetFrameEnd( zend-1 );
if( fend == m_zvEnd )
{
draw->AddLine( wpos + ImVec2( ( fend - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns, h ), 0x22FFFFFF );
}
2017-09-20 22:57:26 +00:00
}
2017-09-21 00:16:01 +00:00
while( false );
// zones
2017-10-01 18:37:35 +00:00
const auto ostep = ImGui::GetFontSize() + 1;
2017-09-21 23:15:58 +00:00
int offset = 20;
for( auto& v : m_threads )
2017-09-21 00:16:01 +00:00
{
draw->AddLine( wpos + ImVec2( 0, offset + ostep - 1 ), wpos + ImVec2( w, offset + ostep - 1 ), 0x33FFFFFF );
draw->AddText( wpos + ImVec2( 0, offset ), 0xFFFFFFFF, GetThreadString( v->id ) );
offset += ostep;
m_lastCpu = -1;
2017-10-04 19:27:06 +00:00
auto depth = DrawZoneLevel( v->timeline, hover, pxns, wpos, offset, 0 );
offset += ostep * ( depth + 1 );
2017-10-04 19:27:06 +00:00
depth = DrawLocks( v->id, hover, pxns, wpos, offset );
offset += ostep * ( depth + 0.2f );
}
}
2017-09-23 22:12:26 +00:00
int View::DrawZoneLevel( const Vector<Event*>& vec, bool hover, double pxns, const ImVec2& wpos, int _offset, int depth )
{
2017-09-24 14:40:16 +00:00
enum { MinVisSize = 3 };
2017-09-23 22:12:26 +00:00
int maxdepth = depth;
2017-09-24 14:40:16 +00:00
auto it = std::lower_bound( vec.begin(), vec.end(), m_zvStart - m_delay, [] ( const auto& l, const auto& r ) { return l->end < r; } );
if( it != vec.end() )
{
const auto w = ImGui::GetWindowContentRegionWidth();
2017-10-01 18:00:53 +00:00
const auto ty = ImGui::GetFontSize();
const auto ostep = ty + 1;
2017-09-23 22:12:26 +00:00
const auto offset = _offset + ostep * depth;
auto draw = ImGui::GetWindowDrawList();
2017-09-24 14:40:16 +00:00
const auto dsz = m_delay * pxns;
2017-09-29 16:55:21 +00:00
const auto rsz = m_resolution * pxns;
2017-09-29 16:55:21 +00:00
const auto zitend = std::lower_bound( vec.begin(), vec.end(), m_zvEnd + m_resolution, [] ( const auto& l, const auto& r ) { return l->start < r; } );
while( it < zitend )
2017-09-21 00:16:01 +00:00
{
auto& ev = **it;
auto& srcloc = GetSourceLocation( ev.srcloc );
2017-10-01 17:31:22 +00:00
const auto color = GetZoneColor( srcloc );
const auto end = GetZoneEnd( ev );
2017-09-29 20:56:12 +00:00
const auto zsz = ( end - ev.start ) * pxns;
2017-09-24 14:40:16 +00:00
if( zsz < MinVisSize )
2017-09-21 20:12:19 +00:00
{
int num = 1;
2017-09-24 14:25:07 +00:00
const auto px0 = ( ev.start - m_zvStart ) * pxns;
auto px1 = ( end - m_zvStart ) * pxns;
auto rend = end;
2017-09-24 14:25:07 +00:00
for(;;)
{
++it;
if( it == zitend ) break;
auto& srcloc2 = GetSourceLocation( (*it)->srcloc );
if( srcloc.color != srcloc2.color ) break;
const auto nend = GetZoneEnd( **it );
const auto pxnext = ( nend - m_zvStart ) * pxns;
2017-09-24 14:40:16 +00:00
if( pxnext - px1 >= MinVisSize * 2 ) break;
2017-09-24 14:25:07 +00:00
px1 = pxnext;
rend = nend;
num++;
2017-09-24 14:25:07 +00:00
}
2017-10-01 18:00:53 +00:00
draw->AddRectFilled( wpos + ImVec2( std::max( px0, -10.0 ), offset ), wpos + ImVec2( std::min( px1, double( w + 10 ) ), offset + ty ), color, 2.f );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( std::max( px0, -10.0 ), offset ), wpos + ImVec2( std::min( px1, double( w + 10 ) ), offset + ty ) ) )
{
ImGui::BeginTooltip();
ImGui::Text( "Zones too small to display: %i", num );
ImGui::Text( "Execution time: %s", TimeToString( rend - ev.start ) );
ImGui::EndTooltip();
2017-09-26 20:08:58 +00:00
if( ImGui::IsMouseClicked( 2 ) && rend - ev.start > 0 )
{
m_zvStartNext = ev.start;
m_zvEndNext = rend;
}
}
2017-09-29 17:13:53 +00:00
char tmp[32];
sprintf( tmp, "%i", num );
const auto tsz = ImGui::CalcTextSize( tmp );
if( tsz.x < px1 - px0 )
{
const auto x = px0 + ( px1 - px0 - tsz.x ) / 2;
draw->AddText( wpos + ImVec2( x, offset ), 0xFF4488DD, tmp );
}
}
else
{
2017-09-28 17:31:27 +00:00
const char* zoneName;
if( ev.text && ev.text->zoneName )
{
zoneName = GetString( ev.text->zoneName );
}
else
{
2017-09-29 20:55:24 +00:00
zoneName = GetString( srcloc.function );
2017-09-28 17:31:27 +00:00
}
int dmul = 1;
if( ev.text )
{
if( ev.text->zoneName ) dmul++;
if( ev.text->userText ) dmul++;
}
bool migration = false;
if( m_lastCpu != ev.cpu_start )
{
if( m_lastCpu != -1 )
{
migration = true;
}
m_lastCpu = ev.cpu_start;
}
if( !ev.child.empty() )
{
const auto d = DrawZoneLevel( ev.child, hover, pxns, wpos, _offset, depth+1 );
if( d > maxdepth ) maxdepth = d;
}
if( ev.end != -1 && m_lastCpu != ev.cpu_end )
{
m_lastCpu = ev.cpu_end;
migration = true;
}
2017-09-28 17:31:27 +00:00
const auto tsz = ImGui::CalcTextSize( zoneName );
2017-09-24 14:40:16 +00:00
const auto pr0 = ( ev.start - m_zvStart ) * pxns;
const auto pr1 = ( end - m_zvStart ) * pxns;
const auto px0 = std::max( pr0, -10.0 );
const auto px1 = std::min( pr1, double( w + 10 ) );
2017-09-25 20:46:14 +00:00
draw->AddRectFilled( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ), color, 2.f );
draw->AddRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ), GetZoneHighlight( ev, migration ), 2.f, -1, GetZoneThickness( ev ) );
if( dsz * dmul >= MinVisSize )
2017-09-24 14:40:16 +00:00
{
draw->AddRectFilled( wpos + ImVec2( pr0, offset ), wpos + ImVec2( std::min( pr0+dsz*dmul, pr1 ), offset + tsz.y ), 0x882222DD, 2.f );
2017-09-24 14:40:16 +00:00
draw->AddRectFilled( wpos + ImVec2( pr1, offset ), wpos + ImVec2( pr1+dsz, offset + tsz.y ), 0x882222DD, 2.f );
}
2017-09-29 16:55:21 +00:00
if( rsz >= MinVisSize )
{
draw->AddLine( wpos + ImVec2( pr0 + rsz, offset + tsz.y/2 ), wpos + ImVec2( pr0 - rsz, offset + tsz.y/2 ), 0xAAFFFFFF );
draw->AddLine( wpos + ImVec2( pr0 + rsz, offset + tsz.y/4 ), wpos + ImVec2( pr0 + rsz, offset + 3*tsz.y/4 ), 0xAAFFFFFF );
draw->AddLine( wpos + ImVec2( pr0 - rsz, offset + tsz.y/4 ), wpos + ImVec2( pr0 - rsz, offset + 3*tsz.y/4 ), 0xAAFFFFFF );
draw->AddLine( wpos + ImVec2( pr1 + rsz, offset + tsz.y/2 ), wpos + ImVec2( pr1 - rsz, offset + tsz.y/2 ), 0xAAFFFFFF );
draw->AddLine( wpos + ImVec2( pr1 + rsz, offset + tsz.y/4 ), wpos + ImVec2( pr1 + rsz, offset + 3*tsz.y/4 ), 0xAAFFFFFF );
draw->AddLine( wpos + ImVec2( pr1 - rsz, offset + tsz.y/4 ), wpos + ImVec2( pr1 - rsz, offset + 3*tsz.y/4 ), 0xAAFFFFFF );
}
2017-09-24 14:25:07 +00:00
if( tsz.x < zsz )
{
2017-09-25 20:05:13 +00:00
const auto x = ( ev.start - m_zvStart ) * pxns + ( ( end - ev.start ) * pxns - tsz.x ) / 2;
if( x < 0 || x > w - tsz.x )
{
2017-10-01 18:10:42 +00:00
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
2017-09-28 17:31:27 +00:00
draw->AddText( wpos + ImVec2( std::max( std::max( 0., px0 ), std::min( double( w - tsz.x ), x ) ), offset ), 0xFFFFFFFF, zoneName );
2017-09-25 20:05:13 +00:00
ImGui::PopClipRect();
}
else
{
2017-09-28 17:31:27 +00:00
draw->AddText( wpos + ImVec2( x, offset ), 0xFFFFFFFF, zoneName );
2017-09-25 20:05:13 +00:00
}
2017-09-24 14:25:07 +00:00
}
else
{
2017-10-01 18:10:42 +00:00
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
2017-09-28 17:31:27 +00:00
draw->AddText( wpos + ImVec2( ( ev.start - m_zvStart ) * pxns, offset ), 0xFFFFFFFF, zoneName );
2017-09-24 14:25:07 +00:00
ImGui::PopClipRect();
}
2017-09-21 00:16:01 +00:00
2017-09-24 14:25:07 +00:00
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ) ) )
{
2017-09-29 20:55:24 +00:00
ZoneTooltip( ev );
2017-09-26 19:49:50 +00:00
if( m_zvStartNext == 0 && ImGui::IsMouseClicked( 2 ) )
2017-09-26 19:49:50 +00:00
{
ZoomToZone( ev );
2017-09-26 19:49:50 +00:00
}
2017-09-29 19:49:14 +00:00
if( ImGui::IsMouseClicked( 0 ) )
{
m_zoneInfoWindow = &ev;
}
2017-09-24 14:25:07 +00:00
}
2017-09-21 23:15:58 +00:00
2017-09-24 14:25:07 +00:00
++it;
}
}
2017-09-21 00:16:01 +00:00
}
2017-09-23 22:12:26 +00:00
return maxdepth;
2017-09-20 22:57:26 +00:00
}
2017-10-04 19:27:06 +00:00
int View::DrawLocks( uint64_t tid, bool hover, double pxns, const ImVec2& wpos, int _offset )
{
int cnt = 0;
for( auto& v : m_lockMap )
{
auto& lockmap = v.second;
if( lockmap.threads.find( tid ) == lockmap.threads.end() ) continue;
auto& tl = lockmap.timeline;
auto it = std::lower_bound( tl.begin(), tl.end(), m_zvStart - m_delay, [] ( const auto& l, const auto& r ) { return l->time < r; } );
if( it != tl.end() )
{
const auto eit = std::lower_bound( tl.begin(), tl.end(), m_zvEnd + m_resolution, [] ( const auto& l, const auto& r ) { return l->time < r; } );
if( it != eit )
{
bool drawn = false;
const auto ty = ImGui::GetFontSize();
const auto ostep = ty + 1;
const auto offset = _offset + ostep * cnt;
auto draw = ImGui::GetWindowDrawList();
auto& srcloc = GetSourceLocation( lockmap.srcloc );
int64_t pos = std::distance( tl.begin(), it );
int64_t last = std::distance( tl.begin(), eit );
while( pos < last )
{
if( tl[pos]->thread == tid )
{
const auto px0 = ( tl[pos]->time - m_zvStart ) * pxns;
draw->AddCircle( wpos + ImVec2( px0, offset + ty / 2 ), 5.f, tl[pos]->type == LockEvent::Type::Wait ? 0xFF0000FF : ( tl[pos]->type == LockEvent::Type::Obtain ? 0xFF00FF00 : 0xFF00FFFF ) );
drawn = true;
}
pos++;
}
if( drawn )
{
draw->AddText( wpos + ImVec2( 0, offset ), 0xFF8888FF, GetString( srcloc.function ) );
cnt++;
}
}
}
}
return cnt;
}
2017-09-29 19:49:14 +00:00
void View::DrawZoneInfoWindow()
{
if( !m_zoneInfoWindow ) return;
2017-09-29 20:06:31 +00:00
2017-09-29 19:49:14 +00:00
auto& ev = *m_zoneInfoWindow;
2017-09-29 20:06:31 +00:00
int dmul = 1;
2017-09-29 19:49:14 +00:00
bool show = true;
2017-09-29 20:32:03 +00:00
ImGui::Begin( "Zone info", &show, ImGuiWindowFlags_ShowBorders );
2017-09-29 20:13:22 +00:00
if( ImGui::Button( "Zoom to zone" ) )
{
ZoomToZone( ev );
}
2017-09-30 11:41:03 +00:00
ImGui::SameLine();
if( ImGui::Button( "Go to parent" ) )
{
if( ev.parent )
{
m_zoneInfoWindow = ev.parent;
}
}
2017-09-29 20:06:31 +00:00
ImGui::Separator();
2017-09-29 19:49:14 +00:00
if( ev.text && ev.text->zoneName )
{
ImGui::Text( "Zone name: %s", GetString( ev.text->zoneName ) );
2017-09-29 20:06:31 +00:00
dmul++;
2017-09-29 19:49:14 +00:00
}
auto& srcloc = GetSourceLocation( ev.srcloc );
ImGui::Text( "Function: %s", GetString( srcloc.function ) );
ImGui::Text( "Location: %s:%i", GetString( srcloc.file ), srcloc.line );
if( ev.text && ev.text->userText )
{
ImGui::Text( "User text: %s", ev.text->userText );
2017-09-29 20:06:31 +00:00
dmul++;
2017-09-29 19:49:14 +00:00
}
2017-09-29 20:06:31 +00:00
ImGui::Separator();
const auto end = GetZoneEnd( ev );
2017-09-29 20:13:22 +00:00
const auto ztime = end - ev.start;
2017-09-29 20:06:31 +00:00
ImGui::Text( "Time from start of program: %s", TimeToString( ev.start - m_frames[0] ) );
2017-09-29 20:13:22 +00:00
ImGui::Text( "Execution time: %s", TimeToString( ztime ) );
ImGui::Text( "Without profiling: %s", TimeToString( ztime - m_delay * dmul ) );
2017-09-29 20:32:03 +00:00
auto ctt = std::make_unique<uint64_t[]>( ev.child.size() );
auto cti = std::make_unique<uint32_t[]>( ev.child.size() );
2017-09-29 20:13:22 +00:00
uint64_t ctime = 0;
2017-09-29 20:32:03 +00:00
for( int i=0; i<ev.child.size(); i++ )
2017-09-29 20:13:22 +00:00
{
2017-09-29 20:32:03 +00:00
const auto cend = GetZoneEnd( *ev.child[i] );
const auto ct = cend - ev.child[i]->start;
ctime += ct;
ctt[i] = ct;
cti[i] = i;
2017-09-29 20:13:22 +00:00
}
2017-09-29 20:32:03 +00:00
std::sort( cti.get(), cti.get() + ev.child.size(), [&ctt] ( const auto& lhs, const auto& rhs ) { return ctt[lhs] > ctt[rhs]; } );
if( !ev.child.empty() )
{
2017-09-29 20:59:11 +00:00
const auto ty = ImGui::GetTextLineHeight();
2017-09-29 20:32:03 +00:00
ImGui::Columns( 2 );
ImGui::Separator();
2017-09-29 20:50:25 +00:00
ImGui::Text( "Child zones: %" PRIu64, ev.child.size() );
2017-09-29 20:32:03 +00:00
ImGui::NextColumn();
2017-09-29 20:50:25 +00:00
ImGui::Text( "Exclusive time: %s (%.2f%%)", TimeToString( ztime - ctime ), double( ztime - ctime ) / ztime * 100 );
2017-09-29 20:32:03 +00:00
ImGui::NextColumn();
ImGui::Separator();
for( int i=0; i<ev.child.size(); i++ )
{
auto& cev = *ev.child[cti[i]];
if( cev.text && cev.text->zoneName )
{
ImGui::Text( "%s", GetString( cev.text->zoneName ) );
}
else
{
auto& srcloc = GetSourceLocation( cev.srcloc );
ImGui::Text( "%s", GetString( srcloc.function ) );
}
if( ImGui::IsItemHovered() )
{
m_zoneHighlight = &cev;
if( ImGui::IsMouseClicked( 0 ) )
{
m_zoneInfoWindow = &cev;
}
if( ImGui::IsMouseClicked( 2 ) )
{
ZoomToZone( cev );
}
2017-09-29 20:57:22 +00:00
ZoneTooltip( cev );
}
2017-09-29 20:32:03 +00:00
ImGui::NextColumn();
2017-09-29 20:50:25 +00:00
const auto part = double( ctt[cti[i]] ) / ztime;
char buf[128];
sprintf( buf, "%s (%.2f%%)", TimeToString( ctt[cti[i]] ), part * 100 );
2017-09-29 20:59:11 +00:00
ImGui::ProgressBar( part, ImVec2( -1, ty ), buf );
2017-09-29 20:32:03 +00:00
ImGui::NextColumn();
}
ImGui::EndColumns();
}
2017-09-29 19:49:14 +00:00
ImGui::End();
2017-09-29 20:06:31 +00:00
2017-09-29 19:49:14 +00:00
if( !show ) m_zoneInfoWindow = nullptr;
}
2017-10-01 17:31:22 +00:00
uint32_t View::GetZoneColor( const Event& ev )
{
return GetZoneColor( GetSourceLocation( ev.srcloc ) );
}
uint32_t View::GetZoneColor( const QueueSourceLocation& srcloc )
{
2017-10-01 17:32:20 +00:00
return srcloc.color != 0 ? ( srcloc.color | 0xFF000000 ) : 0xFFCC5555;
2017-10-01 17:31:22 +00:00
}
uint32_t View::GetZoneHighlight( const Event& ev, bool migration )
2017-09-29 19:57:00 +00:00
{
if( m_zoneInfoWindow == &ev )
{
return 0xFF44DD44;
}
else if( m_zoneHighlight == &ev )
{
return 0xFF4444FF;
}
else if( migration )
{
return 0xFFDD22DD;
}
2017-09-29 19:57:00 +00:00
else
{
const auto color = GetZoneColor( ev );
return 0xFF000000 |
( std::min<int>( 0xFF, ( ( ( color & 0x00FF0000 ) >> 16 ) + 25 ) ) << 16 ) |
( std::min<int>( 0xFF, ( ( ( color & 0x0000FF00 ) >> 8 ) + 25 ) ) << 8 ) |
( std::min<int>( 0xFF, ( ( ( color & 0x000000FF ) ) + 25 ) ) );
2017-09-29 19:57:00 +00:00
}
}
2017-09-30 11:45:02 +00:00
float View::GetZoneThickness( const Event& ev )
{
if( m_zoneInfoWindow == &ev || m_zoneHighlight == &ev )
{
return 3.f;
}
else
{
return 1.f;
}
}
void View::ZoomToZone( const Event& ev )
{
if( ev.end - ev.start <= 0 ) return;
m_zvStartNext = ev.start;
m_zvEndNext = ev.end;
}
2017-09-29 20:55:24 +00:00
void View::ZoneTooltip( const Event& ev )
{
int dmul = 1;
if( ev.text )
{
if( ev.text->zoneName ) dmul++;
if( ev.text->userText ) dmul++;
}
auto& srcloc = GetSourceLocation( ev.srcloc );
const auto filename = GetString( srcloc.file );
const auto line = srcloc.line;
const char* func;
const char* zoneName;
if( ev.text && ev.text->zoneName )
{
zoneName = GetString( ev.text->zoneName );
func = GetString( srcloc.function );
}
else
{
func = zoneName = GetString( srcloc.function );
}
const auto end = GetZoneEnd( ev );
ImGui::BeginTooltip();
ImGui::Text( "%s", func );
ImGui::Text( "%s:%i", filename, line );
ImGui::Text( "Execution time: %s", TimeToString( end - ev.start ) );
ImGui::Text( "Without profiling: %s", TimeToString( end - ev.start - m_delay * dmul ) );
2017-10-01 17:21:25 +00:00
if( ev.cpu_start != -1 )
{
if( ev.end == -1 || ev.cpu_start == ev.cpu_end )
{
ImGui::Text( "CPU: %i", ev.cpu_start );
}
else
{
ImGui::Text( "CPU: %i -> %i", ev.cpu_start, ev.cpu_end );
}
}
2017-09-29 20:55:24 +00:00
if( ev.text && ev.text->userText )
{
ImGui::Text( "" );
ImGui::TextColored( ImVec4( 0xCC / 255.f, 0xCC / 255.f, 0x22 / 255.f, 1.f ), "%s", ev.text->userText );
}
ImGui::EndTooltip();
}
2017-09-30 14:20:08 +00:00
void View::Write( FileWrite& f )
{
2017-09-30 14:58:02 +00:00
f.Write( &m_delay, sizeof( m_delay ) );
f.Write( &m_resolution, sizeof( m_resolution ) );
f.Write( &m_timerMul, sizeof( m_timerMul ) );
2017-10-03 21:29:48 +00:00
uint64_t sz = m_captureName.size();
f.Write( &sz, sizeof( sz ) );
f.Write( m_captureName.c_str(), sz );
sz = m_frames.size();
2017-09-30 14:20:08 +00:00
f.Write( &sz, sizeof( sz ) );
f.Write( m_frames.data(), sizeof( uint64_t ) * sz );
sz = m_strings.size();
f.Write( &sz, sizeof( sz ) );
for( auto& v : m_strings )
{
f.Write( &v.first, sizeof( v.first ) );
sz = v.second.size();
f.Write( &sz, sizeof( sz ) );
f.Write( v.second.c_str(), v.second.size() );
}
sz = m_threadNames.size();
f.Write( &sz, sizeof( sz ) );
for( auto& v : m_threadNames )
{
f.Write( &v.first, sizeof( v.first ) );
sz = v.second.size();
f.Write( &sz, sizeof( sz ) );
f.Write( v.second.c_str(), v.second.size() );
}
sz = m_customStrings.size();
f.Write( &sz, sizeof( sz ) );
for( auto& v : m_customStrings )
{
uint64_t ptr = (uint64_t)v;
f.Write( &ptr, sizeof( ptr ) );
sz = strlen( v );
f.Write( &sz, sizeof( sz ) );
f.Write( v, sz );
}
sz = m_sourceLocation.size();
f.Write( &sz, sizeof( sz ) );
for( auto& v : m_sourceLocation )
{
f.Write( &v.first, sizeof( v.first ) );
f.Write( &v.second, sizeof( v.second ) );
}
2017-10-04 18:10:38 +00:00
sz = m_lockMap.size();
f.Write( &sz, sizeof( sz ) );
for( auto& v : m_lockMap )
{
f.Write( &v.first, sizeof( v.first ) );
2017-10-04 19:36:47 +00:00
f.Write( &v.second.srcloc, sizeof( v.second.srcloc ) );
2017-10-04 18:10:38 +00:00
sz = v.second.threads.size();
f.Write( &sz, sizeof( sz ) );
for( auto& t : v.second.threads )
{
f.Write( &t, sizeof( t ) );
}
sz = v.second.timeline.size();
f.Write( &sz, sizeof( sz ) );
for( auto& lev : v.second.timeline )
{
f.Write( lev, sizeof( LockEvent ) );
}
}
2017-09-30 14:20:08 +00:00
sz = m_threads.size();
f.Write( &sz, sizeof( sz ) );
for( auto& thread : m_threads )
{
f.Write( &thread->id, sizeof( thread->id ) );
WriteTimeline( f, thread->timeline );
}
}
void View::WriteTimeline( FileWrite& f, const Vector<Event*>& vec )
{
uint64_t sz = vec.size();
f.Write( &sz, sizeof( sz ) );
for( auto& v : vec )
{
f.Write( &v->start, sizeof( v->start ) );
f.Write( &v->end, sizeof( v->end ) );
f.Write( &v->srcloc, sizeof( v->srcloc ) );
2017-10-01 17:16:44 +00:00
f.Write( &v->cpu_start, sizeof( v->cpu_start ) );
f.Write( &v->cpu_end, sizeof( v->cpu_end ) );
2017-09-30 14:20:08 +00:00
if( v->text )
{
uint8_t flag = 1;
f.Write( &flag, sizeof( flag ) );
f.Write( &v->text->userText, sizeof( v->text->userText ) );
f.Write( &v->text->zoneName, sizeof( v->text->zoneName ) );
}
else
{
uint8_t flag = 0;
f.Write( &flag, sizeof( flag ) );
}
WriteTimeline( f, v->child );
}
}
2017-09-30 15:09:00 +00:00
void View::ReadTimeline( FileRead& f, Vector<Event*>& vec, Event* parent, const std::unordered_map<uint64_t, const char*>& stringMap )
2017-09-30 14:58:02 +00:00
{
uint64_t sz;
f.Read( &sz, sizeof( sz ) );
2017-09-30 23:54:40 +00:00
vec.reserve( sz );
2017-09-30 14:58:02 +00:00
for( uint64_t i=0; i<sz; i++ )
{
auto zone = m_slab.Alloc<Event>();
2017-09-30 14:59:03 +00:00
m_zonesCnt++;
2017-09-30 14:58:02 +00:00
vec.push_back( zone );
f.Read( &zone->start, sizeof( zone->start ) );
f.Read( &zone->end, sizeof( zone->end ) );
f.Read( &zone->srcloc, sizeof( zone->srcloc ) );
2017-10-01 17:16:44 +00:00
f.Read( &zone->cpu_start, sizeof( zone->cpu_start ) );
f.Read( &zone->cpu_end, sizeof( zone->cpu_end ) );
2017-09-30 14:58:02 +00:00
uint8_t flag;
f.Read( &flag, sizeof( flag ) );
if( flag )
{
zone->text = new TextData;
uint64_t ptr;
f.Read( &ptr, sizeof( ptr ) );
zone->text->userText = ptr == 0 ? nullptr : stringMap.find( ptr )->second;
f.Read( &zone->text->zoneName, sizeof( zone->text->zoneName ) );
}
else
{
zone->text = nullptr;
}
zone->parent = parent;
ReadTimeline( f, zone->child, zone, stringMap );
}
}
2017-09-12 23:33:50 +00:00
}