tracy 学习

2023-11-22 06:10
文章标签 学习 tracy

本文主要是介绍tracy 学习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

https://github.com/wolfpld/tracy

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

Tracy 支持分析 CPU(为 C、C++ 和 Lua 集成提供直接支持。同时,互联网上存在许多其他语言的第三方绑定,例如 Rust 、Zig、C # 、 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

#define TracyPlot( name, val ) tracy::Profiler::PlotData( name, val )static tracy_force_inline void PlotData( const char* name, int64_t val ){
#ifdef TRACY_ON_DEMANDif( !GetProfiler().IsConnected() ) return;
#endifTracyLfqPrepare( QueueType::PlotDataInt );MemWrite( &item->plotDataInt.name, (uint64_t)name );// 名称,之所以搞成了值,是为了避免拷贝MemWrite( &item->plotDataInt.time, GetTime() );// 时间MemWrite( &item->plotDataInt.val, val );// 折线图的值TracyLfqCommit;}template<typename T>
tracy_force_inline void MemWrite( void* ptr, T val )
{memcpy( ptr, &val, sizeof( T ) );
}#define TracyLfqPrepare( _type ) \moodycamel::ConcurrentQueueDefaultTraits::index_t __magic; \auto __token = GetToken(); \auto& __tail = __token->get_tail_index(); \auto item = __token->enqueue_begin( __magic ); \MemWrite( &item->hdr.type, _type );#define TracyLfqCommit \__tail.store( __magic + 1, std::memory_order_release );

看起来是一个无锁队列。

实现细节:

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

Memory Reordering Caught in the Act

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <intrin.h>
#include <stdio.h>// Set either of these to 1 to prevent CPU reordering
#define USE_CPU_FENCE              1
#define USE_SINGLE_HW_THREAD       0//-------------------------------------
//  MersenneTwister
//  A thread-safe random number generator with good randomness
//  in a small number of instructions. We'll use it to introduce
//  random timing delays.
//-------------------------------------
#define MT_IA  397
#define MT_LEN 624class MersenneTwister
{unsigned int m_buffer[MT_LEN];int m_index;public:MersenneTwister(unsigned int seed);// Declare noinline so that the function call acts as a compiler barrier:__declspec(noinline) unsigned int integer();
};MersenneTwister::MersenneTwister(unsigned int seed)
{// Initialize by filling with the seed, then iterating// the algorithm a bunch of times to shuffle things up.for (int i = 0; i < MT_LEN; i++)m_buffer[i] = seed;m_index = 0;for (int i = 0; i < MT_LEN * 100; i++)integer();
}unsigned int MersenneTwister::integer()
{// Indicesint i = m_index;int i2 = m_index + 1; if (i2 >= MT_LEN) i2 = 0; // wrap-aroundint j = m_index + MT_IA; if (j >= MT_LEN) j -= MT_LEN; // wrap-around// Twistunsigned int s = (m_buffer[i] & 0x80000000) | (m_buffer[i2] & 0x7fffffff);unsigned int r = m_buffer[j] ^ (s >> 1) ^ ((s & 1) * 0x9908B0DF);m_buffer[m_index] = r;m_index = i2;// Swizzler ^= (r >> 11);r ^= (r << 7) & 0x9d2c5680UL;r ^= (r << 15) & 0xefc60000UL;r ^= (r >> 18);return r;
}//-------------------------------------
//  Main program, as decribed in the post
//-------------------------------------
HANDLE beginSema1;
HANDLE beginSema2;
HANDLE endSema;int X, Y;
int r1, r2;DWORD WINAPI thread1Func(LPVOID param)
{MersenneTwister random(1);for (;;){WaitForSingleObject(beginSema1, INFINITE);  // Wait for signalwhile (random.integer() % 8 != 0) {}  // Random delay// ----- THE TRANSACTION! -----X = 1;
#if USE_CPU_FENCEMemoryBarrier();  // Prevent CPU reordering
#else_ReadWriteBarrier();  // Prevent compiler reordering only
#endifr1 = Y;ReleaseSemaphore(endSema, 1, NULL);  // Notify transaction complete}return 0;  // Never returns
};DWORD WINAPI thread2Func(LPVOID param)
{MersenneTwister random(2);for (;;){WaitForSingleObject(beginSema2, INFINITE);  // Wait for signalwhile (random.integer() % 8 != 0) {}  // Random delay// ----- THE TRANSACTION! -----Y = 1;
#if USE_CPU_FENCEMemoryBarrier();  // Prevent CPU reordering
#else_ReadWriteBarrier();  // Prevent compiler reordering only
#endifr2 = X;ReleaseSemaphore(endSema, 1, NULL);  // Notify transaction complete}return 0;  // Never returns
};int main()
{// Initialize the semaphoresbeginSema1 = CreateSemaphore(NULL, 0, 99, NULL);beginSema2 = CreateSemaphore(NULL, 0, 99, NULL);endSema = CreateSemaphore(NULL, 0, 99, NULL);// Spawn the threadsHANDLE thread1, thread2;thread1 = CreateThread(NULL, 0, thread1Func, NULL, 0, NULL);thread2 = CreateThread(NULL, 0, thread2Func, NULL, 0, NULL);#if USE_SINGLE_HW_THREAD// Force thread affinities to the same cpu core.SetThreadAffinityMask(thread1, 1);SetThreadAffinityMask(thread2, 1);
#endif// Repeat the experiment ad infinitumint detected = 0;for (int iterations = 1; ; iterations++){// Reset X and YX = 0;Y = 0;// Signal both threadsReleaseSemaphore(beginSema1, 1, NULL);ReleaseSemaphore(beginSema2, 1, NULL);// Wait for both threadsWaitForSingleObject(endSema, INFINITE);WaitForSingleObject(endSema, INFINITE);// Check if there was a simultaneous reorderif (r1 == 0 && r2 == 0){detected++;printf("%d reorders detected after %d iterations\n", detected, iterations);}}return 0;  // Never returns
}

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

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

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

A Fast Lock-Free Queue for C++

折线图消费及渲染

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

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

这篇关于tracy 学习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/408068

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

线性代数|机器学习-P36在图中找聚类

文章目录 1. 常见图结构2. 谱聚类 感觉后面几节课的内容跨越太大,需要补充太多的知识点,教授讲得内容跨越较大,一般一节课的内容是书本上的一章节内容,所以看视频比较吃力,需要先预习课本内容后才能够很好的理解教授讲解的知识点。 1. 常见图结构 假设我们有如下图结构: Adjacency Matrix:行和列表示的是节点的位置,A[i,j]表示的第 i 个节点和第 j 个

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件