当前位置: 首页 > news >正文

C++性能测试工具——Vtune的使用

一、Intel Vtune的安装

在前面初步认识了一下几个性能的测试工具,本篇重点介绍一下Intel VTune Profiler,VTune是一个强大的性能分析工具,它属于Intel oneAPI工具包中工具的一种。VTune的安装只介绍在Linux平台下的场景(Windows安装相对简单)。
1、两种安装方法
第一种方法:
打开网址https://www.intel.com/content/www/us/en/developer/tools/oneapi/toolkits.html#base-kit。找到合适的版本安装,不过这个安装是一个全版本的安装,可能会安装很多其它工具。
第二种方法:
使用命令安装:

sudo yum install intel-oneapi-vtune  # CentOS
sudo apt install intel-oneapi-vtune  # Ubuntu/Debian
或使用离线安装包:
wget https://registrationcenter-download.intel.com/akdlm/IRC_NAS/6bfca885-4156-491e-849b-1cd7da9cc760/intel-oneapi-base-toolkit-2025.1.1.36_offline.sh
sudo sh ./intel-oneapi-base-toolkit-2025.1.1.36_offline.sh -a --silent --cli --eula accept

2、配置
主要是设置环境变量和权限

//配置环境变量
source /opt/intel/oneapi/vtune/latest/env/vars.sh  # oneAPI 版本
source /opt/intel/vtune_amplifier/amplxe-vars.sh    # 老版本
//设置用户权限
sudo usermod -aG vtune $USER

3、验证
可以运行相关脚本:

bash /opt/intel/oneapi/vtune/latest/bin64/vtune-self-checker.sh

二、主要功能

VTune有三种使用方式,一个是使用GUI的方式,另外一种是命令方式,还有一种是远程应用的方式。
1、使用GUI方式
使用下面的命令启动:

vtune-gui  

然后就可以使用UI进行处理相关的测试。首先创建一个新的测试项目,选择相应的类型;其次配置测试程序的路径或进程ID;最后启动分析并查看结果,如对火焰图或调用栈等进行分析。
如下图:
在这里插入图片描述

2、命令方式
执行下面命令:

vtune -collect hotspots -r ./result_dir -- ./your_application    #运行
amplxe-cl -report hotspots -r ./result_dir -format text -report-output ./report.txt #生成报告  

hotspots为分析的类型(其它还有locksandwaits等),生成的报告支持txt,csv及html。
3、远程应用方式
远程方式也比较简单,它也分成有无界面的操作方式,其实这个和VTune本身没有什么太大关系。可以使用远程桌面的一些工具(如向日葵,VNC Server等)或在一些开发IDE上使用插件(如VSCODE的oneAPI插件)。这里就不再展开分析说明。

三、例程应用

下面看一个例程分析,对OneAPI自带的矩阵测试程序进行:

vtune -collect hotspots -r ~/result -- ./matrix
vtune: Warning: Microarchitecture performance insights will not be available. Make sure the sampling driver is installed and enabled on your system.
vtune: Collection started. To stop the collection, either press CTRL-C or enter from another console window: vtune -r /home/fpc/result -command stop.
Addr of buf1 = 0x7f339f7b5010
Offs of buf1 = 0x7f339f7b5180
Addr of buf2 = 0x7f339d7b4010
Offs of buf2 = 0x7f339d7b41c0
Addr of buf3 = 0x7f339b7b3010
Offs of buf3 = 0x7f339b7b3100
Addr of buf4 = 0x7f33997b2010
Offs of buf4 = 0x7f33997b2140
Threads #: 16 Pthreads
Matrix size: 2048
Using multiply kernel: multiply1
Execution time = 3.516 seconds
vtune: Collection stopped.
vtune: Using result path `/home/fpc/result'
vtune: Executing actions 20 % Resolving information for `libtpsstool.so'       
vtune: Warning: Cannot locate debugging information for file `/opt/intel/oneapi/vtune/2024.0/lib64/libtpsstool.so'.
vtune: Executing actions 75 % Generating a report                              Elapsed Time: 3.535sCPU Time: 46.929sEffective Time: 46.929sSpin Time: 0sOverhead Time: 0sTotal Thread Count: 17Paused Time: 0sTop Hotspots
Function   Module     CPU Time  % of CPU Time(%)
---------  ---------  --------  ----------------
multiply1  matrix      46.909s            100.0%
init_arr   matrix       0.010s              0.0%
__GI_      libc.so.6    0.010s              0.0%
Collection and Platform InfoApplication Command Line: ./matrixOperating System: 5.19.0-50-generic DISTRIB_ID=Kylin DISTRIB_RELEASE=V10 DISTRIB_CODENAME=kylin DISTRIB_DESCRIPTION="Kylin V10 SP1" DISTRIB_KYLIN_RELEASE=V10 DISTRIB_VERSION_TYPE=enterprise DISTRIB_VERSION_MODE=normalComputer Name: fjfResult Size: 4.5 MBCollection start time: 10:55:09 12/05/2025 UTCCollection stop time: 10:55:13 12/05/2025 UTCCollector Type: User-mode sampling and tracingCPUName: Intel(R) microarchitecture code named Alderlake-SFrequency: 2.112 GHzLogical CPU Count: 20Cache Allocation TechnologyLevel 2 capability: not detectedLevel 3 capability: not detectedIf you want to skip descriptions of detected performance issues in the report,
enter: vtune -report summary -report-knob show-issues=false -r <my_result_dir>.
Alternatively, you may view the report in the csv format: vtune -report
<report_name> -format=csv.
vtune: Executing actions 100 % done                                

同时,会在指定的目录下,生成一个文件夹,内部有不少的供分析的相关文件,此处为/home/user/result
如果在执行命令时出现:

vtune: Error: Cannot start data collection because the scope of ptrace system call is limited. To enable profiling, please set /proc/sys/kernel/yama/ptrace_scope to 0. To make this change permanent, set kernel.yama.ptrace_scope to 0 in /etc/sysctl.d/10-ptrace.conf and reboot the machine.
vtune: Warning: Microarchitecture performance insights will not be available. Make sure the sampling driver is installed and enabled on your system

可执行命令:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
或
sudo sysctl -w kernel.yama.ptrace_scope=0

四、总结

会熟练的使用各种测试工具,是对程序进行优化的前提。特别是在一些性能需求相当关键的场景下,不借助工具会导致性能优化的效率急剧降低。磨刀不误砍柴工,与诸君共勉!

http://www.xdnf.cn/news/503371.html

相关文章:

  • BC 范式与 4NF
  • 全局异常处理:如何优雅地统一管理业务异常
  • Android屏幕采集编码打包推送RTMP技术详解:从开发到优化与应用
  • 数据结构第七章(四)-B树和B+树
  • Linux `mkdir` 命令深度解析与高阶应用指南
  • [逆向工程]C++实现DLL卸载(二十六)
  • 【算法】分支限界法和贪心、动态规划、回溯、分治法的区别是
  • 围炉夜话:三体阅读分析PPT+文稿
  • Java--利用(堆)获取前k个最小元素
  • 非易失性存储技术综合对比:EEPROM、NVRAM、NOR Flash、NAND Flash和SD卡
  • ​哈夫曼树(Huffman Tree)
  • C++ 回调函数
  • 计算机视觉与深度学习 | Python实现EEMD-LSTM时间序列预测(完整源码和数据)
  • JavaScript基础-预解析
  • 线程(二)OpenJDK 17 中线程启动的完整流程用C++ 源码详解之主-子线程通信机制
  • 如何彻底清空docker里面不使用的容器?
  • deepin v23.1 搜狗输入法next配置中文输入法下默认用英文标点
  • 符合Python风格的对象(对象表示形式)
  • 【机器学习】第二章模型的评估与选择
  • 【LeetCode】大厂面试算法真题回忆(91)--几何平均值最大子数组
  • vue引用cesium,解决“Not allowed to load local resource”报错
  • 调用DeepSeek系列模型问答时,输出只有</think>标签,而没有<think>标签
  • 无人机视角垃圾检测数据集VOC+YOLO格式771张1类别
  • 使用Maven和Ant上传文件到Linux服务器
  • 交流学习 | 江西同为科技有限公司赴海尔总部考察交流
  • Vue3学习(组合式API——父、子组件间通信详解)
  • 大模型之RAG知识库
  • 实验三:计划任务和时钟同步
  • 经典算法 求C(N, K) % mod,保证mod是质数
  • 打造文本差异对比工具 TextDiffX:从想法到实现的完整过程