perf使用示例1

perf使用示例1

perf - Performance analysis tools for Linux

Performance counters for Linux are a new kernel-based subsystem that provide a framework for all things performance analysis. It covers hardware level (CPU/PMU, Performance Monitoring Unit) features and software features (software counters, tracepoints) as well.

perf --help

perf help COMMANDperf COMMAND -h

1.命令简要说明

perf list 命令可以列出所有能够触发 perf 采样点的事件

perf stat [-e <EVENT> | --event=EVENT] [-a] <command> or perf stat [-e <EVENT> | --event=EVENT] [-a] — <command> [<options>] 以概括精简的方式提供被调试程序运行的整体情况和汇总数据

perf top [-e <EVENT> | --event=EVENT] [<options>] 用于实时显示当前系统的性能统计信息

perf record [-e <EVENT> | --event=EVENT] [-l] [-a] <command>

perf -g report 显示调用关系

2.示例1

1)perf stat 命令用于统计进程总体的信息。task-clock:CPU 利用率,该值高,说明程序的多数时间花费在 CPU 计算上而非 IO

2)perf top 命令可查看系统的实时信息,例如系统中最耗时的内核函数或某个用户进程

3)perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果,以此可以找到热点

perf record -e cpu-clock -g 给出函数的调用关系,以便于找到次级热点

[dskong@tecsun perfs]$ cat test.c

//test.c

void longa()

{

int i,j;

for(i = 0; i < 1000000; i++)

j=i; //am I silly or crazy? I feel boring and desperate.

}

void foo2()

{

int i;

for(i=0 ; i < 10; i++)

longa();

}

void foo1()

{

int i;

for(i = 0; i< 100; i++)

longa();

}

int main(void)

{

foo1();

foo2();

}

[dskong@tecsun perfs]$ gcc -g test.c

[dskong@tecsun perfs]$ perf stat ./a.out

[dskong@tecsun perfs]$ perf record ./a.out

[dskong@tecsun perfs]$ perf report

[dskong@tecsun perfs]$ perf annotate

原文

http://whatot.github.io/pub/linux_program/perf%E7%AE%80%E4%BB%8B/

http://blog.chinaunix.net/uid-8350672-id-2984617.html

http://www.dskong.com/archives/223

http://www.pixelbeat.org/programming/profiling/

转载于:https://www.cnblogs.com/mydomain/p/3204491.html

### 使用 Perf 工具进行性能分析 Perf 是一款强大的 Linux 性能分析工具,也被称为 Performance Counters for Linux (PCL),Linux perf events 或者 perf_events[^1]。此工具能够收集线程级、进程级以及 CPU 级别的配置文件数据[^3]。 为了安装并使用 `perf`,需要确保系统上已经安装了一些必要的软件包来支持其核心功能,比如 `gcc`, `make`, `bison`, `flex`, `elfutils`, `libelf-dev`, `libdw-dev` 和 `libaudit-dev`。对于扩展用途还可以考虑安装 `python-dev`(用于 Python 脚本编写)和 `binutils-dev`(用于符号解析)[^2]。 当利用 `perf` 进行工作时有几个建议可以帮助避开常见的陷阱。例如,在启动任何测试之前应该检查打开文件的数量限制设置,因为过低的数值可能会妨碍某些操作正常执行[^4]。 下面是一个简单的命令行例子展示如何使用 `perf record` 来记录应用程序运行期间产生的事件样本: ```bash sudo perf record ./your_application ``` 之后可以通过如下指令查看所录制的数据报告: ```bash perf report ``` 这些基本命令允许用户获取关于程序行为的重要统计信息,从而帮助定位潜在瓶颈或者优化机会。 #### 示例脚本:简单性能剖析会话 ```bash #!/bin/bash # 记录一段时间内的所有CPU周期事件 sudo perf record -e cycles sleep 5 # 显示采集到的结果概览 sudo perf report --stdio ``` 这段 Bash 脚本演示了一个完整的性能测量过程——先通过指定 `-e cycles` 参数告诉 `perf` 只关注 CPU 周期数;接着让系统休眠五秒钟模拟实际应用活动的时间窗口;最后调用 `report` 子命令输出结果摘要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值