From 92f3a4bba054d40f2175102d21f9baaa3904592e Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 16 Jan 2019 01:17:01 +0100 Subject: [PATCH] Add ZoneText and ZoneName to the C API. --- TracyC.h | 8 ++++++ client/TracyProfiler.cpp | 56 ++++++++++++++++++++++++++++++++++++++++ server/TracyWorker.cpp | 32 ++++++++++++++++++++--- server/TracyWorker.hpp | 4 +++ 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/TracyC.h b/TracyC.h index 29ea4228..d4e34053 100644 --- a/TracyC.h +++ b/TracyC.h @@ -1,6 +1,7 @@ #ifndef __TRACYC_HPP__ #define __TRACYC_HPP__ +#include #include #ifdef __cplusplus @@ -16,6 +17,8 @@ typedef const void* TracyCZoneCtx; #define TracyCZoneC(c,x,y) #define TracyCZoneNC(c,x,y,z) #define TracyCZoneEnd(c) +#define TracyCZoneText(c,x,y) +#define TracyCZoneName(c,x,y) #else @@ -46,6 +49,8 @@ typedef const struct ___tracy_c_zone_context TracyCZoneCtx; TracyCZoneCtx ___tracy_emit_zone_begin( const struct ___tracy_source_location_data* srcloc, int active ); TracyCZoneCtx ___tracy_emit_zone_begin_callstack( const struct ___tracy_source_location_data* srcloc, int depth, int active ); void ___tracy_emit_zone_end( TracyCZoneCtx ctx ); +void ___tracy_emit_zone_text( TracyCZoneCtx ctx, const char* txt, size_t size ); +void ___tracy_emit_zone_name( TracyCZoneCtx ctx, const char* txt, size_t size ); #if defined TRACY_HAS_CALLSTACK && defined TRACY_CALLSTACK # define TracyCZone( ctx, active ) static const struct ___tracy_source_location_data TracyConcat(__tracy_source_location,__LINE__) = { NULL, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; TracyCZoneCtx ctx = ___tracy_emit_zone_begin_callstack( &TracyConcat(__tracy_source_location,__LINE__), TRACY_CALLSTACK, active ); @@ -61,6 +66,9 @@ void ___tracy_emit_zone_end( TracyCZoneCtx ctx ); #define TracyCZoneEnd( ctx ) ___tracy_emit_zone_end( ctx ); +#define TracyCZoneText( ctx, txt, size ) ___tracy_emit_zone_text( ctx, txt, size ); +#define TracyCZoneName( ctx, txt, size ) ___tracy_emit_zone_name( ctx, txt, size ); + #endif #ifdef __cplusplus diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 248d9485..cef41a27 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -1860,6 +1860,62 @@ void ___tracy_emit_zone_end( TracyCZoneCtx ctx ) } } +void ___tracy_emit_zone_text( TracyCZoneCtx ctx, const char* txt, size_t size ) +{ + if( !ctx.active ) return; + const auto thread = tracy::GetThreadHandle(); + tracy::Magic magic; + auto& token = tracy::s_token.ptr; + auto ptr = (char*)tracy::tracy_malloc( size+1 ); + memcpy( ptr, txt, size ); + ptr[size] = '\0'; + auto& tail = token->get_tail_index(); +#ifndef TRACY_NO_VERIFY + { + auto item = token->enqueue_begin( magic ); + tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneValidation ); + tracy::MemWrite( &item->zoneValidation.thread, thread ); + tracy::MemWrite( &item->zoneValidation.id, ctx.id ); + tail.store( magic + 1, std::memory_order_release ); + } +#endif + { + auto item = token->enqueue_begin( magic ); + tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneText ); + tracy::MemWrite( &item->zoneText.thread, thread ); + tracy::MemWrite( &item->zoneText.text, (uint64_t)ptr ); + tail.store( magic + 1, std::memory_order_release ); + } +} + +void ___tracy_emit_zone_name( TracyCZoneCtx ctx, const char* txt, size_t size ) +{ + if( !ctx.active ) return; + const auto thread = tracy::GetThreadHandle(); + tracy::Magic magic; + auto& token = tracy::s_token.ptr; + auto ptr = (char*)tracy::tracy_malloc( size+1 ); + memcpy( ptr, txt, size ); + ptr[size] = '\0'; + auto& tail = token->get_tail_index(); +#ifndef TRACY_NO_VERIFY + { + auto item = token->enqueue_begin( magic ); + tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneValidation ); + tracy::MemWrite( &item->zoneValidation.thread, thread ); + tracy::MemWrite( &item->zoneValidation.id, ctx.id ); + tail.store( magic + 1, std::memory_order_release ); + } +#endif + { + auto item = token->enqueue_begin( magic ); + tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneName ); + tracy::MemWrite( &item->zoneText.thread, thread ); + tracy::MemWrite( &item->zoneText.text, (uint64_t)ptr ); + tail.store( magic + 1, std::memory_order_release ); + } +} + #ifdef __cplusplus } #endif diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index ae9a5818..296d48e0 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -2339,6 +2339,20 @@ void Worker::ZoneEndFailure( uint64_t thread ) m_failureData.srcloc = 0; } +void Worker::ZoneTextFailure( uint64_t thread ) +{ + m_failure = Failure::ZoneText; + m_failureData.thread = thread; + m_failureData.srcloc = 0; +} + +void Worker::ZoneNameFailure( uint64_t thread ) +{ + m_failure = Failure::ZoneName; + m_failureData.thread = thread; + m_failureData.srcloc = 0; +} + void Worker::MemFreeFailure( uint64_t thread ) { m_failure = Failure::MemFree; @@ -2414,11 +2428,15 @@ void Worker::ProcessFrameMarkEnd( const QueueFrameMark& ev ) void Worker::ProcessZoneText( const QueueZoneText& ev ) { auto tit = m_threadMap.find( ev.thread ); - assert( tit != m_threadMap.end() ); + if( tit == m_threadMap.end() || tit->second->stack.empty() || tit->second->nextZoneId != tit->second->zoneIdStack.back() ) + { + ZoneTextFailure( ev.thread ); + return; + } auto td = tit->second; + td->nextZoneId = 0; auto& stack = td->stack; - assert( !stack.empty() ); auto zone = stack.back(); auto it = m_pendingCustomStrings.find( ev.text ); assert( it != m_pendingCustomStrings.end() ); @@ -2429,11 +2447,15 @@ void Worker::ProcessZoneText( const QueueZoneText& ev ) void Worker::ProcessZoneName( const QueueZoneText& ev ) { auto tit = m_threadMap.find( ev.thread ); - assert( tit != m_threadMap.end() ); + if( tit == m_threadMap.end() || tit->second->stack.empty() || tit->second->nextZoneId != tit->second->zoneIdStack.back() ) + { + ZoneNameFailure( ev.thread ); + return; + } auto td = tit->second; + td->nextZoneId = 0; auto& stack = td->stack; - assert( !stack.empty() ); auto zone = stack.back(); auto it = m_pendingCustomStrings.find( ev.text ); assert( it != m_pendingCustomStrings.end() ); @@ -3666,6 +3688,8 @@ static const char* s_failureReasons[] = { "", "Invalid order of zone begin and end events.", "Received zone end event without a matching zone begin event.", + "Zone text transfer destination doesn't match active zone.", + "Zone name transfer destination doesn't match active zone.", "Memory free event without a matching allocation." }; diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index c04d21cb..b6d1297f 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -182,6 +182,8 @@ public: None, ZoneStack, ZoneEnd, + ZoneText, + ZoneName, MemFree, NUM_FAILURES @@ -337,6 +339,8 @@ private: void ZoneStackFailure( uint64_t thread, const ZoneEvent* ev ); void ZoneEndFailure( uint64_t thread ); + void ZoneTextFailure( uint64_t thread ); + void ZoneNameFailure( uint64_t thread ); void MemFreeFailure( uint64_t thread ); tracy_force_inline void CheckSourceLocation( uint64_t ptr );