Loading

如何使用perf进行程序分析

 

1.安装。

sudo apt-get install linux-tools

 如果提示没有可安装候选。请输入:

sudo apt-get install linux-perf-version

其中version为你需要的版本。最好与内核版本相同。

 

安装完成后输入

perf top

如可以执行,则安装成功。

注意,如果提示

“E: linux-perf-x.x is not installed.”

是因为你的linux内核版本与perf版本不一致,输入

perf_

后按tab键,通过自动补全补上对应的版本号就行。

 

需要注意的是,如果需要彻底解决内核版本与perf版本不一致的问题(在QT中使用会检测不到perf),或者有洁癖。那么:

chmod 777 /usr/bin/perf
vim /usr/bin/perf

 找到

shopt -s execfail
exec "perf_$version" "$@"

 

修改为

shopt -s execfail

version=5.3
exec "perf_$version" "$@"

这里的version=5.3是因为我的linux内核版本为5.3,替换为你的内核版本即可。

 

2.分析

假设你在当前目录下有一个名为test的可执行文件需要分析。那么

perf stat -e cpu-clock ./test

会返回一个页面展示经过的CPU时钟数和时间。

 

假如你需要记录并分析CPU时钟和page fault

perf record -e cpu-clock,faults ./test

通过增加和替换参数即可指定需要记录和分析的选项。

 

3.查看报告

perf report

通过上下键和回车选择对应的分析选项并进入查看。

 

未完待续

 

 

参考文献:

http://sandsoftwaresound.net/perf/perf-tutorial-hot-spots/

https://docs.cloud.inspur.com/software/perf.html

 

posted @ 2021-03-17 17:21  白叶Stuart  阅读(564)  评论(0编辑  收藏  举报