| 1 | #ifndef __TRACYPROFILER_HPP__ |
| 2 | #define __TRACYPROFILER_HPP__ |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <atomic> |
| 6 | #include <stdint.h> |
| 7 | #include <string.h> |
| 8 | #include <time.h> |
| 9 | |
| 10 | #include "tracy_concurrentqueue.h" |
| 11 | #include "tracy_SPSCQueue.h" |
| 12 | #include "TracyCallstack.hpp" |
| 13 | #include "TracySysTime.hpp" |
| 14 | #include "TracyFastVector.hpp" |
| 15 | #include "../common/TracyQueue.hpp" |
| 16 | #include "../common/TracyAlign.hpp" |
| 17 | #include "../common/TracyAlloc.hpp" |
| 18 | #include "../common/TracyMutex.hpp" |
| 19 | #include "../common/TracyProtocol.hpp" |
| 20 | |
| 21 | #if defined _WIN32 |
| 22 | # include <intrin.h> |
| 23 | #endif |
| 24 | #ifdef __APPLE__ |
| 25 | # include <TargetConditionals.h> |
| 26 | # include <mach/mach_time.h> |
| 27 | #endif |
| 28 | |
| 29 | #if ( defined _WIN32 || ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 ) || ( defined TARGET_OS_IOS && TARGET_OS_IOS == 1 ) ) |
| 30 | # define TRACY_HW_TIMER |
| 31 | #endif |
| 32 | |
| 33 | #ifdef __linux__ |
| 34 | # include <signal.h> |
| 35 | #endif |
| 36 | |
| 37 | #if defined TRACY_TIMER_FALLBACK || !defined TRACY_HW_TIMER |
| 38 | # include <chrono> |
| 39 | #endif |
| 40 | |
| 41 | #ifndef TracyConcat |
| 42 | # define TracyConcat(x,y) TracyConcatIndirect(x,y) |
| 43 | #endif |
| 44 | #ifndef TracyConcatIndirect |
| 45 | # define TracyConcatIndirect(x,y) x##y |
| 46 | #endif |
| 47 | |
| 48 | namespace tracy |
| 49 | { |
| 50 | #if defined(TRACY_DELAYED_INIT) && defined(TRACY_MANUAL_LIFETIME) |
| 51 | TRACY_API void StartupProfiler(); |
| 52 | TRACY_API void ShutdownProfiler(); |
| 53 | #endif |
| 54 | |
| 55 | class GpuCtx; |
| 56 | class Profiler; |
| 57 | class Socket; |
| 58 | class UdpBroadcast; |
| 59 | |
| 60 | struct GpuCtxWrapper |
| 61 | { |
| 62 | GpuCtx* ptr; |
| 63 | }; |
| 64 | |
| 65 | TRACY_API moodycamel::ConcurrentQueue<QueueItem>::ExplicitProducer* GetToken(); |
| 66 | TRACY_API Profiler& GetProfiler(); |
| 67 | TRACY_API std::atomic<uint32_t>& GetLockCounter(); |
| 68 | TRACY_API std::atomic<uint8_t>& GetGpuCtxCounter(); |
| 69 | TRACY_API GpuCtxWrapper& GetGpuCtx(); |
| 70 | TRACY_API uint32_t GetThreadHandle(); |
| 71 | TRACY_API bool ProfilerAvailable(); |
| 72 | TRACY_API bool ProfilerAllocatorAvailable(); |
| 73 | TRACY_API int64_t GetFrequencyQpc(); |
| 74 | |
| 75 | #if defined TRACY_TIMER_FALLBACK && defined TRACY_HW_TIMER && ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 ) |
| 76 | TRACY_API bool HardwareSupportsInvariantTSC(); // check, if we need fallback scenario |
| 77 | #else |
| 78 | # if defined TRACY_HW_TIMER |
| 79 | tracy_force_inline bool HardwareSupportsInvariantTSC() |
| 80 | { |
| 81 | return true; // this is checked at startup |
| 82 | } |
| 83 | # else |
| 84 | tracy_force_inline bool HardwareSupportsInvariantTSC() |
| 85 | { |
| 86 | return false; |
| 87 | } |
| 88 | # endif |
| 89 | #endif |
| 90 | |
| 91 | |
| 92 | struct SourceLocationData |
| 93 | { |
| 94 | const char* name; |
| 95 | const char* function; |
| 96 | const char* file; |
| 97 | uint32_t line; |
| 98 | uint32_t color; |
| 99 | }; |
| 100 | |
| 101 | #ifdef TRACY_ON_DEMAND |
| 102 | struct LuaZoneState |
| 103 | { |
| 104 | uint32_t counter; |
| 105 | bool active; |
| 106 | }; |
| 107 | #endif |
| 108 | |
| 109 | |
| 110 | #define TracyLfqPrepare( _type ) \ |
| 111 | moodycamel::ConcurrentQueueDefaultTraits::index_t __magic; \ |
| 112 | auto __token = GetToken(); \ |
| 113 | auto& __tail = __token->get_tail_index(); \ |
| 114 | auto item = __token->enqueue_begin( __magic ); \ |
| 115 | MemWrite( &item->hdr.type, _type ); |
| 116 | |
| 117 | #define TracyLfqCommit \ |
| 118 | __tail.store( __magic + 1, std::memory_order_release ); |
| 119 | |
| 120 | #define TracyLfqPrepareC( _type ) \ |
| 121 | tracy::moodycamel::ConcurrentQueueDefaultTraits::index_t __magic; \ |
| 122 | auto __token = tracy::GetToken(); \ |
| 123 | auto& __tail = __token->get_tail_index(); \ |
| 124 | auto item = __token->enqueue_begin( __magic ); \ |
| 125 | tracy::MemWrite( &item->hdr.type, _type ); |
| 126 | |
| 127 | #define TracyLfqCommitC \ |
| 128 | __tail.store( __magic + 1, std::memory_order_release ); |
| 129 | |
| 130 | |
| 131 | #ifdef TRACY_FIBERS |
| 132 | # define TracyQueuePrepare( _type ) \ |
| 133 | auto item = Profiler::QueueSerial(); \ |
| 134 | MemWrite( &item->hdr.type, _type ); |
| 135 | # define TracyQueueCommit( _name ) \ |
| 136 | MemWrite( &item->_name.thread, GetThreadHandle() ); \ |
| 137 | Profiler::QueueSerialFinish(); |
| 138 | # define TracyQueuePrepareC( _type ) \ |
| 139 | auto item = tracy::Profiler::QueueSerial(); \ |
| 140 | tracy::MemWrite( &item->hdr.type, _type ); |
| 141 | # define TracyQueueCommitC( _name ) \ |
| 142 | tracy::MemWrite( &item->_name.thread, tracy::GetThreadHandle() ); \ |
| 143 | tracy::Profiler::QueueSerialFinish(); |
| 144 | #else |
| 145 | # define TracyQueuePrepare( _type ) TracyLfqPrepare( _type ) |
| 146 | # define TracyQueueCommit( _name ) TracyLfqCommit |
| 147 | # define TracyQueuePrepareC( _type ) TracyLfqPrepareC( _type ) |
| 148 | # define TracyQueueCommitC( _name ) TracyLfqCommitC |
| 149 | #endif |
| 150 | |
| 151 | |
| 152 | typedef void(*ParameterCallback)( void* data, uint32_t idx, int32_t val ); |
| 153 | typedef char*(*SourceContentsCallback)( void* data, const char* filename, size_t& size ); |
| 154 | |
| 155 | class Profiler |
| 156 | { |
| 157 | struct FrameImageQueueItem |
| 158 | { |
| 159 | void* image; |
| 160 | uint32_t frame; |
| 161 | uint16_t w; |
| 162 | uint16_t h; |
| 163 | bool flip; |
| 164 | }; |
| 165 | |
| 166 | enum class SymbolQueueItemType |
| 167 | { |
| 168 | CallstackFrame, |
| 169 | SymbolQuery, |
| 170 | ExternalName, |
| 171 | KernelCode, |
| 172 | SourceCode |
| 173 | }; |
| 174 | |
| 175 | struct SymbolQueueItem |
| 176 | { |
| 177 | SymbolQueueItemType type; |
| 178 | uint64_t ptr; |
| 179 | uint64_t extra; |
| 180 | uint32_t id; |
| 181 | }; |
| 182 | |
| 183 | public: |
| 184 | Profiler(); |
| 185 | ~Profiler(); |
| 186 | |
| 187 | void SpawnWorkerThreads(); |
| 188 | |
| 189 | static tracy_force_inline int64_t GetTime() |
| 190 | { |
| 191 | #ifdef TRACY_HW_TIMER |
| 192 | # if defined TARGET_OS_IOS && TARGET_OS_IOS == 1 |
| 193 | if( HardwareSupportsInvariantTSC() ) return mach_absolute_time(); |
| 194 | # elif defined _WIN32 |
| 195 | # ifdef TRACY_TIMER_QPC |
| 196 | return GetTimeQpc(); |
| 197 | # else |
| 198 | if( HardwareSupportsInvariantTSC() ) return int64_t( __rdtsc() ); |
| 199 | # endif |
| 200 | # elif defined __i386 || defined _M_IX86 |
| 201 | if( HardwareSupportsInvariantTSC() ) |
| 202 | { |
| 203 | uint32_t eax, edx; |
| 204 | asm volatile ( "rdtsc" : "=a" (eax), "=d" (edx) ); |
| 205 | return ( uint64_t( edx ) << 32 ) + uint64_t( eax ); |
| 206 | } |
| 207 | # elif defined __x86_64__ || defined _M_X64 |
| 208 | if( HardwareSupportsInvariantTSC() ) |
| 209 | { |
| 210 | uint64_t rax, rdx; |
| 211 | asm volatile ( "rdtsc" : "=a" (rax), "=d" (rdx) ); |
| 212 | return (int64_t)(( rdx << 32 ) + rax); |
| 213 | } |
| 214 | # else |
| 215 | # error "TRACY_HW_TIMER detection logic needs fixing" |
| 216 | # endif |
| 217 | #endif |
| 218 | |
| 219 | #if !defined TRACY_HW_TIMER || defined TRACY_TIMER_FALLBACK |
| 220 | # if defined __linux__ && defined CLOCK_MONOTONIC_RAW |
| 221 | struct timespec ts; |
| 222 | clock_gettime( CLOCK_MONOTONIC_RAW, &ts ); |
| 223 | return int64_t( ts.tv_sec ) * 1000000000ll + int64_t( ts.tv_nsec ); |
| 224 | # else |
| 225 | return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count(); |
| 226 | # endif |
| 227 | #endif |
| 228 | |
| 229 | #if !defined TRACY_TIMER_FALLBACK |
| 230 | return 0; // unreachable branch |
| 231 | #endif |
| 232 | } |
| 233 | |
| 234 | tracy_force_inline uint32_t GetNextZoneId() |
| 235 | { |
| 236 | return m_zoneId.fetch_add( 1, std::memory_order_relaxed ); |
| 237 | } |
| 238 | |
| 239 | static tracy_force_inline QueueItem* QueueSerial() |
| 240 | { |
| 241 | auto& p = GetProfiler(); |
| 242 | p.m_serialLock.lock(); |
| 243 | return p.m_serialQueue.prepare_next(); |
| 244 | } |
| 245 | |
| 246 | static tracy_force_inline QueueItem* QueueSerialCallstack( void* ptr ) |
| 247 | { |
| 248 | auto& p = GetProfiler(); |
| 249 | p.m_serialLock.lock(); |
| 250 | p.SendCallstackSerial( ptr ); |
| 251 | return p.m_serialQueue.prepare_next(); |
| 252 | } |
| 253 | |
| 254 | static tracy_force_inline void QueueSerialFinish() |
| 255 | { |
| 256 | auto& p = GetProfiler(); |
| 257 | p.m_serialQueue.commit_next(); |
| 258 | p.m_serialLock.unlock(); |
| 259 | } |
| 260 | |
| 261 | static tracy_force_inline void SendFrameMark( const char* name ) |
| 262 | { |
| 263 | if( !name ) GetProfiler().m_frameCount.fetch_add( 1, std::memory_order_relaxed ); |
| 264 | #ifdef TRACY_ON_DEMAND |
| 265 | if( !GetProfiler().IsConnected() ) return; |
| 266 | #endif |
| 267 | auto item = QueueSerial(); |
| 268 | MemWrite( &item->hdr.type, QueueType::FrameMarkMsg ); |
| 269 | MemWrite( &item->frameMark.time, GetTime() ); |
| 270 | MemWrite( &item->frameMark.name, uint64_t( name ) ); |
| 271 | QueueSerialFinish(); |
| 272 | } |
| 273 | |
| 274 | static tracy_force_inline void SendFrameMark( const char* name, QueueType type ) |
| 275 | { |
| 276 | assert( type == QueueType::FrameMarkMsgStart || type == QueueType::FrameMarkMsgEnd ); |
| 277 | #ifdef TRACY_ON_DEMAND |
| 278 | if( !GetProfiler().IsConnected() ) return; |
| 279 | #endif |
| 280 | auto item = QueueSerial(); |
| 281 | MemWrite( &item->hdr.type, type ); |
| 282 | MemWrite( &item->frameMark.time, GetTime() ); |
| 283 | MemWrite( &item->frameMark.name, uint64_t( name ) ); |
| 284 | QueueSerialFinish(); |
| 285 | } |
| 286 | |
| 287 | static tracy_force_inline void SendFrameImage( const void* image, uint16_t w, uint16_t h, uint8_t offset, bool flip ) |
| 288 | { |
| 289 | #ifndef TRACY_NO_FRAME_IMAGE |
| 290 | auto& profiler = GetProfiler(); |
| 291 | assert( profiler.m_frameCount.load( std::memory_order_relaxed ) < std::numeric_limits<uint32_t>::max() ); |
| 292 | # ifdef TRACY_ON_DEMAND |
| 293 | if( !profiler.IsConnected() ) return; |
| 294 | # endif |
| 295 | const auto sz = size_t( w ) * size_t( h ) * 4; |
| 296 | auto ptr = (char*)tracy_malloc( sz ); |
| 297 | memcpy( ptr, image, sz ); |
| 298 | |
| 299 | profiler.m_fiLock.lock(); |
| 300 | auto fi = profiler.m_fiQueue.prepare_next(); |
| 301 | fi->image = ptr; |
| 302 | fi->frame = uint32_t( profiler.m_frameCount.load( std::memory_order_relaxed ) - offset ); |
| 303 | fi->w = w; |
| 304 | fi->h = h; |
| 305 | fi->flip = flip; |
| 306 | profiler.m_fiQueue.commit_next(); |
| 307 | profiler.m_fiLock.unlock(); |
| 308 | #endif |
| 309 | } |
| 310 | |
| 311 | static tracy_force_inline void PlotData( const char* name, int64_t val ) |
| 312 | { |
| 313 | #ifdef TRACY_ON_DEMAND |
| 314 | if( !GetProfiler().IsConnected() ) return; |
| 315 | #endif |
| 316 | TracyLfqPrepare( QueueType::PlotDataInt ); |
| 317 | MemWrite( &item->plotDataInt.name, (uint64_t)name ); |
| 318 | MemWrite( &item->plotDataInt.time, GetTime() ); |
| 319 | MemWrite( &item->plotDataInt.val, val ); |
| 320 | TracyLfqCommit; |
| 321 | } |
| 322 | |
| 323 | static tracy_force_inline void PlotData( const char* name, float val ) |
| 324 | { |
| 325 | #ifdef TRACY_ON_DEMAND |
| 326 | if( !GetProfiler().IsConnected() ) return; |
| 327 | #endif |
| 328 | TracyLfqPrepare( QueueType::PlotDataFloat ); |
| 329 | MemWrite( &item->plotDataFloat.name, (uint64_t)name ); |
| 330 | MemWrite( &item->plotDataFloat.time, GetTime() ); |
| 331 | MemWrite( &item->plotDataFloat.val, val ); |
| 332 | TracyLfqCommit; |
| 333 | } |
| 334 | |
| 335 | static tracy_force_inline void PlotData( const char* name, double val ) |
| 336 | { |
| 337 | #ifdef TRACY_ON_DEMAND |
| 338 | if( !GetProfiler().IsConnected() ) return; |
| 339 | #endif |
| 340 | TracyLfqPrepare( QueueType::PlotDataDouble ); |
| 341 | MemWrite( &item->plotDataDouble.name, (uint64_t)name ); |
| 342 | MemWrite( &item->plotDataDouble.time, GetTime() ); |
| 343 | MemWrite( &item->plotDataDouble.val, val ); |
| 344 | TracyLfqCommit; |
| 345 | } |
| 346 | |
| 347 | static tracy_force_inline void ConfigurePlot( const char* name, PlotFormatType type, bool step, bool fill, uint32_t color ) |
| 348 | { |
| 349 | TracyLfqPrepare( QueueType::PlotConfig ); |
| 350 | MemWrite( &item->plotConfig.name, (uint64_t)name ); |
| 351 | MemWrite( &item->plotConfig.type, (uint8_t)type ); |
| 352 | MemWrite( &item->plotConfig.step, (uint8_t)step ); |
| 353 | MemWrite( &item->plotConfig.fill, (uint8_t)fill ); |
| 354 | MemWrite( &item->plotConfig.color, color ); |
| 355 | |
| 356 | #ifdef TRACY_ON_DEMAND |
| 357 | GetProfiler().DeferItem( *item ); |
| 358 | #endif |
| 359 | |
| 360 | TracyLfqCommit; |
| 361 | } |
| 362 | |
| 363 | static tracy_force_inline void Message( const char* txt, size_t size, int callstack ) |
| 364 | { |
| 365 | assert( size < std::numeric_limits<uint16_t>::max() ); |
| 366 | #ifdef TRACY_ON_DEMAND |
| 367 | if( !GetProfiler().IsConnected() ) return; |
| 368 | #endif |
| 369 | if( callstack != 0 ) |
| 370 | { |
| 371 | tracy::GetProfiler().SendCallstack( callstack ); |
| 372 | } |
| 373 | |
| 374 | auto ptr = (char*)tracy_malloc( size ); |
| 375 | memcpy( ptr, txt, size ); |
| 376 | |
| 377 | TracyQueuePrepare( callstack == 0 ? QueueType::Message : QueueType::MessageCallstack ); |
| 378 | MemWrite( &item->messageFat.time, GetTime() ); |
| 379 | MemWrite( &item->messageFat.text, (uint64_t)ptr ); |
| 380 | MemWrite( &item->messageFat.size, (uint16_t)size ); |
| 381 | TracyQueueCommit( messageFatThread ); |
| 382 | } |
| 383 | |
| 384 | static tracy_force_inline void Message( const char* txt, int callstack ) |
| 385 | { |
| 386 | #ifdef TRACY_ON_DEMAND |
| 387 | if( !GetProfiler().IsConnected() ) return; |
| 388 | #endif |
| 389 | if( callstack != 0 ) |
| 390 | { |
| 391 | tracy::GetProfiler().SendCallstack( callstack ); |
| 392 | } |
| 393 | |
| 394 | TracyQueuePrepare( callstack == 0 ? QueueType::MessageLiteral : QueueType::MessageLiteralCallstack ); |
| 395 | MemWrite( &item->messageLiteral.time, GetTime() ); |
| 396 | MemWrite( &item->messageLiteral.text, (uint64_t)txt ); |
| 397 | TracyQueueCommit( messageLiteralThread ); |
| 398 | } |
| 399 | |
| 400 | static tracy_force_inline void MessageColor( const char* txt, size_t size, uint32_t color, int callstack ) |
| 401 | { |
| 402 | assert( size < std::numeric_limits<uint16_t>::max() ); |
| 403 | #ifdef TRACY_ON_DEMAND |
| 404 | if( !GetProfiler().IsConnected() ) return; |
| 405 | #endif |
| 406 | if( callstack != 0 ) |
| 407 | { |
| 408 | tracy::GetProfiler().SendCallstack( callstack ); |
| 409 | } |
| 410 | |
| 411 | auto ptr = (char*)tracy_malloc( size ); |
| 412 | memcpy( ptr, txt, size ); |
| 413 | |
| 414 | TracyQueuePrepare( callstack == 0 ? QueueType::MessageColor : QueueType::MessageColorCallstack ); |
| 415 | MemWrite( &item->messageColorFat.time, GetTime() ); |
| 416 | MemWrite( &item->messageColorFat.text, (uint64_t)ptr ); |
| 417 | MemWrite( &item->messageColorFat.b, uint8_t( ( color ) & 0xFF ) ); |
| 418 | MemWrite( &item->messageColorFat.g, uint8_t( ( color >> 8 ) & 0xFF ) ); |
| 419 | MemWrite( &item->messageColorFat.r, uint8_t( ( color >> 16 ) & 0xFF ) ); |
| 420 | MemWrite( &item->messageColorFat.size, (uint16_t)size ); |
| 421 | TracyQueueCommit( messageColorFatThread ); |
| 422 | } |
| 423 | |
| 424 | static tracy_force_inline void MessageColor( const char* txt, uint32_t color, int callstack ) |
| 425 | { |
| 426 | #ifdef TRACY_ON_DEMAND |
| 427 | if( !GetProfiler().IsConnected() ) return; |
| 428 | #endif |
| 429 | if( callstack != 0 ) |
| 430 | { |
| 431 | tracy::GetProfiler().SendCallstack( callstack ); |
| 432 | } |
| 433 | |
| 434 | TracyQueuePrepare( callstack == 0 ? QueueType::MessageLiteralColor : QueueType::MessageLiteralColorCallstack ); |
| 435 | MemWrite( &item->messageColorLiteral.time, GetTime() ); |
| 436 | MemWrite( &item->messageColorLiteral.text, (uint64_t)txt ); |
| 437 | MemWrite( &item->messageColorLiteral.b, uint8_t( ( color ) & 0xFF ) ); |
| 438 | MemWrite( &item->messageColorLiteral.g, uint8_t( ( color >> 8 ) & 0xFF ) ); |
| 439 | MemWrite( &item->messageColorLiteral.r, uint8_t( ( color >> 16 ) & 0xFF ) ); |
| 440 | TracyQueueCommit( messageColorLiteralThread ); |
| 441 | } |
| 442 | |
| 443 | static tracy_force_inline void MessageAppInfo( const char* txt, size_t size ) |
| 444 | { |
| 445 | assert( size < std::numeric_limits<uint16_t>::max() ); |
| 446 | auto ptr = (char*)tracy_malloc( size ); |
| 447 | memcpy( ptr, txt, size ); |
| 448 | TracyLfqPrepare( QueueType::MessageAppInfo ); |
| 449 | MemWrite( &item->messageFat.time, GetTime() ); |
| 450 | MemWrite( &item->messageFat.text, (uint64_t)ptr ); |
| 451 | MemWrite( &item->messageFat.size, (uint16_t)size ); |
| 452 | |
| 453 | #ifdef TRACY_ON_DEMAND |
| 454 | GetProfiler().DeferItem( *item ); |
| 455 | #endif |
| 456 | |
| 457 | TracyLfqCommit; |
| 458 | } |
| 459 | |
| 460 | static tracy_force_inline void MemAlloc( const void* ptr, size_t size, bool secure ) |
| 461 | { |
| 462 | if( secure && !ProfilerAvailable() ) return; |
| 463 | #ifdef TRACY_ON_DEMAND |
| 464 | if( !GetProfiler().IsConnected() ) return; |
| 465 | #endif |
| 466 | const auto thread = GetThreadHandle(); |
| 467 | |
| 468 | GetProfiler().m_serialLock.lock(); |
| 469 | SendMemAlloc( QueueType::MemAlloc, thread, ptr, size ); |
| 470 | GetProfiler().m_serialLock.unlock(); |
| 471 | } |
| 472 | |
| 473 | static tracy_force_inline void MemFree( const void* ptr, bool secure ) |
| 474 | { |
| 475 | if( secure && !ProfilerAvailable() ) return; |
| 476 | #ifdef TRACY_ON_DEMAND |
| 477 | if( !GetProfiler().IsConnected() ) return; |
| 478 | #endif |
| 479 | const auto thread = GetThreadHandle(); |
| 480 | |
| 481 | GetProfiler().m_serialLock.lock(); |
| 482 | SendMemFree( QueueType::MemFree, thread, ptr ); |
| 483 | GetProfiler().m_serialLock.unlock(); |
| 484 | } |
| 485 | |
| 486 | static tracy_force_inline void MemAllocCallstack( const void* ptr, size_t size, int depth, bool secure ) |
| 487 | { |
| 488 | if( secure && !ProfilerAvailable() ) return; |
| 489 | #ifdef TRACY_HAS_CALLSTACK |
| 490 | auto& profiler = GetProfiler(); |
| 491 | # ifdef TRACY_ON_DEMAND |
| 492 | if( !profiler.IsConnected() ) return; |
| 493 | # endif |
| 494 | const auto thread = GetThreadHandle(); |
| 495 | |
| 496 | auto callstack = Callstack( depth ); |
| 497 | |
| 498 | profiler.m_serialLock.lock(); |
| 499 | SendCallstackSerial( callstack ); |
| 500 | SendMemAlloc( QueueType::MemAllocCallstack, thread, ptr, size ); |
| 501 | profiler.m_serialLock.unlock(); |
| 502 | #else |
| 503 | static_cast<void>(depth); // unused |
| 504 | MemAlloc( ptr, size, secure ); |
| 505 | #endif |
| 506 | } |
| 507 | |
| 508 | static tracy_force_inline void MemFreeCallstack( const void* ptr, int depth, bool secure ) |
| 509 | { |
| 510 | if( secure && !ProfilerAvailable() ) return; |
| 511 | if( !ProfilerAllocatorAvailable() ) |
| 512 | { |
| 513 | MemFree( ptr, secure ); |
| 514 | return; |
| 515 | } |
| 516 | #ifdef TRACY_HAS_CALLSTACK |
| 517 | auto& profiler = GetProfiler(); |
| 518 | # ifdef TRACY_ON_DEMAND |
| 519 | if( !profiler.IsConnected() ) return; |
| 520 | # endif |
| 521 | const auto thread = GetThreadHandle(); |
| 522 | |
| 523 | auto callstack = Callstack( depth ); |
| 524 | |
| 525 | profiler.m_serialLock.lock(); |
| 526 | SendCallstackSerial( callstack ); |
| 527 | SendMemFree( QueueType::MemFreeCallstack, thread, ptr ); |
| 528 | profiler.m_serialLock.unlock(); |
| 529 | #else |
| 530 | static_cast<void>(depth); // unused |
| 531 | MemFree( ptr, secure ); |
| 532 | #endif |
| 533 | } |
| 534 | |
| 535 | static tracy_force_inline void MemAllocNamed( const void* ptr, size_t size, bool secure, const char* name ) |
| 536 | { |
| 537 | if( secure && !ProfilerAvailable() ) return; |
| 538 | #ifdef TRACY_ON_DEMAND |
| 539 | if( !GetProfiler().IsConnected() ) return; |
| 540 | #endif |
| 541 | const auto thread = GetThreadHandle(); |
| 542 | |
| 543 | GetProfiler().m_serialLock.lock(); |
| 544 | SendMemName( name ); |
| 545 | SendMemAlloc( QueueType::MemAllocNamed, thread, ptr, size ); |
| 546 | GetProfiler().m_serialLock.unlock(); |
| 547 | } |
| 548 | |
| 549 | static tracy_force_inline void MemFreeNamed( const void* ptr, bool secure, const char* name ) |
| 550 | { |
| 551 | if( secure && !ProfilerAvailable() ) return; |
| 552 | #ifdef TRACY_ON_DEMAND |
| 553 | if( !GetProfiler().IsConnected() ) return; |
| 554 | #endif |
| 555 | const auto thread = GetThreadHandle(); |
| 556 | |
| 557 | GetProfiler().m_serialLock.lock(); |
| 558 | SendMemName( name ); |
| 559 | SendMemFree( QueueType::MemFreeNamed, thread, ptr ); |
| 560 | GetProfiler().m_serialLock.unlock(); |
| 561 | } |
| 562 | |
| 563 | static tracy_force_inline void MemAllocCallstackNamed( const void* ptr, size_t size, int depth, bool secure, const char* name ) |
| 564 | { |
| 565 | if( secure && !ProfilerAvailable() ) return; |
| 566 | #ifdef TRACY_HAS_CALLSTACK |
| 567 | auto& profiler = GetProfiler(); |
| 568 | # ifdef TRACY_ON_DEMAND |
| 569 | if( !profiler.IsConnected() ) return; |
| 570 | # endif |
| 571 | const auto thread = GetThreadHandle(); |
| 572 | |
| 573 | auto callstack = Callstack( depth ); |
| 574 | |
| 575 | profiler.m_serialLock.lock(); |
| 576 | SendCallstackSerial( callstack ); |
| 577 | SendMemName( name ); |
| 578 | SendMemAlloc( QueueType::MemAllocCallstackNamed, thread, ptr, size ); |
| 579 | profiler.m_serialLock.unlock(); |
| 580 | #else |
| 581 | static_cast<void>(depth); // unused |
| 582 | static_cast<void>(name); // unused |
| 583 | MemAlloc( ptr, size, secure ); |
| 584 | #endif |
| 585 | } |
| 586 | |
| 587 | static tracy_force_inline void MemFreeCallstackNamed( const void* ptr, int depth, bool secure, const char* name ) |
| 588 | { |
| 589 | if( secure && !ProfilerAvailable() ) return; |
| 590 | #ifdef TRACY_HAS_CALLSTACK |
| 591 | auto& profiler = GetProfiler(); |
| 592 | # ifdef TRACY_ON_DEMAND |
| 593 | if( !profiler.IsConnected() ) return; |
| 594 | # endif |
| 595 | const auto thread = GetThreadHandle(); |
| 596 | |
| 597 | auto callstack = Callstack( depth ); |
| 598 | |
| 599 | profiler.m_serialLock.lock(); |
| 600 | SendCallstackSerial( callstack ); |
| 601 | SendMemName( name ); |
| 602 | SendMemFree( QueueType::MemFreeCallstackNamed, thread, ptr ); |
| 603 | profiler.m_serialLock.unlock(); |
| 604 | #else |
| 605 | static_cast<void>(depth); // unused |
| 606 | static_cast<void>(name); // unused |
| 607 | MemFree( ptr, secure ); |
| 608 | #endif |
| 609 | } |
| 610 | |
| 611 | static tracy_force_inline void SendCallstack( int depth ) |
| 612 | { |
| 613 | #ifdef TRACY_HAS_CALLSTACK |
| 614 | auto ptr = Callstack( depth ); |
| 615 | TracyQueuePrepare( QueueType::Callstack ); |
| 616 | MemWrite( &item->callstackFat.ptr, (uint64_t)ptr ); |
| 617 | TracyQueueCommit( callstackFatThread ); |
| 618 | #else |
| 619 | static_cast<void>(depth); // unused |
| 620 | #endif |
| 621 | } |
| 622 | |
| 623 | static tracy_force_inline void ParameterRegister( ParameterCallback cb, void* data ) |
| 624 | { |
| 625 | auto& profiler = GetProfiler(); |
| 626 | profiler.m_paramCallback = cb; |
| 627 | profiler.m_paramCallbackData = data; |
| 628 | } |
| 629 | |
| 630 | static tracy_force_inline void ParameterSetup( uint32_t idx, const char* name, bool isBool, int32_t val ) |
| 631 | { |
| 632 | TracyLfqPrepare( QueueType::ParamSetup ); |
| 633 | tracy::MemWrite( &item->paramSetup.idx, idx ); |
| 634 | tracy::MemWrite( &item->paramSetup.name, (uint64_t)name ); |
| 635 | tracy::MemWrite( &item->paramSetup.isBool, (uint8_t)isBool ); |
| 636 | tracy::MemWrite( &item->paramSetup.val, val ); |
| 637 | |
| 638 | #ifdef TRACY_ON_DEMAND |
| 639 | GetProfiler().DeferItem( *item ); |
| 640 | #endif |
| 641 | |
| 642 | TracyLfqCommit; |
| 643 | } |
| 644 | |
| 645 | static tracy_force_inline void SourceCallbackRegister( SourceContentsCallback cb, void* data ) |
| 646 | { |
| 647 | auto& profiler = GetProfiler(); |
| 648 | profiler.m_sourceCallback = cb; |
| 649 | profiler.m_sourceCallbackData = data; |
| 650 | } |
| 651 | |
| 652 | #ifdef TRACY_FIBERS |
| 653 | static tracy_force_inline void EnterFiber( const char* fiber ) |
| 654 | { |
| 655 | TracyQueuePrepare( QueueType::FiberEnter ); |
| 656 | MemWrite( &item->fiberEnter.time, GetTime() ); |
| 657 | MemWrite( &item->fiberEnter.fiber, (uint64_t)fiber ); |
| 658 | TracyQueueCommit( fiberEnter ); |
| 659 | } |
| 660 | |
| 661 | static tracy_force_inline void LeaveFiber() |
| 662 | { |
| 663 | TracyQueuePrepare( QueueType::FiberLeave ); |
| 664 | MemWrite( &item->fiberLeave.time, GetTime() ); |
| 665 | TracyQueueCommit( fiberLeave ); |
| 666 | } |
| 667 | #endif |
| 668 | |
| 669 | void SendCallstack( int depth, const char* skipBefore ); |
| 670 | static void CutCallstack( void* callstack, const char* skipBefore ); |
| 671 | |
| 672 | static bool ShouldExit(); |
| 673 | |
| 674 | tracy_force_inline bool IsConnected() const |
| 675 | { |
| 676 | return m_isConnected.load( std::memory_order_acquire ); |
| 677 | } |
| 678 | |
| 679 | #ifdef TRACY_ON_DEMAND |
| 680 | tracy_force_inline uint64_t ConnectionId() const |
| 681 | { |
| 682 | return m_connectionId.load( std::memory_order_acquire ); |
| 683 | } |
| 684 | |
| 685 | tracy_force_inline void DeferItem( const QueueItem& item ) |
| 686 | { |
| 687 | m_deferredLock.lock(); |
| 688 | auto dst = m_deferredQueue.push_next(); |
| 689 | memcpy( dst, &item, sizeof( item ) ); |
| 690 | m_deferredLock.unlock(); |
| 691 | } |
| 692 | #endif |
| 693 | |
| 694 | void RequestShutdown() { m_shutdown.store( true, std::memory_order_relaxed ); m_shutdownManual.store( true, std::memory_order_relaxed ); } |
| 695 | bool HasShutdownFinished() const { return m_shutdownFinished.load( std::memory_order_relaxed ); } |
| 696 | |
| 697 | void SendString( uint64_t str, const char* ptr, QueueType type ) { SendString( str, ptr, strlen( ptr ), type ); } |
| 698 | void SendString( uint64_t str, const char* ptr, size_t len, QueueType type ); |
| 699 | void SendSingleString( const char* ptr ) { SendSingleString( ptr, strlen( ptr ) ); } |
| 700 | void SendSingleString( const char* ptr, size_t len ); |
| 701 | void SendSecondString( const char* ptr ) { SendSecondString( ptr, strlen( ptr ) ); } |
| 702 | void SendSecondString( const char* ptr, size_t len ); |
| 703 | |
| 704 | |
| 705 | // Allocated source location data layout: |
| 706 | // 2b payload size |
| 707 | // 4b color |
| 708 | // 4b source line |
| 709 | // fsz function name |
| 710 | // 1b null terminator |
| 711 | // ssz source file name |
| 712 | // 1b null terminator |
| 713 | // nsz zone name (optional) |
| 714 | |
| 715 | static tracy_force_inline uint64_t AllocSourceLocation( uint32_t line, const char* source, const char* function ) |
| 716 | { |
| 717 | return AllocSourceLocation( line, source, function, nullptr, 0 ); |
| 718 | } |
| 719 | |
| 720 | static tracy_force_inline uint64_t AllocSourceLocation( uint32_t line, const char* source, const char* function, const char* name, size_t nameSz ) |
| 721 | { |
| 722 | return AllocSourceLocation( line, source, strlen(source), function, strlen(function), name, nameSz ); |
| 723 | } |
| 724 | |
| 725 | static tracy_force_inline uint64_t AllocSourceLocation( uint32_t line, const char* source, size_t sourceSz, const char* function, size_t functionSz ) |
| 726 | { |
| 727 | return AllocSourceLocation( line, source, sourceSz, function, functionSz, nullptr, 0 ); |
| 728 | } |
| 729 | |
| 730 | static tracy_force_inline uint64_t AllocSourceLocation( uint32_t line, const char* source, size_t sourceSz, const char* function, size_t functionSz, const char* name, size_t nameSz ) |
| 731 | { |
| 732 | const auto sz32 = uint32_t( 2 + 4 + 4 + functionSz + 1 + sourceSz + 1 + nameSz ); |
| 733 | assert( sz32 <= std::numeric_limits<uint16_t>::max() ); |
| 734 | const auto sz = uint16_t( sz32 ); |
| 735 | auto ptr = (char*)tracy_malloc( sz ); |
| 736 | memcpy( ptr, &sz, 2 ); |
| 737 | memset( ptr + 2, 0, 4 ); |
| 738 | memcpy( ptr + 6, &line, 4 ); |
| 739 | memcpy( ptr + 10, function, functionSz ); |
| 740 | ptr[10 + functionSz] = '\0'; |
| 741 | memcpy( ptr + 10 + functionSz + 1, source, sourceSz ); |
| 742 | ptr[10 + functionSz + 1 + sourceSz] = '\0'; |
| 743 | if( nameSz != 0 ) |
| 744 | { |
| 745 | memcpy( ptr + 10 + functionSz + 1 + sourceSz + 1, name, nameSz ); |
| 746 | } |
| 747 | return uint64_t( ptr ); |
| 748 | } |
| 749 | |
| 750 | private: |
| 751 | enum class DequeueStatus { DataDequeued, ConnectionLost, QueueEmpty }; |
| 752 | enum class ThreadCtxStatus { Same, Changed, ConnectionLost }; |
| 753 | |
| 754 | static void LaunchWorker( void* ptr ) { ((Profiler*)ptr)->Worker(); } |
| 755 | void Worker(); |
| 756 | |
| 757 | #ifndef TRACY_NO_FRAME_IMAGE |
| 758 | static void LaunchCompressWorker( void* ptr ) { ((Profiler*)ptr)->CompressWorker(); } |
| 759 | void CompressWorker(); |
| 760 | #endif |
| 761 | |
| 762 | #ifdef TRACY_HAS_CALLSTACK |
| 763 | static void LaunchSymbolWorker( void* ptr ) { ((Profiler*)ptr)->SymbolWorker(); } |
| 764 | void SymbolWorker(); |
| 765 | void HandleSymbolQueueItem( const SymbolQueueItem& si ); |
| 766 | #endif |
| 767 | |
| 768 | void ClearQueues( tracy::moodycamel::ConsumerToken& token ); |
| 769 | void ClearSerial(); |
| 770 | DequeueStatus Dequeue( tracy::moodycamel::ConsumerToken& token ); |
| 771 | DequeueStatus DequeueContextSwitches( tracy::moodycamel::ConsumerToken& token, int64_t& timeStop ); |
| 772 | DequeueStatus DequeueSerial(); |
| 773 | ThreadCtxStatus ThreadCtxCheck( uint32_t threadId ); |
| 774 | bool CommitData(); |
| 775 | |
| 776 | tracy_force_inline bool AppendData( const void* data, size_t len ) |
| 777 | { |
| 778 | const auto ret = NeedDataSize( len ); |
| 779 | AppendDataUnsafe( data, len ); |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | tracy_force_inline bool NeedDataSize( size_t len ) |
| 784 | { |
| 785 | assert( len <= TargetFrameSize ); |
| 786 | bool ret = true; |
| 787 | if( m_bufferOffset - m_bufferStart + (int)len > TargetFrameSize ) |
| 788 | { |
| 789 | ret = CommitData(); |
| 790 | } |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | tracy_force_inline void AppendDataUnsafe( const void* data, size_t len ) |
| 795 | { |
| 796 | memcpy( m_buffer + m_bufferOffset, data, len ); |
| 797 | m_bufferOffset += int( len ); |
| 798 | } |
| 799 | |
| 800 | bool SendData( const char* data, size_t len ); |
| 801 | void SendLongString( uint64_t ptr, const char* str, size_t len, QueueType type ); |
| 802 | void SendSourceLocation( uint64_t ptr ); |
| 803 | void SendSourceLocationPayload( uint64_t ptr ); |
| 804 | void SendCallstackPayload( uint64_t ptr ); |
| 805 | void SendCallstackPayload64( uint64_t ptr ); |
| 806 | void SendCallstackAlloc( uint64_t ptr ); |
| 807 | |
| 808 | void QueueCallstackFrame( uint64_t ptr ); |
| 809 | void QueueSymbolQuery( uint64_t symbol ); |
| 810 | void QueueExternalName( uint64_t ptr ); |
| 811 | void QueueKernelCode( uint64_t symbol, uint32_t size ); |
| 812 | void QueueSourceCodeQuery( uint32_t id ); |
| 813 | |
| 814 | bool HandleServerQuery(); |
| 815 | void HandleDisconnect(); |
| 816 | void HandleParameter( uint64_t payload ); |
| 817 | void HandleSymbolCodeQuery( uint64_t symbol, uint32_t size ); |
| 818 | void HandleSourceCodeQuery( char* data, char* image, uint32_t id ); |
| 819 | |
| 820 | void AckServerQuery(); |
| 821 | void AckSymbolCodeNotAvailable(); |
| 822 | |
| 823 | void CalibrateTimer(); |
| 824 | void CalibrateDelay(); |
| 825 | void ReportTopology(); |
| 826 | |
| 827 | static tracy_force_inline void SendCallstackSerial( void* ptr ) |
| 828 | { |
| 829 | #ifdef TRACY_HAS_CALLSTACK |
| 830 | auto item = GetProfiler().m_serialQueue.prepare_next(); |
| 831 | MemWrite( &item->hdr.type, QueueType::CallstackSerial ); |
| 832 | MemWrite( &item->callstackFat.ptr, (uint64_t)ptr ); |
| 833 | GetProfiler().m_serialQueue.commit_next(); |
| 834 | #else |
| 835 | static_cast<void>(ptr); // unused |
| 836 | #endif |
| 837 | } |
| 838 | |
| 839 | static tracy_force_inline void SendMemAlloc( QueueType type, const uint32_t thread, const void* ptr, size_t size ) |
| 840 | { |
| 841 | assert( type == QueueType::MemAlloc || type == QueueType::MemAllocCallstack || type == QueueType::MemAllocNamed || type == QueueType::MemAllocCallstackNamed ); |
| 842 | |
| 843 | auto item = GetProfiler().m_serialQueue.prepare_next(); |
| 844 | MemWrite( &item->hdr.type, type ); |
| 845 | MemWrite( &item->memAlloc.time, GetTime() ); |
| 846 | MemWrite( &item->memAlloc.thread, thread ); |
| 847 | MemWrite( &item->memAlloc.ptr, (uint64_t)ptr ); |
| 848 | if( compile_time_condition<sizeof( size ) == 4>::value ) |
| 849 | { |
| 850 | memcpy( &item->memAlloc.size, &size, 4 ); |
| 851 | memset( &item->memAlloc.size + 4, 0, 2 ); |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | assert( sizeof( size ) == 8 ); |
| 856 | memcpy( &item->memAlloc.size, &size, 4 ); |
| 857 | memcpy( ((char*)&item->memAlloc.size)+4, ((char*)&size)+4, 2 ); |
| 858 | } |
| 859 | GetProfiler().m_serialQueue.commit_next(); |
| 860 | } |
| 861 | |
| 862 | static tracy_force_inline void SendMemFree( QueueType type, const uint32_t thread, const void* ptr ) |
| 863 | { |
| 864 | assert( type == QueueType::MemFree || type == QueueType::MemFreeCallstack || type == QueueType::MemFreeNamed || type == QueueType::MemFreeCallstackNamed ); |
| 865 | |
| 866 | auto item = GetProfiler().m_serialQueue.prepare_next(); |
| 867 | MemWrite( &item->hdr.type, type ); |
| 868 | MemWrite( &item->memFree.time, GetTime() ); |
| 869 | MemWrite( &item->memFree.thread, thread ); |
| 870 | MemWrite( &item->memFree.ptr, (uint64_t)ptr ); |
| 871 | GetProfiler().m_serialQueue.commit_next(); |
| 872 | } |
| 873 | |
| 874 | static tracy_force_inline void SendMemName( const char* name ) |
| 875 | { |
| 876 | assert( name ); |
| 877 | auto item = GetProfiler().m_serialQueue.prepare_next(); |
| 878 | MemWrite( &item->hdr.type, QueueType::MemNamePayload ); |
| 879 | MemWrite( &item->memName.name, (uint64_t)name ); |
| 880 | GetProfiler().m_serialQueue.commit_next(); |
| 881 | } |
| 882 | |
| 883 | #if defined _WIN32 && defined TRACY_TIMER_QPC |
| 884 | static int64_t GetTimeQpc(); |
| 885 | #endif |
| 886 | |
| 887 | double m_timerMul; |
| 888 | uint64_t m_resolution; |
| 889 | uint64_t m_delay; |
| 890 | std::atomic<int64_t> m_timeBegin; |
| 891 | uint32_t m_mainThread; |
| 892 | uint64_t m_epoch, m_exectime; |
| 893 | std::atomic<bool> m_shutdown; |
| 894 | std::atomic<bool> m_shutdownManual; |
| 895 | std::atomic<bool> m_shutdownFinished; |
| 896 | Socket* m_sock; |
| 897 | UdpBroadcast* m_broadcast; |
| 898 | bool m_noExit; |
| 899 | uint32_t m_userPort; |
| 900 | std::atomic<uint32_t> m_zoneId; |
| 901 | int64_t m_samplingPeriod; |
| 902 | |
| 903 | uint32_t m_threadCtx; |
| 904 | int64_t m_refTimeThread; |
| 905 | int64_t m_refTimeSerial; |
| 906 | int64_t m_refTimeCtx; |
| 907 | int64_t m_refTimeGpu; |
| 908 | |
| 909 | void* m_stream; // LZ4_stream_t* |
| 910 | char* m_buffer; |
| 911 | int m_bufferOffset; |
| 912 | int m_bufferStart; |
| 913 | |
| 914 | char* m_lz4Buf; |
| 915 | |
| 916 | FastVector<QueueItem> m_serialQueue, m_serialDequeue; |
| 917 | TracyMutex m_serialLock; |
| 918 | |
| 919 | #ifndef TRACY_NO_FRAME_IMAGE |
| 920 | FastVector<FrameImageQueueItem> m_fiQueue, m_fiDequeue; |
| 921 | TracyMutex m_fiLock; |
| 922 | #endif |
| 923 | |
| 924 | SPSCQueue<SymbolQueueItem> m_symbolQueue; |
| 925 | |
| 926 | std::atomic<uint64_t> m_frameCount; |
| 927 | std::atomic<bool> m_isConnected; |
| 928 | #ifdef TRACY_ON_DEMAND |
| 929 | std::atomic<uint64_t> m_connectionId; |
| 930 | |
| 931 | TracyMutex m_deferredLock; |
| 932 | FastVector<QueueItem> m_deferredQueue; |
| 933 | #endif |
| 934 | |
| 935 | #ifdef TRACY_HAS_SYSTIME |
| 936 | void ProcessSysTime(); |
| 937 | |
| 938 | SysTime m_sysTime; |
| 939 | uint64_t m_sysTimeLast = 0; |
| 940 | #else |
| 941 | void ProcessSysTime() {} |
| 942 | #endif |
| 943 | |
| 944 | ParameterCallback m_paramCallback; |
| 945 | void* m_paramCallbackData; |
| 946 | SourceContentsCallback m_sourceCallback; |
| 947 | void* m_sourceCallbackData; |
| 948 | |
| 949 | char* m_queryImage; |
| 950 | char* m_queryData; |
| 951 | char* m_queryDataPtr; |
| 952 | |
| 953 | #if defined _WIN32 |
| 954 | void* m_exceptionHandler; |
| 955 | #endif |
| 956 | #ifdef __linux__ |
| 957 | struct { |
| 958 | struct sigaction pwr, ill, fpe, segv, pipe, bus, abrt; |
| 959 | } m_prevSignal; |
| 960 | #endif |
| 961 | bool m_crashHandlerInstalled; |
| 962 | }; |
| 963 | |
| 964 | } |
| 965 | |
| 966 | #endif |