From 4551553eb49df23e69e707d8cdea753ecd8d6893 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 25 Nov 2019 23:59:48 +0100 Subject: [PATCH] Implement setting client parameters from server. --- Tracy.hpp | 6 ++++++ client/TracyProfiler.cpp | 31 +++++++++++++++++++++++++++++ client/TracyProfiler.hpp | 8 ++++++++ common/TracyProtocol.hpp | 5 +++-- common/TracyQueue.hpp | 11 ++++++++++ server/TracyEvent.hpp | 9 +++++++++ server/TracyView.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ server/TracyWorker.cpp | 18 +++++++++++++++++ server/TracyWorker.hpp | 6 ++++++ 9 files changed, 135 insertions(+), 2 deletions(-) diff --git a/Tracy.hpp b/Tracy.hpp index 508b2e1f..80c09b38 100644 --- a/Tracy.hpp +++ b/Tracy.hpp @@ -64,6 +64,9 @@ #define TracyMessageCS(x,y,z,w) #define TracyMessageLCS(x,y,z) +#define TracyParameterRegister(x) +#define TracyParameterSetup(x,y,z,w) + #else #include "client/TracyLock.hpp" @@ -166,6 +169,9 @@ # define TracyMessageLCS( txt, color, depth ) TracyMessageLC( txt, color ) #endif +#define TracyParameterRegister( cb ) tracy::Profiler::ParameterRegister( cb ); +#define TracyParameterSetup( idx, name, isBool, val ) tracy::Profiler::ParameterSetup( idx, name, isBool, val ); + #endif #endif diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 671e22be..e9db1c1f 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -1054,6 +1054,7 @@ Profiler::Profiler() , m_connectionId( 0 ) , m_deferredQueue( 64*1024 ) #endif + , m_paramCallback( nullptr ) { assert( !s_instance ); s_instance = this; @@ -2264,6 +2265,9 @@ bool Profiler::HandleServerQuery() SysTraceSendExternalName( ptr ); break; #endif + case ServerQueryParameter: + HandleParameter( ptr ); + break; default: assert( false ); break; @@ -2508,6 +2512,33 @@ void Profiler::ProcessSysTime() } #endif +void Profiler::ParameterSetup( uint32_t idx, const char* name, bool isBool, int32_t val ) +{ + tracy::Magic magic; + auto token = tracy::GetToken(); + auto& tail = token->get_tail_index(); + auto item = token->enqueue_begin( magic ); + tracy::MemWrite( &item->hdr.type, tracy::QueueType::ParamSetup ); + tracy::MemWrite( &item->paramSetup.idx, idx ); + tracy::MemWrite( &item->paramSetup.name, (uint64_t)name ); + tracy::MemWrite( &item->paramSetup.isBool, (uint8_t)isBool ); + tracy::MemWrite( &item->paramSetup.val, val ); + +#ifdef TRACY_ON_DEMAND + DeferItem( *item ); +#endif + + tail.store( magic + 1, std::memory_order_release ); +} + +void Profiler::HandleParameter( uint64_t payload ) +{ + assert( m_paramCallback ); + const auto idx = uint32_t( payload >> 32 ); + const auto val = int32_t( payload & 0xFFFFFFFF ); + m_paramCallback( idx, val ); +} + } #ifdef __cplusplus diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index f3ea50a6..e3f39495 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -80,6 +80,8 @@ struct LuaZoneState using Magic = moodycamel::ConcurrentQueueDefaultTraits::index_t; +typedef void(*ParameterCallback)( uint32_t idx, int32_t val ); + class Profiler { struct FrameImageQueueItem @@ -442,6 +444,9 @@ public: #endif } + static void ParameterRegister( ParameterCallback cb ) { GetProfiler().m_paramCallback = cb; } + static void ParameterSetup( uint32_t idx, const char* name, bool isBool, int32_t val ); + void SendCallstack( int depth, const char* skipBefore ); static void CutCallstack( void* callstack, const char* skipBefore ); @@ -506,6 +511,7 @@ private: bool HandleServerQuery(); void HandleDisconnect(); + void HandleParameter( uint64_t payload ); void CalibrateTimer(); void CalibrateDelay(); @@ -605,6 +611,8 @@ private: #else void ProcessSysTime() {} #endif + + ParameterCallback m_paramCallback; }; }; diff --git a/common/TracyProtocol.hpp b/common/TracyProtocol.hpp index 43144c48..37dfa1fe 100644 --- a/common/TracyProtocol.hpp +++ b/common/TracyProtocol.hpp @@ -9,7 +9,7 @@ namespace tracy constexpr unsigned Lz4CompressBound( unsigned isize ) { return isize + ( isize / 255 ) + 16; } -enum : uint32_t { ProtocolVersion = 23 }; +enum : uint32_t { ProtocolVersion = 24 }; enum : uint32_t { BroadcastVersion = 0 }; using lz4sz_t = uint32_t; @@ -46,7 +46,8 @@ enum ServerQuery : uint8_t ServerQueryCallstackFrame, ServerQueryFrameName, ServerQueryDisconnect, - ServerQueryExternalName + ServerQueryExternalName, + ServerQueryParameter }; struct ServerQueryPacket diff --git a/common/TracyQueue.hpp b/common/TracyQueue.hpp index 113d74d0..e38f7272 100644 --- a/common/TracyQueue.hpp +++ b/common/TracyQueue.hpp @@ -67,6 +67,7 @@ enum class QueueType : uint8_t SysTimeReport, TidToPid, PlotConfig, + ParamSetup, StringData, ThreadName, CustomStringData, @@ -348,6 +349,14 @@ struct QueuePlotConfig uint8_t type; }; +struct QueueParamSetup +{ + uint32_t idx; + uint64_t name; // ptr + uint8_t isBool; + int32_t val; +}; + struct QueueHeader { union @@ -397,6 +406,7 @@ struct QueueItem QueueThreadWakeup threadWakeup; QueueTidToPid tidToPid; QueuePlotConfig plotConfig; + QueueParamSetup paramSetup; }; }; #pragma pack() @@ -465,6 +475,7 @@ static const size_t QueueDataSize[] = { sizeof( QueueHeader ) + sizeof( QueueSysTime ), sizeof( QueueHeader ) + sizeof( QueueTidToPid ), sizeof( QueueHeader ) + sizeof( QueuePlotConfig ), + sizeof( QueueHeader ) + sizeof( QueueParamSetup ), // keep all QueueStringTransfer below sizeof( QueueHeader ) + sizeof( QueueStringTransfer ), // string data sizeof( QueueHeader ) + sizeof( QueueStringTransfer ), // thread name diff --git a/server/TracyEvent.hpp b/server/TracyEvent.hpp index 693abfcc..86376ff9 100644 --- a/server/TracyEvent.hpp +++ b/server/TracyEvent.hpp @@ -593,6 +593,15 @@ struct CpuThreadData enum { CpuThreadDataSize = sizeof( CpuThreadData ) }; + +struct Parameter +{ + uint32_t idx; + StringRef name; + bool isBool; + int32_t val; +}; + } #endif diff --git a/server/TracyView.cpp b/server/TracyView.cpp index d7d3f1e9..502f6580 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -1070,6 +1070,49 @@ bool View::DrawConnection() ImGui::EndPopup(); } + const auto& params = m_worker.GetParameters(); + if( !params.empty() ) + { + ImGui::Separator(); + if( ImGui::TreeNode( "Trace parameters" ) ) + { + ImGui::Columns( 2 ); + ImGui::TextUnformatted( "Name" ); + ImGui::NextColumn(); + ImGui::TextUnformatted( "Value" ); + ImGui::NextColumn(); + ImGui::Separator(); + size_t idx = 0; + for( auto& p : params ) + { + ImGui::TextUnformatted( m_worker.GetString( p.name ) ); + ImGui::NextColumn(); + ImGui::PushID( idx ); + if( p.isBool ) + { + bool val = p.val; + if( ImGui::Checkbox( "", &val ) ) + { + m_worker.SetParameter( idx, int32_t( val ) ); + } + } + else + { + auto val = int( p.val ); + if( ImGui::InputInt( "", &val, 1, 100, ImGuiInputTextFlags_EnterReturnsTrue ) ) + { + m_worker.SetParameter( idx, int32_t( val ) ); + } + } + ImGui::PopID(); + ImGui::NextColumn(); + idx++; + } + ImGui::EndColumns(); + ImGui::TreePop(); + } + } + return true; } diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index 2de11060..a784cfeb 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -3235,6 +3235,9 @@ bool Worker::Process( const QueueItem& ev ) case QueueType::TidToPid: ProcessTidToPid( ev.tidToPid ); break; + case QueueType::ParamSetup: + ProcessParamSetup( ev.paramSetup ); + break; default: assert( false ); break; @@ -4575,6 +4578,12 @@ void Worker::ProcessTidToPid( const QueueTidToPid& ev ) m_data.tidToPid.emplace( ev.tid, ev.pid ); } +void Worker::ProcessParamSetup( const QueueParamSetup& ev ) +{ + CheckString( ev.name ); + m_params.push_back( Parameter { ev.idx, StringRef( StringRef::Ptr, ev.name ), bool( ev.isBool ), ev.val } ); +} + void Worker::MemAllocChanged( int64_t time ) { const auto val = (double)m_data.memory.usage; @@ -5670,4 +5679,13 @@ const char* Worker::UnpackFrameImage( const FrameImage& image ) return m_frameImageCompressedBuffer; } +void Worker::SetParameter( size_t paramIdx, int32_t val ) +{ + assert( paramIdx < m_params.size() ); + m_params[paramIdx].val = val; + const auto idx = uint64_t( m_params[paramIdx].idx ); + const auto v = uint64_t( uint32_t( val ) ); + Query( ServerQueryParameter, ( idx << 32 ) | val ); +} + } diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index 2485605f..32d25c6a 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -419,6 +419,9 @@ public: const char* PackFrameImage( const char* image, uint32_t inBytes, uint32_t& csz ); const char* UnpackFrameImage( const FrameImage& image ); + const Vector& GetParameters() const { return m_params; } + void SetParameter( size_t paramIdx, int32_t val ); + private: void Network(); void Exec(); @@ -479,6 +482,7 @@ private: tracy_force_inline void ProcessContextSwitch( const QueueContextSwitch& ev ); tracy_force_inline void ProcessThreadWakeup( const QueueThreadWakeup& ev ); tracy_force_inline void ProcessTidToPid( const QueueTidToPid& ev ); + tracy_force_inline void ProcessParamSetup( const QueueParamSetup& ev ); tracy_force_inline ZoneEvent* AllocZoneEvent(); tracy_force_inline void ProcessZoneBeginImpl( ZoneEvent* zone, const QueueZoneBegin& ev ); @@ -708,6 +712,8 @@ private: #ifdef TRACY_NO_STATISTICS Vector m_zoneEventPool; #endif + + Vector m_params; }; }