Implement on-demand Lua zone capture.

This commit is contained in:
Bartosz Taudul 2018-07-12 12:53:35 +02:00
parent fbc5556ddd
commit b11695111d
2 changed files with 57 additions and 12 deletions

View File

@ -108,6 +108,8 @@ static inline void LuaRemove( char* script )
#else #else
#include <assert.h>
#include "common/TracyColor.hpp" #include "common/TracyColor.hpp"
#include "common/TracyAlign.hpp" #include "common/TracyAlign.hpp"
#include "common/TracySystem.hpp" #include "common/TracySystem.hpp"
@ -116,12 +118,21 @@ static inline void LuaRemove( char* script )
namespace tracy namespace tracy
{ {
extern thread_local uint32_t s_luaZoneCounter;
extern thread_local bool s_luaZoneActive;
namespace detail namespace detail
{ {
static inline int LuaZoneBegin( lua_State* L ) static inline int LuaZoneBegin( lua_State* L )
{ {
#ifndef TRACY_ON_DEMAND #ifdef TRACY_ON_DEMAND
const auto zoneCnt = s_luaZoneCounter++;
if( zoneCnt != 0 && !s_luaZoneActive ) return 0;
s_luaZoneActive = s_profiler.IsConnected();
if( !s_luaZoneActive ) return 0;
#endif
const uint32_t color = Color::DeepSkyBlue3; const uint32_t color = Color::DeepSkyBlue3;
lua_Debug dbg; lua_Debug dbg;
@ -164,13 +175,18 @@ static inline int LuaZoneBegin( lua_State* L )
MemWrite( &item->zoneBegin.thread, GetThreadHandle() ); MemWrite( &item->zoneBegin.thread, GetThreadHandle() );
MemWrite( &item->zoneBegin.srcloc, (uint64_t)ptr ); MemWrite( &item->zoneBegin.srcloc, (uint64_t)ptr );
tail.store( magic + 1, std::memory_order_release ); tail.store( magic + 1, std::memory_order_release );
#endif
return 0; return 0;
} }
static inline int LuaZoneBeginN( lua_State* L ) static inline int LuaZoneBeginN( lua_State* L )
{ {
#ifndef TRACY_ON_DEMAND #ifdef TRACY_ON_DEMAND
const auto zoneCnt = s_luaZoneCounter++;
if( zoneCnt != 0 && !s_luaZoneActive ) return 0;
s_luaZoneActive = s_profiler.IsConnected();
if( !s_luaZoneActive ) return 0;
#endif
const uint32_t color = Color::DeepSkyBlue3; const uint32_t color = Color::DeepSkyBlue3;
lua_Debug dbg; lua_Debug dbg;
@ -217,13 +233,22 @@ static inline int LuaZoneBeginN( lua_State* L )
MemWrite( &item->zoneBegin.thread, GetThreadHandle() ); MemWrite( &item->zoneBegin.thread, GetThreadHandle() );
MemWrite( &item->zoneBegin.srcloc, (uint64_t)ptr ); MemWrite( &item->zoneBegin.srcloc, (uint64_t)ptr );
tail.store( magic + 1, std::memory_order_release ); tail.store( magic + 1, std::memory_order_release );
#endif
return 0; return 0;
} }
static inline int LuaZoneEnd( lua_State* L ) static inline int LuaZoneEnd( lua_State* L )
{ {
#ifndef TRACY_ON_DEMAND #ifdef TRACY_ON_DEMAND
assert( s_luaZoneCounter != 0 );
s_luaZoneCounter--;
if( !s_luaZoneActive ) return 0;
if( !s_profiler.IsConnected() )
{
s_luaZoneActive = false;
return 0;
}
#endif
Magic magic; Magic magic;
auto& token = s_token.ptr; auto& token = s_token.ptr;
auto& tail = token->get_tail_index(); auto& tail = token->get_tail_index();
@ -238,13 +263,20 @@ static inline int LuaZoneEnd( lua_State* L )
#endif #endif
MemWrite( &item->zoneEnd.thread, GetThreadHandle() ); MemWrite( &item->zoneEnd.thread, GetThreadHandle() );
tail.store( magic + 1, std::memory_order_release ); tail.store( magic + 1, std::memory_order_release );
#endif
return 0; return 0;
} }
static inline int LuaZoneText( lua_State* L ) static inline int LuaZoneText( lua_State* L )
{ {
#ifndef TRACY_ON_DEMAND #ifdef TRACY_ON_DEMAND
if( !s_luaZoneActive ) return 0;
if( !s_profiler.IsConnected() )
{
s_luaZoneActive = false;
return 0;
}
#endif
auto txt = lua_tostring( L, 1 ); auto txt = lua_tostring( L, 1 );
const auto size = strlen( txt ); const auto size = strlen( txt );
@ -259,13 +291,20 @@ static inline int LuaZoneText( lua_State* L )
MemWrite( &item->zoneText.thread, GetThreadHandle() ); MemWrite( &item->zoneText.thread, GetThreadHandle() );
MemWrite( &item->zoneText.text, (uint64_t)ptr ); MemWrite( &item->zoneText.text, (uint64_t)ptr );
tail.store( magic + 1, std::memory_order_release ); tail.store( magic + 1, std::memory_order_release );
#endif
return 0; return 0;
} }
static inline int LuaZoneName( lua_State* L ) static inline int LuaZoneName( lua_State* L )
{ {
#ifndef TRACY_ON_DEMAND #ifdef TRACY_ON_DEMAND
if( !s_luaZoneActive ) return 0;
if( !s_profiler.IsConnected() )
{
s_luaZoneActive = false;
return 0;
}
#endif
auto txt = lua_tostring( L, 1 ); auto txt = lua_tostring( L, 1 );
const auto size = strlen( txt ); const auto size = strlen( txt );
@ -280,13 +319,15 @@ static inline int LuaZoneName( lua_State* L )
MemWrite( &item->zoneText.thread, GetThreadHandle() ); MemWrite( &item->zoneText.thread, GetThreadHandle() );
MemWrite( &item->zoneText.text, (uint64_t)ptr ); MemWrite( &item->zoneText.text, (uint64_t)ptr );
tail.store( magic + 1, std::memory_order_release ); tail.store( magic + 1, std::memory_order_release );
#endif
return 0; return 0;
} }
static inline int LuaMessage( lua_State* L ) static inline int LuaMessage( lua_State* L )
{ {
#ifndef TRACY_ON_DEMAND #ifdef TRACY_ON_DEMAND
if( !s_profiler.IsConnected() ) return 0;
#endif
auto txt = lua_tostring( L, 1 ); auto txt = lua_tostring( L, 1 );
const auto size = strlen( txt ); const auto size = strlen( txt );
@ -302,7 +343,6 @@ static inline int LuaMessage( lua_State* L )
MemWrite( &item->message.thread, GetThreadHandle() ); MemWrite( &item->message.thread, GetThreadHandle() );
MemWrite( &item->message.text, (uint64_t)ptr ); MemWrite( &item->message.text, (uint64_t)ptr );
tail.store( magic + 1, std::memory_order_release ); tail.store( magic + 1, std::memory_order_release );
#endif
return 0; return 0;
} }

View File

@ -179,6 +179,11 @@ struct ThreadNameData;
std::atomic<ThreadNameData*> init_order(104) s_threadNameData( nullptr ); std::atomic<ThreadNameData*> init_order(104) s_threadNameData( nullptr );
#endif #endif
#ifdef TRACY_ON_DEMAND
thread_local uint32_t init_order(104) s_luaZoneCounter( 0 );
thread_local bool init_order(104) s_luaZoneActive( false );
#endif
Profiler init_order(105) s_profiler; Profiler init_order(105) s_profiler;