Perf

Perf 是内置于Linux 内核源码树中的性能剖析(profiling)工具

基于事件采样原理,以性能事件为基础,支持针对处理器相关性能指标与操作系统相关性能指标的性能剖析

可用于性能瓶颈的查找与热点代码的定位

ubuntu 16.04安装perf

1. perf stat

 perf stat通过概括精简的方式提供被调试程序运行的整体情况和汇总数据

t1.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();
 }
复制代码

编译

gcc -o t1 -g t1.c

 perf stat针对t1

perf stat ./t1

Performance counter stats for './t1':

230.236550 task-clock (msec) # 0.998 CPUs utilized
10 context-switches # 0.043 K/sec
0 cpu-migrations # 0.000 K/sec
39 page-faults # 0.169 K/sec
<not supported> cycles
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
<not supported> instructions
<not supported> branches
<not supported> branch-misses

0.230743761 seconds time elapsed

说明:

  task-clock (msec):用于执行程序的CPU时间

  context-switches:程序在运行过程中发生的上下文切换次数

     cpu-migrations:程序在运行过程中发生的CPU迁移次数,即被调度器从一个CPU转移到另外一个CPU上运行

     page-faults:缺页

  cycles:CPU时钟周期

  instructions:该进程在这段时间内完成的CPU指令

  branches:这段时间内发生分支预测的次数

  branches-misses:这段时间内分支预测失败的次数

perf record可以记录系统或软件一段时间内的事件统计情况,再通过perf report进行文本界面的展示

用perf record -F count 来指定采样频率

sudo perf record -F 50000 -e cpu-clock ./t1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.029 MB perf.data (525 samples) ]
 perf report

 2.perf top

perf top 用于实时显示当前系统的性能统计信息

t2.c

int main(){
    int i;
    while(1) i++;
}

编译

gcc -o t2 -g t2.c

运行

./t2

另起一个窗口

 sudo perf top

Samples: 71K of event 'cpu-clock', Event count (approx.): 7612723453
Overhead Shared Object Symbol
99.56% t2 [.] main
0.18% [kernel] [k] e1000_alloc_rx_buffers
0.07% [kernel] [k] __do_softirq
0.02% [kernel] [k] e1000_xmit_frame
0.02% [kernel] [k] e1000_watchdog
0.02% [kernel] [k] 0x00007fff81854c95
0.01% [kernel] [k] vsnprintf

……

 

posted @   慕尘  阅读(633)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2017-05-11 Linux tree命令
点击右上角即可分享
微信分享提示