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-16 23:41:18 +00:00
|
|
|
#include <limits>
|
2017-09-18 23:24:31 +00:00
|
|
|
#include <stdlib.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-17 22:18:36 +00:00
|
|
|
#include "TracyImGui.hpp"
|
2017-09-12 23:33:50 +00:00
|
|
|
#include "TracyView.hpp"
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
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 )
|
2017-09-20 18:38:12 +00:00
|
|
|
, m_hasData( false )
|
2017-09-15 00:29:48 +00:00
|
|
|
, m_mbps( 64 )
|
2017-09-17 11:10:42 +00:00
|
|
|
, 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-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" );
|
|
|
|
}
|
|
|
|
|
|
|
|
View::~View()
|
|
|
|
{
|
|
|
|
m_shutdown.store( true, std::memory_order_relaxed );
|
|
|
|
m_thread.join();
|
2017-09-13 21:36:40 +00:00
|
|
|
|
2017-09-17 11:10:42 +00:00
|
|
|
delete[] m_buffer;
|
|
|
|
LZ4_freeStreamDecode( m_stream );
|
|
|
|
|
2017-09-13 21:36:40 +00:00
|
|
|
assert( s_instance != nullptr );
|
|
|
|
s_instance = nullptr;
|
2017-09-12 23:33:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 00:08:35 +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;
|
2017-09-14 00:15:04 +00:00
|
|
|
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;
|
2017-09-15 22:40:51 +00:00
|
|
|
uint64_t timeStart;
|
2017-09-13 00:08:35 +00:00
|
|
|
|
2017-09-15 22:40:51 +00:00
|
|
|
if( !m_sock.Read( &timeStart, sizeof( timeStart ), &tv, ShouldExit ) ) goto close;
|
2017-09-14 00:15:04 +00:00
|
|
|
if( !m_sock.Read( &lz4, sizeof( lz4 ), &tv, ShouldExit ) ) goto close;
|
2017-09-12 23:54:22 +00:00
|
|
|
|
2017-09-15 22:40:51 +00:00
|
|
|
m_frames.push_back( timeStart );
|
2017-09-20 18:38:12 +00:00
|
|
|
m_hasData.store( true, std::memory_order_release );
|
2017-09-15 22:40:51 +00:00
|
|
|
|
2017-09-20 18:38:12 +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 )
|
|
|
|
{
|
2017-09-17 11:10:42 +00:00
|
|
|
auto buf = m_buffer + m_bufferOffset;
|
2017-09-13 21:40:28 +00:00
|
|
|
char lz4buf[LZ4Size];
|
|
|
|
lz4sz_t lz4sz;
|
2017-09-14 00:15:04 +00:00
|
|
|
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
|
|
|
|
2017-09-17 11:10:42 +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
|
|
|
}
|
2017-09-17 11:10:42 +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;
|
2017-09-14 00:15:04 +00:00
|
|
|
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 )
|
|
|
|
{
|
2017-09-15 18:31:59 +00:00
|
|
|
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:
|
2017-09-14 00:15:04 +00:00
|
|
|
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 )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
ProcessZoneBegin( ev.hdr.id, ev.zoneBegin );
|
|
|
|
break;
|
|
|
|
case QueueType::ZoneEnd:
|
|
|
|
ProcessZoneEnd( ev.hdr.id, ev.zoneEnd );
|
|
|
|
break;
|
2017-09-15 22:30:27 +00:00
|
|
|
case QueueType::FrameMark:
|
2017-09-15 22:40:51 +00:00
|
|
|
ProcessFrameMark( ev.hdr.id );
|
2017-09-15 22:30:27 +00:00
|
|
|
break;
|
2017-09-14 00:00:13 +00:00
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void View::ProcessZoneBegin( uint64_t id, const QueueZoneBegin& ev )
|
|
|
|
{
|
|
|
|
auto it = m_pendingEndZone.find( id );
|
2017-09-15 17:56:55 +00:00
|
|
|
auto zone = m_slab.Alloc<Event>();
|
2017-09-14 00:16:51 +00:00
|
|
|
CheckString( ev.filename );
|
|
|
|
CheckString( ev.function );
|
2017-09-15 17:56:55 +00:00
|
|
|
zone->start = ev.time;
|
2017-09-14 00:00:13 +00:00
|
|
|
std::unique_lock<std::mutex> lock( m_lock );
|
|
|
|
if( it == m_pendingEndZone.end() )
|
|
|
|
{
|
2017-09-15 17:56:55 +00:00
|
|
|
zone->end = -1;
|
|
|
|
NewZone( zone );
|
2017-09-14 00:00:13 +00:00
|
|
|
lock.unlock();
|
2017-09-15 17:56:55 +00:00
|
|
|
m_openZones.emplace( id, zone );
|
2017-09-14 00:00:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert( ev.time <= it->second.time );
|
2017-09-15 17:56:55 +00:00
|
|
|
zone->end = it->second.time;
|
|
|
|
NewZone( zone );
|
2017-09-14 00:00:13 +00:00
|
|
|
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
|
|
|
|
{
|
2017-09-15 17:56:55 +00:00
|
|
|
auto zone = it->second;
|
2017-09-14 00:00:13 +00:00
|
|
|
std::unique_lock<std::mutex> lock( m_lock );
|
2017-09-15 17:56:55 +00:00
|
|
|
assert( ev.time >= zone->start );
|
|
|
|
zone->end = ev.time;
|
|
|
|
UpdateZone( zone );
|
2017-09-14 00:00:13 +00:00
|
|
|
lock.unlock();
|
|
|
|
m_openZones.erase( it );
|
|
|
|
}
|
2017-09-13 21:40:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:40:51 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
m_sock.Send( &ptr, sizeof( ptr ) );
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:43:40 +00:00
|
|
|
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 ) );
|
|
|
|
}
|
|
|
|
|
2017-09-15 17:56:55 +00:00
|
|
|
void View::NewZone( Event* zone )
|
2017-09-14 19:05:01 +00:00
|
|
|
{
|
2017-09-14 19:28:40 +00:00
|
|
|
if( !m_timeline.empty() )
|
|
|
|
{
|
2017-09-15 17:56:55 +00:00
|
|
|
const auto lastend = m_timeline.back()->end;
|
|
|
|
if( lastend != -1 && lastend < zone->start )
|
2017-09-14 19:28:40 +00:00
|
|
|
{
|
2017-09-15 18:17:39 +00:00
|
|
|
m_timeline.push_back( zone );
|
2017-09-14 19:28:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-15 18:17:39 +00:00
|
|
|
m_timeline.push_back( zone );
|
2017-09-14 19:28:40 +00:00
|
|
|
}
|
2017-09-14 19:05:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 17:56:55 +00:00
|
|
|
void View::UpdateZone( Event* zone )
|
2017-09-14 19:05:01 +00:00
|
|
|
{
|
2017-09-15 17:56:55 +00:00
|
|
|
assert( zone->end != -1 );
|
2017-09-14 19:05:01 +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
|
2017-09-18 00:22:59 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-09-18 20:11:38 +00:00
|
|
|
const char* View::TimeToString( uint64_t ns ) const
|
|
|
|
{
|
2017-09-20 19:25:00 +00:00
|
|
|
enum { Pool = 4 };
|
|
|
|
static char bufpool[Pool][64];
|
|
|
|
static int bufsel = 0;
|
|
|
|
char* buf = bufpool[bufsel];
|
|
|
|
bufsel = ( bufsel + 1 ) % Pool;
|
|
|
|
|
2017-09-18 20:11:38 +00:00
|
|
|
if( ns < 1000 )
|
|
|
|
{
|
|
|
|
sprintf( buf, "%i ns", ns );
|
|
|
|
}
|
2017-09-18 20:56:17 +00:00
|
|
|
else if( ns < 1000ull * 1000 )
|
2017-09-18 20:11:38 +00:00
|
|
|
{
|
2017-09-18 20:56:17 +00:00
|
|
|
sprintf( buf, "%.2f us", ns / 1000. );
|
2017-09-18 20:11:38 +00:00
|
|
|
}
|
2017-09-18 20:56:17 +00:00
|
|
|
else if( ns < 1000ull * 1000 * 1000 )
|
2017-09-18 20:11:38 +00:00
|
|
|
{
|
2017-09-18 20:56:17 +00:00
|
|
|
sprintf( buf, "%.2f ms", ns / ( 1000. * 1000. ) );
|
|
|
|
}
|
|
|
|
else if( ns < 1000ull * 1000 * 1000 * 60 )
|
|
|
|
{
|
|
|
|
sprintf( buf, "%.2f s", ns / ( 1000. * 1000. * 1000. ) );
|
2017-09-18 20:11:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-18 20:56:17 +00:00
|
|
|
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. ) );
|
2017-09-18 20:11:38 +00:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-09-15 00:30:22 +00:00
|
|
|
void View::Draw()
|
|
|
|
{
|
|
|
|
s_instance->DrawImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
void View::DrawImpl()
|
|
|
|
{
|
2017-09-20 18:38:12 +00:00
|
|
|
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-15 00:30:22 +00:00
|
|
|
// Connection window
|
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
|
|
|
{
|
2017-09-15 18:31:59 +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-15 18:31:59 +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 );
|
|
|
|
|
2017-09-15 18:31:59 +00:00
|
|
|
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
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
|
|
|
|
// Profiler window
|
|
|
|
ImGui::Begin( "Profiler", nullptr, ImGuiWindowFlags_ShowBorders );
|
2017-09-18 22:26:40 +00:00
|
|
|
if( ImGui::Button( m_pause ? "Resume" : "Pause", ImVec2( 80, 0 ) ) ) m_pause = !m_pause;
|
|
|
|
ImGui::SameLine();
|
2017-09-20 19:28:03 +00:00
|
|
|
ImGui::Text( "Frames: %-7i Time span: %-10s View span: %s", m_frames.size(), TimeToString( GetLastTime() - m_frames[0] ), TimeToString( m_zvEnd - m_zvStart ) );
|
2017-09-18 00:37:25 +00:00
|
|
|
DrawFrames();
|
2017-09-20 22:57:26 +00:00
|
|
|
DrawZones();
|
2017-09-15 22:57:50 +00:00
|
|
|
ImGui::End();
|
2017-09-18 00:37:25 +00:00
|
|
|
|
|
|
|
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 };
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-09-19 22:05:45 +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;
|
|
|
|
|
2017-09-18 23:24:31 +00:00
|
|
|
auto& io = ImGui::GetIO();
|
|
|
|
|
2017-09-20 00:19:39 +00:00
|
|
|
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();
|
|
|
|
|
2017-09-20 00:19:39 +00:00
|
|
|
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
|
|
|
|
2017-09-19 22:05:45 +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 )];
|
2017-09-20 19:21:21 +00:00
|
|
|
m_zvEnd = m_frames.back();
|
|
|
|
}
|
2017-09-18 19:26:22 +00:00
|
|
|
|
2017-09-19 19:10:31 +00:00
|
|
|
if( hover )
|
2017-09-18 23:24:31 +00:00
|
|
|
{
|
2017-09-19 19:10:31 +00:00
|
|
|
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 )
|
2017-09-18 23:24:31 +00:00
|
|
|
{
|
2017-09-19 19:10:31 +00:00
|
|
|
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 )
|
|
|
|
{
|
2017-09-19 19:10:31 +00:00
|
|
|
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::EndTooltip();
|
|
|
|
}
|
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 23:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
2017-09-19 19:10:31 +00:00
|
|
|
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-19 19:10:31 +00:00
|
|
|
|
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 19:21:29 +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 19:21:29 +00:00
|
|
|
|
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 );
|
2017-09-20 19:21:29 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
enum { TimeHeight = 50 };
|
|
|
|
|
|
|
|
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;
|
|
|
|
m_zvStart -= delta * nspx;
|
|
|
|
m_zvEnd -= delta * nspx;
|
|
|
|
io.MouseClickedPos[1].x = io.MousePos.x;
|
|
|
|
}
|
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 )
|
|
|
|
{
|
|
|
|
m_zvStart += int64_t( p1 * 0.1f );
|
|
|
|
m_zvEnd -= int64_t( p2 * 0.1f );
|
|
|
|
}
|
|
|
|
else if( timespan < 1000ull * 1000 * 1000 * 60 )
|
|
|
|
{
|
|
|
|
m_zvStart -= std::max( 1ll, int64_t( p1 * 0.1f ) );
|
|
|
|
m_zvEnd += std::max( 1ll, int64_t( p2 * 0.1f ) );
|
|
|
|
}
|
|
|
|
timespan = m_zvEnd - m_zvStart;
|
|
|
|
pxns = w / double( timespan );
|
|
|
|
}
|
2017-09-20 23:13:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 22:57:26 +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 22:57:26 +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 22:57:26 +00:00
|
|
|
const auto zend = (int)std::distance( m_frames.begin(), zitend );
|
|
|
|
|
|
|
|
for( int i=zbegin; i<zend; i++ )
|
|
|
|
{
|
|
|
|
const auto ftime = GetFrameTime( i );
|
2017-09-20 23:13:23 +00:00
|
|
|
const auto fbegin = (int64_t)GetFrameBegin( i );
|
|
|
|
const auto fend = (int64_t)GetFrameEnd( i );
|
2017-09-20 22:57:26 +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 23:18:24 +00:00
|
|
|
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fend - m_zvStart ) * pxns, tsz.y ) ) )
|
|
|
|
{
|
|
|
|
ImGui::BeginTooltip();
|
|
|
|
ImGui::Text( buf );
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
|
|
|
|
2017-09-20 22:57:26 +00:00
|
|
|
if( fbegin >= m_zvStart )
|
|
|
|
{
|
|
|
|
draw->AddLine( wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, 0 ), wpos + ImVec2( ( fbegin - m_zvStart ) * pxns, h ), 0x22FFFFFF );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fsz >= 5 )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12 23:33:50 +00:00
|
|
|
}
|