From 68f66a923eafed8f599d8b9185841bc8b89384f1 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 7 Nov 2017 00:35:15 +0100 Subject: [PATCH] Lua zone text. --- README.md | 2 +- TracyLua.hpp | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 71c70c90..492dc82e 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ Alternatively, you may want to embed the server in your application, the same wh #### Lua support -To profile Lua code using tracy, include the `tracy/TracyLua.hpp` header file in your Lua wrapper and execute `tracy::LuaRegister( lua_State* )` function to add instrumentation support. In your Lua code, add `tracy.ZoneBegin()` and `tracy.ZoneEnd()` calls to mark execution zones. Double check if you have included all return paths! Use `tracy.Message( text )` to send messages. +To profile Lua code using tracy, include the `tracy/TracyLua.hpp` header file in your Lua wrapper and execute `tracy::LuaRegister( lua_State* )` function to add instrumentation support. In your Lua code, add `tracy.ZoneBegin()` and `tracy.ZoneEnd()` calls to mark execution zones. Double check if you have included all return paths! Use `tracy.ZoneText( text )` to set zone text. Use `tracy.Message( text )` to send messages. Even if tracy is disabled, you still have to pay the no-op function call cost. To prevent that you may want to use the `tracy::LuaRemove( char* script )` function, which will replace instrumentation calls with whitespace. This function does nothing if profiler is enabled. diff --git a/TracyLua.hpp b/TracyLua.hpp index 62ed4ea3..9859da29 100644 --- a/TracyLua.hpp +++ b/TracyLua.hpp @@ -21,10 +21,24 @@ static inline void LuaRegister( lua_State* L ) lua_pushcfunction( L, detail::noop ); lua_setfield( L, -2, "ZoneEnd" ); lua_pushcfunction( L, detail::noop ); + lua_setfield( L, -2, "ZoneText" ); + lua_pushcfunction( L, detail::noop ); lua_setfield( L, -2, "Message" ); lua_setglobal( L, "tracy" ); } +static inline char* FindEnd( char* ptr ) +{ + unsigned int cnt = 1; + while( cnt != 0 ) + { + if( *ptr == '(' ) cnt++; + else if( *ptr == ')' ) cnt--; + ptr++; + } + return ptr; +} + static inline void LuaRemove( char* script ) { while( *script ) @@ -43,6 +57,12 @@ static inline void LuaRemove( char* script ) memset( script, ' ', 17 ); script += 17; } + else if( strncmp( script + 10, "Text(", 5 ) == 0 ) + { + auto end = FindEnd( script + 15 ); + memset( script, ' ', end - script ); + script = end; + } else { script += 10; @@ -50,14 +70,7 @@ static inline void LuaRemove( char* script ) } else if( strncmp( script + 6, "Message(", 8 ) == 0 ) { - unsigned int cnt = 1; - auto end = script + 14; - while( cnt != 0 ) - { - if( *end == '(' ) cnt++; - else if( *end == ')' ) cnt--; - end++; - } + auto end = FindEnd( script + 14 ); memset( script, ' ', end - script ); script = end; } @@ -139,6 +152,25 @@ static inline int LuaZoneEnd( lua_State* L ) return 0; } +static inline int LuaZoneText( lua_State* L ) +{ + auto txt = lua_tostring( L, 1 ); + const auto size = strlen( txt ); + + Magic magic; + auto& token = s_token.ptr; + auto ptr = (char*)tracy_malloc( size+1 ); + memcpy( ptr, txt, size ); + ptr[size] = '\0'; + auto& tail = token->get_tail_index(); + auto item = token->enqueue_begin( magic ); + item->hdr.type = QueueType::ZoneText; + item->zoneText.thread = GetThreadHandle(); + item->zoneText.text = (uint64_t)ptr; + tail.store( magic + 1, std::memory_order_release ); + return 0; +} + static inline int LuaMessage( lua_State* L ) { auto txt = lua_tostring( L, 1 ); @@ -168,6 +200,8 @@ static inline void LuaRegister( lua_State* L ) lua_setfield( L, -2, "ZoneBegin" ); lua_pushcfunction( L, detail::LuaZoneEnd ); lua_setfield( L, -2, "ZoneEnd" ); + lua_pushcfunction( L, detail::LuaZoneText ); + lua_setfield( L, -2, "ZoneText" ); lua_pushcfunction( L, detail::LuaMessage ); lua_setfield( L, -2, "Message" ); lua_setglobal( L, "tracy" );