• tracy 学习


    https://github.com/wolfpld/tracy

    适用于游戏和其他应用的实时、纳秒分辨率、远程控制、支持采样和帧率检测

    Tracy 支持分析 CPU(为 C、C++ 和 Lua 集成提供直接支持。同时,互联网上存在许多其他语言的第三方绑定,例如 Rust 、ZigC #  OCaml  Odin。 )、GPU(所有主要图形 API:OpenGL、Vulkan、Direct3D 11/12、OpenCL。)、内存分配、锁定、上下文切换、自动将屏幕截图归因于捕获的帧等等。

    Client ,采样数据的生产者,即我们要分析的程序

    Server ,采样数据的接受者,同时是一个数据的viewer,并支持数据存储,以及导出csv。

    步骤:

    1. Add the Tracy repository to your project directory.

    2. Tracy source files in the project/tracy/public directory.

    3.  Add TracyClient.cpp as a source file. •

    4. Add tracy/Tracy.hpp as an include file.

    5. Include Tracy.hpp in every file you are interested in profiling.

    6.  Define TRACY_ENABLE for the WHOLE project.

    7.  Add the macro FrameMark at the end of each frame loop. •

    8. Add the macro ZoneScoped as the first line of your function definitions to include them in the profile. •

    9. Compile and run both your application and the profiler server. 

    10. Hit Connect on the profiler server.

    11. Tada! You’re profiling your program!

    作为一个性能检测工具,它自身的性能和精确度如何保证的呢?

    折线图TracyPlot

    1. #define TracyPlot( name, val ) tracy::Profiler::PlotData( name, val )
    2. static tracy_force_inline void PlotData( const char* name, int64_t val )
    3. {
    4. #ifdef TRACY_ON_DEMAND
    5. if( !GetProfiler().IsConnected() ) return;
    6. #endif
    7. TracyLfqPrepare( QueueType::PlotDataInt );
    8. MemWrite( &item->plotDataInt.name, (uint64_t)name );// 名称,之所以搞成了值,是为了避免拷贝
    9. MemWrite( &item->plotDataInt.time, GetTime() );// 时间
    10. MemWrite( &item->plotDataInt.val, val );// 折线图的值
    11. TracyLfqCommit;
    12. }
    13. template<typename T>
    14. tracy_force_inline void MemWrite( void* ptr, T val )
    15. {
    16. memcpy( ptr, &val, sizeof( T ) );
    17. }
    18. #define TracyLfqPrepare( _type ) \
    19. moodycamel::ConcurrentQueueDefaultTraits::index_t __magic; \
    20. auto __token = GetToken(); \
    21. auto& __tail = __token->get_tail_index(); \
    22. auto item = __token->enqueue_begin( __magic ); \
    23. MemWrite( &item->hdr.type, _type );
    24. #define TracyLfqCommit \
    25. __tail.store( __magic + 1, std::memory_order_release );

    看起来是一个无锁队列。

    实现细节:

    A Fast General Purpose Lock-Free Queue for C++

    Memory Reordering Caught in the Act

    1. #define WIN32_LEAN_AND_MEAN
    2. #include
    3. #include
    4. #include
    5. // Set either of these to 1 to prevent CPU reordering
    6. #define USE_CPU_FENCE 1
    7. #define USE_SINGLE_HW_THREAD 0
    8. //-------------------------------------
    9. // MersenneTwister
    10. // A thread-safe random number generator with good randomness
    11. // in a small number of instructions. We'll use it to introduce
    12. // random timing delays.
    13. //-------------------------------------
    14. #define MT_IA 397
    15. #define MT_LEN 624
    16. class MersenneTwister
    17. {
    18. unsigned int m_buffer[MT_LEN];
    19. int m_index;
    20. public:
    21. MersenneTwister(unsigned int seed);
    22. // Declare noinline so that the function call acts as a compiler barrier:
    23. __declspec(noinline) unsigned int integer();
    24. };
    25. MersenneTwister::MersenneTwister(unsigned int seed)
    26. {
    27. // Initialize by filling with the seed, then iterating
    28. // the algorithm a bunch of times to shuffle things up.
    29. for (int i = 0; i < MT_LEN; i++)
    30. m_buffer[i] = seed;
    31. m_index = 0;
    32. for (int i = 0; i < MT_LEN * 100; i++)
    33. integer();
    34. }
    35. unsigned int MersenneTwister::integer()
    36. {
    37. // Indices
    38. int i = m_index;
    39. int i2 = m_index + 1; if (i2 >= MT_LEN) i2 = 0; // wrap-around
    40. int j = m_index + MT_IA; if (j >= MT_LEN) j -= MT_LEN; // wrap-around
    41. // Twist
    42. unsigned int s = (m_buffer[i] & 0x80000000) | (m_buffer[i2] & 0x7fffffff);
    43. unsigned int r = m_buffer[j] ^ (s >> 1) ^ ((s & 1) * 0x9908B0DF);
    44. m_buffer[m_index] = r;
    45. m_index = i2;
    46. // Swizzle
    47. r ^= (r >> 11);
    48. r ^= (r << 7) & 0x9d2c5680UL;
    49. r ^= (r << 15) & 0xefc60000UL;
    50. r ^= (r >> 18);
    51. return r;
    52. }
    53. //-------------------------------------
    54. // Main program, as decribed in the post
    55. //-------------------------------------
    56. HANDLE beginSema1;
    57. HANDLE beginSema2;
    58. HANDLE endSema;
    59. int X, Y;
    60. int r1, r2;
    61. DWORD WINAPI thread1Func(LPVOID param)
    62. {
    63. MersenneTwister random(1);
    64. for (;;)
    65. {
    66. WaitForSingleObject(beginSema1, INFINITE); // Wait for signal
    67. while (random.integer() % 8 != 0) {} // Random delay
    68. // ----- THE TRANSACTION! -----
    69. X = 1;
    70. #if USE_CPU_FENCE
    71. MemoryBarrier(); // Prevent CPU reordering
    72. #else
    73. _ReadWriteBarrier(); // Prevent compiler reordering only
    74. #endif
    75. r1 = Y;
    76. ReleaseSemaphore(endSema, 1, NULL); // Notify transaction complete
    77. }
    78. return 0; // Never returns
    79. };
    80. DWORD WINAPI thread2Func(LPVOID param)
    81. {
    82. MersenneTwister random(2);
    83. for (;;)
    84. {
    85. WaitForSingleObject(beginSema2, INFINITE); // Wait for signal
    86. while (random.integer() % 8 != 0) {} // Random delay
    87. // ----- THE TRANSACTION! -----
    88. Y = 1;
    89. #if USE_CPU_FENCE
    90. MemoryBarrier(); // Prevent CPU reordering
    91. #else
    92. _ReadWriteBarrier(); // Prevent compiler reordering only
    93. #endif
    94. r2 = X;
    95. ReleaseSemaphore(endSema, 1, NULL); // Notify transaction complete
    96. }
    97. return 0; // Never returns
    98. };
    99. int main()
    100. {
    101. // Initialize the semaphores
    102. beginSema1 = CreateSemaphore(NULL, 0, 99, NULL);
    103. beginSema2 = CreateSemaphore(NULL, 0, 99, NULL);
    104. endSema = CreateSemaphore(NULL, 0, 99, NULL);
    105. // Spawn the threads
    106. HANDLE thread1, thread2;
    107. thread1 = CreateThread(NULL, 0, thread1Func, NULL, 0, NULL);
    108. thread2 = CreateThread(NULL, 0, thread2Func, NULL, 0, NULL);
    109. #if USE_SINGLE_HW_THREAD
    110. // Force thread affinities to the same cpu core.
    111. SetThreadAffinityMask(thread1, 1);
    112. SetThreadAffinityMask(thread2, 1);
    113. #endif
    114. // Repeat the experiment ad infinitum
    115. int detected = 0;
    116. for (int iterations = 1; ; iterations++)
    117. {
    118. // Reset X and Y
    119. X = 0;
    120. Y = 0;
    121. // Signal both threads
    122. ReleaseSemaphore(beginSema1, 1, NULL);
    123. ReleaseSemaphore(beginSema2, 1, NULL);
    124. // Wait for both threads
    125. WaitForSingleObject(endSema, INFINITE);
    126. WaitForSingleObject(endSema, INFINITE);
    127. // Check if there was a simultaneous reorder
    128. if (r1 == 0 && r2 == 0)
    129. {
    130. detected++;
    131. printf("%d reorders detected after %d iterations\n", detected, iterations);
    132. }
    133. }
    134. return 0; // Never returns
    135. }

    上面的VC++ 代码,可直观的体验内存序异常

    无锁编程需要解决的是:编译器和CPU 为了优化,只保证单线程的内存序和代码顺序一致。

    为了让多线程编码变得可行,需要增加恰当的指令,让编译器和cpu 都保证内存一致性(粒度不同性能不同)

    A Fast Lock-Free Queue for C++

    折线图消费及渲染

    tracy 在工作线程拿到对应的数据后,会将其插入到plot 列表中。

    之后在主线程的渲染循环中,展示在UI 上:

  • 相关阅读:
    五. 激光雷达建图和定位方案-引言
    Stm32_标准库_期末设计_温度测量&光照测量&手机与芯片通信实现信息的更新
    Konva基本处理流程和相关架构设计
    LC669+670+857+394+337
    MongoDB数据库协议解析及C/C++代码实现
    Zabbix钉钉报警
    C#编程语言在软件开发中的深度应用与实践
    Educational Codeforces Round 133 (Rated for Div. 2)
    隐藏层节点数对网络分类行为的影响
    页表缓存(TLB)和巨型页的实现
  • 原文地址:https://blog.csdn.net/qq_18218335/article/details/132635824