清单 5. 没有 BTB 失效的代码

#include
#include
void foo()
{
int i,j;
for(i=0; i< 10; i++)
j+=2;
}
int main(void)
{
int i;
for(i = 0; i< 100000000; i++)
foo();
return 0;
}

此时再次用 perf stat 采样得到如下结果:

[lm@ovispoly perf]$ ./perf stat ./t3
Performance counter stats for './t3:
2784.004851 task-clock-msecs # 0.927 CPUs
90 context-switches # 0.000 M/sec
8 CPU-migrations # 0.000 M/sec
81 page-faults # 0.000 M/sec
33632545 cycles # 12.081 M/sec (scaled from 99.63%)
42996 instructions # 0.001 IPC (scaled from 99.71%)
1474321780 branches # 529.569 M/sec (scaled from 99.78%)
49733 branch-misses # 0.003 % (scaled from 99.35%)
7073107 cache-references # 2.541 M/sec (scaled from 99.42%)
47958540 cache-misses # 17.226 M/sec (scaled from 99.33%)
3.002673524 seconds time elapsed

Branch-misses 减少了。

本例只是为了演示 perf 对 PMU 的使用,本身并无意义,关于充分利用 processor 进行调优可以参考 Intel 公司出品的调优手册,其他的处理器可能有不同的方法,还希望读者明鉴。

小结

以上介绍的这些 perf 用法主要着眼点在于对于应用程序的性能统计分析,本文的第二部分将继续讲述 perf 的一些特殊用法,并偏重于内核本身的性能统计分析。

调优是需要综合知识的工作,要不断地修炼自己。Perf 虽然是一把宝剑,但宝剑配英雄,只有武功高强的大侠才能随心所欲地使用它。以我的功力,也只能道听途说地讲述一些关于宝刀的事情。但若本文能引起您对宝刀的兴趣,那么也算是有一点儿作用了。


相关内容