TracyScoped.hpp (3705B)
1 #ifndef __TRACYSCOPED_HPP__ 2 #define __TRACYSCOPED_HPP__ 3 4 #include <stdint.h> 5 #include <string.h> 6 7 #include "../common/TracySystem.hpp" 8 #include "../common/TracyAlign.hpp" 9 #include "../common/TracyAlloc.hpp" 10 #include "TracyProfiler.hpp" 11 12 namespace tracy 13 { 14 15 class ScopedZone 16 { 17 public: 18 tracy_force_inline ScopedZone( const SourceLocationData* srcloc, bool is_active = true ) 19 #ifdef TRACY_ON_DEMAND 20 : m_active( is_active && GetProfiler().IsConnected() ) 21 , m_connectionId( GetProfiler().ConnectionId() ) 22 #else 23 : m_active( is_active ) 24 #endif 25 { 26 if( !m_active ) return; 27 Magic magic; 28 auto token = GetToken(); 29 auto& tail = token->get_tail_index(); 30 auto item = token->enqueue_begin( magic ); 31 MemWrite( &item->hdr.type, QueueType::ZoneBegin ); 32 MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); 33 MemWrite( &item->zoneBegin.srcloc, (uint64_t)srcloc ); 34 tail.store( magic + 1, std::memory_order_release ); 35 } 36 37 tracy_force_inline ScopedZone( const SourceLocationData* srcloc, int depth, bool is_active = true ) 38 #ifdef TRACY_ON_DEMAND 39 : m_active( is_active && GetProfiler().IsConnected() ) 40 , m_connectionId( GetProfiler().ConnectionId() ) 41 #else 42 : m_active( is_active ) 43 #endif 44 { 45 if( !m_active ) return; 46 Magic magic; 47 auto token = GetToken(); 48 auto& tail = token->get_tail_index(); 49 auto item = token->enqueue_begin( magic ); 50 MemWrite( &item->hdr.type, QueueType::ZoneBeginCallstack ); 51 MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); 52 MemWrite( &item->zoneBegin.srcloc, (uint64_t)srcloc ); 53 tail.store( magic + 1, std::memory_order_release ); 54 55 GetProfiler().SendCallstack( depth ); 56 } 57 58 tracy_force_inline ~ScopedZone() 59 { 60 if( !m_active ) return; 61 #ifdef TRACY_ON_DEMAND 62 if( GetProfiler().ConnectionId() != m_connectionId ) return; 63 #endif 64 Magic magic; 65 auto token = GetToken(); 66 auto& tail = token->get_tail_index(); 67 auto item = token->enqueue_begin( magic ); 68 MemWrite( &item->hdr.type, QueueType::ZoneEnd ); 69 MemWrite( &item->zoneEnd.time, Profiler::GetTime() ); 70 tail.store( magic + 1, std::memory_order_release ); 71 } 72 73 tracy_force_inline void Text( const char* txt, size_t size ) 74 { 75 if( !m_active ) return; 76 #ifdef TRACY_ON_DEMAND 77 if( GetProfiler().ConnectionId() != m_connectionId ) return; 78 #endif 79 Magic magic; 80 auto token = GetToken(); 81 auto ptr = (char*)tracy_malloc( size+1 ); 82 memcpy( ptr, txt, size ); 83 ptr[size] = '\0'; 84 auto& tail = token->get_tail_index(); 85 auto item = token->enqueue_begin( magic ); 86 MemWrite( &item->hdr.type, QueueType::ZoneText ); 87 MemWrite( &item->zoneText.text, (uint64_t)ptr ); 88 tail.store( magic + 1, std::memory_order_release ); 89 } 90 91 tracy_force_inline void Name( const char* txt, size_t size ) 92 { 93 if( !m_active ) return; 94 #ifdef TRACY_ON_DEMAND 95 if( GetProfiler().ConnectionId() != m_connectionId ) return; 96 #endif 97 Magic magic; 98 auto token = GetToken(); 99 auto ptr = (char*)tracy_malloc( size+1 ); 100 memcpy( ptr, txt, size ); 101 ptr[size] = '\0'; 102 auto& tail = token->get_tail_index(); 103 auto item = token->enqueue_begin( magic ); 104 MemWrite( &item->hdr.type, QueueType::ZoneName ); 105 MemWrite( &item->zoneText.text, (uint64_t)ptr ); 106 tail.store( magic + 1, std::memory_order_release ); 107 } 108 109 private: 110 const bool m_active; 111 112 #ifdef TRACY_ON_DEMAND 113 uint64_t m_connectionId; 114 #endif 115 }; 116 117 } 118 119 #endif