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.535s
    CPU Time: 46.929s
        Effective Time: 46.929s
        Spin Time: 0s
        Overhead Time: 0s
    Total Thread Count: 17
    Paused Time: 0s

Top 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 Info
    Application Command Line: ./matrix
    Operating 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=normal
    Computer Name: fjf
    Result Size: 4.5 MB
    Collection start time: 10:55:09 12/05/2025 UTC
    Collection stop time: 10:55:13 12/05/2025 UTC
    Collector Type: User-mode sampling and tracing
    CPU
        Name: Intel(R) microarchitecture code named Alderlake-S
        Frequency: 2.112 GHz
        Logical CPU Count: 20
        Cache Allocation Technology
            Level 2 capability: not detected
            Level 3 capability: not detected

If 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

四、总结

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值