2017-09-10 15:43:56 +00:00
|
|
|
#include <assert.h>
|
2017-09-10 18:07:38 +00:00
|
|
|
#include <chrono>
|
2017-09-11 20:51:47 +00:00
|
|
|
#include <memory>
|
2017-09-10 15:43:56 +00:00
|
|
|
|
2017-09-11 20:51:47 +00:00
|
|
|
#include "../common/TracySocket.hpp"
|
2017-09-10 15:43:56 +00:00
|
|
|
#include "TracyProfiler.hpp"
|
2017-09-10 15:46:20 +00:00
|
|
|
#include "TracySystem.hpp"
|
2017-09-10 15:43:56 +00:00
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2017-09-10 18:02:40 +00:00
|
|
|
extern const char* PointerCheckA;
|
|
|
|
const char* PointerCheckB = "tracy";
|
|
|
|
|
2017-09-10 18:07:38 +00:00
|
|
|
static inline int64_t GetTime()
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
static Profiler* s_instance = nullptr;
|
|
|
|
|
|
|
|
Profiler::Profiler()
|
2017-09-10 18:14:16 +00:00
|
|
|
: m_timeBegin( GetTime() )
|
|
|
|
, m_shutdown( false )
|
2017-09-10 18:08:42 +00:00
|
|
|
, m_id( 0 )
|
2017-09-10 15:43:56 +00:00
|
|
|
{
|
2017-09-10 18:02:40 +00:00
|
|
|
assert( PointerCheckA == PointerCheckB );
|
2017-09-10 15:43:56 +00:00
|
|
|
assert( !s_instance );
|
|
|
|
s_instance = this;
|
|
|
|
|
|
|
|
m_thread = std::thread( [this] { Worker(); } );
|
2017-09-10 15:46:20 +00:00
|
|
|
SetThreadName( m_thread, "Tracy Profiler" );
|
2017-09-10 15:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Profiler::~Profiler()
|
|
|
|
{
|
|
|
|
assert( s_instance );
|
|
|
|
s_instance = nullptr;
|
|
|
|
|
|
|
|
m_shutdown.store( true, std::memory_order_relaxed );
|
|
|
|
m_thread.join();
|
|
|
|
}
|
|
|
|
|
2017-09-10 18:08:42 +00:00
|
|
|
uint64_t Profiler::GetNewId()
|
|
|
|
{
|
|
|
|
return s_instance->m_id.fetch_add( 1, std::memory_order_relaxed );
|
|
|
|
}
|
|
|
|
|
2017-09-10 18:09:14 +00:00
|
|
|
void Profiler::ZoneBegin( QueueZoneBegin&& data )
|
|
|
|
{
|
|
|
|
QueueItem item { QueueType::ZoneBegin, GetTime() };
|
|
|
|
item.zoneBegin = std::move( data );
|
2017-09-10 18:52:10 +00:00
|
|
|
s_instance->m_queue.enqueue( GetToken(), std::move( item ) );
|
2017-09-10 18:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Profiler::ZoneEnd( QueueZoneEnd&& data )
|
|
|
|
{
|
|
|
|
QueueItem item { QueueType::ZoneEnd, GetTime() };
|
|
|
|
item.zoneEnd = std::move( data );
|
2017-09-10 18:52:10 +00:00
|
|
|
s_instance->m_queue.enqueue( GetToken(), std::move( item ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Profiler* Profiler::Instance()
|
|
|
|
{
|
|
|
|
return s_instance;
|
2017-09-10 18:09:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
void Profiler::Worker()
|
|
|
|
{
|
2017-09-11 22:38:33 +00:00
|
|
|
enum { BulkSize = 64000 / QueueItemSize };
|
2017-09-10 18:23:06 +00:00
|
|
|
moodycamel::ConsumerToken token( m_queue );
|
|
|
|
|
2017-09-11 20:51:47 +00:00
|
|
|
ListenSocket listen;
|
|
|
|
listen.Listen( "8086", 8 );
|
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
2017-09-11 20:51:47 +00:00
|
|
|
std::unique_ptr<Socket> sock;
|
|
|
|
for(;;)
|
2017-09-10 18:23:06 +00:00
|
|
|
{
|
2017-09-11 20:51:47 +00:00
|
|
|
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
|
|
|
|
sock = listen.Accept();
|
|
|
|
if( sock ) break;
|
2017-09-10 18:23:06 +00:00
|
|
|
}
|
2017-09-11 20:51:47 +00:00
|
|
|
|
|
|
|
sock->Send( &m_timeBegin, sizeof( m_timeBegin ) );
|
|
|
|
|
|
|
|
for(;;)
|
2017-09-10 18:23:06 +00:00
|
|
|
{
|
2017-09-11 20:51:47 +00:00
|
|
|
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
|
|
|
|
|
|
|
|
QueueItem item[BulkSize];
|
|
|
|
const auto sz = m_queue.try_dequeue_bulk( token, item, BulkSize );
|
|
|
|
if( sz > 0 )
|
|
|
|
{
|
|
|
|
if( sock->Send( item, sz * sizeof( QueueItem ) ) == -1 ) break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
}
|
2017-09-10 18:23:06 +00:00
|
|
|
}
|
2017-09-10 15:43:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|