From 68ff33d0ba11877d588004697facf718a5f95f4f Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 6 Dec 2019 00:15:46 +0100 Subject: [PATCH] Extract source location allocation functionality. --- TracyLua.hpp | 100 ++++----------------------------------- client/TracyProfiler.hpp | 40 ++++++++++++++++ 2 files changed, 48 insertions(+), 92 deletions(-) diff --git a/TracyLua.hpp b/TracyLua.hpp index b63e09b0..fd0f3822 100644 --- a/TracyLua.hpp +++ b/TracyLua.hpp @@ -201,27 +201,7 @@ static inline int LuaZoneBeginS( lua_State* L ) lua_Debug dbg; lua_getstack( L, 1, &dbg ); lua_getinfo( L, "Snl", &dbg ); - - const uint32_t line = dbg.currentline; - const auto func = dbg.name ? dbg.name : dbg.short_src; - const auto fsz = strlen( func ); - const auto ssz = strlen( dbg.source ); - - // Data layout: - // 4b payload size - // 4b color - // 4b source line - // fsz function name - // 1b null terminator - // ssz source file name - // 1b null terminator - const uint32_t sz = uint32_t( 4 + 4 + 4 + fsz + 1 + ssz + 1 ); - auto ptr = (char*)tracy_malloc( sz ); - memcpy( ptr, &sz, 4 ); - memset( ptr + 4, 0, 4 ); - memcpy( ptr + 8, &line, 4 ); - memcpy( ptr + 12, func, fsz+1 ); - memcpy( ptr + 12 + fsz + 1, dbg.source, ssz + 1 ); + const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src ); Magic magic; auto token = GetToken(); @@ -229,7 +209,7 @@ static inline int LuaZoneBeginS( lua_State* L ) auto item = token->enqueue_begin( magic ); MemWrite( &item->hdr.type, QueueType::ZoneBeginAllocSrcLocCallstack ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); - MemWrite( &item->zoneBegin.srcloc, (uint64_t)ptr ); + MemWrite( &item->zoneBegin.srcloc, srcloc ); tail.store( magic + 1, std::memory_order_release ); #ifdef TRACY_CALLSTACK @@ -254,31 +234,9 @@ static inline int LuaZoneBeginNS( lua_State* L ) lua_Debug dbg; lua_getstack( L, 1, &dbg ); lua_getinfo( L, "Snl", &dbg ); - - const uint32_t line = dbg.currentline; - const auto func = dbg.name ? dbg.name : dbg.short_src; size_t nsz; const auto name = lua_tolstring( L, 1, &nsz ); - const auto fsz = strlen( func ); - const auto ssz = strlen( dbg.source ); - - // Data layout: - // 4b payload size - // 4b color - // 4b source line - // fsz function name - // 1b null terminator - // ssz source file name - // 1b null terminator - // nsz zone name - const uint32_t sz = uint32_t( 4 + 4 + 4 + fsz + 1 + ssz + 1 + nsz ); - auto ptr = (char*)tracy_malloc( sz ); - memcpy( ptr, &sz, 4 ); - memset( ptr + 4, 0, 4 ); - memcpy( ptr + 8, &line, 4 ); - memcpy( ptr + 12, func, fsz+1 ); - memcpy( ptr + 12 + fsz + 1, dbg.source, ssz + 1 ); - memcpy( ptr + 12 + fsz + 1 + ssz + 1, name, nsz ); + const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src, name, nsz ); Magic magic; auto token = GetToken(); @@ -286,7 +244,7 @@ static inline int LuaZoneBeginNS( lua_State* L ) auto item = token->enqueue_begin( magic ); MemWrite( &item->hdr.type, QueueType::ZoneBeginAllocSrcLocCallstack ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); - MemWrite( &item->zoneBegin.srcloc, (uint64_t)ptr ); + MemWrite( &item->zoneBegin.srcloc, srcloc ); tail.store( magic + 1, std::memory_order_release ); #ifdef TRACY_CALLSTACK @@ -315,27 +273,7 @@ static inline int LuaZoneBegin( lua_State* L ) lua_Debug dbg; lua_getstack( L, 1, &dbg ); lua_getinfo( L, "Snl", &dbg ); - - const uint32_t line = dbg.currentline; - const auto func = dbg.name ? dbg.name : dbg.short_src; - const auto fsz = strlen( func ); - const auto ssz = strlen( dbg.source ); - - // Data layout: - // 4b payload size - // 4b color - // 4b source line - // fsz function name - // 1b null terminator - // ssz source file name - // 1b null terminator - const uint32_t sz = uint32_t( 4 + 4 + 4 + fsz + 1 + ssz + 1 ); - auto ptr = (char*)tracy_malloc( sz ); - memcpy( ptr, &sz, 4 ); - memset( ptr + 4, 0, 4 ); - memcpy( ptr + 8, &line, 4 ); - memcpy( ptr + 12, func, fsz+1 ); - memcpy( ptr + 12 + fsz + 1, dbg.source, ssz + 1 ); + const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src ); Magic magic; auto token = GetToken(); @@ -343,7 +281,7 @@ static inline int LuaZoneBegin( lua_State* L ) auto item = token->enqueue_begin( magic ); MemWrite( &item->hdr.type, QueueType::ZoneBeginAllocSrcLoc ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); - MemWrite( &item->zoneBegin.srcloc, (uint64_t)ptr ); + MemWrite( &item->zoneBegin.srcloc, srcloc ); tail.store( magic + 1, std::memory_order_release ); return 0; #endif @@ -364,31 +302,9 @@ static inline int LuaZoneBeginN( lua_State* L ) lua_Debug dbg; lua_getstack( L, 1, &dbg ); lua_getinfo( L, "Snl", &dbg ); - - const uint32_t line = dbg.currentline; - const auto func = dbg.name ? dbg.name : dbg.short_src; size_t nsz; const auto name = lua_tolstring( L, 1, &nsz ); - const auto fsz = strlen( func ); - const auto ssz = strlen( dbg.source ); - - // Data layout: - // 4b payload size - // 4b color - // 4b source line - // fsz function name - // 1b null terminator - // ssz source file name - // 1b null terminator - // nsz zone name - const uint32_t sz = uint32_t( 4 + 4 + 4 + fsz + 1 + ssz + 1 + nsz ); - auto ptr = (char*)tracy_malloc( sz ); - memcpy( ptr, &sz, 4 ); - memset( ptr + 4, 0, 4 ); - memcpy( ptr + 8, &line, 4 ); - memcpy( ptr + 12, func, fsz+1 ); - memcpy( ptr + 12 + fsz + 1, dbg.source, ssz + 1 ); - memcpy( ptr + 12 + fsz + 1 + ssz + 1, name, nsz ); + const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src, name, nsz ); Magic magic; auto token = GetToken(); @@ -396,7 +312,7 @@ static inline int LuaZoneBeginN( lua_State* L ) auto item = token->enqueue_begin( magic ); MemWrite( &item->hdr.type, QueueType::ZoneBeginAllocSrcLoc ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); - MemWrite( &item->zoneBegin.srcloc, (uint64_t)ptr ); + MemWrite( &item->zoneBegin.srcloc, srcloc ); tail.store( magic + 1, std::memory_order_release ); return 0; #endif diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 254e07fe..b36a36d7 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -477,6 +477,46 @@ public: void SendString( uint64_t ptr, const char* str, QueueType type ); + + // Allocated source location data layout: + // 4b payload size + // 4b color + // 4b source line + // fsz function name + // 1b null terminator + // ssz source file name + // 1b null terminator + // nsz zone name (optional) + + static tracy_force_inline uint64_t AllocSourceLocation( uint32_t line, const char* source, const char* function ) + { + const auto fsz = strlen( function ); + const auto ssz = strlen( source ); + const uint32_t sz = uint32_t( 4 + 4 + 4 + fsz + 1 + ssz + 1 ); + auto ptr = (char*)tracy_malloc( sz ); + memcpy( ptr, &sz, 4 ); + memset( ptr + 4, 0, 4 ); + memcpy( ptr + 8, &line, 4 ); + memcpy( ptr + 12, function, fsz+1 ); + memcpy( ptr + 12 + fsz + 1, source, ssz + 1 ); + return uint64_t( ptr ); + } + + static tracy_force_inline uint64_t AllocSourceLocation( uint32_t line, const char* source, const char* function, const char* name, size_t nameSz ) + { + const auto fsz = strlen( function ); + const auto ssz = strlen( source ); + const uint32_t sz = uint32_t( 4 + 4 + 4 + fsz + 1 + ssz + 1 + nameSz ); + auto ptr = (char*)tracy_malloc( sz ); + memcpy( ptr, &sz, 4 ); + memset( ptr + 4, 0, 4 ); + memcpy( ptr + 8, &line, 4 ); + memcpy( ptr + 12, function, fsz+1 ); + memcpy( ptr + 12 + fsz + 1, source, ssz + 1 ); + memcpy( ptr + 12 + fsz + 1 + ssz + 1, name, nameSz ); + return uint64_t( ptr ); + } + private: enum class DequeueStatus { DataDequeued, ConnectionLost, QueueEmpty };