From 6d490ffd28642b41861290c2c21c3483c6bc1fb6 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 20 Oct 2021 23:18:03 +0200 Subject: [PATCH] Revert "Use SPSC queue for frame images." This reverts commit 02e76faff7a17a907457078997c42c1b0d6b0f0f. Let's not limit frame image reporting to just one thread. --- client/TracyProfiler.cpp | 69 +++++++++++++++++++++++++++++----------- client/TracyProfiler.hpp | 20 ++++++------ 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index f42cf9bd..5e4e6db5 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -1146,7 +1146,8 @@ Profiler::Profiler() , m_serialQueue( 1024*1024 ) , m_serialDequeue( 1024*1024 ) #ifndef TRACY_NO_FRAME_IMAGE - , m_fiQueue( 64 ) + , m_fiQueue( 16 ) + , m_fiDequeue( 16 ) #endif , m_frameCount( 0 ) , m_isConnected( false ) @@ -1784,30 +1785,60 @@ void Profiler::CompressWorker() for(;;) { const auto shouldExit = ShouldExit(); - FrameImageQueueItem fi; - if( m_fiQueue.try_dequeue( fi ) ) - { - const auto w = fi.w; - const auto h = fi.h; - const auto csz = size_t( w * h / 2 ); - auto etc1buf = (char*)tracy_malloc( csz ); - CompressImageDxt1( (const char*)fi.image, etc1buf, w, h ); - tracy_free( fi.image ); - TracyLfqPrepare( QueueType::FrameImage ); - MemWrite( &item->frameImageFat.image, (uint64_t)etc1buf ); - MemWrite( &item->frameImageFat.frame, fi.frame ); - MemWrite( &item->frameImageFat.w, w ); - MemWrite( &item->frameImageFat.h, h ); - uint8_t flip = fi.flip; - MemWrite( &item->frameImageFat.flip, flip ); - TracyLfqCommit; + { + bool lockHeld = true; + while( !m_fiLock.try_lock() ) + { + if( m_shutdownManual.load( std::memory_order_relaxed ) ) + { + lockHeld = false; + break; + } + } + if( !m_fiQueue.empty() ) m_fiQueue.swap( m_fiDequeue ); + if( lockHeld ) + { + m_fiLock.unlock(); + } + } + + const auto sz = m_fiDequeue.size(); + if( sz > 0 ) + { + auto fi = m_fiDequeue.data(); + auto end = fi + sz; + while( fi != end ) + { + const auto w = fi->w; + const auto h = fi->h; + const auto csz = size_t( w * h / 2 ); + auto etc1buf = (char*)tracy_malloc( csz ); + CompressImageDxt1( (const char*)fi->image, etc1buf, w, h ); + tracy_free( fi->image ); + + TracyLfqPrepare( QueueType::FrameImage ); + MemWrite( &item->frameImageFat.image, (uint64_t)etc1buf ); + MemWrite( &item->frameImageFat.frame, fi->frame ); + MemWrite( &item->frameImageFat.w, w ); + MemWrite( &item->frameImageFat.h, h ); + uint8_t flip = fi->flip; + MemWrite( &item->frameImageFat.flip, flip ); + TracyLfqCommit; + + fi++; + } + m_fiDequeue.clear(); } else { - if( shouldExit ) return; std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) ); } + + if( shouldExit ) + { + return; + } } } #endif diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 63fe5318..7ac2f00c 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -8,7 +8,6 @@ #include #include "tracy_concurrentqueue.h" -#include "tracy_readerwriterqueue.h" #include "TracyCallstack.hpp" #include "TracySysTime.hpp" #include "TracyFastVector.hpp" @@ -243,13 +242,15 @@ public: auto ptr = (char*)tracy_malloc( sz ); memcpy( ptr, image, sz ); - profiler.m_fiQueue.emplace( FrameImageQueueItem { - ptr, - uint32_t( profiler.m_frameCount.load( std::memory_order_relaxed ) - offset ), - w, - h, - flip - } ); + profiler.m_fiLock.lock(); + auto fi = profiler.m_fiQueue.prepare_next(); + fi->image = ptr; + fi->frame = uint32_t( profiler.m_frameCount.load( std::memory_order_relaxed ) - offset ); + fi->w = w; + fi->h = h; + fi->flip = flip; + profiler.m_fiQueue.commit_next(); + profiler.m_fiLock.unlock(); #endif } @@ -808,7 +809,8 @@ private: TracyMutex m_serialLock; #ifndef TRACY_NO_FRAME_IMAGE - ReaderWriterQueue m_fiQueue; + FastVector m_fiQueue, m_fiDequeue; + TracyMutex m_fiLock; #endif std::atomic m_frameCount;