zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Perf详解编程语言

编程语言 详解 perf
2023-06-13 09:11:53 时间

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

Perf详解编程语言

 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

……

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20331.html

cjavalinux