前言
该篇主要是记录如何使用 Linux 自带的 Pref 工具看 C++程序的性能问题。该篇只是简单的记录如何收集性能数据,具体分析这里不做描述。
安装 perf
sudo apt-get install linux-tools-common
sudo apt-get install linux-tools-$(uname -r) linux-cloud-tools-$(uname -r)
附加设置
sudo sysctl kernel.perf_event_paranoid=-1
sudo sysctl kernel.kptr_restrict=0
容器中安装 Perf
FROM ubuntu:16.04
RUN apt-get update && \
apt-get -y install sudo
RUN apt-get --assume-yes install linux-tools-generic
RUN apt-get --assume-yes install linux-tools-4.4.0-116-generic linux-cloud-tools-4.4.0-116-generic linux-tools-generic linux-cloud-tools-generic
RUN sudo sysctl kernel.perf_event_paranoid=-1
RUN sudo sysctl kernel.kptr_restrict=0
ARG BUILD_TYPE
ADD ${BUILD_TYPE}/bin/application /bin/
ENTRYPOINT ["/bin/application"]
记录 perf 示例
选项 1: 启动应用程序后记录 perf
./application_name
perf record -g -s -p `ps -ef | grep application_name | grep -v grep | awk '{print $2}'`
选项 2: 启动应用程序时记录 perf
perf record -g -s ./application_name
运行 load 测试 (h2load)
h2load -D 2 -c 10 -m 10 -H':method:POST' -H'content-type:application/json' your_restful_uri -d data.txt
检查报告 (将存储在一个名为 perfl.data 的文件)
perf report -d applicationname -g graph,0.05,caller,function
or
perf report
总结
这里就是简单的记录下如何安装 Perf 和如何收集 perf 数据,对于如何分析,还得根据自己的 C++程序,然后对收集到的数据,做具体分析。