From 38a0718b7030699ca35a7d71e779081ec9f2e3d6 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 6 Dec 2023 00:24:30 +0100 Subject: [PATCH] Fix invalid tracy events from C# profiling api when profiler gets connected mid-event --- .../Scripting/Internal/EngineInternalCalls.cpp | 12 ++++++++++-- Source/ThirdParty/tracy/client/TracyScoped.hpp | 5 +++-- Source/ThirdParty/tracy/common/TracySystem.hpp | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Scripting/Internal/EngineInternalCalls.cpp b/Source/Engine/Scripting/Internal/EngineInternalCalls.cpp index e3e5b4342..08a93328b 100644 --- a/Source/Engine/Scripting/Internal/EngineInternalCalls.cpp +++ b/Source/Engine/Scripting/Internal/EngineInternalCalls.cpp @@ -108,6 +108,7 @@ namespace }; ChunkedArray ManagedSourceLocations; + uint32 ManagedEventsCount[PLATFORM_THREADS_LIMIT] = { 0 }; #endif #endif } @@ -145,7 +146,9 @@ DEFINE_INTERNAL_CALL(void) ProfilerInternal_BeginEvent(MString* nameObj) srcLoc->color = 0; } //static constexpr tracy::SourceLocationData tracySrcLoc{ nullptr, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; - tracy::ScopedZone::Begin(srcLoc); + const bool tracyActive = tracy::ScopedZone::Begin(srcLoc); + if (tracyActive) + ManagedEventsCount[Platform::GetCurrentThreadID()]++; #endif #endif #endif @@ -155,7 +158,12 @@ DEFINE_INTERNAL_CALL(void) ProfilerInternal_EndEvent() { #if COMPILE_WITH_PROFILER #if TRACY_ENABLE - tracy::ScopedZone::End(); + auto& tracyActive = ManagedEventsCount[Platform::GetCurrentThreadID()]; + if (tracyActive > 0) + { + tracyActive--; + tracy::ScopedZone::End(); + } #endif ProfilerCPU::EndEvent(); #endif diff --git a/Source/ThirdParty/tracy/client/TracyScoped.hpp b/Source/ThirdParty/tracy/client/TracyScoped.hpp index bb916aa57..2182bf65b 100644 --- a/Source/ThirdParty/tracy/client/TracyScoped.hpp +++ b/Source/ThirdParty/tracy/client/TracyScoped.hpp @@ -12,15 +12,16 @@ namespace tracy { -void ScopedZone::Begin(const SourceLocationData* srcloc) +bool ScopedZone::Begin(const SourceLocationData* srcloc) { #ifdef TRACY_ON_DEMAND - if (!GetProfiler().IsConnected()) return; + if (!GetProfiler().IsConnected()) return false; #endif TracyLfqPrepare( QueueType::ZoneBegin ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); MemWrite( &item->zoneBegin.srcloc, (uint64_t)srcloc ); TracyQueueCommit( zoneBeginThread ); + return true; } void ScopedZone::End() diff --git a/Source/ThirdParty/tracy/common/TracySystem.hpp b/Source/ThirdParty/tracy/common/TracySystem.hpp index 7a88a00b1..497d047e5 100644 --- a/Source/ThirdParty/tracy/common/TracySystem.hpp +++ b/Source/ThirdParty/tracy/common/TracySystem.hpp @@ -39,7 +39,7 @@ struct TRACY_API SourceLocationData class TRACY_API ScopedZone { public: - static void Begin( const SourceLocationData* srcloc ); + static bool Begin( const SourceLocationData* srcloc ); static void End(); ScopedZone( const ScopedZone& ) = delete;