From 65e76e1277f4e7a0683a7b4c366381dfe0d4f7c5 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Sat, 19 Nov 2022 17:27:59 +0000 Subject: [PATCH] u/trace_marker: Tracy support --- src/xrt/auxiliary/util/CMakeLists.txt | 5 ++ src/xrt/auxiliary/util/u_trace_marker.h | 93 ++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/xrt/auxiliary/util/CMakeLists.txt b/src/xrt/auxiliary/util/CMakeLists.txt index 9c3ea5284..ceb2480a4 100644 --- a/src/xrt/auxiliary/util/CMakeLists.txt +++ b/src/xrt/auxiliary/util/CMakeLists.txt @@ -113,6 +113,11 @@ if(XRT_FEATURE_TRACING AND XRT_HAVE_PERCETTO) target_link_libraries(aux_util PUBLIC percetto::percetto) endif() +# Is basically used everywhere, only used in debugging. +if(XRT_FEATURE_TRACING AND XRT_HAVE_TRACY) + target_link_libraries(aux_util PUBLIC xrt-external-tracy) +endif() + # Is basically used everywhere, so link with here. if(ANDROID) target_link_libraries(aux_util PUBLIC ${ANDROID_LOG_LIBRARY}) diff --git a/src/xrt/auxiliary/util/u_trace_marker.h b/src/xrt/auxiliary/util/u_trace_marker.h index 3d2d38c56..9633cceac 100644 --- a/src/xrt/auxiliary/util/u_trace_marker.h +++ b/src/xrt/auxiliary/util/u_trace_marker.h @@ -30,6 +30,14 @@ #include #endif +#if defined(XRT_FEATURE_TRACING) && defined(XRT_HAVE_TRACY) +#define U_TRACE_TRACY +#include "tracy/TracyC.h" +#ifdef __cplusplus +#include "tracy/Tracy.hpp" +#endif +#endif + #ifdef __cplusplus extern "C" { @@ -128,13 +136,94 @@ u_trace_marker_init(void); #define U_TRACE_TARGET_SETUP(WHICH) +/* + * + * Tracy support. + * + */ + +#elif defined(XRT_HAVE_TRACY) // && XRT_FEATURE_TRACING + +// Different wrappers for different cases. +#ifdef __cplusplus + +#define U_TRACE_FUNC(CATEGORY) ZoneScoped + +#define U_TRACE_IDENT(CATEGORY, IDENT) ZoneScopedN(#IDENT) + +#elif !defined(XRT_OS_WINDOWS) // !__cplusplus + +static inline void +u_trace_scope_cleanup(TracyCZoneCtx *ctx_ptr) +{ + TracyCZoneEnd(*ctx_ptr); +} + +#define U_TRACE_FUNC(CATEGORY) \ + static const struct ___tracy_source_location_data __func_loc = { \ + NULL, __func__, __FILE__, (uint32_t)__LINE__, 0, \ + }; \ + TracyCZoneCtx __attribute__((cleanup(u_trace_scope_cleanup))) ctx = \ + ___tracy_emit_zone_begin(&__func_loc, true); \ + (void)ctx + +#define U_TRACE_IDENT(CATEGORY, IDENT) \ + static const struct ___tracy_source_location_data __##IDENT##_loc = { \ + #IDENT, __func__, __FILE__, (uint32_t)__LINE__, 0, \ + }; \ + TracyCZoneCtx __attribute__((cleanup(u_trace_scope_cleanup))) ctx##IDENT = \ + ___tracy_emit_zone_begin(&__##IDENT##_loc, true); \ + (void)ctx##IDENT + +#else // !XRT_OS_WINDOWS && !__cplusplus + +#define U_TRACE_FUNC(CATEGORY) \ + do { \ + } while (false) + +#define U_TRACE_IDENT(CATEGORY, IDENT) \ + do { \ + } while (false) + +#endif // !XRT_OS_WINDOWS && !__cplusplus + + +#define U_TRACE_EVENT_BEGIN_ON_TRACK(CATEGORY, TRACK, TIME, NAME) \ + do { \ + } while (false) + +#define U_TRACE_EVENT_BEGIN_ON_TRACK_DATA(CATEGORY, TRACK, TIME, NAME, ...) \ + do { \ + } while (false) + +#define U_TRACE_EVENT_END_ON_TRACK(CATEGORY, TRACK, TIME) \ + do { \ + } while (false) + +#define U_TRACE_INSTANT_ON_TRACK(CATEGORY, TRACK, TIME, NAME) \ + do { \ + } while (false) + +#define U_TRACE_CATEGORY_IS_ENABLED(_) (true) // All categories are always enabled with Tracy. + +#define U_TRACE_SET_THREAD_NAME(STRING) \ + do { \ + /* To help with thread ordering and seeing when a thread is created. */ \ + TracyCZoneN(created, "created", true); \ + TracyCSetThreadName(STRING); \ + TracyCZoneEnd(created); \ + } while (false) + +#define U_TRACE_TARGET_SETUP(WHICH) + + /* * * Percetto support. * */ -#elif defined(XRT_HAVE_PERCETTO) // && XRT_FEATURE_TRACKING +#elif defined(XRT_HAVE_PERCETTO) // && XRT_FEATURE_TRACKING && !XRT_HAVE_TRACY #ifndef XRT_OS_LINUX #error "Tracing only supported on Linux" @@ -190,7 +279,7 @@ PERCETTO_TRACK_DECLARE(pa_wait); u_trace_marker_setup(WHICH); \ } -#else // !XRT_FEATURE_TRACING && !XRT_HAVE_PERCETTO +#else // !XRT_FEATURE_TRACING && !XRT_HAVE_PERCETTO && !XRT_HAVE_TRACY #error "Need to have Percetto/Perfetto"