perf 工具介绍2

[root@localhost ~]# cat test1.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();
}
[root@localhost ~]# gcc -g -o t1 test1.c
[root@localhost ~]# perf stat ./t1

 Performance counter stats for './t1':

     288.130025  task-clock-msecs         #      0.980 CPUs 
              0  context-switches         #      0.000 M/sec
              0  CPU-migrations           #      0.000 M/sec
             85  page-faults              #      0.000 M/sec
              0  cycles                   #      0.000 M/sec
              0  instructions             #      0.000 IPC  
              0  cache-references         #      0.000 M/sec
              0  cache-misses             #      0.000 M/sec

    0.294152404  seconds time elapsed


程序 t1 是一个 CPU bound 型,因为 task-clock-msecs 接近 1。

对 t1 进行调优应该要找到热点 ( 即最耗时的代码片段 ),再看看是否能够提高热点代码的效率。

缺省情况下,除了 task-clock-msecs 之外,perf stat 还给出了其他几个最常用的统计信息:

Task-clock-msecs:CPU 利用率,该值高,说明程序的多数时间花费在 CPU 计算上而非 IO。

Context-switches:进程切换次数,记录了程序运行过程中发生了多少次进程切换,频繁的进程切换是应该避免的。

     Cache-misses: 程序运行过程中总体的 cache 利用情况,如果该值过高,说明程序的 cache 利用不好

CPU-migrations :   表示进程 t1 运行过程中发生了多少次 CPU 迁移,即被调度器从一个 CPU 转移到另外一个 CPU 上运行。

               Cycles: 处理器时钟,一条机器指令可能需要多个 cycles,

        Instructions:  机器指令数目。

                  IPC:是 Instructions/Cycles 的比值,该值越大越好,说明程序充分利用了处理器的特性。

Cache-references: cache 命中的次数

Cache-misses: cache 失效的次数。

通过指定 -e 选项,您可以改变 perf stat 的缺省事件,查看您所感兴趣的特殊的事件

原文地址:https://www.cnblogs.com/zengkefu/p/4943722.html