diff --git a/TracyC.h b/TracyC.h new file mode 100644 index 00000000..29ea4228 --- /dev/null +++ b/TracyC.h @@ -0,0 +1,70 @@ +#ifndef __TRACYC_HPP__ +#define __TRACYC_HPP__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef TRACY_ENABLE + +typedef const void* TracyCZoneCtx; + +#define TracyCZone(c,x) +#define TracyCZoneN(c,x,y) +#define TracyCZoneC(c,x,y) +#define TracyCZoneNC(c,x,y,z) +#define TracyCZoneEnd(c) + +#else + +#ifndef TracyConcat +# define TracyConcat(x,y) TracyConcatIndirect(x,y) +#endif +#ifndef TracyConcatIndirect +# define TracyConcatIndirect(x,y) x##y +#endif + +struct ___tracy_source_location_data +{ + const char* name; + const char* function; + const char* file; + uint32_t line; + uint32_t color; +}; + +struct ___tracy_c_zone_context +{ + uint32_t id; + int active; +}; + +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 ); + +#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 ); +# define TracyCZoneN( ctx, name, active ) static const struct ___tracy_source_location_data TracyConcat(__tracy_source_location,__LINE__) = { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; TracyCZoneCtx ctx = ___tracy_emit_zone_begin_callstack( &TracyConcat(__tracy_source_location,__LINE__), TRACY_CALLSTACK, active ); +# define TracyCZoneC( ctx, color, active ) static const struct ___tracy_source_location_data TracyConcat(__tracy_source_location,__LINE__) = { NULL, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; TracyCZoneCtx ctx = ___tracy_emit_zone_begin_callstack( &TracyConcat(__tracy_source_location,__LINE__), TRACY_CALLSTACK, active ); +# define TracyCZoneNC( ctx, name, color, active ) static const struct ___tracy_source_location_data TracyConcat(__tracy_source_location,__LINE__) = { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; TracyCZoneCtx ctx = ___tracy_emit_zone_begin_callstack( &TracyConcat(__tracy_source_location,__LINE__), TRACY_CALLSTACK, active ); +#else +# 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( &TracyConcat(__tracy_source_location,__LINE__), active ); +# define TracyCZoneN( ctx, name, active ) static const struct ___tracy_source_location_data TracyConcat(__tracy_source_location,__LINE__) = { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; TracyCZoneCtx ctx = ___tracy_emit_zone_begin( &TracyConcat(__tracy_source_location,__LINE__), active ); +# define TracyCZoneC( ctx, color, active ) static const struct ___tracy_source_location_data TracyConcat(__tracy_source_location,__LINE__) = { NULL, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; TracyCZoneCtx ctx = ___tracy_emit_zone_begin( &TracyConcat(__tracy_source_location,__LINE__), active ); +# define TracyCZoneNC( ctx, name, color, active ) static const struct ___tracy_source_location_data TracyConcat(__tracy_source_location,__LINE__) = { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; TracyCZoneCtx ctx = ___tracy_emit_zone_begin( &TracyConcat(__tracy_source_location,__LINE__), active ); +#endif + +#define TracyCZoneEnd( ctx ) ___tracy_emit_zone_end( ctx ); + +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index f0e74e60..88b147e9 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -44,6 +44,7 @@ #include "TracyScoped.hpp" #include "TracyProfiler.hpp" #include "TracyThread.hpp" +#include "../TracyC.h" #ifdef __GNUC__ #define init_order( val ) __attribute__ ((init_priority(val))) @@ -1737,4 +1738,88 @@ void Profiler::SendCallstack( int depth, uint64_t thread, const char* skipBefore } +#ifdef __cplusplus +extern "C" { +#endif + +TracyCZoneCtx ___tracy_emit_zone_begin( const struct ___tracy_source_location_data* srcloc, int active ) +{ + ___tracy_c_zone_context ctx; +#ifdef TRACY_ON_DEMAND + ctx.active = tracy::s_profiler.IsConnected(); +#else + ctx.active = active; +#endif + if( !ctx.active ) return ctx; + tracy::Magic magic; + auto& token = tracy::s_token.ptr; + auto& tail = token->get_tail_index(); + auto item = token->enqueue_begin( magic ); + tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneBegin ); +#ifdef TRACY_RDTSCP_OPT + tracy::MemWrite( &item->zoneBegin.time, tracy::Profiler::GetTime( item->zoneBegin.cpu ) ); +#else + uint32_t cpu; + tracy::MemWrite( &item->zoneBegin.time, tracy::Profiler::GetTime( cpu ) ); + tracy::MemWrite( &item->zoneBegin.cpu, cpu ); +#endif + tracy::MemWrite( &item->zoneBegin.thread, tracy::GetThreadHandle() ); + tracy::MemWrite( &item->zoneBegin.srcloc, (uint64_t)srcloc ); + tail.store( magic + 1, std::memory_order_release ); + return ctx; +} + +TracyCZoneCtx ___tracy_emit_zone_begin_callstack( const struct ___tracy_source_location_data* srcloc, int depth, int active ) +{ + ___tracy_c_zone_context ctx; +#ifdef TRACY_ON_DEMAND + ctx.active = tracy::s_profiler.IsConnected(); +#else + ctx.active = active; +#endif + if( !ctx.active ) return ctx; + const auto thread = tracy::GetThreadHandle(); + tracy::Magic magic; + auto& token = tracy::s_token.ptr; + auto& tail = token->get_tail_index(); + auto item = token->enqueue_begin( magic ); + tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneBeginCallstack ); +#ifdef TRACY_RDTSCP_OPT + tracy::MemWrite( &item->zoneBegin.time, tracy::Profiler::GetTime( item->zoneBegin.cpu ) ); +#else + uint32_t cpu; + tracy::MemWrite( &item->zoneBegin.time, tracy::Profiler::GetTime( cpu ) ); + tracy::MemWrite( &item->zoneBegin.cpu, cpu ); +#endif + tracy::MemWrite( &item->zoneBegin.thread, thread ); + tracy::MemWrite( &item->zoneBegin.srcloc, (uint64_t)srcloc ); + tail.store( magic + 1, std::memory_order_release ); + + tracy::s_profiler.SendCallstack( depth, thread ); + return ctx; +} + +void ___tracy_emit_zone_end( TracyCZoneCtx ctx ) +{ + if( !ctx.active ) return; + tracy::Magic magic; + auto& token = tracy::s_token.ptr; + auto& tail = token->get_tail_index(); + auto item = token->enqueue_begin( magic ); + tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneEnd ); +#ifdef TRACY_RDTSCP_OPT + tracy::MemWrite( &item->zoneEnd.time, tracy::Profiler::GetTime( item->zoneEnd.cpu ) ); +#else + uint32_t cpu; + tracy::MemWrite( &item->zoneEnd.time, tracy::Profiler::GetTime( cpu ) ); + tracy::MemWrite( &item->zoneEnd.cpu, cpu ); +#endif + tracy::MemWrite( &item->zoneEnd.thread, tracy::GetThreadHandle() ); + tail.store( magic + 1, std::memory_order_release ); +} + +#ifdef __cplusplus +} +#endif + #endif diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 095b6888..f3f2ec6d 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -30,8 +30,12 @@ # endif #endif -#define TracyConcat(x,y) TracyConcatIndirect(x,y) -#define TracyConcatIndirect(x,y) x##y +#ifndef TracyConcat +# define TracyConcat(x,y) TracyConcatIndirect(x,y) +#endif +#ifndef TracyConcatIndirect +# define TracyConcatIndirect(x,y) x##y +#endif namespace tracy {