Lua message sending.

This commit is contained in:
Bartosz Taudul 2017-11-07 00:28:23 +01:00
parent 9046a72f82
commit 27dd46a97d
2 changed files with 53 additions and 9 deletions

View File

@ -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!
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.
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.

View File

@ -20,6 +20,8 @@ static inline void LuaRegister( lua_State* L )
lua_setfield( L, -2, "ZoneBegin" );
lua_pushcfunction( L, detail::noop );
lua_setfield( L, -2, "ZoneEnd" );
lua_pushcfunction( L, detail::noop );
lua_setfield( L, -2, "Message" );
lua_setglobal( L, "tracy" );
}
@ -27,21 +29,41 @@ static inline void LuaRemove( char* script )
{
while( *script )
{
if( strncmp( script, "tracy.Zone", 10 ) == 0 )
if( strncmp( script, "tracy.", 6 ) == 0 )
{
if( strncmp( script + 10, "End()", 5 ) == 0 )
if( strncmp( script + 6, "Zone", 4 ) == 0 )
{
memset( script, ' ', 15 );
script += 15;
if( strncmp( script + 10, "End()", 5 ) == 0 )
{
memset( script, ' ', 15 );
script += 15;
}
else if( strncmp( script + 10, "Begin()", 7 ) == 0 )
{
memset( script, ' ', 17 );
script += 17;
}
else
{
script += 10;
}
}
else if( strncmp( script + 10, "Begin()", 7 ) == 0 )
else if( strncmp( script + 6, "Message(", 8 ) == 0 )
{
memset( script, ' ', 17 );
script += 17;
unsigned int cnt = 1;
auto end = script + 14;
while( cnt != 0 )
{
if( *end == '(' ) cnt++;
else if( *end == ')' ) cnt--;
end++;
}
memset( script, ' ', end - script );
script = end;
}
else
{
script += 10;
script += 6;
}
}
else
@ -117,6 +139,26 @@ static inline int LuaZoneEnd( lua_State* L )
return 0;
}
static inline int LuaMessage( 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<moodycamel::CanAlloc>( magic );
item->hdr.type = QueueType::Message;
item->message.time = Profiler::GetTime();
item->message.thread = GetThreadHandle();
item->message.text = (uint64_t)ptr;
tail.store( magic + 1, std::memory_order_release );
return 0;
}
}
static inline void LuaRegister( lua_State* L )
@ -126,6 +168,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::LuaMessage );
lua_setfield( L, -2, "Message" );
lua_setglobal( L, "tracy" );
}