tracy/server/TracyView.cpp
2017-09-19 00:36:28 +02:00

508 lines
14 KiB
C++
Executable File

#ifdef _MSC_VER
# include <winsock2.h>
#else
# include <sys/time.h>
#endif
#include <algorithm>
#include <assert.h>
#include <limits>
#include "../common/TracyProtocol.hpp"
#include "../common/TracySystem.hpp"
#include "../common/TracyQueue.hpp"
#include "TracyImGui.hpp"
#include "TracyView.hpp"
namespace tracy
{
static View* s_instance = nullptr;
View::View( const char* addr )
: m_addr( addr )
, m_shutdown( false )
, m_connected( false )
, m_mbps( 64 )
, m_stream( LZ4_createStreamDecode() )
, m_buffer( new char[TargetFrameSize*3] )
, m_bufferOffset( 0 )
, m_frameScale( 0 )
, m_pause( false )
, m_frameStart( 0 )
{
assert( s_instance == nullptr );
s_instance = this;
m_thread = std::thread( [this] { Worker(); } );
SetThreadName( m_thread, "Tracy View" );
}
View::~View()
{
m_shutdown.store( true, std::memory_order_relaxed );
m_thread.join();
delete[] m_buffer;
LZ4_freeStreamDecode( m_stream );
assert( s_instance != nullptr );
s_instance = nullptr;
}
bool View::ShouldExit()
{
return s_instance->m_shutdown.load( std::memory_order_relaxed );
}
void View::Worker()
{
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000;
for(;;)
{
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
if( !m_sock.Connect( m_addr.c_str(), "8086" ) ) continue;
std::chrono::time_point<std::chrono::high_resolution_clock> t0;
uint8_t lz4;
uint64_t bytes = 0;
uint64_t timeStart;
if( !m_sock.Read( &timeStart, sizeof( timeStart ), &tv, ShouldExit ) ) goto close;
if( !m_sock.Read( &lz4, sizeof( lz4 ), &tv, ShouldExit ) ) goto close;
m_frames.push_back( timeStart );
LZ4_setStreamDecode( m_stream, nullptr, 0 );
m_connected.store( true, std::memory_order_relaxed );
t0 = std::chrono::high_resolution_clock::now();
for(;;)
{
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
if( lz4 )
{
auto buf = m_buffer + m_bufferOffset;
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;
bytes += sizeof( lz4sz ) + lz4sz;
auto sz = LZ4_decompress_safe_continue( m_stream, lz4buf, buf, lz4sz, TargetFrameSize );
assert( sz >= 0 );
const char* ptr = buf;
const char* end = buf + sz;
while( ptr < end )
{
auto ev = (QueueItem*)ptr;
DispatchProcess( *ev, ptr );
}
m_bufferOffset += sz;
if( m_bufferOffset > TargetFrameSize * 2 ) m_bufferOffset = 0;
}
else
{
QueueItem ev;
if( !m_sock.Read( &ev.hdr, sizeof( QueueHeader ), &tv, ShouldExit ) ) goto close;
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;
}
bytes += sizeof( QueueHeader ) + payload; // ignores string transfer
DispatchProcess( ev );
}
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 );
m_mbps.erase( m_mbps.begin() );
m_mbps.emplace_back( bytes / ( td * 125.f ) );
t0 = t1;
bytes = 0;
}
}
close:
m_sock.Close();
m_connected.store( false, std::memory_order_relaxed );
}
}
void View::DispatchProcess( const QueueItem& ev )
{
if( ev.hdr.type == QueueType::StringData )
{
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 );
AddString( ev.hdr.id, std::string( buf, buf+sz ) );
}
else
{
Process( ev );
}
}
void View::DispatchProcess( const QueueItem& ev, const char*& ptr )
{
ptr += QueueDataSize[ev.hdr.idx];
if( ev.hdr.type == QueueType::StringData )
{
uint16_t sz;
memcpy( &sz, ptr, sizeof( sz ) );
ptr += sizeof( sz );
AddString( ev.hdr.id, std::string( ptr, ptr+sz ) );
ptr += sz;
}
else
{
Process( ev );
}
}
void View::Process( const QueueItem& ev )
{
switch( ev.hdr.type )
{
case QueueType::ZoneBegin:
ProcessZoneBegin( ev.hdr.id, ev.zoneBegin );
break;
case QueueType::ZoneEnd:
ProcessZoneEnd( ev.hdr.id, ev.zoneEnd );
break;
case QueueType::FrameMark:
ProcessFrameMark( ev.hdr.id );
break;
default:
assert( false );
break;
}
}
void View::ProcessZoneBegin( uint64_t id, const QueueZoneBegin& ev )
{
auto it = m_pendingEndZone.find( id );
auto zone = m_slab.Alloc<Event>();
CheckString( ev.filename );
CheckString( ev.function );
zone->start = ev.time;
std::unique_lock<std::mutex> lock( m_lock );
if( it == m_pendingEndZone.end() )
{
zone->end = -1;
NewZone( zone );
lock.unlock();
m_openZones.emplace( id, zone );
}
else
{
assert( ev.time <= it->second.time );
zone->end = it->second.time;
NewZone( zone );
lock.unlock();
m_pendingEndZone.erase( it );
}
}
void View::ProcessZoneEnd( uint64_t id, const QueueZoneEnd& ev )
{
auto it = m_openZones.find( id );
if( it == m_openZones.end() )
{
m_pendingEndZone.emplace( id, ev );
}
else
{
auto zone = it->second;
std::unique_lock<std::mutex> lock( m_lock );
assert( ev.time >= zone->start );
zone->end = ev.time;
UpdateZone( zone );
lock.unlock();
m_openZones.erase( it );
}
}
void View::ProcessFrameMark( uint64_t id )
{
assert( !m_frames.empty() );
const auto lastframe = m_frames.back();
if( lastframe < id )
{
std::unique_lock<std::mutex> lock( m_lock );
m_frames.push_back( id );
}
else
{
auto it = std::lower_bound( m_frames.begin(), m_frames.end(), id );
std::unique_lock<std::mutex> lock( m_lock );
m_frames.insert( it, id );
}
}
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 );
m_sock.Send( &ptr, sizeof( ptr ) );
}
void View::AddString( uint64_t ptr, std::string&& str )
{
assert( m_strings.find( ptr ) == m_strings.end( ptr ) );
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 ) );
}
void View::NewZone( Event* zone )
{
if( !m_timeline.empty() )
{
const auto lastend = m_timeline.back()->end;
if( lastend != -1 && lastend < zone->start )
{
m_timeline.push_back( zone );
}
else
{
}
}
else
{
m_timeline.push_back( zone );
}
}
void View::UpdateZone( Event* zone )
{
assert( zone->end != -1 );
}
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();
}
}
uint64_t View::GetLastTime() const
{
uint64_t last = 0;
if( !m_frames.empty() ) last = m_frames.back();
if( !m_timeline.empty() )
{
auto ev = m_timeline.back();
if( ev->end > (int64_t)last ) last = ev->end;
}
return last;
}
const char* View::TimeToString( uint64_t ns ) const
{
static char buf[64];
if( ns < 1000 )
{
sprintf( buf, "%i ns", ns );
}
else if( ns < 1000ull * 1000 )
{
sprintf( buf, "%.2f us", ns / 1000. );
}
else if( ns < 1000ull * 1000 * 1000 )
{
sprintf( buf, "%.2f ms", ns / ( 1000. * 1000. ) );
}
else if( ns < 1000ull * 1000 * 1000 * 60 )
{
sprintf( buf, "%.2f s", ns / ( 1000. * 1000. * 1000. ) );
}
else
{
const auto m = ns / ( 1000ull * 1000 * 1000 * 60 );
const auto s = ns - m * ( 1000ull * 1000 * 1000 * 60 );
sprintf( buf, "%i:%04.1f", m, s / ( 1000. * 1000. * 1000. ) );
}
return buf;
}
void View::Draw()
{
s_instance->DrawImpl();
}
void View::DrawImpl()
{
// Connection window
ImGui::Begin( m_addr.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ShowBorders );
{
std::lock_guard<std::mutex> lock( m_mbpslock );
const auto mbps = m_mbps.back();
char buf[64];
if( mbps < 0.1f )
{
sprintf( buf, "%6.2f Kbps", mbps * 1000.f );
}
else
{
sprintf( buf, "%6.2f Mbps", mbps );
}
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 ) );
}
ImGui::Text( "Memory usage: %.2f MB", memUsage.load( std::memory_order_relaxed ) / ( 1024.f * 1024.f ) );
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 );
{
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;
ImGui::Text( "FPS: %6.1f Frame time: %.2f ms", fps, dtm );
}
}
ImGui::End();
// Profiler window
ImGui::Begin( "Profiler", nullptr, ImGuiWindowFlags_ShowBorders );
if( ImGui::Button( m_pause ? "Resume" : "Pause", ImVec2( 80, 0 ) ) ) m_pause = !m_pause;
ImGui::SameLine();
ImGui::Text( "Frames: %-7i Time span: %s", m_frames.size(), TimeToString( GetLastTime() - m_frames[0] ) );
DrawFrames();
ImGui::End();
ImGui::ShowTestWindow();
}
static ImU32 GetFrameColor( uint64_t frameTime )
{
enum { BestTime = 1000 * 1000 * 1000 / 143 };
enum { GoodTime = 1000 * 1000 * 1000 / 59 };
enum { BadTime = 1000 * 1000 * 1000 / 29 };
return frameTime > BadTime ? 0xFF2222DD :
frameTime > GoodTime ? 0xFF22DDDD :
frameTime > BestTime ? 0xFF22DD22 : 0xFFDD9900;
}
void View::DrawFrames()
{
enum { Offset = 25 };
enum { Height = 40 };
enum { MaxFrameTime = 50 * 1000 * 1000 }; // 50ms
ImGuiWindow* window = ImGui::GetCurrentWindow();
if( window->SkipItems ) return;
const auto wpos = ImGui::GetWindowPos() + ImGui::GetWindowContentRegionMin() + ImVec2( 0, Offset );
const auto wspace = ImGui::GetWindowContentRegionMax() - ImGui::GetWindowContentRegionMin();
const auto w = wspace.x;
auto draw = ImGui::GetWindowDrawList();
draw->AddRectFilled( wpos, wpos + ImVec2( w, Height ), 0x33FFFFFF );
bool hover = ImGui::IsMouseHoveringRect( wpos, wpos + ImVec2( w, 60 ) );
if( hover )
{
const auto wheel = ImGui::GetIO().MouseWheel;
if( wheel > 0 )
{
if( m_frameScale > 0 ) m_frameScale--;
}
else if( wheel < 0 )
{
if( m_frameScale < 10 ) m_frameScale++;
}
}
const int fwidth = m_frameScale == 0 ? 4 : 1;
const int group = m_frameScale < 2 ? 1 : ( 1 << ( m_frameScale - 1 ) );
const int total = m_frames.size();
const int onScreen = ( w - 2 ) / fwidth * group;
if( !m_pause ) m_frameStart = total < onScreen ? 0 : total - onScreen;
int i = 0, idx = 0;
while( i < onScreen && m_frameStart + idx < total )
{
uint64_t f = GetFrameTime( m_frameStart + idx );
int g;
if( group > 1 )
{
g = std::min( group, total - ( m_frameStart + idx ) );
for( int j=1; j<g; j++ )
{
f = std::max( f, GetFrameTime( m_frameStart + idx + j ) );
}
}
bool fhover = false;
const auto h = float( std::min<uint64_t>( MaxFrameTime, f ) ) / MaxFrameTime * ( Height - 2 );
if( fwidth != 1 )
{
draw->AddRectFilled( wpos + ImVec2( 1 + i*4, Height-1-h ), wpos + ImVec2( 4 + i*4, Height-1 ), GetFrameColor( f ) );
if( hover ) fhover = ImGui::IsMouseHoveringRect( wpos + ImVec2( 1 + i*4, 1 ), wpos + ImVec2( 5 + i*4, Height - 1 ) );
}
else
{
draw->AddLine( wpos + ImVec2( 1+i, Height-2-h ), wpos + ImVec2( 1+i, Height-2 ), GetFrameColor( f ) );
if( hover ) fhover = ImGui::IsMouseHoveringRect( wpos + ImVec2( 1+i, 1 ), wpos + ImVec2( 2+i, Height - 1 ) );
}
if( fhover )
{
hover = false;
ImGui::BeginTooltip();
if( group > 1 )
{
ImGui::Text( "Frames: %i - %i (%i)", m_frameStart + idx, m_frameStart + idx + g - 1, g );
ImGui::Text( "Max frame time: %s", TimeToString( f ) );
}
else
{
ImGui::Text( "Frame: %i", m_frameStart + idx );
ImGui::Text( "Frame time: %s", TimeToString( f ) );
}
ImGui::EndTooltip();
}
i++;
idx += group;
}
}
}