From 3c00ce09588be613ed36c8ad91880fa5dc5b7cb8 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 11 Nov 2017 19:44:09 +0100 Subject: [PATCH] GPU context registration. --- TracyOpenGL.hpp | 48 ++++++++++++++++++++++++++++++++++++++++ client/TracyProfiler.cpp | 1 + client/TracyProfiler.hpp | 1 + common/TracyQueue.hpp | 10 +++++++++ server/TracyEvent.hpp | 5 +++++ server/TracyView.cpp | 12 ++++++++++ server/TracyView.hpp | 2 ++ 7 files changed, 79 insertions(+) create mode 100644 TracyOpenGL.hpp diff --git a/TracyOpenGL.hpp b/TracyOpenGL.hpp new file mode 100644 index 00000000..4c2b99d2 --- /dev/null +++ b/TracyOpenGL.hpp @@ -0,0 +1,48 @@ +#ifndef __TRACYOPENGL_HPP__ +#define __TRACYOPENGL_HPP__ + +#ifdef TRACY_ENABLE + +#include + +#include "client/TracyProfiler.hpp" + +namespace tracy +{ + +extern std::atomic s_gpuCtxCounter; + +template +class GpuCtx +{ +public: + GpuCtx() + : m_context( s_gpuCtxCounter.fetch_add( 1, std::memory_order_relaxed ) ) + { + glGenQueries( Num, m_query ); + + int64_t tgpu; + glGetInteger64v( GL_TIMESTAMP, &tgpu ); + int64_t tcpu = Profiler::GetTime(); + + Magic magic; + auto& token = s_token.ptr; + auto& tail = token->get_tail_index(); + auto item = token->enqueue_begin( magic ); + item->hdr.type = QueueType::GpuNewContext; + item->gpuNewContext.cputime = tcpu; + item->gpuNewContext.gputime = tgpu; + item->gpuNewContext.context = m_context; + tail.store( magic + 1, std::memory_order_release ); + } + +private: + unsigned int m_query[Num]; + uint16_t m_context; +}; + +} + +#endif + +#endif diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 217170ce..85ae0d0d 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -98,6 +98,7 @@ static InitTimeWrapper init_order(101) s_initTime { Profiler::GetTime() }; static RPMallocInit init_order(102) s_rpmalloc_init; moodycamel::ConcurrentQueue init_order(103) s_queue( QueuePrealloc ); std::atomic init_order(104) s_lockCounter( 0 ); +std::atomic init_order(104) s_gpuCtxCounter( 0 ); #ifdef TRACY_COLLECT_THREAD_NAMES struct ThreadNameData; diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 43cf1432..78f01bba 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -10,6 +10,7 @@ #include "../common/tracy_lz4.hpp" #include "../common/TracyQueue.hpp" #include "../common/TracyAlloc.hpp" +#include "../common/TracySystem.hpp" #if defined _MSC_VER || defined __CYGWIN__ # include diff --git a/common/TracyQueue.hpp b/common/TracyQueue.hpp index a10797fb..9d00944f 100644 --- a/common/TracyQueue.hpp +++ b/common/TracyQueue.hpp @@ -29,6 +29,7 @@ enum class QueueType : uint8_t PlotName, Message, MessageLiteral, + GpuNewContext, NUM_TYPES }; @@ -138,6 +139,13 @@ struct QueueMessage uint64_t text; // ptr }; +struct QueueGpuNewContext +{ + int64_t cputime; + int64_t gputime; + uint16_t context; +}; + struct QueueHeader { union @@ -165,6 +173,7 @@ struct QueueItem QueueLockMark lockMark; QueuePlotData plotData; QueueMessage message; + QueueGpuNewContext gpuNewContext; }; }; @@ -194,6 +203,7 @@ static const size_t QueueDataSize[] = { sizeof( QueueHeader ) + sizeof( QueueStringTransfer ), // plot name sizeof( QueueHeader ) + sizeof( QueueMessage ), sizeof( QueueHeader ) + sizeof( QueueMessage ), // literal + sizeof( QueueHeader ) + sizeof( QueueGpuNewContext ), }; static_assert( QueueItemSize == 32, "Queue item size not 32 bytes" ); diff --git a/server/TracyEvent.hpp b/server/TracyEvent.hpp index a6a5c2ef..d169587f 100644 --- a/server/TracyEvent.hpp +++ b/server/TracyEvent.hpp @@ -112,6 +112,11 @@ struct ThreadData Vector messages; }; +struct GpuCtxData +{ + int64_t timeDiff; +}; + struct LockMap { uint32_t srcloc; diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 39776d3c..cd4284eb 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -588,6 +588,9 @@ void View::Process( const QueueItem& ev ) case QueueType::MessageLiteral: ProcessMessageLiteral( ev.message ); break; + case QueueType::GpuNewContext: + ProcessGpuNewContext( ev.gpuNewContext ); + break; case QueueType::Terminate: m_terminate = true; break; @@ -844,6 +847,15 @@ void View::ProcessMessageLiteral( const QueueMessage& ev ) InsertMessageData( msg, ev.thread ); } +void View::ProcessGpuNewContext( const QueueGpuNewContext& ev ) +{ + assert( ev.context == m_gpuData.size() ); + auto gpu = m_slab.Alloc(); + gpu->timeDiff = int64_t( ev.cputime * m_timerMul - ev.gputime ); + std::lock_guard lock( m_lock ); + m_gpuData.push_back( gpu ); +} + void View::CheckString( uint64_t ptr ) { if( m_strings.find( ptr ) != m_strings.end() ) return; diff --git a/server/TracyView.hpp b/server/TracyView.hpp index 51e12a0e..4b71d768 100644 --- a/server/TracyView.hpp +++ b/server/TracyView.hpp @@ -67,6 +67,7 @@ private: void ProcessPlotData( const QueuePlotData& ev ); void ProcessMessage( const QueueMessage& ev ); void ProcessMessageLiteral( const QueueMessage& ev ); + void ProcessGpuNewContext( const QueueGpuNewContext& ev ); void CheckString( uint64_t ptr ); void CheckThreadString( uint64_t id ); @@ -158,6 +159,7 @@ private: Vector m_plots; Vector m_messages; Vector m_textData; + Vector m_gpuData; std::unordered_map m_strings; std::unordered_map m_threadNames; std::unordered_map m_sourceLocation;